-
Notifications
You must be signed in to change notification settings - Fork 85
/
PhysicalServices.java
1704 lines (1493 loc) · 67.6 KB
/
PhysicalServices.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) 2006, 2021 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.core.sirius.analysis;
import static org.polarsys.capella.common.helpers.cache.ModelCache.getCache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.diagram.AbstractDNode;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DEdge;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.DNodeContainer;
import org.eclipse.sirius.diagram.DSemanticDiagram;
import org.eclipse.sirius.diagram.EdgeStyle;
import org.eclipse.sirius.diagram.EdgeTarget;
import org.eclipse.sirius.diagram.business.internal.metamodel.helper.MappingWithInterpreterHelper;
import org.eclipse.sirius.diagram.description.AbstractNodeMapping;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.DiagramElementMapping;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.description.style.EdgeStyleDescription;
import org.eclipse.sirius.diagram.model.business.internal.operations.DDiagramSpecOperations;
import org.eclipse.sirius.tools.api.SiriusPlugin;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.sirius.viewpoint.RGBValues;
import org.eclipse.swt.graphics.RGB;
import org.polarsys.capella.common.data.modellingcore.AbstractNamedElement;
import org.polarsys.capella.common.data.modellingcore.AbstractTrace;
import org.polarsys.capella.common.data.modellingcore.ModelElement;
import org.polarsys.capella.common.helpers.EObjectExt;
import org.polarsys.capella.common.helpers.EcoreUtil2;
import org.polarsys.capella.common.helpers.SimpleOrientedGraph;
import org.polarsys.capella.common.mdsofa.common.constant.ICommonConstants;
import org.polarsys.capella.core.business.queries.IBusinessQuery;
import org.polarsys.capella.core.business.queries.capellacore.BusinessQueriesProvider;
import org.polarsys.capella.core.commands.preferences.service.ScopedCapellaPreferencesStore;
import org.polarsys.capella.core.commands.preferences.util.PreferencesHelper;
import org.polarsys.capella.core.data.capellacore.CapellaElement;
import org.polarsys.capella.core.data.cs.AbstractDeploymentLink;
import org.polarsys.capella.core.data.cs.AbstractPathInvolvedElement;
import org.polarsys.capella.core.data.cs.AbstractPhysicalLinkEnd;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.ComponentPkg;
import org.polarsys.capella.core.data.cs.CsFactory;
import org.polarsys.capella.core.data.cs.CsPackage;
import org.polarsys.capella.core.data.cs.DeployableElement;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.cs.PhysicalLink;
import org.polarsys.capella.core.data.cs.PhysicalLinkEnd;
import org.polarsys.capella.core.data.cs.PhysicalPath;
import org.polarsys.capella.core.data.cs.PhysicalPathInvolvement;
import org.polarsys.capella.core.data.cs.PhysicalPathReference;
import org.polarsys.capella.core.data.cs.PhysicalPort;
import org.polarsys.capella.core.data.ctx.SystemComponent;
import org.polarsys.capella.core.data.fa.ComponentPort;
import org.polarsys.capella.core.data.fa.ComponentPortAllocation;
import org.polarsys.capella.core.data.helpers.cs.services.PhysicalLinkExt;
import org.polarsys.capella.core.data.la.LogicalComponent;
import org.polarsys.capella.core.data.pa.PaFactory;
import org.polarsys.capella.core.data.pa.PaPackage;
import org.polarsys.capella.core.data.pa.PhysicalArchitecture;
import org.polarsys.capella.core.data.pa.PhysicalComponent;
import org.polarsys.capella.core.data.pa.PhysicalComponentNature;
import org.polarsys.capella.core.data.pa.PhysicalComponentPkg;
import org.polarsys.capella.core.data.pa.deployment.DeploymentFactory;
import org.polarsys.capella.core.data.pa.deployment.PartDeploymentLink;
import org.polarsys.capella.core.model.helpers.BlockArchitectureExt;
import org.polarsys.capella.core.model.helpers.ComponentExt;
import org.polarsys.capella.core.model.helpers.ComponentPkgExt;
import org.polarsys.capella.core.model.helpers.PartExt;
import org.polarsys.capella.core.model.helpers.PhysicalComponentExt;
import org.polarsys.capella.core.model.helpers.PhysicalPathExt;
import org.polarsys.capella.core.model.helpers.graph.PhysicalPathInternalLinksGraph;
import org.polarsys.capella.core.model.helpers.graph.PhysicalPathInternalLinksGraph.InternalLinkEdge;
import org.polarsys.capella.core.model.helpers.graph.PhysicalPathInvolvementGraph;
import org.polarsys.capella.core.model.helpers.graph.PhysicalPathInvolvementGraph.InvolvementEdge;
import org.polarsys.capella.core.model.preferences.CapellaModelPreferencesPlugin;
import org.polarsys.capella.core.sirius.analysis.cache.DEdgeIconCache;
import org.polarsys.capella.core.sirius.analysis.cache.PhysicalPathCache;
import org.polarsys.capella.core.sirius.analysis.constants.MappingConstantsHelper;
import org.polarsys.capella.core.sirius.analysis.preferences.DiagramProcessChainPathPreferencePage;
import org.polarsys.capella.core.sirius.analysis.tool.HashMapSet;
/**
*/
public class PhysicalServices {
private static PhysicalServices __service = null;
public static PhysicalServices getService() {
if (__service == null) {
__service = new PhysicalServices();
}
return __service;
}
private static final Integer THICK_EDGE_PHYSICAL_PATH = Integer.valueOf(4);
private static final String INCOMPLETE_PHYSICAL_PATH_LABEL = "incomplete"; //$NON-NLS-1$
private static final String INVALID_PHYSICAL_PATH_LABEL = "invalid"; //$NON-NLS-1$
public boolean isMultipleDeploymentAllowed() {
return CapellaModelPreferencesPlugin.getDefault().isMultipleDeploymentAllowed();
}
/**
* Returns all semantics of source views for the given path
*/
public Collection<EObject> getPhysicalPathSources(PhysicalPath path) {
// a link is not oriented, so return both bounds
Set<EObject> result = new HashSet<>();
for (PhysicalPathInvolvement anInvolvement : PhysicalPathExt.getFlatInvolvementsOf(path,
CsPackage.Literals.PHYSICAL_LINK)) {
PhysicalLink currentExchange = (PhysicalLink) anInvolvement.getInvolved();
if ((currentExchange != null) && !currentExchange.eIsProxy()) {
EObject source = PhysicalLinkExt.getSourcePort(currentExchange);
result.add(source);
EObject target = PhysicalLinkExt.getTargetPort(currentExchange);
result.add(target);
}
}
return result;
}
/**
* Returns all semantics of target views for the given path
*/
public Collection<EObject> getPhysicalPathTargets(PhysicalPath path) {
// a link is not oriented, so return both bounds
return getPhysicalPathSources(path);
}
/**
* @param context
* @return
*/
public List<EObject> getAvailableComponentsToDeploy(PhysicalComponent context) {
IBusinessQuery query = BusinessQueriesProvider.getInstance().getContribution(PaPackage.Literals.PHYSICAL_COMPONENT,
CsPackage.Literals.ABSTRACT_DEPLOYMENT_LINK__DEPLOYED_ELEMENT);
return query.getAvailableElements(context);
}
public List<EObject> getAvailableComponentsToDeploy(Part context) {
IBusinessQuery query = BusinessQueriesProvider.getInstance().getContribution(CsPackage.Literals.PART,
CsPackage.Literals.ABSTRACT_DEPLOYMENT_LINK__DEPLOYED_ELEMENT);
List<EObject> availableElements = query.getAvailableElements(context);
return availableElements;
}
public List<EObject> getDeployedComponents(EObject context) {
if (context instanceof PhysicalComponent) {
IBusinessQuery query = BusinessQueriesProvider.getInstance().getContribution(
PaPackage.Literals.PHYSICAL_COMPONENT, CsPackage.Literals.ABSTRACT_DEPLOYMENT_LINK__DEPLOYED_ELEMENT);
return query.getCurrentElements(context, false);
} else if (context instanceof Part) {
IBusinessQuery query = BusinessQueriesProvider.getInstance().getContribution(CsPackage.Literals.PART,
CsPackage.Literals.ABSTRACT_DEPLOYMENT_LINK__DEPLOYED_ELEMENT);
return query.getCurrentElements(context, false);
}
return Collections.emptyList();
}
public List<Part> getAvailableComponentsToDeploy(Part part, boolean behaviour) {
List<Part> returnedList = new ArrayList<>();
for (EObject anElement : getAvailableComponentsToDeploy(part)) {
if (anElement instanceof Part) {
Part aPart = (Part) anElement;
if ((behaviour && isBehaviour(aPart)) || (!behaviour && isNode(aPart))) {
returnedList.add(aPart);
}
}
}
return returnedList;
}
public List<Part> getDeployedComponents(Part part, boolean behaviour) {
List<Part> returnedList = new ArrayList<>();
for (EObject anElement : getDeployedComponents(part)) {
if (anElement instanceof Part) {
Part aPart = (Part) anElement;
if ((behaviour && isBehaviour(aPart)) || (!behaviour && isNode(aPart))) {
returnedList.add(aPart);
}
}
}
return returnedList;
}
public boolean isValidPPDInvolveComponent(DSemanticDecorator context) {
return context instanceof DDiagram;
}
public boolean isValidPPDInvolvePart(DSemanticDecorator context) {
return context instanceof DDiagram;
}
public boolean isValidPPDInvolvePhysicalPath(DSemanticDecorator context) {
return context instanceof DDiagram;
}
public boolean isValidPPDInvolvePhysicalLink(EObject context, PhysicalPathInvolvement source,
PhysicalPathInvolvement target) {
return isAnEdgeInvolvementAvailableInPPD(context, source, target);
}
public boolean isValidPPDInvolvePhysicalLinkAndComponent(DSemanticDecorator context) {
return true;
}
public boolean isValidPPDInvolvePhysicalLinkAndPhysicalPath(DSemanticDecorator context) {
return context instanceof AbstractDNode;
}
public boolean isValidPPDInvolvePhysicalLinkAndPart(DSemanticDecorator context) {
return true;
}
public Collection<PhysicalPath> getPPDInvolvePhysicalPathScope(DSemanticDecorator diagram) {
EObject chain = diagram.getTarget();
if (!(chain instanceof PhysicalPath)) {
return Collections.emptyList();
}
BlockArchitecture architecture = BlockArchitectureExt.getRootBlockArchitecture(chain);
// remove all chains owning the current chain
List<PhysicalPath> chains = PhysicalPathExt.getAllPhysicalPaths(architecture);
List<PhysicalPath> result = new LinkedList<>();
for (PhysicalPath definedChain : chains) {
boolean toAdd = true;
if (definedChain.equals(chain)) {
toAdd = false;
}
if (toAdd) {
for (PhysicalPathInvolvement involvement : PhysicalPathExt.getFlatInvolvementsOf(definedChain,
CsPackage.Literals.PHYSICAL_PATH)) {
if (chain.equals(involvement.getInvolved())) {
toAdd = false;
break;
}
}
}
if (toAdd) {
result.add(definedChain);
}
}
return result;
}
public Collection<Part> getPPDInvolvePartScope(DSemanticDecorator context) {
EObject target = context.getTarget();
BlockArchitecture architecture = BlockArchitectureExt.getRootBlockArchitecture(target);
Collection<Part> parts = new HashSet<>();
for (Part part : PartExt.getAllPartsFromPhysicalArchitecture((PhysicalArchitecture) architecture)) {
if (part.getAbstractType() instanceof PhysicalComponent) {
if (PhysicalComponentNature.NODE.equals(((PhysicalComponent) part.getAbstractType()).getNature())) {
parts.add(part);
}
}
}
return parts;
}
public Collection<Component> getPPDInvolveComponentScope(DSemanticDecorator context) {
EObject target = context.getTarget();
BlockArchitecture architecture = BlockArchitectureExt.getRootBlockArchitecture(target);
Collection<Component> parts = new HashSet<>();
for (Part part : PartExt.getAllPartsFromBlockArch(architecture)) {
Component component = (Component) part.getAbstractType();
if ((component != null) && !PhysicalComponentExt.isPhysicalComponentRoot(component)) {
parts.add(component);
}
}
return parts;
}
/**
* @param selectedElement
* @return called by show/hide PhysicalLinks tool (Physical Architecture Blank Diagram)
*/
public List<PhysicalLink> getDisplayedPhysicalLinks(DNodeContainer selectedElement) {
List<PhysicalLink> returnedList = new ArrayList<>();
List<DEdge> incomingOutgoingEdges = new ArrayList<>();
for (DNode aNode : selectedElement.getOwnedBorderedNodes()) {
if (aNode.getTarget() instanceof PhysicalPort) {
incomingOutgoingEdges.addAll(CapellaServices.getService().getIncomingEdges(aNode));
incomingOutgoingEdges.addAll(CapellaServices.getService().getOutgoingEdges(aNode));
}
}
for (DEdge anEdge : incomingOutgoingEdges) {
String edgeMappingName = anEdge.getMapping().getName();
if (anEdge.getTarget() instanceof PhysicalLink
&& !(edgeMappingName.equals(IMappingNameConstants.PAB_COMPUTED_PHYSICAL_LINK)
|| edgeMappingName.equals(IMappingNameConstants.LAB_COMPUTED_PHYSICAL_LINK))) {
returnedList.add((PhysicalLink) anEdge.getTarget());
}
}
return returnedList;
}
/**
* @param context
* @return called by show/hide PhysicalLinks tool (Physical Architecture Blank Diagram)
*/
public List<PhysicalLink> getAvailablePhysicalLinksToInsert(DNodeContainer context, DDiagram diagram) {
List<PhysicalLink> returnedList = new ArrayList<>();
List<PhysicalLink> allPhysicalLinks = new ArrayList<>();
List<PhysicalLink> existingExchangesInDiagram = new ArrayList<>();
for (DEdge anEdge : CapellaServices.getService().getDiagramContainer(context).getEdges()) {
if (anEdge.getTarget() instanceof PhysicalLink) {
existingExchangesInDiagram.add((PhysicalLink) anEdge.getTarget());
}
}
Component currentComponent = null;
EObject target = context.getTarget();
if (target instanceof Component) {
currentComponent = (Component) CsServices.getService().getComponentType((Component) target);
} else if (target instanceof Part) {
currentComponent = (Component) CsServices.getService().getComponentType((Part) target);
}
for (PhysicalPort aPort : ComponentExt.getOwnedPhysicalPort(currentComponent)) {
allPhysicalLinks.addAll(aPort.getInvolvedLinks());
}
for (PhysicalLink aPhysicalLink : allPhysicalLinks) {
if (!existingExchangesInDiagram.contains(aPhysicalLink)) {
returnedList.add(aPhysicalLink);
}
}
return returnedList;
}
/**
* @param context
* @return called by show/hide PhysicalLinks tool (Physical Architecture Blank Diagram)
*/
public List<PhysicalLink> getAvailablePhysicalLinksToInsertOld(DNodeContainer context) {
List<PhysicalLink> returnedList = new ArrayList<>();
List<PhysicalLink> allPhysicalLinks = new ArrayList<>();
List<PhysicalLink> existingExchangesInDiagram = new ArrayList<>();
for (DEdge anEdge : CapellaServices.getService().getDiagramContainer(context).getEdges()) {
if (anEdge.getTarget() instanceof PhysicalLink) {
existingExchangesInDiagram.add((PhysicalLink) anEdge.getTarget());
}
}
Component currentComponent = null;
EObject target = context.getTarget();
if (target instanceof Component) {
currentComponent = (Component) CsServices.getService().getComponentType((Component) target);
} else if (target instanceof Part) {
currentComponent = (Component) CsServices.getService().getComponentType((Part) target);
}
for (PhysicalPort aPort : ComponentExt.getOwnedPhysicalPort(currentComponent)) {
allPhysicalLinks.addAll(aPort.getInvolvedLinks());
}
for (PhysicalLink aPhysicalLink : allPhysicalLinks) {
if (!existingExchangesInDiagram.contains(aPhysicalLink)) {
returnedList.add(aPhysicalLink);
}
}
return returnedList;
}
public List<PhysicalPath> getAllPhysicalPaths(EObject context) {
return PhysicalPathExt.getAllPhysicalPaths(BlockArchitectureExt.getRootBlockArchitecture(context));
}
/**
* Create a component exchange in an architecture blank diagram. Create port if selected views are not targeting port
*
* @param context
* @param sourceView
* @param targetView
* @return
*/
public EObject createABPhysicalLink(EObject context, DSemanticDecorator sourceView, DSemanticDecorator targetView) {
EObject sourceTarget = sourceView.getTarget();
EObject targetTarget = targetView.getTarget();
DDiagram diagram = CapellaServices.getService().getDiagramContainer(sourceView);
EObject sourceRelatedPart = CsServices.getService().getRelatedPart(sourceView);
EObject targetRelatedPart = CsServices.getService().getRelatedPart(targetView);
Part sourcePart = null;
Part targetPart = null;
if (sourceRelatedPart instanceof Part) {
sourcePart = (Part) sourceRelatedPart;
}
if (targetRelatedPart instanceof Part) {
targetPart = (Part) targetRelatedPart;
}
EdgeTarget nodeSource = null;
EdgeTarget nodeTarget = null;
// Create or retrieve sourcePort
PhysicalPort sourcePort = null;
if (sourceTarget instanceof PhysicalPort) {
sourcePort = (PhysicalPort) sourceTarget;
nodeSource = (EdgeTarget) sourceView;
} else if (sourcePart != null) {
sourcePort = CsFactory.eINSTANCE.createPhysicalPort();
((Component) sourcePart.getType()).getOwnedFeatures().add(sourcePort);
CapellaServices.getService().creationService(sourcePort);
nodeSource = CsServices.getService().createViewOrGetPhysicalPort((DNodeContainer) sourceView, sourcePort)
.getKey();
}
// Create or retrieve targetPort
PhysicalPort targetPort = null;
if (targetTarget instanceof PhysicalPort) {
targetPort = (PhysicalPort) targetTarget;
nodeTarget = (EdgeTarget) targetView;
} else if (targetPart != null) {
targetPort = CsFactory.eINSTANCE.createPhysicalPort();
((Component) targetPart.getType()).getOwnedFeatures().add(targetPort);
CapellaServices.getService().creationService(targetPort);
nodeTarget = CsServices.getService().createViewOrGetPhysicalPort((DNodeContainer) targetView, targetPort)
.getKey();
}
// Create physical link
PhysicalLink exchange = CsFactory.eINSTANCE.createPhysicalLink();
// Set source
if (CsServices.getService().isMultipartMode((ModelElement) sourceTarget)) {
PhysicalLinkEnd end = CsFactory.eINSTANCE.createPhysicalLinkEnd();
end.setPart(sourcePart);
end.setPort(sourcePort);
exchange.getLinkEnds().add(end);
exchange.getOwnedPhysicalLinkEnds().add(end);
CapellaServices.getService().creationService(end);
} else {
exchange.getLinkEnds().add(sourcePort);
}
// Set target
if (CsServices.getService().isMultipartMode((ModelElement) sourceTarget)) {
PhysicalLinkEnd end = CsFactory.eINSTANCE.createPhysicalLinkEnd();
end.setPart(targetPart);
end.setPort(targetPort);
exchange.getLinkEnds().add(end);
exchange.getOwnedPhysicalLinkEnds().add(end);
CapellaServices.getService().creationService(end);
} else {
exchange.getLinkEnds().add(targetPort);
}
// Attach to parent
org.polarsys.capella.core.model.helpers.PhysicalLinkExt.attachToDefaultContainer(exchange);
CapellaServices.getService().creationService(exchange);
DiagramServices.getDiagramServices().createEdge(FaServices.getFaServices().getMappingABPhysicalLink(diagram),
nodeSource, nodeTarget, exchange);
CsServices.getService().setInterpreterVariable(context, "result", exchange);
return context;
}
public EObject createPhysicalPath(EObject context, List<EObject> views, EObject source) {
if (!views.isEmpty() && source != null) {
List<PhysicalLink> newList = new ArrayList<>();
for (EObject aSelectedElement : views) {
if ((aSelectedElement instanceof DEdge) && (((DEdge) aSelectedElement).getTarget() != null)
&& (((DEdge) aSelectedElement).getTarget() instanceof PhysicalLink)) {
newList.add((PhysicalLink) ((DEdge) aSelectedElement).getTarget());
}
}
Part sourcePath;
if (canBeInvolvedInPhysicalPath(source)) {
sourcePath = ((Component) source).getRepresentingParts().get(0);
} else {
sourcePath = (Part) source;
}
EObject ancestor = EcoreUtil2.getCommonAncestor(newList);
Component container = CapellaServices.getService().getComponentContainer(ancestor);
if (container == null) {
EObject target = ((DSemanticDiagram) CapellaServices.getService().getDiagramContainer(views.get(0)))
.getTarget();
container = CapellaServices.getService().getComponentContainer(target);
}
return PhysicalPathExt.createPhysicalPath(container, newList, sourcePath);
}
return null;
}
public boolean canBeInvolvedInPhysicalPath(EObject source) {
return canHavePhysicalPort(source);
}
public boolean canHavePhysicalPort(EObject source) {
if (source instanceof LogicalComponent) {
LogicalComponent container = (LogicalComponent) ComponentExt.getParentRootComponent(source);
BlockArchitecture architecture = BlockArchitectureExt.getRootBlockArchitecture(source);
LogicalComponent system = (LogicalComponent) architecture.getSystem();
return source.equals(system) || (container != null && !container.equals(system));
}
return (((source instanceof SystemComponent) || (source instanceof PhysicalComponent)));
}
/**
*
* @param context
* @param views
* @return The available source for the creation of PhysicalPath by applying one time invoked Part algorithm.
*/
public List<EObject> getAvailableSourcesOfPhysicalPath(EObject context, List<EObject> views) {
HashMap<Part, Integer> parts = new HashMap<>();
List<EObject> result = new ArrayList<>();
for (EObject aSelectedElement : views) {
if ((aSelectedElement instanceof DEdge) && (((DEdge) aSelectedElement).getTarget() != null)
&& (((DEdge) aSelectedElement).getTarget() instanceof PhysicalLink)) {
PhysicalLink currentLink = (PhysicalLink) ((DEdge) aSelectedElement).getTarget();
Part sourcePart = PhysicalLinkExt.getSourcePart(currentLink);
Part targetPart = PhysicalLinkExt.getTargetPart(currentLink);
int i = 1;
if (parts.containsKey(sourcePart)) {
i = parts.get(sourcePart).intValue() + 1;
}
parts.put(sourcePart, Integer.valueOf(i));
i = 1;
if (parts.containsKey(targetPart)) {
i = parts.get(targetPart).intValue() + 1;
}
parts.put(targetPart, Integer.valueOf(i));
}
}
for (Entry<Part, Integer> me : parts.entrySet()) {
if (me.getValue().intValue() == 1) {
if (CsServices.getService().isMultipartMode(me.getKey())) {
result.add(me.getKey());
} else {
result.add(me.getKey().getType());
}
}
}
return result;
}
public EObject getPhysicalPathSource(EObject context, List<EObject> views) {
return getAvailableSourcesOfPhysicalPath(context, views).get(0);
}
public boolean isValidPhysicalPort(EObject context, EObject container) {
if (container instanceof Part) {
return canHavePhysicalPort(((Part) container).getAbstractType());
}
return false;
}
public boolean canBeInvolvedInPhysicalLink(EObject context) {
if (context instanceof Part) {
return canHavePhysicalPort(((Part) context).getAbstractType());
}
return context instanceof PhysicalPort;
}
public boolean isValidCreationPhysicalLink(EObject source, EObject target) {
if (source instanceof PhysicalPort && source.equals(target)) {
return false;
}
return canBeInvolvedInPhysicalLink(source) && canBeInvolvedInPhysicalLink(target);
}
public boolean isValidPhysicalPathSelection(EObject context, List<EObject> views) {
SimpleOrientedGraph<Part> graph = new SimpleOrientedGraph<>();
if (!views.isEmpty()) {
for (EObject aSelectedElement : views) {
if ((aSelectedElement instanceof DEdge) && (((DEdge) aSelectedElement).getTarget() != null)
&& (((DEdge) aSelectedElement).getTarget() instanceof PhysicalLink)) {
PhysicalLink aSelectedExchange = (PhysicalLink) ((DEdge) aSelectedElement).getTarget();
Part sourcePart = PhysicalLinkExt.getSourcePart(aSelectedExchange);
Part targetPart = PhysicalLinkExt.getTargetPart(aSelectedExchange);
graph.addNode(sourcePart, targetPart);
}
}
if (!graph.isEmpty()) {
for (Part aPart : graph.getGraph().keySet()) {
if (graph.getNotOrientedNeighbours(aPart).size() > 2) {
// the selection can not contain alternative
return false;
}
}
return graph.isValid() && !getAvailableSourcesOfPhysicalPath(context, views).isEmpty();
}
return false;
}
return false;
}
public EObject showHidePhysicalPaths(EObject context, List<PhysicalPath> selectedPaths, DDiagram diagram) {
Map<PhysicalPath, DNode> displayedPaths = new HashMap<>();
for (DNode aNode : diagram.getNodes()) {
if (aNode.getTarget() instanceof PhysicalPath) {
displayedPaths.put((PhysicalPath) aNode.getTarget(), aNode);
}
}
NodeMapping physicalPathMapping = DiagramServices.getDiagramServices().getNodeMapping(diagram,
MappingConstantsHelper.getMappingPhysicalPath(diagram));
for (PhysicalPath aPath : selectedPaths) {
if (!displayedPaths.containsKey(aPath)) {
DiagramServices.getDiagramServices().createNode(physicalPathMapping, aPath, diagram, diagram);
}
}
for (Entry<PhysicalPath, DNode> me : displayedPaths.entrySet()) {
if (!selectedPaths.contains(me.getKey())) {
DiagramServices.getDiagramServices().removeNodeView(me.getValue());
}
}
return context;
}
public List<PhysicalPath> getAvailablePhysicalPathsToInsert(EObject element, DSemanticDiagram diagram) {
Set<EObject> viewTargets = new HashSet<>();
for (DEdge anEdge : diagram.getEdges()) {
if (anEdge.getTarget() instanceof PhysicalLink) {
viewTargets.add(anEdge.getTarget());
}
}
List<PhysicalPath> result = new ArrayList<>();
for (PhysicalPath aPath : getAllPhysicalPaths(diagram.getTarget())) {
Collection<PhysicalLink> involvedLinks = PhysicalPathExt.getFlatPhysicalLinks(aPath);
if (!result.contains(aPath)) {
for (EObject link : involvedLinks) {
if (viewTargets.contains(link)) {
result.add(aPath);
break;
}
}
}
}
return result;
}
public PhysicalPort getRelatedPort(AbstractPhysicalLinkEnd end) {
return PhysicalLinkExt.getRelatedPort(end);
}
public List<PhysicalPath> getDisplayedPhysicalPaths(DDiagram diagram) {
List<PhysicalPath> returnedList = new ArrayList<>();
for (DNode aNode : diagram.getNodes()) {
if (aNode.getTarget() instanceof PhysicalPath) {
returnedList.add((PhysicalPath) aNode.getTarget());
}
}
return returnedList;
}
public Map<PhysicalPath, DNode> getDisplayedPhysicalPathsAndNodes(DDiagram diagram) {
Map<PhysicalPath, DNode> returnedMap = new HashMap<>();
for (DNode aNode : diagram.getNodes()) {
if (aNode.getTarget() instanceof PhysicalPath) {
returnedMap.put((PhysicalPath) aNode.getTarget(), aNode);
}
}
return returnedMap;
}
public EObject reconnectPhysicalLink(PhysicalLink link, AbstractPhysicalLinkEnd source,
AbstractPhysicalLinkEnd target) {
int index = link.getLinkEnds().indexOf(source);
link.getLinkEnds().add(index, target);
link.getLinkEnds().remove(source);
return link;
}
/**
* Manage allocation of PC deployment
*
* @param view
* @param selectedElements
* @param isBehaviour
* @return
*/
public EObject manageComponentDeployments(DNodeContainer view, List<DeployableElement> selectedElements,
boolean isBehaviour) {
Part currentPart = (Part) view.getTarget();
DDiagram currentDiagram = CapellaServices.getService().getDiagramContainer(view);
Map<Part, DNodeContainer> displayedDeployments = new HashMap<>();
ContainerMapping deploymentMapping = getDeploymentMapping(currentDiagram);
for (DDiagramElement aDiagramElement : view.getOwnedDiagramElements()) {
if (aDiagramElement.getMapping().equals(deploymentMapping)) {
displayedDeployments.put((Part) aDiagramElement.getTarget(), (DNodeContainer) aDiagramElement);
}
}
List<AbstractDeploymentLink> linksToRemove = new ArrayList<>(1);
for (AbstractDeploymentLink aLink : currentPart.getOwnedDeploymentLinks()) {
if (!selectedElements.contains(aLink.getDeployedElement())) {
if (aLink.getDeployedElement() instanceof Part) {
Part aDeployedPart = (Part) aLink.getDeployedElement();
if ((isBehaviour && isBehaviour(aDeployedPart)) || (!isBehaviour && isNode(aDeployedPart))) {
if (displayedDeployments.containsKey(aDeployedPart)) {
// remove the view
DiagramServices.getDiagramServices().removeContainerView(displayedDeployments.get(aDeployedPart));
}
// collect the deployed links to remove
linksToRemove.add(aLink);
}
}
}
}
CapellaServices.getService().removeElements(linksToRemove);
// create the view
List<Part> deployedComponents = getDeployedComponents(currentPart, isBehaviour);
for (DeployableElement aSelectedElement : selectedElements) {
if (!deployedComponents.contains(aSelectedElement)) {
PartDeploymentLink newLink = DeploymentFactory.eINSTANCE.createPartDeploymentLink();
currentPart.getOwnedDeploymentLinks().add(newLink);
newLink.setLocation(currentPart);
newLink.setDeployedElement(aSelectedElement);
DiagramServices.getDiagramServices().createContainer(deploymentMapping, aSelectedElement, view, currentDiagram);
}
}
return view;
}
public ContainerMapping getDeploymentMapping(DDiagram diagram) {
return DiagramServices.getDiagramServices().getContainerMapping(diagram,
MappingConstantsHelper.getMappingABDeployedElement(diagram));
}
public boolean isBehaviour(Part part) {
EObject componentType = CsServices.getService().getComponentType(part);
PhysicalComponent comp = null;
if (componentType instanceof PhysicalComponent) {
comp = (PhysicalComponent) componentType;
return PhysicalComponentNature.BEHAVIOR.equals(comp.getNature());
}
return false;
}
public boolean isNode(Part part) {
EObject componentType = CsServices.getService().getComponentType(part);
PhysicalComponent comp = null;
if (componentType instanceof PhysicalComponent) {
comp = (PhysicalComponent) componentType;
return PhysicalComponentNature.NODE.equals(comp.getNature());
}
return false;
}
public boolean isPhysicalActor(Part part) {
EObject componentType = CsServices.getService().getComponentType(part);
return ComponentExt.isActor(componentType);
}
public void updateInternalPhysicalPaths(DDiagram diagram) {
// Find displayed physical paths
HashMap<PhysicalPath, DNode> displayedPaths = computePhysicalPathToNodeMap(diagram);
// Find displayed Internal Links
HashMap<PhysicalPath, Set<DEdge>> physicalPathToVisibleInternalEdges = computePhysicalPathToEdgesMap(diagram);
// Remove internal Links if the physical path is not displayed
for (Entry<PhysicalPath, Set<DEdge>> entry : physicalPathToVisibleInternalEdges.entrySet()) {
PhysicalPath physicalPath = entry.getKey();
if (!displayedPaths.containsKey(physicalPath)) {
Set<DEdge> physicalPathEdges = entry.getValue();
for (DEdge edge : physicalPathEdges) {
edge.getSemanticElements().remove(physicalPath);
if (edge.getSemanticElements().isEmpty()) {
diagram.getOwnedDiagramElements().remove(edge);
}
}
}
}
// Remove INVALID internal link edge
for (Entry<PhysicalPath, Set<DEdge>> entry : physicalPathToVisibleInternalEdges.entrySet()) {
Set<DEdge> edges = entry.getValue();
for (DEdge edge : edges) {
if (!isValidInternalLinkEdge(entry.getKey(), edge.getSourceNode(), edge.getTargetNode())
&& entry.getKey().equals(edge.getTarget())) {
edge.getSemanticElements().remove(entry.getKey());
if (edge.getSemanticElements().isEmpty()) {
DiagramServices.getDiagramServices().removeEdgeView(edge); // We remove the edge view here, on FC this is
// done in precondition of the edge
}
}
}
}
}
public Collection<EObject> getPPInternalLinkSemanticElements(DEdge view) {
Collection<PhysicalPath> displayedPaths = getDisplayedPhysicalPaths(view.getParentDiagram());
return view.getSemanticElements().stream().filter(x -> displayedPaths.contains(x)).collect(Collectors.toList());
}
public void updatePhysicalPathStyles(DDiagram diagram) {
// Find displayed physical paths
HashMap<PhysicalPath, DNode> displayedPaths = computePhysicalPathToNodeMap(diagram);
HashMapSet<DSemanticDecorator, RGBValues> viewColors = new HashMapSet<>();
// Find displayed Physical Links
HashMap<PhysicalLink, DEdge> displayedPhysicalLinks = computePhysicalLinkToEdgeMap(diagram);
// Find physical links that must be colored
HashMap<DEdge, Set<PhysicalPath>> coloredLinks = computeEdgeToPhysicalPathsMap(displayedPaths,
displayedPhysicalLinks);
ArrayList<DEdge> displayedLinks = new ArrayList<>();
for (Entry<PhysicalPath, DNode> entry : displayedPaths.entrySet()) {
DNode node = entry.getValue();
PhysicalPath path = entry.getKey();
updatePhysicalPathNodeColor(node, displayedPaths.values());
RGBValues color = ShapeUtil.getNodeColorStyle(node);
PhysicalPathInvolvementGraph graph = PhysicalPathCache.getInstance().getInvolvementGraph(path);
// Add color for related physical links
for (InvolvementEdge edge : graph.getEdges().values()) {
PhysicalLink involvedPL = graph.getInvolvedPhysicalLink(edge);
if (involvedPL != null) {
for (DSemanticDecorator view : DiagramServices.getDiagramServices().getDiagramElements(diagram, involvedPL)) {
viewColors.put(view, color);
if (view instanceof DEdge) {
Set<PhysicalPath> paths = coloredLinks.computeIfAbsent((DEdge) view, k -> new HashSet<>());
paths.add(path);
}
}
}
}
// Create and highlight internal links
PhysicalPathInternalLinksGraph linksGraph = PhysicalPathCache.getInstance().getInternalLinksGraph(graph);
for (InternalLinkEdge edge : linksGraph.getEdges().values()) {
PhysicalPort source = edge.getSource().getSemantic();
PhysicalPort target = edge.getTarget().getSemantic();
// In multi-part mode, there can be many nodes in a diagram for the same physical port
Collection<DSemanticDecorator> sourceNodes = DiagramServices.getDiagramServices().getDiagramElements(diagram,
source);
Collection<DSemanticDecorator> targetNodes = DiagramServices.getDiagramServices().getDiagramElements(diagram,
target);
for (DSemanticDecorator sourceNode : sourceNodes) {
for (DSemanticDecorator targetNode : targetNodes) {
DEdge link = createInternalLink((DNode) sourceNode, (DNode) targetNode, path);
if (link != null) {
displayedLinks.add(link);
}
}
}
}
}
for (DEdge link : displayedLinks) {
ArrayList<RGBValues> pathColors = new ArrayList<>();
for (EObject pp : link.getSemanticElements()) {
if (displayedPaths.containsKey(pp)) {
RGBValues color = ShapeUtil.getNodeColorStyle(displayedPaths.get(pp));
pathColors.add(color);
}
}
customizeInternalLinksEdgeStyle(link, pathColors.size() == 1 ? pathColors.get(0) : ShapeUtil.getBlackColor());
}
customizePhysicalLinkEdgeLabels(coloredLinks, displayedPaths);
// Update all physical links
for (DEdge view : diagram.getEdges()) {
EObject target = view.getTarget();
if (target instanceof PhysicalLink) {
if (viewColors.containsKey(view)) {
customizePhysicalLinkEdgeStyle(view, ShapeUtil.getColor(viewColors.get(view)));
} else {
resetPhysicalLinkStyle(view);
resetPhysicalLinkEdgeLabels(view);
}
}
}
}
public void resetPhysicalLinkEdgeLabels(DEdge aPL) {
DEdgeIconCache.getInstance().removeIcon(aPL);
DEdgeIconCache.getInstance().setLabel(aPL, ICommonConstants.EMPTY_STRING);
DiagramServices.getDiagramServices().refreshBeginEndLabels(aPL);
CapellaServices.getService().refreshElement(aPL);
}
/**
* Add pie icons on the begin and the end labels of a Physical Link
*
* @param coloredLinks
* @param displayedPaths
*/
public void customizePhysicalLinkEdgeLabels(Map<DEdge, Set<PhysicalPath>> coloredLinks,
Map<PhysicalPath, DNode> displayedPaths) {
for (Map.Entry<DEdge, Set<PhysicalPath>> entry : coloredLinks.entrySet()) {
DEdge edge = entry.getKey();
Set<PhysicalPath> paths = entry.getValue();
updatePhysicalLinkPieIcon(edge, paths, displayedPaths);
DiagramServices.getDiagramServices().refreshBeginEndLabels(edge);
CapellaServices.getService().refreshElement(edge);
}
}
public void updatePhysicalLinkPieIcon(DEdge edge, Set<PhysicalPath> paths, Map<PhysicalPath, DNode> displayedPaths) {
List<RGBValues> pathColors = paths.stream().map(displayedPaths::get).map(ShapeUtil::getNodeColorStyle)
.collect(Collectors.toList());
if (pathColors.size() > 1) {
DEdgeIconCache.getInstance().setIcon(edge, pathColors);
DEdgeIconCache.getInstance().setLabel(edge, DiagramServices.getDiagramServices()
.getOverlappedLabels(paths.stream().map(AbstractNamedElement::getName).collect(Collectors.toList())));
} else {
DEdgeIconCache.getInstance().removeIcon(edge);
DEdgeIconCache.getInstance().setLabel(edge, ICommonConstants.EMPTY_STRING);
}
}
private HashMap<DEdge, Set<PhysicalPath>> computeEdgeToPhysicalPathsMap(HashMap<PhysicalPath, DNode> displayedPaths,
HashMap<PhysicalLink, DEdge> displayedPhysicalLinks) {
// Colored Physical Links
HashMap<DEdge, Set<PhysicalPath>> coloredLinks = new HashMap<>();
for (Entry<PhysicalPath, DNode> me : displayedPaths.entrySet()) {
for (PhysicalLink link : PhysicalPathExt.getFlatPhysicalLinks(me.getKey())) {
if (displayedPhysicalLinks.containsKey(link)) {
DEdge linkEdge = displayedPhysicalLinks.get(link);
if (!coloredLinks.containsKey(linkEdge)) {
Set<PhysicalPath> newSet = new HashSet<>();
newSet.add(me.getKey());
coloredLinks.put(linkEdge, newSet);
} else {
coloredLinks.get(linkEdge).add(me.getKey());
}
}
}
}
return coloredLinks;
}
/**
* Returns the displayed internal links per PhysicalPath
*/
private HashMap<PhysicalPath, Set<DEdge>> computePhysicalPathToEdgesMap(DDiagram diagram) {
HashMap<PhysicalPath, Set<DEdge>> displayedIL = new HashMap<>();
for (DEdge anEdge : DDiagramSpecOperations.getEdgesFromMapping(diagram,
getPhysicalPathInternLinkEdgeMapping(diagram))) {
for (EObject semantic : anEdge.getSemanticElements()) {
if (semantic instanceof PhysicalPath) {
displayedIL.computeIfAbsent((PhysicalPath) semantic, x -> new HashSet<>()).add(anEdge);
}
}
}
return displayedIL;
}
private HashMap<PhysicalLink, DEdge> computePhysicalLinkToEdgeMap(DDiagram diagram) {
// Displayed Physical Links
HashMap<PhysicalLink, DEdge> displayedPhysicalLinks = new HashMap<>();
for (DEdge anEdge : diagram.getEdges()) {