-
Notifications
You must be signed in to change notification settings - Fork 11
/
EmbeddedEditorInstanceHelper.java
702 lines (631 loc) · 27.3 KB
/
EmbeddedEditorInstanceHelper.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
/*******************************************************************************
* 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.helper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.sirius.business.api.dialect.DialectManager;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.sequence.SequenceDDiagram;
import org.eclipse.sirius.diagram.ui.tools.api.editor.DDiagramEditor;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.model.XtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
import org.polarsys.capella.common.data.modellingcore.AbstractNamedElement;
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.capellacore.CapellaElement;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.ExchangeItemAllocation;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.ctx.SystemAnalysis;
import org.polarsys.capella.core.data.epbs.EPBSArchitecture;
import org.polarsys.capella.core.data.fa.AbstractFunction;
import org.polarsys.capella.core.data.fa.FunctionInputPort;
import org.polarsys.capella.core.data.fa.FunctionOutputPort;
import org.polarsys.capella.core.data.fa.FunctionalExchange;
import org.polarsys.capella.core.data.information.AbstractEventOperation;
import org.polarsys.capella.core.data.information.AbstractInstance;
import org.polarsys.capella.core.data.interaction.InstanceRole;
import org.polarsys.capella.core.data.interaction.Scenario;
import org.polarsys.capella.core.data.interaction.ScenarioKind;
import org.polarsys.capella.core.data.interaction.StateFragment;
import org.polarsys.capella.core.data.la.LogicalArchitecture;
import org.polarsys.capella.core.data.oa.OperationalActor;
import org.polarsys.capella.core.data.oa.OperationalAnalysis;
import org.polarsys.capella.core.data.oa.Role;
import org.polarsys.capella.core.data.pa.PhysicalArchitecture;
import org.polarsys.capella.core.model.helpers.BlockArchitectureExt;
import org.polarsys.capella.core.model.helpers.OperationalAnalysisExt;
import org.polarsys.capella.core.model.helpers.ScenarioExt;
import org.polarsys.capella.core.sequencediag.ScenarioService;
import org.polarsys.capella.core.sirius.analysis.FaServices;
import org.polarsys.capella.core.sirius.analysis.InteractionServices;
import org.polarsys.capella.scenario.editor.EmbeddedEditorInstance;
public class EmbeddedEditorInstanceHelper {
public static final List<String> PARTICIPANT_KEYWORDS = Arrays.asList(DslConstants.ACTOR, DslConstants.ENTITY,
DslConstants.ROLE, DslConstants.ACTIVITY, DslConstants.COMPONENT, DslConstants.CONFIGURATION_ITEM,
DslConstants.FUNCTION);
public static EList<InstanceRole> getAvailableInstanceRolesInDiagram() {
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return currentScenario.getOwnedInstanceRoles();
}
public static List<InstanceRole> getAvailableInstanceRoles() {
List<String> participantKeywords = EmbeddedEditorInstanceHelper.PARTICIPANT_KEYWORDS;
List<InstanceRole> instanceRoles = new ArrayList<InstanceRole>();
for (String keyword : participantKeywords) {
if (checkValidKeyword(keyword)) {
List<InstanceRole> availableInstanceRoleForKeyword;
if (keyword.equals(DslConstants.FUNCTION) || keyword.equals(DslConstants.ACTIVITY)) {
availableInstanceRoleForKeyword = getAvailableElements(keyword).stream()
.map(element -> ((AbstractFunction) element).getRepresentingInstanceRoles()).flatMap(Collection::stream)
.distinct().collect(Collectors.toList());
} else {
availableInstanceRoleForKeyword = getAvailableElements(keyword).stream()
.map(element -> ((AbstractInstance) element).getRepresentingInstanceRoles()).flatMap(Collection::stream)
.distinct().collect(Collectors.toList());
}
instanceRoles.addAll(availableInstanceRoleForKeyword);
}
}
return instanceRoles.stream().distinct().collect(Collectors.toList());
}
private static List<EObject> getAvailableAbstractInstances() {
List<String> participantKeywords = EmbeddedEditorInstanceHelper.PARTICIPANT_KEYWORDS;
List<EObject> instances = new ArrayList<>();
for (String keyword : participantKeywords) {
if (checkValidKeyword(keyword)) {
instances.addAll(getAvailableElements(keyword));
}
}
return instances.stream().distinct().collect(Collectors.toList());
}
public static String getScenarioType() {
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return currentScenario.getKind().toString();
}
public static boolean isInteractionScenario() {
Scenario scenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return scenario.getKind().equals(ScenarioKind.INTERACTION);
}
public static boolean isInterfaceScenario() {
Scenario scenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return scenario.getKind().equals(ScenarioKind.INTERFACE);
}
public static boolean isESScenario() {
Scenario scenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return ((scenario.getKind() == ScenarioKind.INTERACTION) || (scenario.getKind() == ScenarioKind.DATA_FLOW));
}
public static boolean isCEScenario() {
return ScenarioExt.isDataFlowBehaviouralScenario(EmbeddedEditorInstance.getAssociatedScenarioDiagram());
}
public static boolean isFEScenario() {
return ScenarioExt.isDataFlowFunctionalScenario(EmbeddedEditorInstance.getAssociatedScenarioDiagram());
}
public static boolean isFSScenario() {
return ScenarioExt.isFunctionalScenario(EmbeddedEditorInstance.getAssociatedScenarioDiagram());
}
public static AbstractFunction getSourceFunctionOfExchange(FunctionalExchange exchange) {
FunctionOutputPort source = exchange.getSourceFunctionOutputPort();
if(source != null && source.eContainer() instanceof AbstractFunction) {
return (AbstractFunction) source.eContainer();
}
return null;
}
public static String getSourceFunctionNameOfExchange(FunctionalExchange exchange) {
AbstractFunction function = getSourceFunctionOfExchange(exchange);
return function != null ? function.getName() : null;
}
public static AbstractFunction getTargetFunctionOfExchange(FunctionalExchange exchange) {
FunctionInputPort target = exchange.getTargetFunctionInputPort();
if(target != null && target.eContainer() instanceof AbstractFunction) {
return (AbstractFunction) target.eContainer();
}
return null;
}
public static String getTargetFunctionNameOfExchange(FunctionalExchange exchange) {
AbstractFunction function = getTargetFunctionOfExchange(exchange);
return function != null ? function.getName() : null;
}
public static EObject getScenarioLevel() {
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
return BlockArchitectureExt.getRootBlockArchitecture(currentScenario);
}
/**
* get the names of the available exchanges
*
* @param source
* - the name of the source element
* @param target
* - the name of the target element
* @return list of exchanges
*
*/
public static List<String> getExchangeNames(String source, String target) {
List<String> messages = new ArrayList<String>();
List<AbstractEventOperation> exchanges = getAvailableExchanges(source, target);
for (AbstractEventOperation exchange : exchanges) {
if (isInterfaceScenario()) {
messages.add(((ExchangeItemAllocation) exchange).getAllocatedItem().getName());
} else {
messages.add(exchange.getName());
}
}
return messages;
}
/**
* get the names of the available exchanges
*
* @param source
* - the name of the source element
* @param target
* - the name of the target element
* @return list of exchanges
*
*/
public static List<AbstractEventOperation> getExchangeMessages(String source, String target) {
List<AbstractEventOperation> exchanges = getAvailableExchanges(source, target);
return exchanges;
}
/**
* returns the list of available exchanges possible to be inserted between source and target
*
* @param source
* - the name of the source element
* @param target
* - the name of the target element
* @return list of exchanges
*
*/
public static List<AbstractEventOperation> getAvailableExchanges(String source, String target) {
InstanceRole sourceIr = getInstanceRoleFromScenario(source);
InstanceRole targetIr = getInstanceRoleFromScenario(target);
if (sourceIr != null && targetIr != null) {
return getAllAvailableExchanges(sourceIr.getRepresentedInstance(), targetIr.getRepresentedInstance());
}
List<AbstractEventOperation> availableExchanges = new ArrayList<AbstractEventOperation>();
List<EObject> sourceCandidates = getAbstractInstanceCandidates(source);
List<EObject> targetCandidates = getAbstractInstanceCandidates(target);
for (EObject sourceObj : sourceCandidates) {
for (EObject targetObj : targetCandidates) {
AbstractInstance sourceInst = sourceObj instanceof AbstractInstance ? (AbstractInstance) sourceObj : null;
AbstractInstance targetInst = targetObj instanceof AbstractInstance ? (AbstractInstance) targetObj : null;
availableExchanges.addAll(getAllAvailableExchanges(sourceInst, targetInst));
}
}
return availableExchanges;
}
/**
* returns the list of available exchanges possible to be inserted between source and target
*
* @param source
* - the source element
* @param target
* - the target element
* @return list of exchanges
*
*/
@SuppressWarnings("unchecked")
private static List<AbstractEventOperation> getAllAvailableExchanges(AbstractInstance sourceInst,
AbstractInstance targetInst) {
List<AbstractEventOperation> exchangesAvailable = new ArrayList<AbstractEventOperation>();
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
switch (currentScenario.getKind()) {
case FUNCTIONAL:
if (sourceInst instanceof AbstractFunction && targetInst instanceof AbstractFunction) {
exchangesAvailable.addAll(DataFlowHelper
.getAvailableFonctionalExchangesFromFunctions((AbstractFunction) sourceInst, (AbstractFunction) targetInst)
.stream().filter(x -> x instanceof AbstractEventOperation).collect(Collectors.toList()));
}
break;
case INTERFACE:
List<CapellaElement> exchanges = SelectInvokedOperationModelForSharedDataAndEvent
.getAvailableExchangeItems(sourceInst, targetInst, false);
for (CapellaElement exchange : exchanges) {
if (exchange instanceof ExchangeItemAllocation) {
exchangesAvailable.add((AbstractEventOperation) exchange);
}
}
break;
case DATA_FLOW:
case INTERACTION:
if (ScenarioExt.isFunctionalScenario(currentScenario)) {
if (sourceInst instanceof AbstractFunction && targetInst instanceof AbstractFunction) {
exchangesAvailable = DataFlowHelper
.getAvailableFonctionalExchangesFromFunctions((AbstractFunction) sourceInst,
(AbstractFunction) targetInst)
.stream().filter(x -> x instanceof AbstractEventOperation).collect(Collectors.toList());
}
} else {
Component sourceComp = sourceInst != null && sourceInst.getAbstractType() instanceof Component
? (Component) sourceInst.getAbstractType()
: null;
Component targetComp = targetInst != null && targetInst.getAbstractType() instanceof Component
? (Component) targetInst.getAbstractType()
: null;
exchangesAvailable.addAll(
(List<AbstractEventOperation>) DataFlowHelper.getAvailableComponentExchanges(sourceComp, targetComp));
exchangesAvailable.addAll(DataFlowHelper.getAvailableFonctionalExchanges(sourceInst, targetInst).stream()
.filter(x -> x instanceof AbstractEventOperation).collect(Collectors.toList()));
}
break;
default:
break;
}
return exchangesAvailable;
}
/**
* returns the list of available elements as names that could be inserted
*
* @param keyword
* @return boolean
*
*/
public static List<String> getAvailablePartNames(String keyword) {
List<String> partNames = new ArrayList<String>();
Collection<? extends EObject> elements = getAvailableElements(keyword);
for (EObject element : elements) {
if (element instanceof AbstractNamedElement) {
partNames.add(((AbstractNamedElement) element).getName());
}
}
return partNames;
}
/**
* returns the list of available abstract functions that could be inserted on the current scenario
*
* @return collection of elements
*
*/
private static Collection<? extends EObject> getAvailableAbstractFunctions() {
Collection<? extends EObject> elements = FaServices.getFaServices().getAllAbstractFunctions(
BlockArchitectureExt.getRootBlockArchitecture(EmbeddedEditorInstance.getAssociatedScenarioDiagram()));
return elements;
}
/**
* returns the list of available components that could be inserted on the current scenario
*
* @return collection of elements
*
*/
public static Collection<? extends EObject> getAvailableComponents() {
Scenario scenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
List<Part> elements = (new InteractionServices())
.getISScopeInsertComponents(EmbeddedEditorInstance.getAssociatedScenarioDiagram());
for (InstanceRole role : scenario.getOwnedInstanceRoles()) {
if (role.getRepresentedInstance() instanceof Part) {
elements.add((Part) role.getRepresentedInstance());
}
}
return elements;
}
/**
* returns the list of available actors that could be inserted on the current scenario
*
* @return collection of elements
*
*/
public static Collection<? extends EObject> getAvailableActors() {
Scenario scenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
BlockArchitecture analysis = BlockArchitectureExt.getRootBlockArchitecture(scenario);
if (analysis instanceof OperationalAnalysis) {
return getOESScopeInsertEntitiesRoles(scenario).stream()
.filter(x -> x instanceof Part && ((Part)x).getAbstractType() instanceof OperationalActor)
.collect(Collectors.toList());
}
return (new InteractionServices()).getISScopeInsertActors(scenario);
}
/**
* returns the list of available roles that could be inserted on the current scenario
*
* @return collection of elements
*
*/
public static Collection<? extends EObject> getAvailableRoles() {
return getOESScopeInsertEntitiesRoles(EmbeddedEditorInstance.getAssociatedScenarioDiagram())
.stream().filter(x -> x instanceof Role).collect(Collectors.toList());
}
private static Collection<EObject> getOESScopeInsertEntitiesRoles(Scenario scenario) {
Collection<EObject> roots = new ArrayList<EObject>();
roots.addAll(new ScenarioService().getAllMultiInstanceRoleParts(scenario));
BlockArchitecture analysis = BlockArchitectureExt.getRootBlockArchitecture(scenario);
if (analysis instanceof OperationalAnalysis) {
roots.addAll(OperationalAnalysisExt.getAllRoles((OperationalAnalysis) analysis));
}
return roots;
}
/**
* returns the list of available elements that could be inserted for the given keyword (which can be actor, component,
* function, entity, role, activity, configuration_item)
*
* @param keyword
* @return boolean
*
*/
public static Collection<? extends EObject> getAvailableElements(String keyword) {
switch (keyword) {
case DslConstants.ACTIVITY:
case DslConstants.FUNCTION:
return getAvailableAbstractFunctions();
case DslConstants.ROLE:
return getAvailableRoles();
case DslConstants.ACTOR:
return getAvailableActors();
default:
// gets all the components (non actors)
return getAvailableComponents();
}
}
/**
* returns instance role associated to the source (source is the name of the instance role)
*
* @param soruce
* (name of the instance role)
* @return InstanceRole
*
*/
public static InstanceRole getInstanceRole(String source) {
List<InstanceRole> instanceRoles = getAvailableInstanceRolesInDiagram();
InstanceRole instanceRole = null;
for (Iterator<InstanceRole> iterator = instanceRoles.iterator(); iterator.hasNext();) {
InstanceRole role = iterator.next();
if (role.getName().equals(source)) {
instanceRole = role;
break;
}
}
return instanceRole;
}
public static InstanceRole getInstanceRoleFromScenario(String source) {
List<InstanceRole> instanceRoles = getAvailableInstanceRoles();
InstanceRole instanceRole = null;
for (Iterator<InstanceRole> iterator = instanceRoles.iterator(); iterator.hasNext();) {
InstanceRole role = iterator.next();
if (role.getName().equals(source)) {
instanceRole = role;
break;
}
}
return instanceRole;
}
public static List<EObject> getAbstractInstanceCandidates(String name) {
List<EObject> instances = getAvailableAbstractInstances().stream()
.filter(inst -> (inst instanceof AbstractNamedElement && ((AbstractNamedElement) inst).getName().equals(name)))
.collect(Collectors.toList());
if (instances.isEmpty())
instances.add(null);
return instances;
}
/**
* helper function that validates that a keyword typed in a text scenario is valid, based on scenario type and
* architecture level
*
* @param keyword
* @return boolean
*
*/
public static boolean checkValidKeyword(String keyword) {
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
BlockArchitecture blockArchitecture = BlockArchitectureExt.getRootBlockArchitecture(currentScenario);
if (currentScenario.getKind() == ScenarioKind.INTERACTION) {
if (ScenarioExt.isFunctionalScenario(currentScenario)) {
return keyword.equals(DslConstants.ACTIVITY);
}
return keyword.equals(DslConstants.ENTITY) || keyword.equals(DslConstants.ACTOR)
|| keyword.equals(DslConstants.ROLE);
}
// IS and ES
if (currentScenario.getKind() == ScenarioKind.INTERFACE || currentScenario.getKind() == ScenarioKind.DATA_FLOW) {
if (blockArchitecture instanceof SystemAnalysis || blockArchitecture instanceof LogicalArchitecture
|| blockArchitecture instanceof PhysicalArchitecture) {
return keyword.equals(DslConstants.ACTOR) || keyword.equals(DslConstants.COMPONENT);
}
if (blockArchitecture instanceof EPBSArchitecture) {
return keyword.equals(DslConstants.CONFIGURATION_ITEM);
}
}
// FS Scenarios
if (currentScenario.getKind() == ScenarioKind.FUNCTIONAL) {
if (blockArchitecture instanceof SystemAnalysis || blockArchitecture instanceof LogicalArchitecture
|| blockArchitecture instanceof PhysicalArchitecture) {
return keyword.equals(DslConstants.FUNCTION);
}
}
return false;
}
public static boolean checkValidTimeline(String timelineName) {
if (timelineName == null || getInstanceRoleFromScenario(timelineName) == null)
return false;
return true;
}
/**
* Get all available state fragments (mode, state or allocated function) for a specific instance role
*
* @param type
* - the type of state fragment: mode, state or allocated function
* @param timelineName
* - the timeline name
* @return List<String> - return the list with all available state fragments
*/
public static List<String> getAvailableStateFragments(String type, String timelineName) {
if (timelineName == null || type == null) {
return new ArrayList<String>();
}
EObject element = getInstanceRoleFromScenario(timelineName);
if (element == null)
return new ArrayList<String>();
if (type.equals(DslConstants.FUNCTION))
return getAllocatedFunctionsName(element);
Collection<AbstractState> modesAndStates = ScenarioExt
.getAvailableStateModeStateFragment(((InstanceRole) element).getRepresentedInstance());
if (modesAndStates.isEmpty())
return new ArrayList<String>();
if (type.equals(DslConstants.MODE))
return getModesNames(modesAndStates);
return getStatesNames(modesAndStates);
}
private static List<String> getModesNames(Collection<AbstractState> modesAndStates) {
List<String> modesNames = modesAndStates.stream().filter(x -> x instanceof Mode).map(x -> x.getName())
.collect(Collectors.toList());
return modesNames;
}
private static List<String> getStatesNames(Collection<AbstractState> modesAndStates) {
List<String> statesNames = modesAndStates.stream().filter(x -> x instanceof State && !(x instanceof Mode))
.map(x -> x.getName()).collect(Collectors.toList());
return statesNames;
}
private static List<String> getAllocatedFunctionsName(EObject element) {
Collection<AbstractFunction> allocFunctions = ScenarioExt
.getAvailableFunctionsStateFragment(((InstanceRole) element).getRepresentedInstance());
if (!allocFunctions.isEmpty()) {
return allocFunctions.stream().map(x -> x.getName()).collect(Collectors.toList());
}
return new ArrayList<String>();
}
public static void updateModel(String content) {
XtextDocument document = EmbeddedEditorInstance.getEmbeddedEditor().getDocument();
document.set(content);
document.readOnly(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
}
});
}
public static String getModelContent() {
XtextDocument document = EmbeddedEditorInstance.getEmbeddedEditor().getDocument();
return document.get();
}
public static List<AbstractState> getStates(InstanceRole element) {
Collection<AbstractState> modesAndStates = ScenarioExt
.getAvailableStateModeStateFragment(element.getRepresentedInstance());
return modesAndStates.stream().filter(x -> x instanceof State && !(x instanceof Mode)).collect(Collectors.toList());
}
public static List<AbstractState> getModes(InstanceRole element) {
Collection<AbstractState> modesAndStates = ScenarioExt
.getAvailableStateModeStateFragment(element.getRepresentedInstance());
return modesAndStates.stream().filter(x -> x instanceof Mode).collect(Collectors.toList());
}
public static List<AbstractFunction> getAllocatedFunctions(InstanceRole element) {
return ScenarioExt.getAvailableFunctionsStateFragment(element.getRepresentedInstance()).stream()
.collect(Collectors.toList());
}
public static String getStateFragmentType(StateFragment capellaStateFragment) {
if (capellaStateFragment.getRelatedAbstractFunction() != null) {
return DslConstants.FUNCTION;
}
if (capellaStateFragment.getRelatedAbstractState() != null
&& capellaStateFragment.getRelatedAbstractState() instanceof Mode) {
return DslConstants.MODE;
}
return DslConstants.STATE;
}
public static String getStateFragmentName(StateFragment capellaStateFragment) {
if (capellaStateFragment.getRelatedAbstractFunction() != null) {
return capellaStateFragment.getRelatedAbstractFunction().getName();
} else if (capellaStateFragment.getRelatedAbstractState() != null) {
return capellaStateFragment.getRelatedAbstractState().getName();
}
return null;
}
/*
* get the list of opened and diagrams
*/
private static List<DDiagram> getOpenedRepresentations() {
List<DDiagram> diagrams = new ArrayList<DDiagram>();
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (activePage == null) {
return null;
}
for (IEditorReference ref : activePage.getEditorReferences()) {
IEditorPart editor = ref.getEditor(false);
if (editor != null && editor instanceof DDiagramEditor) {
DDiagramEditor dEditor = (DDiagramEditor) editor;
DRepresentation representation = dEditor.getRepresentation();
if (representation instanceof DDiagram) {
diagrams.add((DDiagram) representation);
}
}
}
return diagrams;
}
/*
* get the first opened sequence diagram
*/
public static DDiagram getFirstOpenedRepresentation() {
List<DDiagram> diagrams = getOpenedRepresentations();
if (!diagrams.isEmpty()) {
return diagrams.get(0);
}
return null;
}
/*
* In case we have a sequence diagram, set the initial diagram to this opened one
*/
public static void setOpenedRepresentation() {
DDiagram diagram = getFirstOpenedRepresentation();
if (diagram instanceof SequenceDDiagram) {
EmbeddedEditorInstance.setDDiagram(diagram);
}
}
/*
* check if the associated diagram of the text editor is opened
*/
public static boolean isOpenedRepresentation() {
List<DDiagram> diagrams = getOpenedRepresentations();
return diagrams.contains(EmbeddedEditorInstance.getDDiagram());
}
public static void refreshAssociatedDiagram() {
DDiagram diagram = EmbeddedEditorInstance.getDDiagram();
if (diagram != null) {
DialectManager.INSTANCE.refresh(diagram, new NullProgressMonitor());
}
}
/*
* Get all possible scenario references names
*/
public static List<String> getReferencedScenariosName() {
return getReferencedScenarios().stream().map(x -> ((Scenario) x).getName()).collect(Collectors.toList());
}
/*
* Get referenced scenario with the given name
*/
public static Scenario getScenarioWithGivenName(String name) {
List<EObject> referencedScenarios = getReferencedScenarios().stream().filter(x -> ((Scenario) x).getName().equals(name)).collect(Collectors.toList());
if (!referencedScenarios.isEmpty()) {
return (Scenario) referencedScenarios.get(0);
}
return null;
}
/*
* Get all possible scenario references
*/
public static List<EObject> getReferencedScenarios() {
ScenarioService scenarioService = new ScenarioService();
Scenario currentScenario = EmbeddedEditorInstance.getAssociatedScenarioDiagram();
List<EObject> referenceScope = scenarioService.getReferenceScope(currentScenario);
return referenceScope;
}
}