-
Notifications
You must be signed in to change notification settings - Fork 11
/
XtextToDiagramCommands.java
2460 lines (2219 loc) · 111 KB
/
XtextToDiagramCommands.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.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.sequence.SequenceDDiagram;
import org.eclipse.sirius.diagram.sequence.business.internal.operation.SynchronizeGraphicalOrderingOperation;
import org.eclipse.sirius.diagram.sequence.business.internal.refresh.RefreshLayoutCommand;
import org.eclipse.sirius.viewpoint.description.AnnotationEntry;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.validation.Issue;
import org.polarsys.capella.common.helpers.EObjectExt;
import org.polarsys.capella.common.menu.dynamic.CreationHelper;
import org.polarsys.capella.core.data.capellacommon.AbstractState;
import org.polarsys.capella.core.data.capellacore.CapellacoreFactory;
import org.polarsys.capella.core.data.capellacore.Constraint;
import org.polarsys.capella.core.data.cs.ExchangeItemAllocation;
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.information.AbstractEventOperation;
import org.polarsys.capella.core.data.information.AbstractInstance;
import org.polarsys.capella.core.data.information.datavalue.DatavalueFactory;
import org.polarsys.capella.core.data.information.datavalue.OpaqueExpression;
import org.polarsys.capella.core.data.interaction.AbstractFragment;
import org.polarsys.capella.core.data.interaction.CombinedFragment;
import org.polarsys.capella.core.data.interaction.Event;
import org.polarsys.capella.core.data.interaction.EventReceiptOperation;
import org.polarsys.capella.core.data.interaction.EventSentOperation;
import org.polarsys.capella.core.data.interaction.Execution;
import org.polarsys.capella.core.data.interaction.ExecutionEnd;
import org.polarsys.capella.core.data.interaction.ExecutionEvent;
import org.polarsys.capella.core.data.interaction.FragmentEnd;
import org.polarsys.capella.core.data.interaction.InstanceRole;
import org.polarsys.capella.core.data.interaction.InteractionFactory;
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.InteractionPackage;
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.StateFragment;
import org.polarsys.capella.core.data.interaction.TimeLapse;
import org.polarsys.capella.core.model.helpers.AbstractFragmentExt;
import org.polarsys.capella.core.model.helpers.ConstraintExt;
import org.polarsys.capella.core.model.helpers.ScenarioExt;
import org.polarsys.capella.scenario.editor.EmbeddedEditorInstance;
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.CreateMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.DeleteMessage;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Message;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Operand;
import org.polarsys.capella.scenario.editor.dsl.textualScenario.Model;
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.helpers.TextualScenarioHelper;
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;
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.LostMessage;
public class XtextToDiagramCommands {
public static void process(Scenario scenario, EmbeddedEditorView embeddedEditorViewPart) {
if (scenario != null) {
if (embeddedEditorViewPart != null) {
TextualScenarioProvider p = embeddedEditorViewPart.getProvider();
XtextResource resource = p.getResource();
List<Issue> issues = HelperCommands.getValidationIsuesTextResource(resource);
if (issues.isEmpty()) {
EList<EObject> content = resource.getContents();
Model domainModel = (Model) content.get(0);
// get participants
EList<Participant> participants = domainModel.getParticipants();
// get elements
EList<Element> elements = domainModel.getElements();
doEditingOnParticipants(scenario, participants);
doEditingOnElements(scenario, elements);
// do refresh - when the messages associated with the removed actors are deleted too,
// a refresh is needed to update also the editor
// EmbeddedEditorView eeView = XtextEditorHelper.getActiveEmbeddedEditorView();
// Scenario scenarioDiagram = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
// DiagramToXtextCommands.process(scenarioDiagram, eeView);
} else {
HelperCommands.showDialogTextualEditor("Invalid data", "Please fix the errors in the textual editor!" +
HelperCommands.getFormattedIssues(issues));
}
}
} else {
HelperCommands.showDialogTextualEditor("Unknowned scenario", "The associated scenario diagram is unknown!");
}
}
/**
* Generates the participants in the Capella diagram
*
* @param scenario
* The scenario diagram
* @param participants
* List of participants defined in the xtext editor
* @param messages
*
*/
private static void doEditingOnParticipants(Scenario scenario, EList<Participant> participants) {
// Make sure your element is attached to a resource, otherwise this will return null
TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(scenario);
domain.getCommandStack().execute(new RecordingCommand(domain) {
@Override
protected void doExecute() {
InstanceRole instanceRole;
EList<InstanceRole> instanceRoles = scenario.getOwnedInstanceRoles();
for (Iterator<Participant> iterator = participants.iterator(); iterator.hasNext();) {
EObject participant = iterator.next();
String instanceName = ((Participant) participant).getName();
// if the participant doesn't exist, create it
if (instanceRoles.stream().filter(ir -> ir.getName().equals(instanceName)).collect(Collectors.toList())
.isEmpty()) {
instanceRole = InteractionFactory.eINSTANCE.createInstanceRole();
instanceRole.setName(instanceName);
String keyword = ((Participant) participant).getKeyword();
EObject capellaParticipant = null;
List<? extends EObject> capellaParticipants = EmbeddedEditorInstanceHelper.getAvailableElements(keyword)
.stream().filter(f -> ((AbstractInstance) f).getName().equals(instanceName))
.collect(Collectors.toList());
if (!capellaParticipants.isEmpty()) {
capellaParticipant = capellaParticipants.get(0);
}
instanceRole.setRepresentedInstance((AbstractInstance) capellaParticipant);
instanceRoles.add(instanceRole);
}
}
// remove all instance roles from diagram that do not exist in editor
removeParticipantsFromDiagram(scenario, participants);
// do instance roles reorder if they do not match the order of the editor participants
reorderParticipants(instanceRoles, participants);
}
});
}
/**
* Remove all instance roles that are in the diagram, but not in the editor. Remove all related elements.
*
* @param scenario
* The scenario diagram
* @param participants
* Participants from editor
*/
private static void removeParticipantsFromDiagram(Scenario scenario, EList<Participant> participants) {
List<InstanceRole> irToRemove = new ArrayList<>();
EList<InstanceRole> instanceRoles = scenario.getOwnedInstanceRoles();
for (InstanceRole ir : instanceRoles) {
List<String> participantsName = participants.stream().map(x -> x.getName()).collect(Collectors.toList());
if (!participantsName.contains(ir.getName())) {
irToRemove.add(ir);
// remove all related diagram elements
removeParticipantRelatedElements(scenario, ir);
}
}
for (InstanceRole ir : irToRemove) {
instanceRoles.remove(ir);
}
}
/**
* Remove all related elements to the giver instance role.
*
* @param scenario
* The scenario diagram
* @param instanceRole
* Instance role for which we want to remove the related elements
*/
private static void removeParticipantRelatedElements(Scenario scenario, InstanceRole instanceRole) {
removeParticipantRelatedMessages(scenario, instanceRole.getName());
removeParticipantRelatedStateFragments(scenario, instanceRole.getName());
removeParticipantRelatedCombinedFragments(scenario, instanceRole);
}
/**
* Order participants in Capella diagram in the same order they appear in editor.
*
* @param instanceRoles
* List of instance roles in the diagram
* @param participants
* List of participants in the editor
* @return true if the order was changed, false if not
*/
private static boolean reorderParticipants(List<InstanceRole> instanceRoles, EList<Participant> participants) {
boolean updateNeeded = false;
for (int i = 0; i < instanceRoles.size(); i++) {
if (!instanceRoles.get(i).getName().equals(participants.get(i).getName())) {
for (int j = 0; j < participants.size(); j++) {
if (instanceRoles.get(i).getName().equals(participants.get(j).getName())) {
((EList<InstanceRole>) instanceRoles).move(j, instanceRoles.get(i));
updateNeeded = true;
}
}
}
}
return updateNeeded;
}
/**
* Remove all messages containing the participant that has just been deleted
*
* @param scenario
* The scenario diagram
* @param participantName
* The participant that has just been deleted
*/
private static void removeParticipantRelatedMessages(Scenario scenario, String participantName) {
List<SequenceMessage> messagesToBeDeleted = new ArrayList();
EList<SequenceMessage> messages = scenario.getOwnedMessages();
for (SequenceMessage message : messages) {
if ((message.getSendingEnd() != null && !message.getSendingEnd().getCoveredInstanceRoles().isEmpty()
&& message.getSendingEnd().getCoveredInstanceRoles().get(0).getName().equals(participantName))
|| (message.getReceivingEnd() != null && !message.getReceivingEnd().getCoveredInstanceRoles().isEmpty()
&& message.getReceivingEnd().getCoveredInstanceRoles().get(0).getName().equals(participantName))) {
messagesToBeDeleted.add(message);
}
}
for (SequenceMessage message : messagesToBeDeleted) {
removeMessageFromScenario(scenario, message);
}
}
/**
* Remove from diagram all messages related to the participant that has just been deleted
*
* @param scenario
* The scenario diagram
* @param participantName
* The participant that has just been deleted
*/
protected static void removeParticipantRelatedStateFragments(Scenario scenario, String participantName) {
List<TimeLapse> stateFragmentsToBeDeleted = scenario.getOwnedTimeLapses().stream()
.filter(timelapse -> timelapse instanceof StateFragment
&& timelapse.getStart().getCoveredInstanceRoles().get(0).getName().equals(participantName))
.collect(Collectors.toList());
for (TimeLapse timeLapse : stateFragmentsToBeDeleted) {
removeStateFragmentFromScenario(scenario, timeLapse);
}
}
/**
* Remove from diagram all combined fragment related to the participant that has just been deleted
*
* @param scenario
* The scenario diagram
* @param participantName
* The participant that has just been deleted
*/
private static void removeParticipantRelatedCombinedFragments(Scenario scenario, InstanceRole instanceRole) {
List<TimeLapse> combinedFragmentsToBeDeleted = scenario.getOwnedTimeLapses().stream()
.filter(timelapse -> timelapse instanceof CombinedFragment
&& timelapse.getStart().getCoveredInstanceRoles().contains(instanceRole))
.collect(Collectors.toList());
for (TimeLapse timeLapse : combinedFragmentsToBeDeleted) {
removeCombinedFragmentFromScenario(scenario, timeLapse);
}
}
/**
* Synchronize graphical ordering in diagram
*/
private static void syncGraphicalOrdering() {
DDiagram dDiagram = EmbeddedEditorInstance.getDDiagram();
((SequenceDDiagram) dDiagram).getTarget();
EList<AnnotationEntry> ownedAnnotationEntries = dDiagram.getOwnedAnnotationEntries();
Diagram diagram = null;
for (AnnotationEntry annotationEntry : ownedAnnotationEntries) {
if ((annotationEntry != null) && (annotationEntry.getData() instanceof Diagram)) {
diagram = (Diagram) annotationEntry.getData();
break;
}
}
if (diagram != null) {
Command refreshLayoutCommand = new RefreshLayoutCommand(TransactionUtil.getEditingDomain(dDiagram), diagram,
true);
refreshLayoutCommand.execute();
EmbeddedEditorInstanceHelper.refreshAssociatedDiagram();
}
}
/**
* Create and put in order all objects necessary to display the text scenario in the Capella diagram. This includes
* sequence messages, combined fragments, state fragments
*
* @param scenario
* The scenario diagram
* @param elements
* The list of elements in editor
*/
private static void doEditingOnElements(Scenario scenario, EList<Element> elements) {
// Make sure your element is attached to a resource, otherwise this will return null
TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(scenario);
domain.getCommandStack().execute(new RecordingCommand(domain) {
@Override
protected void doExecute() {
// remove messages, state fragments, combined fragment & references that appear in diagram and do not appear in text
cleanUp(scenario, elements);
// This list will store Capella messages that have already been matched with an xtext message
List<SequenceMessage> processedCapellaMessages = new ArrayList<>();
// This list will store Capella combined fragments that have already been matched with an xtext combined fragment
ArrayList<CombinedFragment> processedCapellaCombinedFragments = new ArrayList<>();
// This list will store Capella state fragments that have already been matched with an xtext state fragments
ArrayList<org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment> processedStateFragments = new ArrayList <>();
// This list will store Capella references that have already been matched with an xtext reference
ArrayList<Reference> processedReferences = new ArrayList<>();
editElements(scenario, elements, processedCapellaMessages, processedCapellaCombinedFragments, processedStateFragments, processedReferences);
// Reorder scenario, this means reordering the interaction fragments and sequence messages lists
reorderCapellaScenario(scenario, elements);
// refresh visual editor
syncGraphicalOrdering();
}
/**
* Reorder interaction fragments and messages (their start, end and execution end) according to the xtext scenario
*
* @param scenario
* The scenario diagram
* @param elements
* The list of elements in editor
*/
private void reorderCapellaScenario(Scenario scenario, EList<Element> elements) {
// compose new lists of sequence messages and interaction fragments, ordered based on xtext messages
List<SequenceMessage> capellaSequenceMessages = new ArrayList<>();
List<InteractionFragment> interactionFragments = new ArrayList<>();
List<InteractionFragment> executionEndsToProcess = new ArrayList<>();
// this list will store Capella messages that are already matched with xtext messages
List<SequenceMessage> processedCapellaMessages = new ArrayList<>();
// this list will store Capella combined fragments that are already matched with xtext combined fragments
ArrayList<CombinedFragment> processedCapellaCombinedFragments = new ArrayList<>();
reorderCapellaFragments(scenario, elements, capellaSequenceMessages, interactionFragments,
executionEndsToProcess, processedCapellaMessages, processedCapellaCombinedFragments);
// Replace sequence message list and interaction fragments list in the real scenario
// with the newly computed lists
scenario.getOwnedInteractionFragments().clear();
scenario.getOwnedInteractionFragments().addAll(interactionFragments);
scenario.getOwnedMessages().clear();
scenario.getOwnedMessages().addAll(capellaSequenceMessages);
}
/**
* Reorder interaction fragments and messages (their start, end and execution end) according to the xtext scenario
*
* @param scenario
* The scenario diagram
* @param elements
* The list of elements in editor
* @param processedCapellaMessages
* @param processedCapellaCombinedFragments
*/
private void reorderCapellaFragments(Scenario scenario, EList<Element> elements,
List<SequenceMessage> capellaSequenceMessages, List<InteractionFragment> interactionFragments,
List<InteractionFragment> executionEndsToProcess, List<SequenceMessage> processedCapellaMessages,
ArrayList<CombinedFragment> processedCapellaCombinedFragments) {
for (Iterator<Element> iterator = elements.iterator(); iterator.hasNext();) {
EObject elementFromXtext = iterator.next();
if (elementFromXtext instanceof ParticipantDeactivation) {
reorderCapellaDeactivationMessages((ParticipantDeactivation) elementFromXtext, interactionFragments,
capellaSequenceMessages, executionEndsToProcess);
} else if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) {
reorderCapellaSequenceMessages(scenario,
(org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) elementFromXtext,
interactionFragments, capellaSequenceMessages, executionEndsToProcess, processedCapellaMessages);
} else if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) {
org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment textCombinedFragment =
(org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) elementFromXtext;
CombinedFragment capellaCombinedFragment = getCorrespondingCapellaCombinedFragment(scenario,
textCombinedFragment, processedCapellaCombinedFragments);
if (capellaCombinedFragment != null) {
// start the Combined Fragment Block
interactionFragments.add(capellaCombinedFragment.getStart());
List<InteractionOperand> orderedCapellaOperands =
getOrderedCapellaInteractionOperands(scenario, textCombinedFragment, capellaCombinedFragment);
// add content in the first operand of the Combined Fragment Block
interactionFragments.add(orderedCapellaOperands.get(0));
reorderCapellaFragments(scenario, textCombinedFragment.getBlock().getBlockElements(),
capellaSequenceMessages, interactionFragments, executionEndsToProcess, processedCapellaMessages,
processedCapellaCombinedFragments);
// reorder the other operands
EList<Operand> textOperands = textCombinedFragment.getOperands();
for (int i = 0; i < textOperands.size(); i++) {
InteractionOperand operand = orderedCapellaOperands.get(i + 1);
// add content in the first operand of the Combined Fragment Block
interactionFragments.add(operand);
reorderCapellaFragments(scenario, textOperands.get(i).getBlock().getBlockElements(),
capellaSequenceMessages, interactionFragments, executionEndsToProcess, processedCapellaMessages,
processedCapellaCombinedFragments);
}
// finish the Combined Fragment Block
interactionFragments.add(capellaCombinedFragment.getFinish());
}
} else if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) {
StateFragment stateFragment = getCorrespondingCapellaStateFragment(scenario, interactionFragments,
(org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) elementFromXtext);
if (stateFragment != null) {
interactionFragments.add(stateFragment.getStart());
interactionFragments.add(stateFragment.getFinish());
}
} else if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.Reference) {
InteractionUse reference = getCorrespondingCapellaReference(scenario, (Reference) elementFromXtext, interactionFragments);
if (reference != null) {
interactionFragments.add(reference.getStart());
interactionFragments.add(reference.getFinish());
}
}
}
}
/**
* Move the interaction fragment representing the execution end to the correct position in the interaction
* fragments list, according to the xtext scenario
*
* @param scenario
* The scenario diagram
* @param participantDeactivationMessage
* The ParticipantDeactivation message in the xtext scenario
* @param interactionFragments
* The new list of ordered interaction fragments that will be used to update the Capella diagram
* according to the xtext scenario
* @param capellaSequenceMessages
* The new list of Capella sequence messages that will be used to update the Capella diagram
* according to the xtext scenario
* @param executionEndsToProcess
* List of execution ends that have to be processed (moved on the correct order in the list of
* interaction fragments.
*/
private void reorderCapellaDeactivationMessages(ParticipantDeactivation participantDeactivationMessage,
List<InteractionFragment> interactionFragments, List<SequenceMessage> capellaSequenceMessages,
List<InteractionFragment> executionEndsToProcess) {
// This is a ParticipantDeactivationMessage, so the execution on the corresponding timeline finished.
// We must move the interaction fragment representing the execution end on the correct position in the
// ownedInteractionFragments ordered list
// find the timeline (instance role) of the execution that has to end. Search by participant name
InstanceRole instanceRole = EmbeddedEditorInstanceHelper
.getInstanceRole(participantDeactivationMessage.getName());
doDeactivationSequenceMessageForReorder(interactionFragments, instanceRole, executionEndsToProcess,
capellaSequenceMessages);
}
/**
* Move the interaction fragments belonging to a sequence message to the correct position in the interaction
* fragments list, according to the xtext scenario
*
* @param scenario
* The scenario diagram
* @param elementFromXtext
* The element in the xtext scenario
* @param interactionFragments
* The new list of ordered interaction fragments that will be used to update the Capella diagram
* according to the xtext scenario
* @param capellaSequenceMessages
* The new list of Capella sequence messages that will be used to update the Capella diagram
* according to the xtext scenario
* @param executionEndsToProcess
* List of execution ends that have to be processed (moved on the correct order in the list of
* interaction fragments.
* @param processedCapellaMessages
*/
private void reorderCapellaSequenceMessages(Scenario scenario, Message elementFromXtext,
List<InteractionFragment> interactionFragments, List<SequenceMessage> capellaSequenceMessages,
List<InteractionFragment> executionEndsToProcess, List<SequenceMessage> processedCapellaMessages) {
// This is a sequence message, it can be with execution and/or with return
if (foundInstanceRolesOnMessageEnds(elementFromXtext)) {
SequenceMessage capellaSequenceMessage = getCorrespondingCapellaSequenceMessage(scenario, elementFromXtext, processedCapellaMessages);
if (capellaSequenceMessage != null) {
capellaSequenceMessages.add(capellaSequenceMessage);
// insert message ends in interaction fragments list
if(capellaSequenceMessage.getSendingEnd() != null) {
interactionFragments.add(capellaSequenceMessage.getSendingEnd());
}
if(capellaSequenceMessage.getReceivingEnd() != null) {
interactionFragments.add(capellaSequenceMessage.getReceivingEnd());
}
if (!(capellaSequenceMessage.getKind().equals(MessageKind.CREATE)
|| capellaSequenceMessage.getKind().equals(MessageKind.DELETE))) {
Execution execution = getExecutionForSequenceMessage(scenario, capellaSequenceMessage);
// For simple messages, execution end will be added here, if no return branch
if (!hasExecution(elementFromXtext)) {
if (!hasReturn(elementFromXtext)) {
if (execution != null)
interactionFragments.add(execution.getFinish());
} else {
// Simple message has return branch, add the return sequence message here
SequenceMessage capellaReturnSequenceMessage = SequenceMessageExt
.getOppositeSequenceMessage(capellaSequenceMessage);
if (capellaReturnSequenceMessage == null) {
capellaReturnSequenceMessage = SequenceMessageExt.findReplySequenceMessage(capellaSequenceMessage);
}
interactionFragments.add(capellaReturnSequenceMessage.getSendingEnd());
interactionFragments.add(capellaReturnSequenceMessage.getReceivingEnd());
capellaSequenceMessages.add(capellaReturnSequenceMessage);
}
} else {
// For withExecution messages, execution end will be added when deactivate message will be found.
// At that point, the return message will be moved in order, too, if this is the case
if (execution != null)
executionEndsToProcess.add(execution.getFinish());
}
}
}
}
}
/**
* Check if message ends are on valid timelines (instance roles)
*
* @param elementFromXtext
* The element in the xtext scenario
* @return true if instance roles found, false otherwise
*/
private boolean foundInstanceRolesOnMessageEnds(Message elementFromXtext) {
if (elementFromXtext instanceof SequenceMessageType) {
InstanceRole source = EmbeddedEditorInstanceHelper
.getInstanceRole(((SequenceMessageType) elementFromXtext).getSource());
InstanceRole target = EmbeddedEditorInstanceHelper
.getInstanceRole(((SequenceMessageType) elementFromXtext).getTarget());
return source != null && target != null;
}
if (elementFromXtext instanceof ArmTimerMessage) {
InstanceRole timeline = EmbeddedEditorInstanceHelper
.getInstanceRole(((ArmTimerMessage) elementFromXtext).getParticipant());
return timeline != null;
}
if (elementFromXtext instanceof LostMessage) {
InstanceRole timeline = EmbeddedEditorInstanceHelper
.getInstanceRole(((LostMessage) elementFromXtext).getSource());
return timeline != null;
}
if (elementFromXtext instanceof FoundMessage) {
InstanceRole timeline = EmbeddedEditorInstanceHelper
.getInstanceRole(((FoundMessage) elementFromXtext).getTarget());
return timeline != null;
}
return false;
}
});
}
/**
* Create all objects necessary to display the text scenario in the Capella diagram. This includes sequence messages,
* combined fragments, state fragments
*
* @param scenario
* The scenario diagram
* @param elements
* The list of elements in editor
* @param processedCapellaMessages
* @param processedCapellaCombinedFragments
*/
private static void editElements(Scenario scenario, EList<Element> elements,
List<SequenceMessage> processedCapellaMessages, ArrayList<CombinedFragment> processedCapellaCombinedFragments,
ArrayList<org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment> previousStateFragments,
ArrayList<Reference> previousReferences) {
EList<SequenceMessage> capellaSequenceMessages = scenario.getOwnedMessages();
for (Iterator<Element> iterator = elements.iterator(); iterator.hasNext();) {
EObject xtextElement = iterator.next();
if (xtextElement instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.Message
&& !(xtextElement instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.ParticipantDeactivation)) {
// This is a sequence message, it can be with execution and/or with return
org.polarsys.capella.scenario.editor.dsl.textualScenario.Message xtextSeqMessage =
(org.polarsys.capella.scenario.editor.dsl.textualScenario.Message) xtextElement;
InstanceRole source = null;
InstanceRole target = null;
if (xtextSeqMessage instanceof SequenceMessageType) {
source = EmbeddedEditorInstanceHelper.getInstanceRole(((SequenceMessageType) xtextSeqMessage).getSource());
target = EmbeddedEditorInstanceHelper.getInstanceRole(((SequenceMessageType) xtextSeqMessage).getTarget());
} else if (xtextSeqMessage instanceof LostMessage) {
source = EmbeddedEditorInstanceHelper.getInstanceRole(((LostMessage) xtextSeqMessage).getSource());
} else if (xtextSeqMessage instanceof FoundMessage) {
target = EmbeddedEditorInstanceHelper.getInstanceRole(((FoundMessage) xtextSeqMessage).getTarget());
} else if (xtextSeqMessage instanceof ArmTimerMessage) {
source = EmbeddedEditorInstanceHelper.getInstanceRole(((ArmTimerMessage) xtextSeqMessage).getParticipant());
target = source;
}
if (!foundMatchingCapellaMessage(capellaSequenceMessages, xtextSeqMessage, processedCapellaMessages)) {
SequenceMessage sequenceMessage = createCapellaSequenceMessage(scenario, source, target, xtextSeqMessage);
capellaSequenceMessages.add(sequenceMessage);
processedCapellaMessages.add(sequenceMessage);
// if message has return branch, create corresponding Capella return message
if (source != null && target != null && hasReturn(xtextSeqMessage)) {
SequenceMessage opposingSequenceMessage = createCapellaSequenceMessage(scenario, target, source,
xtextSeqMessage, true);
Execution execution = getExecutionForSequenceMessage(scenario, sequenceMessage);
if (execution != null)
execution.setFinish(opposingSequenceMessage.getSendingEnd());
capellaSequenceMessages.add(opposingSequenceMessage);
}
}
} else if (xtextElement instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) {
// Check if we need to create CombinedFragment
CombinedFragment capellaFragment = getCorrespondingCapellaCombinedFragment(scenario,
(org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) xtextElement,
processedCapellaCombinedFragments);
if (capellaFragment == null) {
InteractionFragment lastInteractionFragment = null;
if (!capellaSequenceMessages.isEmpty()) {
SequenceMessage lastSequenceMessage = capellaSequenceMessages.get(capellaSequenceMessages.size() - 1);
lastInteractionFragment = lastSequenceMessage.getReceivingEnd();
}
createCapellaCombinedFragmentBlock(scenario,
(org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) xtextElement,
lastInteractionFragment, processedCapellaMessages, processedCapellaCombinedFragments,
previousStateFragments, previousReferences);
} else {
// combined fragment found, check its contents
editElements(scenario,
((org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) xtextElement).getBlock()
.getBlockElements(),
processedCapellaMessages, processedCapellaCombinedFragments, previousStateFragments, previousReferences);
for (Operand operand : ((org.polarsys.capella.scenario.editor.dsl.textualScenario.CombinedFragment) xtextElement)
.getOperands()) {
editElements(scenario, operand.getBlock().getBlockElements(), processedCapellaMessages,
processedCapellaCombinedFragments, previousStateFragments, previousReferences);
}
}
} else if (xtextElement instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) {
previousStateFragments
.add((org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) xtextElement);
editStateFragment(scenario,
(org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) xtextElement,
previousStateFragments);
} else if (xtextElement instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.Reference) {
previousReferences.add((Reference) xtextElement);
editReference(scenario, (org.polarsys.capella.scenario.editor.dsl.textualScenario.Reference) xtextElement,
previousReferences);
}
}
}
/**
* Clean up messages, state fragments, combined fragments and references that appear in diagram and do not appear in text
*
* @param scenario
* Scenario
* @param model
* Domain Model - xtext
*/
private static void cleanUp(Scenario scenario, List<Element> elements) {
List<Element> allXtextElements = getAllXtextElements(elements, new ArrayList<>());
cleanUpMessages(scenario, allXtextElements);
cleanUpStateFragments(scenario, allXtextElements);
cleanUpCombinedFragments(scenario, allXtextElements);
cleanUpReferences(scenario, allXtextElements);
}
/**
* Check to see if there is any message matching the given xtext message, that was not yet processed
*
* @param capellaSequenceMessages
* List of sequence messages to search into
* @param processedXtextMessages
* @param source
* Source instance role
* @param target
* Target instance role
* @param messageName
* Message name
* @param processedCapellaMessages
* List of Capella messages that have already been matched with xtext messages
* @return true if a message with these attributes was found, false otherwise
*/
private static boolean foundMatchingCapellaMessage(EList<SequenceMessage> capellaSequenceMessages,
org.polarsys.capella.scenario.editor.dsl.textualScenario.Message seqMessage,
List<SequenceMessage> processedCapellaMessages) {
// find matching Capella message that was not previously processed
List<SequenceMessage> matchingMessages = capellaSequenceMessages.stream()
.filter(x -> !processedCapellaMessages.contains(x) && isSameMessage(seqMessage, x))
.collect(Collectors.toList());
if (!matchingMessages.isEmpty()) {
processedCapellaMessages.add(matchingMessages.get(0));
return true;
}
return false;
}
/**
* Check to see if the given message has return branch
*
* @param elementFromXtext
* The element representing the message in the xtext editor
* @return true if message has return branch, false otherwise
*/
private static boolean hasReturn(Message elementFromXtext) {
if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage)
return ((org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) elementFromXtext).getReturn() != null;
return false;
}
/**
* Check to see if the given message has execution (withExecution keyword present)
*
* @param elementFromXtext
* The element representing the message in the xtext editor
* @return true if message has execution, false otherwise
*/
protected static boolean hasExecution(Message elementFromXtext) {
if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) {
return ((org.polarsys.capella.scenario.editor.dsl.textualScenario.SequenceMessage) elementFromXtext).getExecution() != null;
}
if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.ArmTimerMessage) {
return ((org.polarsys.capella.scenario.editor.dsl.textualScenario.ArmTimerMessage) elementFromXtext).getExecution() != null;
}
if (elementFromXtext instanceof org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage) {
return ((org.polarsys.capella.scenario.editor.dsl.textualScenario.FoundMessage) elementFromXtext).getExecution() != null;
}
return false;
}
/**
* Create/update a state fragment using the information in the xtext scenario
*
* @param scenario
* The scenario diagram
* @param xtextElement
* The element representing the state fragment in the xtext scenario
* @param previousEditorStateFragments
* The list of previously occurring matching state fragments
*/
private static void editStateFragment(Scenario scenario,
org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment xtextElement,
List<org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment> previousEditorStateFragments) {
InstanceRole instanceRole = EmbeddedEditorInstanceHelper.getInstanceRole(xtextElement.getTimeline());
EList<TimeLapse> ownedTimeLapses = scenario.getOwnedTimeLapses();
List<EObject> simillarStateFragments = previousEditorStateFragments.stream()
.filter(x -> ((org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) x).getTimeline()
.equals(xtextElement.getTimeline())
&& ((org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) x).getName()
.equals(xtextElement.getName())
&& ((org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment) x).getKeyword()
.equals(xtextElement.getKeyword()))
.collect(Collectors.toList());
EObject relatedElement = getRelatedElement(instanceRole, xtextElement);
if (relatedElement == null)
return;
List<TimeLapse> capellaStateFragments = getStateFragmentsWithGivenAttributes(scenario,
instanceRole, xtextElement, relatedElement);
if (capellaStateFragments.isEmpty() || capellaStateFragments.size() < simillarStateFragments.size()) {
InteractionState interactionStateStart = createInteractionState(DslConstants.START, relatedElement, instanceRole);
InteractionState interactionStateEnd = createInteractionState(DslConstants.FINISH, relatedElement, instanceRole);
scenario.getOwnedInteractionFragments().add(interactionStateStart);
scenario.getOwnedInteractionFragments().add(interactionStateEnd);
StateFragment stateFragment = createStateFragment(interactionStateStart, interactionStateEnd, relatedElement);
ownedTimeLapses.add(stateFragment);
}
}
/**
* Return a list of state fragments with the given attributes (instance role and related element)
*
* @param scenario
* The scenario diagram
* @param instanceRole
* The timeline for this state fragment
* @param xtextElement
* The element representing the state fragment in the xtext scenario
* @param previousEditorStateFragments
* The list of previously occurring matching state fragments
* @param relatedElement
* The element representing Capella abstract function or abstract state related with a state fragment
* @return the list of state fragments with the given attributes
*/
private static List<TimeLapse> getStateFragmentsWithGivenAttributes(Scenario scenario,
InstanceRole instanceRole, org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment xtextElement,
EObject relatedElement) {
if (xtextElement.getKeyword().equals(DslConstants.FUNCTION))
return scenario.getOwnedTimeLapses().stream()
.filter(
x -> x instanceof StateFragment && relatedElement.equals(((StateFragment) x).getRelatedAbstractFunction())
&& ((StateFragment) x).getStart().getCoveredInstanceRoles().get(0).equals(instanceRole))
.collect(Collectors.toList());
return scenario.getOwnedTimeLapses().stream()
.filter(x -> x instanceof StateFragment && relatedElement.equals(((StateFragment) x).getRelatedAbstractState())
&& ((StateFragment) x).getStart().getCoveredInstanceRoles().get(0).equals(instanceRole))
.collect(Collectors.toList());
}
/**
* Return the related abstract function or abstract state for a given state fragment
*
* @param scenario
* The scenario diagram
* @param instanceRole
* The timeline for this state fragment
* @param stateFragment
* The element representing the state fragment in the xtext scenario
* @return the related abstract function or abstract state for the given state fragment
*/
private static EObject getRelatedElement(InstanceRole instanceRole,
org.polarsys.capella.scenario.editor.dsl.textualScenario.StateFragment stateFragment) {
if (stateFragment.getKeyword().equals(DslConstants.FUNCTION)) {
return getCorrespondingCapellaAbstractFunction(instanceRole, stateFragment.getName());
}
return getCorrespondingCapellaAbstractState(instanceRole, stateFragment.getName(), stateFragment.getKeyword());
}
/**
* Create a state fragment with the given attributes
*
* @param interactionStateStart
* InteractionState representing the start of the state fragment
* @param interactionStateEnd
* InteractionState representing the end of the state fragment
* @param relatedElement
* The related abstract function or abstract state for the given state fragment
* @return the newly created state fragment
*/
private static StateFragment createStateFragment(InteractionState interactionStateStart,
InteractionState interactionStateEnd, EObject relatedElement) {
StateFragment stateFragment = InteractionFactory.eINSTANCE.createStateFragment();
stateFragment.setStart(interactionStateStart);
stateFragment.setFinish(interactionStateEnd);
if (relatedElement instanceof AbstractFunction) {
stateFragment.setRelatedAbstractFunction((AbstractFunction) relatedElement);
} else {
stateFragment.setRelatedAbstractState((AbstractState) relatedElement);
}
return stateFragment;
}
/**
* Create an interaction state with the given attributes
*
* @param name
* The name of the interaction state
* @param relatedElement
* The related abstract function or abstract state for the given state fragment
* @param instanceRole
* Timeline (instance role) for the interaction state
* @return the newly created interaction state
*/
private static InteractionState createInteractionState(String name, EObject relatedElement, InstanceRole instanceRole) {
InteractionState interactionState = InteractionFactory.eINSTANCE.createInteractionState();
interactionState.setName(name);
interactionState.getCoveredInstanceRoles().add(instanceRole);
if (relatedElement instanceof AbstractFunction) {
interactionState.setRelatedAbstractFunction((AbstractFunction) relatedElement);
} else {
interactionState.setRelatedAbstractState((AbstractState) relatedElement);
}
return interactionState;
}
/**
* Return the corresponding Capella abstract function with the given attributes
*
* @param scenario
* The scenario diagram
* @param instanceRole
* Timeline (instance role) for the interaction state
* @param name
* Name of the abstract function
* @return the corresponding Capella abstract function or null if no abstract function with the given attributes was found
*/
private static AbstractFunction getCorrespondingCapellaAbstractFunction(InstanceRole instanceRole, String name) {
List<AbstractFunction> availableFunctions = EmbeddedEditorInstanceHelper.getAllocatedFunctions(instanceRole);
for (AbstractFunction availableFunction : availableFunctions) {
if (availableFunction.getName().equals(name))
return availableFunction;
}
return null;
}
/**
* Return the corresponding Capella abstract state with the given attributes
*
* @param scenario
* The scenario diagram
* @param instanceRole
* Timeline (instance role) for the interaction state
* @param name
* Name of the abstract state
* @param keyword
* STATE or MODE
* @return the corresponding Capella abstract state or null if no abstract state with the given attributes was found
*/
private static AbstractState getCorrespondingCapellaAbstractState(InstanceRole instanceRole, String name, String keyword) {
List<AbstractState> availableStates;
if (keyword.equals(DslConstants.STATE)) {
availableStates = EmbeddedEditorInstanceHelper.getStates(instanceRole);
} else {
availableStates = EmbeddedEditorInstanceHelper.getModes(instanceRole);
}
for (AbstractState availableState : availableStates) {
if (availableState.getName().equals(name))
return availableState;
}
return null;
}
/**
* Return the Execution object for the given Capella sequence message
*