-
Notifications
You must be signed in to change notification settings - Fork 85
/
FunctionalChainServices.java
1917 lines (1675 loc) · 77.2 KB
/
FunctionalChainServices.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 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.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.DrawerStyle;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.sirius.diagram.AbstractDNode;
import org.eclipse.sirius.diagram.BorderedStyle;
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.DiagramPackage;
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.DiagramElementMapping;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.filter.FilterDescription;
import org.eclipse.sirius.diagram.description.style.BorderedStyleDescription;
import org.eclipse.sirius.diagram.description.style.EdgeStyleDescription;
import org.eclipse.sirius.diagram.ui.business.api.view.SiriusGMFHelper;
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.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.polarsys.capella.common.data.modellingcore.AbstractNamedElement;
import org.polarsys.capella.common.helpers.EObjectExt;
import org.polarsys.capella.common.helpers.SimpleOrientedGraph;
import org.polarsys.capella.common.mdsofa.common.constant.ICommonConstants;
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.InvolvedElement;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.fa.AbstractFunction;
import org.polarsys.capella.core.data.fa.AbstractFunctionalChainContainer;
import org.polarsys.capella.core.data.fa.ControlNode;
import org.polarsys.capella.core.data.fa.ControlNodeKind;
import org.polarsys.capella.core.data.fa.FaPackage;
import org.polarsys.capella.core.data.fa.FunctionPort;
import org.polarsys.capella.core.data.fa.FunctionalChain;
import org.polarsys.capella.core.data.fa.FunctionalChainInvolvement;
import org.polarsys.capella.core.data.fa.FunctionalChainInvolvementFunction;
import org.polarsys.capella.core.data.fa.FunctionalChainInvolvementLink;
import org.polarsys.capella.core.data.fa.FunctionalChainReference;
import org.polarsys.capella.core.data.fa.FunctionalExchange;
import org.polarsys.capella.core.data.fa.ReferenceHierarchyContext;
import org.polarsys.capella.core.data.fa.SequenceLink;
import org.polarsys.capella.core.data.fa.SequenceLinkEnd;
import org.polarsys.capella.core.data.helpers.fa.services.FunctionExt;
import org.polarsys.capella.core.data.information.ExchangeItem;
import org.polarsys.capella.core.data.interaction.AbstractCapability;
import org.polarsys.capella.core.data.interaction.FunctionalChainAbstractCapabilityInvolvement;
import org.polarsys.capella.core.data.oa.OperationalProcess;
import org.polarsys.capella.core.model.helpers.BlockArchitectureExt;
import org.polarsys.capella.core.model.helpers.ExchangeItemExt;
import org.polarsys.capella.core.model.helpers.FunctionalChainExt;
import org.polarsys.capella.core.model.helpers.FunctionalExchangeExt;
import org.polarsys.capella.core.model.helpers.SequenceLinkEndExt;
import org.polarsys.capella.core.model.helpers.SequenceLinkExt;
import org.polarsys.capella.core.model.helpers.graph.InternalLinksGraph;
import org.polarsys.capella.core.model.helpers.graph.InternalLinksGraph.InternalLinkEdge;
import org.polarsys.capella.core.model.helpers.graph.InvolvementGraph;
import org.polarsys.capella.core.model.helpers.graph.InvolvementGraph.InvolvementEdge;
import org.polarsys.capella.core.model.helpers.graph.InvolvementGraph.InvolvementNode;
import org.polarsys.capella.core.sirius.analysis.accelerators.SelectOrCreateFunctionalExchangeDialog;
import org.polarsys.capella.core.sirius.analysis.accelerators.SelectOrCreateFunctionalExchangeDialog.NewFEData;
import org.polarsys.capella.core.sirius.analysis.cache.DEdgeIconCache;
import org.polarsys.capella.core.sirius.analysis.cache.FunctionalChainCache;
import org.polarsys.capella.core.sirius.analysis.constants.MappingConstantsHelper;
import org.polarsys.capella.core.sirius.analysis.helpers.FunctionalChainReferenceHierarchyHelper;
import org.polarsys.capella.core.sirius.analysis.preferences.DiagramProcessChainPathPreferencePage;
import org.polarsys.capella.core.sirius.analysis.tool.HashMapSet;
/**
* Services for functional chain.
*/
public class FunctionalChainServices {
public static final Integer THICK_BORDER_SOURCE_FUNCTION = Integer.valueOf(4);
public static final Integer THICK_BORDER_TARGET_FUNCTION = Integer.valueOf(4);
public static final Integer THICK_EDGE_FUNCTIONAL_CHAIN = Integer.valueOf(4);
public static final String INCOMPLETE_FUNCTIONAL_CHAIN_LABEL = "incomplete"; //$NON-NLS-1$
public static final String INVALID_FUNCTIONAL_CHAIN_LABEL = "invalid"; //$NON-NLS-1$
public static final String FCR_CONTAINER_COLLAPSED_INDICATOR = " [+]"; //$NON-NLS-1$
public static final String IT = "IT"; //$NON-NLS-1$
private static FunctionalChainServices singleton = null;
public static FunctionalChainServices getFunctionalChainServices() {
if (singleton == null) {
singleton = new FunctionalChainServices();
}
return singleton;
}
/**
* Returns all the functional chain involvement links for a function chain, including those on recursive levels. This
* function is tail recursive.
*
* @param chain
* the functional chain.
* @return all the functional chain involvement links for a function chain, including those on recursive levels.
*/
public Collection<FunctionalChainInvolvementLink> getAllFunctionalChainInvolvementLinks(FunctionalChain chain) {
Set<FunctionalChainInvolvementLink> result = new HashSet<>();
getAllFunctionalChainInvolvementLinks(chain, result);
return result;
}
/**
* This is a tail recursive version that returns all the functional chain involvement links for a function chain,
* including those on recursive levels.
*
* @param chain
* the functional chain.
* @param linksAcumulator
* the functional chain involvment links acumulator.
*/
private void getAllFunctionalChainInvolvementLinks(FunctionalChain chain,
Collection<FunctionalChainInvolvementLink> linksAcumulator) {
for (FunctionalChainInvolvement involvement : chain.getOwnedFunctionalChainInvolvements()) {
if (involvement instanceof FunctionalChainInvolvementLink) {
linksAcumulator.add((FunctionalChainInvolvementLink) involvement);
} else if (involvement instanceof FunctionalChainReference) {
FunctionalChain referencedChain = ((FunctionalChainReference) involvement).getReferencedFunctionalChain();
if (referencedChain != null) {
getAllFunctionalChainInvolvementLinks(referencedChain, linksAcumulator);
}
}
}
}
/**
* Returns all semantics of source views for the given chain
*/
public Collection<EObject> getFunctionalChainSources(FunctionalChain chain) {
InvolvementGraph g = FunctionalChainCache.getInstance().getInvolvementGraph(chain);
InternalLinksGraph linksGraph = FunctionalChainCache.getInstance().getInternalLinksGraph(g);
return linksGraph.getEdges().values().stream().map(e -> e.getSource().getSemantic()).collect(Collectors.toSet());
}
/**
* Returns all semantics of target views for the given chain
*/
public Collection<EObject> getFunctionalChainTargets(FunctionalChain chain) {
InvolvementGraph g = FunctionalChainCache.getInstance().getInvolvementGraph(chain);
InternalLinksGraph linksGraph = FunctionalChainCache.getInstance().getInternalLinksGraph(g);
return linksGraph.getEdges().values().stream().map(e -> e.getTarget().getSemantic()).collect(Collectors.toSet());
}
/**
* Returns all semantics of source views for the given chain
*/
public Collection<EObject> getFunctionalChainsToDisplays(EObject chain, DSemanticDiagram diagram) {
HashMap<FunctionalChain, DNode> functionalChains = getDisplayedFunctionalChains(diagram);
return (Collection) functionalChains.keySet();
}
/**
* Returns the best function through the elements collection
*/
public AbstractFunction getBestFunction(AbstractFunction element, Collection<? extends EObject> elements) {
EObject current = element;
while (current != null) {
if (current instanceof AbstractFunction && elements.contains(current)) {
return (AbstractFunction) current;
}
current = current.eContainer();
}
return null;
}
public List<FunctionalChain> getFunctionalChainsToInsert(EObject context, DDiagram currentDiagram,
List<FunctionalChain> allFunctionalChains) {
List<FunctionalChain> returnedList = new ArrayList<FunctionalChain>();
for (DDiagramElement anObject : DiagramServices.getDiagramServices().getDiagramElements(currentDiagram)) {
if (anObject.getTarget() instanceof AbstractFunction) {
AbstractFunction aFunction = (AbstractFunction) anObject.getTarget();
for (FunctionalChain aFunctionalChain : allFunctionalChains) {
Collection<EObject> involvedFunctions = FunctionalChainExt.getFlatInvolvedElements(aFunctionalChain,
FaPackage.Literals.ABSTRACT_FUNCTION);
if (!returnedList.contains(aFunctionalChain)) {
if (involvedFunctions.contains(aFunction)) {
returnedList.add(aFunctionalChain);
} else {
for (AbstractFunction aSubFunction : FunctionExt.getAllAbstractFunctions(aFunction)) {
if (involvedFunctions.contains(aSubFunction)) {
returnedList.add(aFunctionalChain);
}
}
}
}
}
}
}
return returnedList;
}
/**
* Update the style for functional chains. Colored border, highlight edges.
*
* @param diagram
*/
public void updateFunctionalChainStyles(DDiagram diagram) {
HashMap<FunctionalChain, DNode> functionalChains = getDisplayedFunctionalChains(diagram);
HashMapSet<DSemanticDecorator, RGBValues> colors = new HashMapSet<>();
Map<DEdge, Set<FunctionalChain>> coloredLinks = new HashMap<>();
Collection<DEdge> internalLinks = new ArrayList<DEdge>();
for (Entry<FunctionalChain, DNode> entry : functionalChains.entrySet()) {
FunctionalChain chain = entry.getKey();
DNode chainView = entry.getValue();
updateFunctionalChainNodeColor(chainView, functionalChains.values());
InvolvementGraph graph = FunctionalChainCache.getInstance().getInvolvementGraph(chain);
RGBValues color = ShapeUtil.getNodeColorStyle(chainView);
if (color == null) {
continue;
}
// Add color for starting and ending functions and control nodes
for (InvolvementNode node : graph.getNodes().values()) {
if (graph.isInvolvingFunction(node) && (graph.isStartingFunction(node) || graph.isEndingFunction(node))) {
AbstractFunction function = graph.getInvolvedFunction(node);
Collection<DSemanticDecorator> views = getBestDisplayedFunctionNode(function, diagram);
for (DSemanticDecorator view : views) {
colors.put(view, color);
}
}
}
// Add color for related functional exchanges and sequence links
for (InvolvementEdge edge : graph.getEdges().values()) {
EObject semantic = null;
if (graph.isInvolvingFunctionalExchange(edge)) {
semantic = graph.getInvolvedFunctionalExchange(edge);
} else if (graph.isSequenceLink(edge)) {
semantic = graph.getSequenceLink(edge);
}
if (semantic != null) {
for (DSemanticDecorator view : DiagramServices.getDiagramServices().getDiagramElements(diagram, semantic)) {
colors.put(view, color);
if (semantic instanceof FunctionalExchange && view instanceof DEdge) {
Set<FunctionalChain> chains = coloredLinks.computeIfAbsent((DEdge) view, k -> new HashSet<>());
chains.add(chain);
}
}
}
}
// Create and highlight internal links
if (!(chain instanceof OperationalProcess)) {
InternalLinksGraph linksGraph = FunctionalChainCache.getInstance().getInternalLinksGraph(graph);
for (InternalLinkEdge edge : linksGraph.getEdges().values()) {
FunctionPort source = edge.getSource().getSemantic();
FunctionPort target = edge.getTarget().getSemantic();
DDiagramElement sourceNode = DiagramServices.getDiagramServices().getDiagramElement(diagram, source);
DDiagramElement targetNode = DiagramServices.getDiagramServices().getDiagramElement(diagram, target);
if (sourceNode != null && targetNode != null) {
DEdge link = createInternalLink((EdgeTarget) sourceNode, (EdgeTarget) targetNode, chain);
if (link != null) {
internalLinks.add(link);
}
}
}
}
}
for (DEdge link : internalLinks) {
ArrayList<RGBValues> chainColors = new ArrayList<RGBValues>();
for (EObject fc : link.getSemanticElements()) {
if (functionalChains.containsKey(fc)) {
RGBValues color = ShapeUtil.getNodeColorStyle(functionalChains.get(fc));
chainColors.add(color);
}
}
customizeFunctionalChainEdgeStyle(link, chainColors.size() == 1 ? chainColors.get(0) : ShapeUtil.getBlackColor());
}
// Browse all nodes/containers looking for functions
for (DDiagramElement view : DiagramServices.getDiagramServices().getAllAbstractNodes(diagram, false)) {
EObject target = view.getTarget();
if (target instanceof AbstractFunction) {
if (colors.containsKey(view)) {
customizeSourceFunctionStyle(view, ShapeUtil.getColor(colors.get(view)));
} else {
resetFunctionStyle(view);
}
}
}
customizeFunctionalExchangeEdgeLabels(coloredLinks, functionalChains);
// Update all functional exchanges and sequence links
for (DEdge view : diagram.getEdges()) {
EObject target = view.getTarget();
if (target instanceof FunctionalExchange || target instanceof SequenceLink) {
if (colors.containsKey(view)) {
customizeFunctionalChainEdgeStyle(view, ShapeUtil.getColor(colors.get(view)));
} else {
resetFunctionalExchangeStyle(view);
resetFunctionalExchangeEdgeLabels(view);
}
}
}
}
/**
* Add pie icons on the begin and the end labels of a Functional Exchange
*
* @param coloredLinks
* @param displayedFCs
*/
public void customizeFunctionalExchangeEdgeLabels(Map<DEdge, Set<FunctionalChain>> coloredLinks,
Map<FunctionalChain, DNode> displayedFCs) {
for (Map.Entry<DEdge, Set<FunctionalChain>> entry : coloredLinks.entrySet()) {
DEdge edge = entry.getKey();
Set<FunctionalChain> chains = entry.getValue();
updateFunctionalExchangePieIcon(edge, chains, displayedFCs);
DiagramServices.getDiagramServices().refreshBeginEndLabels(edge);
CapellaServices.getService().refreshElement(edge);
}
}
public void updateFunctionalExchangePieIcon(DEdge edge, Set<FunctionalChain> chains,
Map<FunctionalChain, DNode> displayedChains) {
List<RGBValues> pathColors = chains.stream().map(displayedChains::get).map(ShapeUtil::getNodeColorStyle)
.collect(Collectors.toList());
if (pathColors.size() > 1) {
DEdgeIconCache.getInstance().setIcon(edge, pathColors);
DEdgeIconCache.getInstance().setLabel(edge, DiagramServices.getDiagramServices()
.getOverlappedLabels(chains.stream().map(AbstractNamedElement::getName).collect(Collectors.toList())));
} else {
DEdgeIconCache.getInstance().removeIcon(edge);
DEdgeIconCache.getInstance().setLabel(edge, ICommonConstants.EMPTY_STRING);
}
}
public void resetFunctionalExchangeEdgeLabels(DEdge edge) {
DEdgeIconCache.getInstance().removeIcon(edge);
DEdgeIconCache.getInstance().setLabel(edge, ICommonConstants.EMPTY_STRING);
DiagramServices.getDiagramServices().refreshBeginEndLabels(edge);
CapellaServices.getService().refreshElement(edge);
}
/**
* Retrieve map of displayed functional chains and associated DNode
*/
public HashMap<FunctionalChain, DNode> getDisplayedFunctionalChains(DDiagram diagram) {
HashMap<FunctionalChain, DNode> functionalChainToNodeMap = new HashMap<>();
for (DDiagramElement aNode : diagram.getOwnedDiagramElements()) {
if ((aNode instanceof DNode) && (aNode.getTarget() instanceof FunctionalChain)) {
functionalChainToNodeMap.put((FunctionalChain) aNode.getTarget(), (DNode) aNode);
}
}
return functionalChainToNodeMap;
}
/**
* Retrieve set of displayed functional chains
*/
public Set<FunctionalChain> getDisplayedFunctionalChainsOnDiagram(DDiagram diagram) {
Set<FunctionalChain> functionalChains = new HashSet<>();
for (DDiagramElement aNode : diagram.getOwnedDiagramElements()) {
if ((aNode instanceof DNode) && (aNode.getTarget() instanceof FunctionalChain)) {
functionalChains.add((FunctionalChain) aNode.getTarget());
}
}
return functionalChains;
}
public Collection<EObject> getFCInternalLinkSemanticElements(DEdge view) {
Collection<FunctionalChain> displayedChains = getDisplayedFunctionalChainsOnDiagram(view.getParentDiagram());
return view.getSemanticElements().stream().filter(x -> displayedChains.contains(x)).collect(Collectors.toList());
}
/**
* returns whether an internal link can be created between given both bordered nodes
*
* @param currentSourceNode
* @param currentTargetNode
* @return
*/
public boolean isValidInternalLinkEdge(FunctionalChain chain, EdgeTarget currentSourceNode,
EdgeTarget currentTargetNode) {
if (currentSourceNode == null || currentSourceNode.getIncomingEdges().isEmpty()) {
return false;
}
if (currentTargetNode == null || currentTargetNode.getOutgoingEdges().isEmpty()) {
return false;
}
// At least one incoming edge should be visible
if (!hasVisibleEdge(currentSourceNode.getIncomingEdges())) {
return false;
}
// At least one outgoing edge should be visible
if (!hasVisibleEdge(currentTargetNode.getOutgoingEdges())) {
return false;
}
// Allow internal links only on same parent
EObject sourceParent = currentSourceNode.eContainer();
EObject targetParent = currentTargetNode.eContainer();
if (sourceParent == null || targetParent == null || !sourceParent.equals(targetParent)) {
return false;
}
// If there is a link between these two ports
InvolvementGraph g = FunctionalChainCache.getInstance().getInvolvementGraph(chain);
InternalLinksGraph g2 = FunctionalChainCache.getInstance().getInternalLinksGraph(g);
if (!g2.hasInternalLink(((DDiagramElement) currentSourceNode).getTarget(),
((DDiagramElement) currentTargetNode).getTarget())) {
return false;
}
DDiagram diagram = ((DDiagramElement) currentSourceNode).getParentDiagram();
DEdge anotherEdge = DiagramServices.getDiagramServices().findInternalLinkEdge(diagram, currentSourceNode,
currentTargetNode, getInternLinkEdgeMapping(diagram));
boolean hasAnotherEdge = anotherEdge != null && !(chain.equals(anotherEdge.getTarget()));
return !hasAnotherEdge;
}
private boolean hasVisibleEdge(EList<DEdge> edges) {
for (DEdge edge : edges) {
if (!DiagramServices.getDiagramServices().isHidden(edge)
&& !DiagramServices.getDiagramServices().isFiltered(edge)) {
return true;
}
}
return false;
}
/**
* @param fc
* @return
*/
protected Collection<FunctionalExchange> getFlatPreviousFunctionalExchanges(FunctionalChain fc,
FunctionalChainInvolvement fci) {
Collection<FunctionalExchange> result = new HashSet<>();
for (FunctionalChainInvolvement involvment : FunctionalChainExt.getFlatPreviousExchangeInvolvements(fci)) {
if (involvment.getInvolved() instanceof FunctionalExchange) {
result.add((FunctionalExchange) involvment.getInvolved());
}
}
return result;
}
/**
* @param fc
* @return
*/
protected Collection<FunctionalExchange> getFlatNextFunctionalExchanges(FunctionalChain fc,
FunctionalChainInvolvement fci) {
Collection<FunctionalExchange> result = new HashSet<>();
for (FunctionalChainInvolvement involvment : FunctionalChainExt.getFlatNextExchangeInvolvements(fci)) {
if (involvment.getInvolved() instanceof FunctionalExchange) {
result.add((FunctionalExchange) involvment.getInvolved());
}
}
return result;
}
/**
* @param sourceNode
* @param targetNode
* @param color
* @return
*/
public DEdge createInternalLink(EdgeTarget sourceNode, EdgeTarget targetNode, FunctionalChain fc) {
// Before creating, check if the internal link is possible to display, depending on conditions of their 2 ports.
DDiagram diagram = CapellaServices.getService().getDiagramContainer(sourceNode);
EdgeMapping mapping = getInternLinkEdgeMapping(diagram);
DEdge newEdge = DiagramServices.getDiagramServices().findInternalLinkEdge(diagram, sourceNode, targetNode, mapping);
if (newEdge == null && isValidInternalLinkEdge(fc, sourceNode, targetNode)) {
DiagramServices.getDiagramServices().createEdge(mapping, sourceNode, targetNode, fc);
newEdge = DiagramServices.getDiagramServices().findInternalLinkEdge(diagram, sourceNode, targetNode, mapping);
}
if (newEdge != null && !newEdge.getSemanticElements().contains(fc)) {
newEdge.getSemanticElements().add(fc);
}
return newEdge;
}
public EdgeMapping getInternLinkEdgeMapping(DDiagram diagram) {
return DiagramServices.getDiagramServices().getEdgeMapping(diagram,
MappingConstantsHelper.getInternLinkEdgeMapping(diagram));
}
/**
* @param aEdge
*/
public void resetFunctionalExchangeStyle(DEdge aEdge) {
DiagramElementMapping mapping = DiagramServices.getDiagramServices().getEdgeMapping(aEdge);
if (mapping != null) {
EdgeStyle edgeStyle = aEdge.getOwnedStyle();
Integer currentSize = edgeStyle.getSize();
// reset style & color : if current size is the default size of Functional Chain applied by FunctionalChains (e.g.
// as custom feature)
if ((null != currentSize) && currentSize.equals(THICK_EDGE_FUNCTIONAL_CHAIN)
&& ShapeUtil.isCustomisation(edgeStyle, DiagramPackage.Literals.EDGE_STYLE__SIZE)) {
// get default style size of an edge
EdgeStyleDescription desc = (EdgeStyleDescription) getMappingHelper(aEdge).getBestStyleDescription(mapping,
aEdge.getTarget(), aEdge, aEdge.eContainer(), CapellaServices.getService().getDiagramContainer(aEdge));
String defaultStyleSize = desc.getSizeComputationExpression();
if (defaultStyleSize != null) {
if (ShapeUtil.resetEdgeThickStyle(aEdge, Integer.valueOf(defaultStyleSize))) {
ShapeUtil.resetEdgeColorStyle(aEdge, ShapeUtil.getDefaultColor(aEdge, desc, desc.getStrokeColor()));
}
}
}
}
}
/**
* @param value
* @param displayedFC
*/
public boolean resetFunctionStyle(DDiagramElement functionNode) {
boolean result = false;
String defaultStyleSize = null;
Integer currentSize = null;
BorderedStyle style = null;
if (functionNode instanceof AbstractDNode) {
AbstractDNode node = (AbstractDNode) functionNode;
BorderedStyleDescription desc = (BorderedStyleDescription) getMappingHelper(functionNode).getBestStyleDescription(
functionNode.getDiagramElementMapping(), functionNode.getTarget(), functionNode, functionNode.eContainer(),
CapellaServices.getService().getDiagramContainer(functionNode));
if (desc != null) {
defaultStyleSize = desc.getBorderSizeComputationExpression();
style = (BorderedStyle) ShapeUtil.getCurrentStyle(node);
if (style != null) {
currentSize = style.getBorderSize();
}
if ((null != currentSize) && (null != defaultStyleSize)) {
// apply style & color : if currentSize is equal to default size + if current size is equal to default size of
// Functional Chain
if (currentSize.equals(THICK_BORDER_SOURCE_FUNCTION) || currentSize.equals(THICK_BORDER_TARGET_FUNCTION)
|| currentSize.equals(defaultStyleSize)) {
if (ShapeUtil.resetBorderStyle(node, Integer.valueOf(desc.getBorderSizeComputationExpression()))) {
if (ShapeUtil.resetBorderColorStyle(node, ShapeUtil.getDefaultColor(node, desc, desc.getBorderColor()))) {
result = true;
}
}
}
}
}
}
return result;
}
public void customizeFunctionalChainEdgeStyle(DEdge edge, RGBValues color) {
// get current style size of an edge
EdgeStyle edgeStyle = edge.getOwnedStyle();
Integer currentSize = edgeStyle.getSize();
// get default style size of an edge
DiagramElementMapping mapping = DiagramServices.getDiagramServices().getEdgeMapping(edge);
if (mapping != null) {
EdgeStyleDescription desc = (EdgeStyleDescription) getMappingHelper(edge).getBestStyleDescription(mapping,
edge.getTarget(), edge, edge.eContainer(), CapellaServices.getService().getDiagramContainer(edge));
if (null != desc) {
// assuming it is an integer value
String defaultSize = desc.getSizeComputationExpression();
if ((null != defaultSize) && (null != currentSize) && (currentSize.equals(Integer.valueOf(defaultSize))
|| (currentSize.equals(THICK_EDGE_FUNCTIONAL_CHAIN)))) {
// apply change
customizeEdgeStyle(edge, color);
}
}
}
}
public void customizeInternalLinksEdgeStyle(DEdge edge, RGBValues color) {
customizeEdgeStyle(edge, color);
}
public void customizeEdgeStyle(DEdge edge, RGBValues color) {
RGB rgbColor = new RGB(color.getRed(), color.getGreen(), color.getBlue());
ShapeUtil.setEdgeColorStyle(edge, rgbColor);
ShapeUtil.setEdgeThickStyle(edge, THICK_EDGE_FUNCTIONAL_CHAIN);
}
public void customizeFunctionStyle(DDiagramElement functionNode, RGBValues color) {
// Change color border style
RGB rgbColor = new RGB(color.getRed(), color.getGreen(), color.getBlue());
if (functionNode instanceof AbstractDNode) {
ShapeUtil.setBorderColorStyle(((AbstractDNode) functionNode), rgbColor);
}
}
public void customizeSourceFunctionStyle(DDiagramElement functionNode, RGBValues color) {
customizeFunctionStyle(functionNode, color);
if (functionNode instanceof AbstractDNode) {
ShapeUtil.setBorderStyle(((AbstractDNode) functionNode), THICK_BORDER_SOURCE_FUNCTION);
}
}
public void customizeTargetFunctionStyle(DDiagramElement functionNode, RGBValues color) {
customizeFunctionStyle(functionNode, color);
if (functionNode instanceof AbstractDNode) {
ShapeUtil.setBorderStyle(((AbstractDNode) functionNode), THICK_BORDER_TARGET_FUNCTION);
}
}
/**
* @param function
* @param displayedFunctions
* @return the function or one of its container contained in the map keys
*/
public Collection<DSemanticDecorator> getBestDisplayedFunctionNode(AbstractFunction function, DDiagram diagram) {
EObject semantic = function;
Collection<DSemanticDecorator> elements = DiagramServices.getDiagramServices().getDiagramElements(diagram,
function);
while (elements.isEmpty() && semantic != null) {
elements = DiagramServices.getDiagramServices().getDiagramElements(diagram, semantic);
semantic = semantic.eContainer();
}
return elements;
}
public boolean isCompleteFunctionalChain(FunctionalChain fc, DDiagram diagram) {
Set<FunctionalExchange> functionalExchangesOnTheChain = FunctionalChainExt.getFlatFunctionalExchanges(fc);
int numberOfVisibleRelatedFEEdges = 0;
for (DEdge anEdge : diagram.getEdges()) {
if (anEdge.isVisible()) {
EObject edgeTarget = anEdge.getTarget();
if (edgeTarget instanceof FunctionalExchange && functionalExchangesOnTheChain.contains(edgeTarget)) {
numberOfVisibleRelatedFEEdges++;
}
}
}
return numberOfVisibleRelatedFEEdges == functionalExchangesOnTheChain.size();
}
public boolean isFCRegion(EObject view) {
return view instanceof AbstractDNode && IMappingNameConstants.FCD_CHAIN_REFERENCE_CHAIN__MAPPING_NAME.equals(((AbstractDNode) view).getDiagramElementMapping().getName());
}
public String getFCInvolvmentLinkLabel(FunctionalChainInvolvementLink involvementLink, DDiagram diagram) {
String label = "";
InvolvedElement involved = involvementLink.getInvolved();
if (involved instanceof FunctionalExchange) {
FunctionalExchange functionalExchange = (FunctionalExchange) involvementLink.getInvolved();
label = functionalExchange.getName();
Set<String> filterNames = diagram.getActivatedFilters().stream().map(FilterDescription::getName)
.collect(Collectors.toSet());
boolean showExchangeItems = filterNames.contains(IMappingNameConstants.SHOW_EXCHANGE_ITEMS);
boolean showExchangeItemsParameters = filterNames.contains(IMappingNameConstants.SHOW_EXCHANGE_ITEMS_PARAMETERS);
boolean showFEExchangeItems = filterNames.contains(IMappingNameConstants.SHOW_FUNCTIONAL_EXCHANGES_ECHANGE_ITEMS);
if (showExchangeItems || showFEExchangeItems) {
StringBuilder sb = new StringBuilder();
if (showFEExchangeItems) {
sb.append(label);
sb.append("["); //$NON-NLS-1$
}
EList<ExchangeItem> exchangedItemsForLabel = involvementLink.getExchangedItems().isEmpty()
? functionalExchange.getExchangedItems()
: involvementLink.getExchangedItems();
String exchangedItemsLabel = exchangedItemsForLabel.stream()
.map(ei -> ExchangeItemExt.getEILabel(ei, showExchangeItemsParameters).toString())
.collect(Collectors.joining(", "));
sb.append(exchangedItemsLabel);
if (showFEExchangeItems) {
sb.append("]"); //$NON-NLS-1$
}
label = sb.toString();
}
} else if (involved instanceof AbstractFunction) {
AbstractFunction function = (AbstractFunction) involved;
label = function.getName();
}
return label;
}
public String getFunctionalChainLabel(FunctionalChain chain, DDiagram diagram) {
boolean displayIncompleteLabel = false;
boolean displayInvalidLabel = false;
if (chain instanceof OperationalProcess) {
displayIncompleteLabel = ScopedCapellaPreferencesStore.getBoolean(
DiagramProcessChainPathPreferencePage.NAME_PREF_DISPLAY_INCOMPLETE_IN_OPERATIONAL_PROCESS_LABEL,
PreferencesHelper.getProject(chain));
displayInvalidLabel = ScopedCapellaPreferencesStore.getBoolean(
DiagramProcessChainPathPreferencePage.NAME_PREF_DISPLAY_INVALID_IN_OPERATIONAL_PROCESS_LABEL,
PreferencesHelper.getProject(chain));
} else {
displayIncompleteLabel = ScopedCapellaPreferencesStore.getBoolean(
DiagramProcessChainPathPreferencePage.NAME_PREF_DISPLAY_INCOMPLETE_IN_FUNCTIONAL_CHAIN_LABEL,
PreferencesHelper.getProject(chain));
displayInvalidLabel = ScopedCapellaPreferencesStore.getBoolean(
DiagramProcessChainPathPreferencePage.NAME_PREF_DISPLAY_INVALID_IN_FUNCTIONAL_CHAIN_LABEL,
PreferencesHelper.getProject(chain));
}
List<String> chainStatusLabels = new ArrayList<>();
if (displayIncompleteLabel && !isCompleteFunctionalChain(chain, diagram)) {
chainStatusLabels.add(INCOMPLETE_FUNCTIONAL_CHAIN_LABEL);
}
if (displayInvalidLabel && !FunctionalChainExt.isFunctionalChainValid(chain)) {
chainStatusLabels.add(INVALID_FUNCTIONAL_CHAIN_LABEL);
}
String chainLabel = EObjectExt.getText(chain);
String chainStatusLabel = chainStatusLabels.isEmpty() ? ""
: chainStatusLabels.stream().collect(Collectors.joining(", ", " (", ")"));
return chainLabel + chainStatusLabel;
}
public void updateFunctionalChainNodeColor(DNode fcNode, Collection<DNode> visibleFunctionalChains) {
RGBValues color = ShapeUtil.getNodeColorStyle(fcNode);
ColorManager colorManager = ColorManager.getInstance();
List<RGB> colorList = colorManager.getColorList();
boolean changeColor = false;
if (ShapeUtil.isSameColor(color, colorManager.getGrayColor())) {
changeColor = true;
}
for (DNode aFc : visibleFunctionalChains) {
if (!aFc.equals(fcNode)) {
RGBValues nodeColor = ShapeUtil.getNodeColorStyle(aFc);
if (ShapeUtil.isSameColor(nodeColor, color)) {
changeColor = true;
}
ShapeUtil.removeColorFromList(nodeColor, colorList);
}
}
if (!changeColor) {
return;
}
if (!colorList.isEmpty()) {
ShapeUtil.setColorStyle(fcNode, colorList.get(0));
}
}
public List<FunctionalExchange> getAvailableExchanges(EObject context, AbstractFunction source,
AbstractFunction target) {
List<FunctionalExchange> returnedFunctionalExchanges = new ArrayList<>();
List<FunctionalExchange> incoming = FunctionExt.getIncomingExchange(target);
List<FunctionalExchange> outgoing = FunctionExt.getOutGoingExchange(source);
for (FunctionalExchange aFunctionalExchange : incoming) {
if (outgoing.contains(aFunctionalExchange)) {
returnedFunctionalExchanges.add(aFunctionalExchange);
}
}
return returnedFunctionalExchanges;
}
public List<FunctionalChainInvolvement> getPreviousInvolvements(FunctionalChainInvolvement involvement) {
return involvement.getPreviousFunctionalChainInvolvements();
}
public EObject createFunctionalChain(EObject context, List<EObject> views) {
if (!views.isEmpty()) {
List<EObject> newList = new ArrayList<>();
AbstractFunction aFunction = null;
for (EObject aSelectedElement : views) {
if ((aSelectedElement instanceof DDiagramElement)
&& (((DDiagramElement) aSelectedElement).getTarget() != null)) {
newList.add(((DDiagramElement) aSelectedElement).getTarget());
if ((aFunction == null) && (((DDiagramElement) aSelectedElement).getTarget() instanceof FunctionalExchange)) {
AbstractFunction aSourceFunction = FunctionExt
.getIncomingAbstractFunction(((FunctionalExchange) ((DDiagramElement) aSelectedElement).getTarget()));
aFunction = FunctionExt.getRootFunction(aSourceFunction);
}
}
}
if (aFunction != null) {
EObject diagramContainer = ((DSemanticDiagram) CapellaServices.getService().getDiagramContainer(views.get(0)))
.getTarget();
FunctionalChain newFC;
if (diagramContainer instanceof AbstractFunctionalChainContainer) {
newFC = FunctionalChainExt.createFunctionalChain((AbstractFunctionalChainContainer) diagramContainer,
newList);
} else {
newFC = FunctionalChainExt.createFunctionalChain(aFunction, newList);
}
return newFC;
}
}
return context;
}
public void removeFunctionalChainAbstractCapabilityInvolvement(AbstractCapability capability, EObject target) {
Set<FunctionalChainAbstractCapabilityInvolvement> toRemove = new HashSet<FunctionalChainAbstractCapabilityInvolvement>();
for (FunctionalChainAbstractCapabilityInvolvement inv : capability
.getOwnedFunctionalChainAbstractCapabilityInvolvements()) {
if (inv.getInvolved().equals(target)) {
toRemove.add(inv);
}
}
for (FunctionalChainAbstractCapabilityInvolvement involvement : toRemove) {
involvement.destroy();
}
}
public FunctionalChainAbstractCapabilityInvolvement createFunctionalChainAbstractCapabilityInvolvement(
AbstractCapability capability, FunctionalChain target) {
return FunctionalChainExt.createFunctionalChainAbstractCapabilityInvolvement(capability, target);
}
public boolean isValidFunctionalChainSelection(EObject context, List<EObject> views) {
SimpleOrientedGraph<AbstractFunction> graph = new SimpleOrientedGraph<AbstractFunction>();
if (!views.isEmpty()) {
for (EObject aSelectedElement : views) {
if ((aSelectedElement instanceof DEdge) && (((DEdge) aSelectedElement).getTarget() != null)
&& (((DEdge) aSelectedElement).getTarget() instanceof FunctionalExchange)) {
FunctionalExchange aSelectedExchange = (FunctionalExchange) ((DEdge) aSelectedElement).getTarget();
AbstractFunction sourceFunction = FunctionExt.getIncomingAbstractFunction(aSelectedExchange);
AbstractFunction targetFunction = FunctionExt.getOutGoingAbstractFunction(aSelectedExchange);
graph.addNode(sourceFunction, targetFunction);
}
}
if (!graph.isEmpty()) {
return graph.isValid();
}
return false;
}
return false;
}
/**
* Returns the scope used for the selection wizard in the Involve Function tool.
*
* @param container
* the container
* @return the scope used for the selection wizard in the Involve Function tool.
*/
public List<AbstractFunction> computeFCIFunctionScope(EObject container) {
BlockArchitecture architecture = BlockArchitectureExt.getRootBlockArchitecture(container);
if (architecture != null) {
return FunctionExt.getAllAbstractFunctions(architecture);
}
return Collections.emptyList();
}
/**
* Returns the incoming and outgoing Function Exchanges for the current node.
*
* @param node
* the node
* @return the outgoing Function Exchanges.
*/
protected Set<FunctionalExchange> getAllFunctionalExchanges(DNode node) {
return DiagramServices.getDiagramServices().getAllEdges(node).stream().map(DEdge::getTarget)
.filter(FunctionalChainInvolvementLink.class::isInstance)
.map(link -> ((FunctionalChainInvolvementLink) link).getInvolved()).filter(FunctionalExchange.class::isInstance)
.map(FunctionalExchange.class::cast).collect(Collectors.toSet());
}
/**
* Returns the ordered scope used for the selection wizard in the Involve Functional Exchange and Function tool.
*
* @param node
* the container node
* @return the ordered scope used for the selection wizard in the Involve Functional Exchange and Function tool.
*/
public Collection<FunctionalExchange> computeFCIFunctionalExchangeAndFunctionScope(DNode node) {
Set<FunctionalExchange> existingInvolvedFunctionalExchanges = getAllFunctionalExchanges(node);
FunctionalChainInvolvementFunction selectedFunction = (FunctionalChainInvolvementFunction) node.getTarget();
Collection<FunctionalExchange> possibleFunctionalExchanges = new LinkedHashSet<>();
possibleFunctionalExchanges.addAll(FunctionalChainExt.getFlatIncomingExchanges(selectedFunction));
possibleFunctionalExchanges.addAll(FunctionalChainExt.getFlatOutgoingExchanges(selectedFunction));
possibleFunctionalExchanges.removeAll(existingInvolvedFunctionalExchanges);
return possibleFunctionalExchanges;
}
/**
* Precondition for the creation of a Functional Chain Involvement Link having a Functional Exchange as Involved.
*
* @param sourceView
* the semantic source
*
* @param targetView
* the semantic target
*
* @return true if a link can be created, false otherwise.
*/
public boolean isValidFCILinkExchange(DNode sourceView, DNode targetView) {
EObject source = sourceView.getTarget();
EObject target = targetView.getTarget();
if (source instanceof FunctionalChainInvolvementFunction && target instanceof FunctionalChainInvolvementFunction) {
FunctionalChainInvolvementFunction sourceFunction = (FunctionalChainInvolvementFunction) source;
FunctionalChainInvolvementFunction targetFunction = (FunctionalChainInvolvementFunction) target;
Collection<FunctionalExchange> commonExchanges = FunctionalChainExt
.getFlatCommonFunctionalExchanges(sourceFunction, targetFunction);
return !commonExchanges.isEmpty() && !doesConnectionExist(targetView, sourceView, new HashSet<>());
}
return false;
}
/**
* Precondition for the creation of a Functional Chain Involvement Link having a Function as Involved.
*
* @param source
* the semantic source
* @param sourceView
* the source view
* @param target
* the semantic target
* @param targetView
* the target view
* @return true if a link can be created, false otherwise.
*/
public boolean isValidFCILinkFunction(FunctionalChainInvolvementFunction source, EdgeTarget sourceView,
FunctionalChainInvolvementFunction target, EdgeTarget targetView) {
return sourceView != targetView && isSameFunctionInvolved(source, target) && !isSameFunctionalChain(source, target);
}
/**
* Returns true if the same function is involved by both parameters, false otherwise.
*
* @param sourceInvolvement
* the source involvement parameter.
* @param targetInvolvement
* the target involvement parameter.
* @return true if the same function is involved by both parameters, false otherwise
*/