-
Notifications
You must be signed in to change notification settings - Fork 11
/
DiagramToXtextCommands.java
1025 lines (950 loc) · 46.8 KB
/
DiagramToXtextCommands.java
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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.scenario.editor.embeddededitor.commands;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.xtext.resource.XtextResource;
import org.polarsys.capella.common.data.modellingcore.AbstractType;
import org.polarsys.capella.core.data.capellacommon.AbstractState;
import org.polarsys.capella.core.data.capellacommon.Mode;
import org.polarsys.capella.core.data.capellacommon.State;
import org.polarsys.capella.core.data.cs.AbstractActor;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.ctx.Actor;
import org.polarsys.capella.core.data.epbs.ConfigurationItem;
import org.polarsys.capella.core.data.fa.AbstractFunction;
import org.polarsys.capella.core.data.helpers.interaction.services.ExecutionEndExt;
import org.polarsys.capella.core.data.helpers.interaction.services.SequenceMessageExt;
import org.polarsys.capella.core.data.interaction.ExecutionEnd;
import org.polarsys.capella.core.data.interaction.FragmentEnd;
import org.polarsys.capella.core.data.interaction.InstanceRole;
import org.polarsys.capella.core.data.interaction.InteractionFragment;
import org.polarsys.capella.core.data.interaction.InteractionOperand;
import org.polarsys.capella.core.data.interaction.InteractionOperatorKind;
import org.polarsys.capella.core.data.interaction.InteractionState;
import org.polarsys.capella.core.data.interaction.InteractionUse;
import org.polarsys.capella.core.data.interaction.MessageEnd;
import org.polarsys.capella.core.data.interaction.MessageKind;
import org.polarsys.capella.core.data.interaction.Scenario;
import org.polarsys.capella.core.data.interaction.SequenceMessage;
import org.polarsys.capella.core.data.interaction.TimeLapse;
import org.polarsys.capella.core.data.interaction.StateFragment;
import org.polarsys.capella.core.data.interaction.AbstractFragment;
import org.polarsys.capella.core.data.interaction.CombinedFragment;
import org.polarsys.capella.core.data.oa.Entity;
import org.polarsys.capella.core.data.oa.OperationalActivity;
import org.polarsys.capella.core.data.oa.OperationalActor;
import org.polarsys.capella.core.data.oa.Role;
import org.polarsys.capella.core.model.helpers.AbstractFragmentExt;
import org.polarsys.capella.core.model.helpers.ScenarioExt;
import org.polarsys.capella.core.sirius.analysis.SequenceDiagramServices;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.ArmTimerMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Block;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Element;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.LostFoundMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Model;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Operand;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Participant;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.ParticipantDeactivation;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Reference;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessageType;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.TextualScenarioFactory;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.impl.TextualScenarioFactoryImpl;
import org.polarsys.capella.scenario.editor.dsl.provider.TextualScenarioProvider;
import org.polarsys.capella.scenario.editor.embeddededitor.views.EmbeddedEditorView;
import org.polarsys.capella.scenario.editor.helper.DslConstants;
import org.polarsys.capella.scenario.editor.helper.EmbeddedEditorInstanceHelper;
public class DiagramToXtextCommands {
/**
* updates the xtext editor with data from the Capella model
*
* @param scenario
* @param embeddedEditorViewPart
* @return
*/
public static void process(Scenario scenario, EmbeddedEditorView embeddedEditorViewPart) {
if (scenario != null && embeddedEditorViewPart != null) {
TextualScenarioProvider textualScenarioProvider = embeddedEditorViewPart.getProvider();
if (textualScenarioProvider != null) {
XtextResource resource = textualScenarioProvider.getResource();
EList<EObject> content = resource.getContents();
TextualScenarioFactory factory = new TextualScenarioFactoryImpl();
Model domainModel = HelperCommands.getModel(embeddedEditorViewPart);
if (domainModel != null) {
HelperCommands.clearModel(domainModel);
// Generate Participants
generateParticipants(domainModel, scenario, factory);
// Generate Sequence Messages
generateElements(domainModel, scenario, factory);
content.add(domainModel);
try {
String serialized = ((XtextResource) domainModel.eResource()).getSerializer().serialize(domainModel);
EmbeddedEditorInstanceHelper.updateModel(serialized);
} catch (Exception e) {
HelperCommands.showDialogTextualEditor(HelperCommands.DIALOG_TITLE_UNABLE_TO_REFRESH,
HelperCommands.DIALOG_MESSAGE_ERROR_REFRESH);
}
} else {
HelperCommands.showDialogTextualEditor(HelperCommands.DIALOG_TITLE_UNABLE_TO_REFRESH,
HelperCommands.DIALOG_MESSAGE_ERROR_REFRESH);
}
}
} else {
HelperCommands.showDialogTextualEditor(HelperCommands.DIALOG_TITLE_UNABLE_TO_REFRESH,
HelperCommands.DIALOG_MESSAGE_ERROR_REFRESH + " Unknown associated scenario!");
}
}
/**
* generate the participants objects (actors, functions, components etc) that will be written in the xtext scenario
*
* @param domaninModel
* - the xtext model
* @param scenario
* - the current selected scenario
* @param factory
*/
private static void generateParticipants(Model domainModel, Scenario scenario, TextualScenarioFactory factory) {
// get all instance roles (actors) from diagram
EList<InstanceRole> instanceRoleList = scenario.getOwnedInstanceRoles();
// get all participants/actors from editor
EList<Participant> participants = domainModel.getParticipants();
// remove all participants
participants.clear();
// recreate the list of participants
for (InstanceRole instanceRole : instanceRoleList) {
AbstractType irType = instanceRole.getRepresentedInstance().getAbstractType();
if (irType != null) {
createParticipantComponent(irType, instanceRole, participants, factory);
} else {
createParticipantFunction(instanceRole, participants, factory);
}
}
}
/**
* create the xtext component type object
*
* @param irType
* - Capella AbstractType
* @param instanceRole
* - the Capella instanceRole
* @param participants
* @param factory
*/
private static void createParticipantComponent(AbstractType irType, InstanceRole instanceRole,
EList<Participant> participants, TextualScenarioFactory factory) {
if (irType instanceof OperationalActor) {
addActor(instanceRole.getName(), participants, factory);
} else if (irType instanceof Entity) {
addEntity(instanceRole.getName(), participants, factory);
} else if (irType instanceof ConfigurationItem) {
addConfigItem(instanceRole.getName(), participants, factory);
} else if (irType instanceof AbstractActor) {
addActor(instanceRole.getName(), participants, factory);
} else if (irType instanceof Component) {
addComponent(instanceRole.getName(), participants, factory);
}
}
/**
* create the xtext function type object
*
* @param instanceRole
* - the Capella instanceRole
* @param participants
* @param factory
*/
private static void createParticipantFunction(InstanceRole instanceRole,
EList<Participant> participants, TextualScenarioFactory factory) {
if (instanceRole.getRepresentedInstance() instanceof OperationalActivity) {
addActivity(instanceRole.getName(), participants, factory);
} else if (instanceRole.getRepresentedInstance() instanceof Role) {
addRole(instanceRole.getName(), participants, factory);
} else if (instanceRole.getRepresentedInstance() instanceof AbstractFunction) {
addFunction(instanceRole.getName(), participants, factory);
}
}
/**
* create an an xtext actor object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext actor
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addActor(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Actor actor = factory.createActor();
actor.setName(name);
actor.setKeyword(DslConstants.ACTOR);
participants.add(actor);
}
/**
* create an an xtext activity object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext activity
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addActivity(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Activity activity = factory.createActivity();
activity.setName(name);
activity.setKeyword(DslConstants.ACTIVITY);
participants.add(activity);
}
/**
* create an an xtext component object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext component
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addComponent(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Component component = factory.createComponent();
component.setName(name);
component.setKeyword(DslConstants.COMPONENT);
participants.add(component);
}
/**
* create an an xtext entity object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext entity
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addEntity(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Entity entity = factory.createEntity();
entity.setName(name);
entity.setKeyword(DslConstants.ENTITY);
participants.add(entity);
}
/**
* create an an xtext configuration_item object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext configuration_item
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addConfigItem(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.ConfigurationItem configItem = factory
.createConfigurationItem();
configItem.setName(name);
configItem.setKeyword(DslConstants.CONFIGURATION_ITEM);
participants.add(configItem);
}
/**
* create an an xtext function object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext function
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addFunction(String name, EList<Participant> participants,
TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Function function = factory.createFunction();
function.setName(name);
function.setKeyword(DslConstants.FUNCTION);
participants.add(function);
}
/**
* create an an xtext role object using the TextualScenarioFactory object
*
* @param name
* - the name of the xtext role
* @param participants
* - the current list of xtext participants (actors, components, functions) in which we add the new element
* @param factory
*/
private static void addRole(String name, EList<Participant> participants, TextualScenarioFactory factory) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Role role = factory.createRole();
role.setName(name);
role.setKeyword(DslConstants.ROLE);
participants.add(role);
}
/**
* generate xtext elements (messages, combined fragments, state-modes);
* elements are all the other elements that we can add in a scenario diagram and are not participants (instance roles)
*
* @param domain model
* - the xtext model object in which we add the elements found in diagram
* @param factory
* - the factory object is used to create xtext elements
*/
private static void generateElements(Model domainModel, Scenario scenario, TextualScenarioFactory factory) {
EList<Element> elements = domainModel.getElements();
List<InteractionFragment> fragments = SequenceDiagramServices.getOrderedInteractionFragments(scenario);
// ends contains the interaction fragments from the scenario diagram
// we will go trough this array and based on its content, we generate xtext elements
Object[] ends = fragments.toArray();
EList<TimeLapse> timeLapses = scenario.getOwnedTimeLapses();
// The list of fragments contains both ends of each sequence message (sender and receiver)
// and only one end of each execution (the one where execution ends). This means that we should skip
// the receiving end for each message, so that we don't duplicate the generated xtext message.
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.Message> messagesToDeactivate = new ArrayDeque <>();
// we need to save the combined fragments and blocks on a stack because this
// elements may contain other inner elements and we need to know when to close a combined fragment
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment> combinedFragments = new ArrayDeque <>();
Deque<Block> blockOperands = new ArrayDeque <>();
int i = 0;
while (i < ends.length) {
if (ends[i] instanceof MessageEnd) {
if (isTimerReceivingEnd((MessageEnd) ends[i])) {
//skip TIMER receiving ends, as the sending end will give the message to put in xtext
i = i + 1;
} else {
i = generateMessages(ends, i, messagesToDeactivate, combinedFragments, blockOperands, elements, factory);
}
} else if (ends[i] instanceof ExecutionEnd) {
generateDeactivatioOnMessages((ExecutionEnd) ends[i], messagesToDeactivate, blockOperands, elements, factory);
i++;
} else if (ends[i] instanceof FragmentEnd) {
AbstractFragment abstractFragment = ((FragmentEnd) ends[i]).getAbstractFragment();
if (abstractFragment instanceof CombinedFragment) {
i = processCombinedFragments(ends, i, combinedFragments, blockOperands, elements, scenario, factory);
} else if (abstractFragment instanceof InteractionUse) {
if (((FragmentEnd) ends[i]).getName().equals("start")) {
generateReference((InteractionUse) abstractFragment, factory, elements, blockOperands);
}
i++;
}
} else if (ends[i] instanceof InteractionOperand) {
generateOperandsOnCombinedFragment((InteractionOperand) ends[i], combinedFragments, blockOperands, factory);
i++;
} else if (ends[i] instanceof InteractionState) {
generateStateFragment((InteractionState) ends[i], timeLapses, blockOperands, elements, factory);
i++;
} else {
i = i + 1;
}
}
}
private static boolean isTimerReceivingEnd(MessageEnd messageEnd) {
SequenceMessage currentSequenceMessage = messageEnd.getMessage();
return currentSequenceMessage != null &&
currentSequenceMessage.getKind() == MessageKind.TIMER &&
currentSequenceMessage.getReceivingEnd().equals(messageEnd);
}
/**
* process those ends in the ends array that are related to a combined fragment structure
*
* @param ends
* - the array of interaction fragments found in the Capella Scenario Diagram
* @param index
* - the index used to access elements from ends array
* @param combinedFraments
* - the stack of currently encountered and unclosed combined fragments, we use this stack, pop from it to close a combined fragment
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
*/
private static int processCombinedFragments(Object[] ends, int index,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment> combinedFragments,
Deque<Block> blockOperands,
EList<Element> elements,
Scenario scenario,
TextualScenarioFactory factory) {
if (((FragmentEnd) ends[index])
.getAbstractFragment() instanceof CombinedFragment) {
// obtain the combined fragment
CombinedFragment capellaCombinedFragment = (CombinedFragment) ((FragmentEnd) ends[index])
.getAbstractFragment();
// get the operands for the combined fragment and if the next element, on i+1 is an InteractionOperand and
// belongs to the combined fragment, then create a new Combined fragment structure, otherwise, the Fragment end
// is the end of a previous message
List<InteractionOperand> operands = AbstractFragmentExt.getOwnedOperands(capellaCombinedFragment, scenario);
if (ends.length > index + 1 && ends[index + 1] instanceof InteractionOperand && operands.contains(ends[index + 1])) {
generateCombinedFragments((InteractionOperand) ends[index + 1], capellaCombinedFragment, combinedFragments,
blockOperands, elements, factory);
index++;
} else {
// here is the end of the combined fragment sequence, extract the last processed combined fragment and its
// last block
if (!combinedFragments.isEmpty())
combinedFragments.pop();
if (!blockOperands.isEmpty())
blockOperands.pop();
}
}
index++;
return index;
}
/**
* create a full xtext combined fragment (with its operands)
*
* @param interactionOperand
* - the interactionOperand Capella element encountered in the Interaction Fragments ends array
* @param capellaCombinedFragment
* @param combinedFraments
* - the stack of currently encountered and unclosed combined fragments, we use this stack, pop from it to close a combined fragment
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* @param elements
* - the elements (messages, combined fragments, states) that are directly in the model object (not the ones encountered in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
*/
private static void generateCombinedFragments(InteractionOperand interactionOperand,
CombinedFragment capellaCombinedFragment,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment> combinedFragments,
Deque<Block> blockOperands,
EList<Element> elements,
TextualScenarioFactory factory) {
// when we encounter a combination of FragmentEnd + Interaction operand, generate combined fragment sequence
org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment xtextCombinedFragment = createCombinedFragment(factory, interactionOperand,
capellaCombinedFragment.getOperator());
combinedFragments.push(xtextCombinedFragment);
// add the new encountered combined fragment, to the model, or to a block
if (blockOperands.isEmpty()) {
elements.add(xtextCombinedFragment);
} else {
blockOperands.peek().getBlockElements().add(xtextCombinedFragment);
}
// create the other operands belonging to a combined fragment
Block block = createBlock(factory);
blockOperands.push(block);
xtextCombinedFragment.setBlock(block);
}
/**
* create an xtext operand that is contained in a combined fragment
*
* @param interactionOperand
* - the interactionOperand Capella element encountered in the Interaction Fragments ends array
* @param combinedFraments
* - the stack of currently encountered and unclosed combined fragments, we use this stack, pop from it to close a combined fragment
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
*/
private static void generateOperandsOnCombinedFragment(InteractionOperand operand,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment> combinedFragments,
Deque<Block> blockOperands, TextualScenarioFactory factory) {
// the previous operation block is ended, extract it from the stack, we are done with it
if (!blockOperands.isEmpty())
blockOperands.pop();
if (!combinedFragments.isEmpty()) {
// generate a new branch for combined fragment (else sequence)
Block block = addOperandBlock(factory, combinedFragments.peek(), operand);
blockOperands.push(block);
}
}
/**
* create an xtext deactivation object that will close a previousely encountered message with execution
*
* @param executionEnd
* - the executionEnd Capella element encountered in the Interaction Fragments ends array
* @param messagesToDeactivate
* - the stack of currently encountered and unclosed messages with execution;
* - we use pop from it to close a previously encountered message with execution
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* - we use this variable here, because a deactivation messages can be contained in an operand
* - the deactivation message can be contained directly in the elements array from the model or in an operand
* @param elements
* - the elements (messages, combined fragments, states) that are directly in the model object (not the ones encountered in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
*/
private static void generateDeactivatioOnMessages(ExecutionEnd executionEnd,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.Message> messagesToDeactivate,
Deque<Block> blockOperands, EList<Element> elements, TextualScenarioFactory factory) {
SequenceMessage capellaSequenceMessage = ExecutionEndExt.getMessage(executionEnd);
ParticipantDeactivation participantDeactivationMsg = createDeactivationMessage(factory, capellaSequenceMessage);
// add the deactivation, to the model, or to a block
if (blockOperands.isEmpty()) {
elements.add(participantDeactivationMsg);
} else {
blockOperands.peek().getBlockElements().add((org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) participantDeactivationMsg);
}
setWithExecutionOnMessageToDeactivate(messagesToDeactivate);
}
/**
* generates the xtext messages that will be displayed in the text exitor, it uses at input the array of message ends from Capella (the ends array)
*
* @param ends
* - array of Interaction Fragments found in the Capella Scenario Diagram
* @param index
* - the index we use to know at wich Message End we are (from the ends array)
* @param messagesToDeactivate
* - we keep in a stack the messages that we need to deactivate
* @param combinedFragments
* - the stack of combined fragments
* @param blockOperands
* - the stack of currently encountered operands in a combined fragment
* @param elements
* - the elements (messages, combined fragments, states) that are directly in the model object (not the ones encountered in a combined fragment)
* @param factory
* @return - the index from the message ends array until where we parsed the messages
*/
private static int generateMessages(Object[] ends, int index,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.Message> messagesToDeactivate,
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment> combinedFragments,
Deque<Block> blockOperands, EList<Element> elements,
TextualScenarioFactory factory) {
SequenceMessage currentSequenceMessage = ((MessageEnd) ends[index]).getMessage();
if (currentSequenceMessage != null) {
if (currentSequenceMessage.getKind() == MessageKind.REPLY) {
// this is the reply message for currentSequenceMessage, at the end of the current execution
EObject participantDeactivateMsg = getParticipantDeactivationMsgFromMessageEnd(ends[index], factory);
// add the deactivation, to the model, or to a block
if (blockOperands.isEmpty()) {
elements.add((Element) participantDeactivateMsg);
} else {
blockOperands.peek().getBlockElements().add((org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) participantDeactivateMsg);
}
setWithExecutionOnMessageToDeactivate(messagesToDeactivate);
// skip another end, because it will be the corresponding receiving end of the REPLY message
index = index + 2;
} else {
// this is a sequence message without return branch OR the first part of a sequence message with return branch
EObject message = createMessage(ends[index], factory);
// if this sequence message has return branch, add return to the xtext message
currentSequenceMessage = ((MessageEnd) ends[index]).getMessage();
if (message instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage &&
ScenarioExt.hasReply(currentSequenceMessage)) {
((org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) message)
.setReturn(DslConstants.WITH_RETURN);
}
// add the sequence message, to the model, or to a block
if (blockOperands.isEmpty()) {
elements.add((Element) message);
} else {
blockOperands.peek().getBlockElements().add((org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) message);
}
int step = 0;
if(message instanceof LostFoundMessage) {
index = index + 1;
step = 1;
} else {
// skip the next MessageEnd (the receiving end), as it will generate the same xtext message
index = index + 2;
step = 2;
}
boolean isSimpleMessage = false;
// check to see if this is a simple message (in this case, the next fragment will be its own execution end
// or its own reply message)
if (index < ends.length && ends[index] instanceof ExecutionEnd) {
// check if end is its own execution end
SequenceMessage seqMessFromMessageEnd = ((MessageEnd) ends[index - step]).getMessage();
SequenceMessage seqMessFromExecutionEnd = ExecutionEndExt.getMessage((ExecutionEnd) ends[index]);
if (seqMessFromMessageEnd.equals(seqMessFromExecutionEnd)) {
// nothing to do, skip this execution end
index = index + 1;
isSimpleMessage = true;
} else {
addMessageToDeactivate(messagesToDeactivate, message);
}
} else if (index < ends.length && ends[index] instanceof MessageEnd) {
// check if end is its own reply message
SequenceMessage seqMessFromMessageEnd = ((MessageEnd) ends[index - step]).getMessage();
SequenceMessage seqMessFromNextMessageEnd = ((MessageEnd) ends[index]).getMessage();
SequenceMessage replyMessage = seqMessFromNextMessageEnd != null
&& seqMessFromNextMessageEnd.getKind() == MessageKind.REPLY
? SequenceMessageExt.getOppositeSequenceMessage(seqMessFromMessageEnd)
: null;
if (replyMessage != null && replyMessage.equals(seqMessFromNextMessageEnd)) {
// nothing to do, skip this message end and the next one, they belong to the same message
index = index + 2;
isSimpleMessage = true;
} else {
addMessageToDeactivate(messagesToDeactivate, message);
}
} else {
addMessageToDeactivate(messagesToDeactivate, message);
}
// don't skip the next end, if it belongs to the same TIMER message
if (currentSequenceMessage.getKind() == MessageKind.TIMER && index < ends.length && !isSimpleMessage
&& doSkipNextFragment((InteractionFragment) ends[index], currentSequenceMessage)) {
// only for withExecution messages
index = index - 1;
}
}
return index;
}
return index+1;
}
/**
* check if we need to skip this interaction fragment
*
* @param fragment
* @param currentSequenceMessage
* @return boolean
*/
private static boolean doSkipNextFragment(InteractionFragment fragment, SequenceMessage currentSequenceMessage) {
if (fragment instanceof ExecutionEnd) {
// don't skip the next ExecutionEnd, as it might belong to another message
return !currentSequenceMessage.equals(ExecutionEndExt.getMessage((ExecutionEnd) fragment));
}
if (fragment instanceof MessageEnd) {
// don't skip the next MessageEnd, as it might belong to another message
return !currentSequenceMessage.equals(((MessageEnd) fragment).getMessage());
}
return false;
}
/**
* add elements to the messagesToDeactivate stack;
* this stack contains all the messages that we need to deactivate (can be sequence messages or arm timer)
*
* @param messagesToDeactivate
* - this is a stack with messages with execution that need to be closed by a deactivate
* - the first element from this stack is extracted and the withExecution field is set
* @param message
* - the xtext message that will be added to the messagesToDeactivate stack
*/
private static void addMessageToDeactivate(
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.Message> messagesToDeactivate,
EObject message) {
if (message instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage ||
message instanceof ArmTimerMessage ||
message instanceof FoundMessage) {
messagesToDeactivate.push((org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) message);
}
}
/**
* set the withExecution field on an xtext Message
*
* @param messagesToDeactivate
* - this is a stack with messages with execution that need to be closed by a deactivate
* - the first element from this stack is extracted and the withExecution field is set
*/
private static void setWithExecutionOnMessageToDeactivate(
Deque<org.polarsys.capella.scenario.editor.dsl.textualScenario.Message> messagesToDeactivate) {
if (!messagesToDeactivate.isEmpty()) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.Message currentSequenceMessage = messagesToDeactivate
.pop();
if (currentSequenceMessage instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) {
((org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) currentSequenceMessage)
.setExecution(DslConstants.WITH_EXECUTION);
} else if (currentSequenceMessage instanceof FoundMessage) {
((org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage) currentSequenceMessage)
.setExecution(DslConstants.WITH_EXECUTION);
} else if (currentSequenceMessage instanceof ArmTimerMessage) {
((org.polarsys.capella.scenario.editor.dsl.textualScenario.ArmTimerMessage) currentSequenceMessage)
.setExecution(DslConstants.WITH_EXECUTION);
}
}
}
/**
* generates the ParticipantDeactivation message with input from the MessageEnd in the Capella diagram
*
* @param object
* - this is the MessageEnd
* @param factory
* - this is the factory to create ParticipantDeactivation type of message
* @return - EObject containing the ParticipantDeactivation message
*/
private static EObject getParticipantDeactivationMsgFromMessageEnd(Object object, TextualScenarioFactory factory) {
MessageEnd end = (MessageEnd) object;
SequenceMessage seqMessage = end.getMessage();
String timeLineToDeactivate = null;
if (seqMessage.getKind() == MessageKind.REPLY) {
timeLineToDeactivate = seqMessage.getSendingEnd().getCoveredInstanceRoles().get(0).getName();
} else {
timeLineToDeactivate = seqMessage.getReceivingEnd().getCoveredInstanceRoles().get(0).getName();
}
return createDeactivationMessage(factory, timeLineToDeactivate);
}
/**
* create the ParticipantDeactivation message with input from a Capella sequence message
*
* @param factory
* - this is the factory to create ParticipantDeactivation type of message
* @param capellaSequenceMessage
* @return - ParticipantDeactivation the xtext Participant deactivation message
*/
private static ParticipantDeactivation createDeactivationMessage(TextualScenarioFactory factory,
SequenceMessage capellaSequenceMessage) {
String timelineToDeactivate = capellaSequenceMessage.getReceivingEnd().getCoveredInstanceRoles().get(0).getName();
return createDeactivationMessage(factory, timelineToDeactivate);
}
/**
* create the ParticipantDeactivation message that will deactivate the given timeline
*
* @param factory
* - this is the factory to create ParticipantDeactivation type of message
* @param timelineToDeactivate
* @return ParticipantDeactivation
* - the xtext Participant deactivation message
*/
private static ParticipantDeactivation createDeactivationMessage(TextualScenarioFactory factory,
String timelineToDeactivate) {
ParticipantDeactivation participantDeactivationMsg = factory.createParticipantDeactivation();
participantDeactivationMsg.setName(timelineToDeactivate);
participantDeactivationMsg.setKeyword(DslConstants.DEACTIVATE);
return participantDeactivationMsg;
}
/**
* create an xtext message using as input a message end encountered in the Capella Scenario Diagram
*
* @param object
* - this is the MessageEnd
* @param factory
* - this is the factory to create the xtext message
* @return - EObject the xtext message object created in this method
*/
private static EObject createMessage(Object object, TextualScenarioFactory factory) {
SequenceMessageType xtextSequenceMessage = null;
MessageEnd end = (MessageEnd) object;
SequenceMessage capellaSequenceMessage = end.getMessage();
switch (capellaSequenceMessage.getKind()) {
case REPLY:
break;
case TIMER:
org.polarsys.capella.scenario.editor.dsl.textualScenario.ArmTimerMessage xtextArmTimerMessage = factory.createArmTimerMessage();
xtextArmTimerMessage.setArrow("->>");
xtextArmTimerMessage.setName(capellaSequenceMessage.getName());
xtextArmTimerMessage.setDoubleDot(":");
if (capellaSequenceMessage.getSendingEnd() != null
&& !capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().isEmpty()) {
xtextArmTimerMessage.setParticipant(capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().get(0).getName());
}
return xtextArmTimerMessage;
case CREATE:
xtextSequenceMessage = factory.createCreateMessage();
xtextSequenceMessage.setArrow("->+");
break;
case DELETE:
xtextSequenceMessage = factory.createDeleteMessage();
xtextSequenceMessage.setArrow("->x");
break;
case ASYNCHRONOUS_CALL:
case SYNCHRONOUS_CALL:
default:
// case lost message
if(capellaSequenceMessage.getSendingEnd() != null && capellaSequenceMessage.getReceivingEnd() == null) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.LostMessage xtextLostMessage = factory.createLostMessage();
xtextLostMessage.setArrow("->o");
xtextLostMessage.setDoubleDot(":");
xtextLostMessage.setName(capellaSequenceMessage.getName());
if (!capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().isEmpty()) {
xtextLostMessage.setSource(capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().get(0).getName());
}
return xtextLostMessage;
}
// case found message
else if(capellaSequenceMessage.getSendingEnd() == null && capellaSequenceMessage.getReceivingEnd() != null) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage xtextFoundMessage = factory.createFoundMessage();
xtextFoundMessage.setArrow("o->");
xtextFoundMessage.setDoubleDot(":");
xtextFoundMessage.setName(capellaSequenceMessage.getName());
if (!capellaSequenceMessage.getReceivingEnd().getCoveredInstanceRoles().isEmpty()) {
xtextFoundMessage.setTarget(capellaSequenceMessage.getReceivingEnd().getCoveredInstanceRoles().get(0).getName());
}
return xtextFoundMessage;
} else {
xtextSequenceMessage = factory.createSequenceMessage();
xtextSequenceMessage.setArrow("->");
}
break;
}
if (xtextSequenceMessage != null) {
xtextSequenceMessage.setName(capellaSequenceMessage.getName());
if (capellaSequenceMessage.getSendingEnd() != null
&& !capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().isEmpty()) {
xtextSequenceMessage.setSource(capellaSequenceMessage.getSendingEnd().getCoveredInstanceRoles().get(0).getName());
}
if (capellaSequenceMessage.getReceivingEnd() != null
&& !capellaSequenceMessage.getReceivingEnd().getCoveredInstanceRoles().isEmpty()) {
xtextSequenceMessage.setTarget(capellaSequenceMessage.getReceivingEnd().getCoveredInstanceRoles().get(0).getName());
}
xtextSequenceMessage.setDoubleDot(":");
}
return xtextSequenceMessage;
}
/**
* create an xtext combined fragment
*
* @param factory
* - this is the factory to create the xtext message
* @param operand
* - the first operand in a combined fragment
* @param operatorKind
* - can be alt, loop, par etc
* @return org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment
*/
private static org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment createCombinedFragment(TextualScenarioFactory factory,
InteractionOperand operand,
InteractionOperatorKind operatorKind) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment xtextCombinedFragment = factory.createCombinedFragment();
xtextCombinedFragment.setKeyword(operatorKind.toString().toLowerCase());
xtextCombinedFragment.setOver(DslConstants.OVER);
EList<InstanceRole> coveredInstanceRoles = operand.getCoveredInstanceRoles();
for (InstanceRole ir : coveredInstanceRoles) {
xtextCombinedFragment.getTimelines().add(ir.getName());
}
xtextCombinedFragment.setExpression(HelperCommands.getExpressionText(operand));
return xtextCombinedFragment;
}
/**
* create an xtext block object that belongs to an xtext combined fragment
*
* @param factory
* - this is the factory to create the xtext message
* @param xtextCombinedFragment
* - the xtext combined fragment that is the container of this block
* @param operand
* - the first operand in a combined fragment
* @return Block
*/
private static Block addOperandBlock(TextualScenarioFactory factory,
org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment xtextCombinedFragment,
InteractionOperand operand) {
Operand operandBlock = factory.createOperand();
operandBlock.setExpression(HelperCommands.getExpressionText(operand));
if(xtextCombinedFragment.getKeyword().equals(DslConstants.ALT)) {
operandBlock.setKeyword("else");
}
xtextCombinedFragment.getOperands().add(operandBlock);
Block block = createBlock(factory);
operandBlock.setBlock(block);
return block;
}
private static Block createBlock(TextualScenarioFactory factory) {
Block block = factory.createBlock();
block.setBegin("{");
block.setEnd("}");
return block;
}
/**
* process those ends in the ends array that are related to a state/mode structure
*
* @param interactionState
* - the InteractionState Capella object processed from the ends Interaction Fragments array
* @param timelapses
* - the Capella Timelapses over which this state is displayed
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* - we pass this stack when generating the xtext states because we may encounter a state object in an operand of a combined fragment
* @param elements
* - the elements (messages, combined fragments, states) that are directly in the model object (not the ones encountered in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
* @param factory
*/
private static void generateStateFragment(InteractionState interactionState, EList<TimeLapse> timeLapses,
Deque<Block> blockOperands, EList<Element> elements, TextualScenarioFactory factory) {
for (EObject timeLapse : timeLapses) {
if (timeLapse instanceof StateFragment && interactionState != null &&
interactionState.equals(((StateFragment) timeLapse).getStart())) {
String timelineName = interactionState.getCovered().getName();
org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment xtextStateFragment = createStateFragment(
factory, timeLapse, timelineName);
if (blockOperands.isEmpty()) {
elements.add(xtextStateFragment);
} else {
blockOperands.peek().getBlockElements()
.add((org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) xtextStateFragment);
}
}
}
}
/**
* Create a new StateFragment object and set the name, keyword, timeline
*
* @param factory
* - this is the factory to create StateFragment
* @param fragment
* - the state fragment from diagram
* @param timelineName
* - the name of the timeline
* @return - StateFragment the new created StateFragment
*/
private static org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment createStateFragment(TextualScenarioFactory factory, Object fragment,
String timelineName) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment xtextStateFragment = factory.createStateFragment();
AbstractState stateMode = ((StateFragment) fragment)
.getRelatedAbstractState();
if (stateMode != null) {
if (stateMode instanceof Mode) {
xtextStateFragment.setKeyword(DslConstants.MODE);
} else if (stateMode instanceof State) {
xtextStateFragment.setKeyword(DslConstants.STATE);
}
xtextStateFragment.setName(stateMode.getName());
} else {
AbstractFunction function = ((StateFragment) fragment)
.getRelatedAbstractFunction();
if (function != null) {
xtextStateFragment.setKeyword(DslConstants.FUNCTION);
xtextStateFragment.setName(function.getName());
}
}
xtextStateFragment.setOn(DslConstants.ON);
xtextStateFragment.setTimeline(timelineName);
return xtextStateFragment;
}
/**
* process those ends in the ends array that are related to a reference
*
* @param abstractFragment
* - the InteractionState Capella object processed from the ends Interaction Fragments array
* @param blockOperands
* - the stack of currently encountered and unclosed operands (contained in a combined fragment)
* - we pass this stack when generating the xtext states because we may encounter a state object in an operand of a combined fragment
* @param elements
* - the elements (messages, combined fragments, states) that are directly in the model object (not the ones encountered in a combined fragment)
* @param factory
* - the Textual Scenario Factory that we use to create xtext objects and add them to the model
*/
private static void generateReference(InteractionUse abstractFragment, TextualScenarioFactory factory, EList<Element> elements, Deque<Block> blockOperands) {
EList<InstanceRole> coveredInstanceRoles = abstractFragment.getStart().getCoveredInstanceRoles();
List<String> timelines = coveredInstanceRoles.stream().map(x -> x.getName()).collect(Collectors.toList());
String name = abstractFragment.getReferencedScenario().getName();
Reference xtextReference = createXtextReference(name, timelines, factory);
if (blockOperands.isEmpty()) {
elements.add(xtextReference);