forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_microflows_builder_actions.go
More file actions
943 lines (854 loc) · 32.1 KB
/
cmd_microflows_builder_actions.go
File metadata and controls
943 lines (854 loc) · 32.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
// SPDX-License-Identifier: Apache-2.0
// Package executor - Microflow builder: CRUD & data actions
package executor
import (
"fmt"
"strings"
"github.com/mendixlabs/mxcli/mdl/ast"
"github.com/mendixlabs/mxcli/mdl/types"
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/domainmodel"
"github.com/mendixlabs/mxcli/sdk/microflows"
)
// addCreateVariableAction creates a DECLARE statement as a CreateVariableAction.
func (fb *flowBuilder) addCreateVariableAction(s *ast.DeclareStmt) model.ID {
// Resolve TypeEnumeration → TypeEntity ambiguity using the domain model
declType := s.Type
if declType.Kind == ast.TypeEnumeration && declType.EnumRef != nil && fb.backend != nil {
if fb.isEntity(declType.EnumRef.Module, declType.EnumRef.Name) {
declType = ast.DataType{Kind: ast.TypeEntity, EntityRef: declType.EnumRef}
}
}
// Register the variable as declared
typeName := declType.Kind.String()
fb.declaredVars[s.Variable] = typeName
action := µflows.CreateVariableAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
VariableName: s.Variable,
DataType: convertASTToMicroflowDataType(declType, nil),
InitialValue: fb.exprToString(s.InitialValue),
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addChangeVariableAction creates a SET statement as a ChangeVariableAction.
func (fb *flowBuilder) addChangeVariableAction(s *ast.MfSetStmt) model.ID {
// Validate that the variable has been declared
if !fb.isVariableDeclared(s.Target) {
fb.addErrorWithExample(
fmt.Sprintf("variable '%s' is not declared", s.Target),
errorExampleDeclareVariable(s.Target))
}
action := µflows.ChangeVariableAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
VariableName: s.Target,
Value: fb.exprToString(s.Value),
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addCreateObjectAction creates a CREATE OBJECT statement.
func (fb *flowBuilder) addCreateObjectAction(s *ast.CreateObjectStmt) model.ID {
action := µflows.CreateObjectAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
OutputVariable: s.Variable,
Commit: microflows.CommitTypeNo,
}
// Set entity reference as qualified name (BY_NAME_REFERENCE)
entityQN := ""
if s.EntityType.Module != "" && s.EntityType.Name != "" {
entityQN = s.EntityType.Module + "." + s.EntityType.Name
action.EntityQualifiedName = entityQN
}
// Register variable type for CHANGE statements
if fb.varTypes != nil && entityQN != "" {
fb.varTypes[s.Variable] = entityQN
}
// Build InitialMembers for each SET assignment
for _, change := range s.Changes {
memberChange := µflows.MemberChange{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Type: microflows.MemberChangeTypeSet,
Value: fb.memberExpressionToString(change.Value, entityQN, change.Attribute),
}
fb.resolveMemberChange(memberChange, change.Attribute, entityQN)
action.InitialMembers = append(action.InitialMembers, memberChange)
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addCommitAction creates a COMMIT statement.
func (fb *flowBuilder) addCommitAction(s *ast.MfCommitStmt) model.ID {
action := µflows.CommitObjectsAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
CommitVariable: s.Variable,
WithEvents: s.WithEvents,
RefreshInClient: s.RefreshInClient,
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addDeleteAction creates a DELETE statement.
func (fb *flowBuilder) addDeleteAction(s *ast.DeleteObjectStmt) model.ID {
action := µflows.DeleteObjectAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
DeleteVariable: s.Variable,
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addRollbackAction creates a ROLLBACK statement.
func (fb *flowBuilder) addRollbackAction(s *ast.RollbackStmt) model.ID {
action := µflows.RollbackObjectAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
RollbackVariable: s.Variable,
RefreshInClient: s.RefreshInClient,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addChangeObjectAction creates a CHANGE statement.
func (fb *flowBuilder) addChangeObjectAction(s *ast.ChangeObjectStmt) model.ID {
// Empty non-committing changes need RefreshInClient to satisfy Studio Pro
// consistency checks; explicit `refresh` keeps the same flag for all changes.
action := µflows.ChangeObjectAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ChangeVariable: s.Variable,
Commit: microflows.CommitTypeNo,
RefreshInClient: s.RefreshInClient || len(s.Changes) == 0,
}
// Look up entity type from variable scope
entityQN := ""
if fb.varTypes != nil {
entityQN = fb.varTypes[s.Variable]
}
// Build MemberChange items for each SET assignment
for _, change := range s.Changes {
memberChange := µflows.MemberChange{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Type: microflows.MemberChangeTypeSet,
Value: fb.memberExpressionToString(change.Value, entityQN, change.Attribute),
}
fb.resolveMemberChange(memberChange, change.Attribute, entityQN)
action.Changes = append(action.Changes, memberChange)
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addRetrieveAction creates a RETRIEVE statement.
func (fb *flowBuilder) addRetrieveAction(s *ast.RetrieveStmt) model.ID {
var source microflows.RetrieveSource
if s.StartVariable != "" {
// Association retrieve: RETRIEVE $List FROM $Parent/Module.AssocName
// Always use AssociationRetrieveSource to preserve the original syntax.
// The runtime resolves traversal direction from association metadata.
assocQN := s.Source.Module + "." + s.Source.Name
// Look up association to determine type and direction.
// For Reference associations, AssociationRetrieveSource always returns a single
// object (the entity on the other end). When the user navigates from the child
// (non-owner) side, the intent is to get a list of parent entities — we must use
// a DatabaseRetrieveSource with XPath constraint instead.
assocInfo := fb.lookupAssociation(s.Source.Module, s.Source.Name)
startVarType := ""
if fb.varTypes != nil {
startVarType = fb.varTypes[s.StartVariable]
}
outputUsedAsList := fb.listInputVariables != nil && fb.listInputVariables[s.Variable]
outputUsedAsObject := fb.objectInputVariables != nil && fb.objectInputVariables[s.Variable]
// Owner-both Reference associations need later usage context: the same
// compact retrieve can be consumed as either a list or a single object.
expandReverseReference := assocInfo != nil &&
assocInfo.Type == domainmodel.AssociationTypeReference &&
assocInfo.Owner != "" &&
assocInfo.parentPersistable &&
assocInfo.childEntityQN != "" &&
startVarType == assocInfo.childEntityQN &&
(assocInfo.Owner != domainmodel.AssociationOwnerBoth || outputUsedAsList && !outputUsedAsObject)
if expandReverseReference {
// Reverse traversal on Reference: child → parent (one-to-many)
// Use DatabaseRetrieveSource with XPath to get a list of parent entities
dbSource := µflows.DatabaseRetrieveSource{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
EntityQualifiedName: assocInfo.parentEntityQN,
XPathConstraint: "[" + assocQN + " = $" + s.StartVariable + "]",
}
source = dbSource
if fb.varTypes != nil {
fb.varTypes[s.Variable] = "List of " + assocInfo.parentEntityQN
}
} else {
// Forward traversal or ReferenceSet: use AssociationRetrieveSource
source = µflows.AssociationRetrieveSource{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
StartVariable: s.StartVariable,
AssociationQualifiedName: assocQN,
}
if fb.varTypes != nil {
if assocInfo != nil && assocInfo.Type == domainmodel.AssociationTypeReference {
// Forward Reference traversal returns a single object. Legacy or
// non-persistable reverse traversal can still use association
// source syntax, but keeps list typing for downstream actions.
otherEntity := assocInfo.childEntityQN
if startVarType == assocInfo.childEntityQN {
otherEntity = assocInfo.parentEntityQN
}
if startVarType == assocInfo.childEntityQN && !outputUsedAsObject {
fb.varTypes[s.Variable] = "List of " + otherEntity
} else {
fb.varTypes[s.Variable] = otherEntity
}
} else if assocInfo != nil && assocInfo.Type == domainmodel.AssociationTypeReferenceSet {
// ReferenceSet traversal returns a list of the entity on the other side,
// not a list typed as the association itself.
otherEntity := assocInfo.childEntityQN
if startVarType == assocInfo.childEntityQN {
otherEntity = assocInfo.parentEntityQN
}
if otherEntity != "" {
fb.varTypes[s.Variable] = "List of " + otherEntity
} else {
fb.varTypes[s.Variable] = "List of " + assocQN
}
} else {
// ReferenceSet or unknown: returns a list
fb.varTypes[s.Variable] = "List of " + assocQN
}
}
}
} else {
// Database retrieve: RETRIEVE $List FROM Module.Entity WHERE ...
entityQN := s.Source.Module + "." + s.Source.Name
dbSource := µflows.DatabaseRetrieveSource{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
EntityQualifiedName: entityQN,
}
// Set range if LIMIT is specified
if s.Limit != "" {
rangeType := microflows.RangeTypeCustom
// LIMIT 1 with no offset uses RangeTypeFirst for single object retrieval
if s.Limit == "1" && s.Offset == "" {
rangeType = microflows.RangeTypeFirst
}
dbSource.Range = µflows.Range{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
RangeType: rangeType,
Limit: s.Limit,
Offset: s.Offset,
}
}
// Convert WHERE expression if present
// XPath constraints are stored with square brackets in BSON: [expression]
if s.Where != nil {
dbSource.XPathConstraint = "[" + expressionToXPath(s.Where) + "]"
}
// Convert SORT BY columns if present
if len(s.SortColumns) > 0 {
for _, col := range s.SortColumns {
// Resolve attribute path - if just a simple name, prefix with entity
attrPath := col.Attribute
if !strings.Contains(attrPath, ".") {
attrPath = entityQN + "." + attrPath
} else {
// Validate that qualified attribute path belongs to the retrieved entity
// Expected format: Module.Entity.Attribute
parts := strings.Split(attrPath, ".")
if len(parts) >= 3 {
// Extract entity from attribute path (first two parts)
attrEntityQN := parts[0] + "." + parts[1]
if attrEntityQN != entityQN {
fb.addError("sort by attribute '%s' does not belong to entity '%s'", col.Attribute, entityQN)
continue // Skip this sort column but continue processing others
}
}
}
direction := microflows.SortDirectionAscending
if col.Order == "desc" {
direction = microflows.SortDirectionDescending
}
dbSource.Sorting = append(dbSource.Sorting, µflows.SortItem{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
AttributeQualifiedName: attrPath,
Direction: direction,
})
}
}
source = dbSource
// Register variable type for CHANGE statements
// RETRIEVE with LIMIT 1 returns a single entity, otherwise returns a List
if fb.varTypes != nil {
if s.Limit == "1" {
// LIMIT 1 returns a single entity
fb.varTypes[s.Variable] = entityQN
} else {
// No LIMIT or LIMIT > 1 returns a list
fb.varTypes[s.Variable] = "List of " + entityQN
}
}
}
action := µflows.RetrieveAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
OutputVariable: s.Variable,
Source: source,
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addListOperationAction creates list operations like HEAD, TAIL, FIND, etc.
func (fb *flowBuilder) addListOperationAction(s *ast.ListOperationStmt) model.ID {
var operation microflows.ListOperation
switch s.Operation {
case ast.ListOpHead:
operation = µflows.HeadOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
}
case ast.ListOpTail:
operation = µflows.TailOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
}
case ast.ListOpFind:
operation = µflows.FindOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
Expression: fb.exprToString(s.Condition),
}
case ast.ListOpFilter:
operation = µflows.FilterOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
Expression: fb.exprToString(s.Condition),
}
case ast.ListOpSort:
// Resolve entity type from input variable for qualified attribute names
entityType := ""
if fb.varTypes != nil {
listType := fb.varTypes[s.InputVariable]
if after, ok := strings.CutPrefix(listType, "List of "); ok {
entityType = after
}
}
// Build sort items from SortSpecs
var sortItems []*microflows.SortItem
for _, spec := range s.SortSpecs {
direction := microflows.SortDirectionAscending
if !spec.Ascending {
direction = microflows.SortDirectionDescending
}
// Build fully qualified attribute name: Entity.Attribute
attrQN := spec.Attribute
if entityType != "" && !strings.Contains(spec.Attribute, ".") {
attrQN = entityType + "." + spec.Attribute
}
sortItems = append(sortItems, µflows.SortItem{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
AttributeQualifiedName: attrQN,
Direction: direction,
})
}
operation = µflows.SortOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
Sorting: sortItems,
}
case ast.ListOpUnion:
operation = µflows.UnionOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable1: s.InputVariable,
ListVariable2: s.SecondVariable,
}
case ast.ListOpIntersect:
operation = µflows.IntersectOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable1: s.InputVariable,
ListVariable2: s.SecondVariable,
}
case ast.ListOpSubtract:
operation = µflows.SubtractOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable1: s.InputVariable,
ListVariable2: s.SecondVariable,
}
case ast.ListOpContains:
operation = µflows.ContainsOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
ObjectVariable: s.SecondVariable, // The item to check
}
case ast.ListOpEquals:
operation = µflows.EqualsOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable1: s.InputVariable,
ListVariable2: s.SecondVariable,
}
case ast.ListOpRange:
rangeOp := µflows.ListRangeOperation{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ListVariable: s.InputVariable,
}
if s.OffsetExpr != nil {
rangeOp.OffsetExpression = fb.exprToString(s.OffsetExpr)
}
if s.LimitExpr != nil {
rangeOp.LimitExpression = fb.exprToString(s.LimitExpr)
}
operation = rangeOp
default:
return ""
}
action := µflows.ListOperationAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Operation: operation,
OutputVariable: s.OutputVariable,
}
// Track output variable type for operations that preserve/produce list types
if fb.varTypes != nil && s.OutputVariable != "" && s.InputVariable != "" {
inputType := fb.varTypes[s.InputVariable]
switch s.Operation {
case ast.ListOpFilter, ast.ListOpSort, ast.ListOpTail, ast.ListOpUnion, ast.ListOpIntersect, ast.ListOpSubtract, ast.ListOpRange:
// These operations preserve the list type
if inputType != "" {
fb.varTypes[s.OutputVariable] = inputType
}
case ast.ListOpHead, ast.ListOpFind:
// These return a single element (remove "List of " prefix)
if after, ok := strings.CutPrefix(inputType, "List of "); ok {
fb.varTypes[s.OutputVariable] = after
}
// CONTAINS and EQUALS return Boolean, no need to track
}
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addAggregateListAction creates aggregate operations like COUNT, SUM, AVERAGE, etc.
func (fb *flowBuilder) addAggregateListAction(s *ast.AggregateListStmt) model.ID {
var function microflows.AggregateFunction
switch s.Operation {
case ast.AggregateCount:
function = microflows.AggregateFunctionCount
case ast.AggregateSum:
function = microflows.AggregateFunctionSum
case ast.AggregateAverage:
function = microflows.AggregateFunctionAverage
case ast.AggregateMinimum:
function = microflows.AggregateFunctionMin
case ast.AggregateMaximum:
function = microflows.AggregateFunctionMax
case ast.AggregateReduce:
function = microflows.AggregateFunctionReduce
default:
return ""
}
action := µflows.AggregateListAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
InputVariable: s.InputVariable,
OutputVariable: s.OutputVariable,
Function: function,
}
if s.IsExpression && s.Expression != nil {
action.UseExpression = true
action.Expression = expressionToString(s.Expression)
} else if s.Attribute != "" {
// For SUM/AVG/MIN/MAX, build qualified attribute name from variable type
if fb.varTypes != nil {
listType := fb.varTypes[s.InputVariable]
if after, ok := strings.CutPrefix(listType, "List of "); ok {
entityType := after
action.AttributeQualifiedName = entityType + "." + s.Attribute
}
}
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addCreateListAction creates a CREATE LIST OF statement.
func (fb *flowBuilder) addCreateListAction(s *ast.CreateListStmt) model.ID {
entityQN := ""
if s.EntityType.Module != "" && s.EntityType.Name != "" {
entityQN = s.EntityType.Module + "." + s.EntityType.Name
}
action := µflows.CreateListAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
OutputVariable: s.Variable,
EntityQualifiedName: entityQN,
}
// Register variable type as list
if fb.varTypes != nil && entityQN != "" {
fb.varTypes[s.Variable] = "List of " + entityQN
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addAddToListAction creates an ADD TO list statement.
func (fb *flowBuilder) addAddToListAction(s *ast.AddToListStmt) model.ID {
action := µflows.ChangeListAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Type: microflows.ChangeListTypeAdd,
ChangeVariable: s.List,
Value: "$" + s.Item,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addRemoveFromListAction creates a REMOVE FROM list statement.
func (fb *flowBuilder) addRemoveFromListAction(s *ast.RemoveFromListStmt) model.ID {
action := µflows.ChangeListAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Type: microflows.ChangeListTypeRemove,
ChangeVariable: s.List,
Value: "$" + s.Item,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// isEntity checks whether a qualified name refers to an entity in the domain model.
func (fb *flowBuilder) isEntity(moduleName, entityName string) bool {
if fb.backend == nil {
return false
}
mod, err := fb.backend.GetModuleByName(moduleName)
if err != nil || mod == nil {
return false
}
dm, err := fb.backend.GetDomainModel(mod.ID)
if err != nil || dm == nil {
return false
}
for _, e := range dm.Entities {
if e.Name == entityName {
return true
}
}
return false
}
// resolveMemberChange determines whether a member name is an association or attribute
// and sets the appropriate field on the MemberChange. It queries the domain model
// to check if the name matches an association on the entity; if no metadata is
// available, it falls back to a name-shape heuristic.
//
// memberName can be either bare ("Order_Customer") or qualified ("MfTest.Order_Customer").
func (fb *flowBuilder) resolveMemberChange(mc *microflows.MemberChange, memberName string, entityQN string) {
if entityQN == "" {
// Entity type of $variable is unknown (e.g., the variable comes from a
// java action whose return type isn't registered, or from the iterator
// of an untyped loop). Without the entity we cannot query the domain
// model — but we must NOT silently drop the member name, otherwise
// `change $x (Module.Assoc = $y)` would round-trip as `change $x ( = $y)`
// which is invalid MDL. Fall back to a shape heuristic:
//
// * no dot -> bare attribute name
// * exactly one dot -> `Module.Assoc` (association)
// * two or more dots -> `Module.Entity.Attribute` (qualified attribute)
//
// Two-dot names are never associations in MDL (association names carry a
// single qualifier — the module), so they must stay on AttributeQualified-
// Name even when the entity type is unknown. This avoids miscategorising
// something like `change $x (MyModule.MyEntity.Offset = 1)` as an
// association change.
resolveMemberChangeFallback(mc, memberName, "")
return
}
// Split entity qualified name into module and entity
parts := strings.SplitN(entityQN, ".", 2)
if len(parts) != 2 {
mc.AttributeQualifiedName = entityQN + "." + memberName
return
}
moduleName := parts[0]
// If memberName is already qualified (e.g., "MfTest.Order_Customer"),
// extract the bare name for association lookup.
bareName := memberName
qualifiedName := memberName
if dot := strings.Index(memberName, "."); dot >= 0 {
bareName = memberName[dot+1:]
// qualifiedName is already set to the full memberName
} else {
qualifiedName = moduleName + "." + memberName
}
// Query domain model to check if this member is an association
if fb.backend != nil {
if mod, err := fb.backend.GetModuleByName(moduleName); err == nil && mod != nil {
if dm, err := fb.backend.GetDomainModel(mod.ID); err == nil && dm != nil {
for _, a := range dm.Associations {
if a.Name == bareName {
mc.AssociationQualifiedName = qualifiedName
return
}
}
for _, a := range dm.CrossAssociations {
if a.Name == bareName {
mc.AssociationQualifiedName = qualifiedName
return
}
}
// Not an association — it's an attribute
if strings.Contains(memberName, ".") {
// Already qualified, don't double-qualify
mc.AttributeQualifiedName = memberName
} else {
mc.AttributeQualifiedName = entityQN + "." + memberName
}
return
}
}
}
resolveMemberChangeFallback(mc, memberName, entityQN)
}
// resolveMemberChangeFallback preserves the authored member name shape when the
// entity metadata is unavailable.
//
// - 0 dots => bare attribute name. If entityQN is known, qualify it as
// `Module.Entity.Attribute`; otherwise preserve the bare attribute.
// - 1 dot => association qualified by module (`Module.Association`).
// - >=2 dots => fully qualified attribute (`Module.Entity.Attribute`).
func resolveMemberChangeFallback(mc *microflows.MemberChange, memberName string, entityQN string) {
if memberName == "" {
return
}
switch strings.Count(memberName, ".") {
case 0:
if entityQN == "" {
mc.AttributeQualifiedName = memberName
} else {
mc.AttributeQualifiedName = entityQN + "." + memberName
}
case 1:
mc.AssociationQualifiedName = memberName
default:
mc.AttributeQualifiedName = memberName
}
}
// assocLookupResult holds resolved association metadata.
type assocLookupResult struct {
Type domainmodel.AssociationType
Owner domainmodel.AssociationOwner
parentEntityQN string // Qualified name of the parent (FROM/owner) entity
childEntityQN string // Qualified name of the child (TO/referenced) entity
parentPersistable bool
childPersistable bool
}
// lookupAssociation finds an association by module and name, returning its type
// and the qualified names of its parent and child entities. Returns nil if the
// association cannot be found (e.g., backend is nil or module doesn't exist).
func (fb *flowBuilder) lookupAssociation(moduleName, assocName string) *assocLookupResult {
if fb.backend == nil {
return nil
}
mod, err := fb.backend.GetModuleByName(moduleName)
if err != nil || mod == nil {
return nil
}
dm, err := fb.backend.GetDomainModel(mod.ID)
if err != nil || dm == nil {
return nil
}
// Build entity ID → qualified name map
entityNames := make(map[model.ID]string, len(dm.Entities))
entityPersistable := make(map[model.ID]bool, len(dm.Entities))
for _, e := range dm.Entities {
entityNames[e.ID] = moduleName + "." + e.Name
entityPersistable[e.ID] = e.Persistable
}
for _, a := range dm.Associations {
if a.Name == assocName {
return &assocLookupResult{
Type: a.Type,
Owner: a.Owner,
parentEntityQN: entityNames[a.ParentID],
childEntityQN: entityNames[a.ChildID],
parentPersistable: entityPersistable[a.ParentID],
childPersistable: entityPersistable[a.ChildID],
}
}
}
return nil
}