-
Notifications
You must be signed in to change notification settings - Fork 85
/
ComponentExt.java
2993 lines (2659 loc) · 102 KB
/
ComponentExt.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, 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.core.model.helpers;
import static org.polarsys.capella.common.helpers.cache.ModelCache.getCache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.validation.IValidationContext;
import org.polarsys.capella.common.data.modellingcore.AbstractExchangeItem;
import org.polarsys.capella.common.data.modellingcore.AbstractInformationFlow;
import org.polarsys.capella.common.data.modellingcore.AbstractType;
import org.polarsys.capella.common.data.modellingcore.AbstractTypedElement;
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.core.data.capellacommon.CapabilityRealizationInvolvedElement;
import org.polarsys.capella.core.data.capellacommon.CapabilityRealizationInvolvement;
import org.polarsys.capella.core.data.capellacommon.CapellacommonFactory;
import org.polarsys.capella.core.data.capellacommon.State;
import org.polarsys.capella.core.data.capellacommon.StateMachine;
import org.polarsys.capella.core.data.capellacore.CapellaElement;
import org.polarsys.capella.core.data.capellacore.Feature;
import org.polarsys.capella.core.data.capellacore.GeneralizableElement;
import org.polarsys.capella.core.data.capellacore.Relationship;
import org.polarsys.capella.core.data.capellacore.Structure;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.ComponentArchitecture;
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.DeploymentTarget;
import org.polarsys.capella.core.data.cs.ExchangeItemAllocation;
import org.polarsys.capella.core.data.cs.Interface;
import org.polarsys.capella.core.data.cs.InterfaceImplementation;
import org.polarsys.capella.core.data.cs.InterfacePkg;
import org.polarsys.capella.core.data.cs.InterfaceUse;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.cs.PhysicalPort;
import org.polarsys.capella.core.data.ctx.CtxFactory;
import org.polarsys.capella.core.data.ctx.SystemComponent;
import org.polarsys.capella.core.data.ctx.SystemComponentPkg;
import org.polarsys.capella.core.data.epbs.ConfigurationItem;
import org.polarsys.capella.core.data.epbs.ConfigurationItemPkg;
import org.polarsys.capella.core.data.fa.AbstractFunction;
import org.polarsys.capella.core.data.fa.ComponentExchange;
import org.polarsys.capella.core.data.fa.ComponentExchangeEnd;
import org.polarsys.capella.core.data.fa.ComponentExchangeKind;
import org.polarsys.capella.core.data.fa.ComponentPort;
import org.polarsys.capella.core.data.fa.ComponentPortKind;
import org.polarsys.capella.core.data.fa.FaFactory;
import org.polarsys.capella.core.data.fa.FaPackage;
import org.polarsys.capella.core.data.fa.FunctionalExchange;
import org.polarsys.capella.core.data.fa.OrientationPortKind;
import org.polarsys.capella.core.data.helpers.capellacore.services.GeneralizableElementExt;
import org.polarsys.capella.core.data.helpers.fa.services.FunctionExt;
import org.polarsys.capella.core.data.helpers.fa.services.FunctionalExt;
import org.polarsys.capella.core.data.information.AbstractInstance;
import org.polarsys.capella.core.data.information.DataPkg;
import org.polarsys.capella.core.data.information.ExchangeItem;
import org.polarsys.capella.core.data.information.ExchangeItemInstance;
import org.polarsys.capella.core.data.information.InformationFactory;
import org.polarsys.capella.core.data.information.Port;
import org.polarsys.capella.core.data.information.communication.CommunicationFactory;
import org.polarsys.capella.core.data.information.communication.CommunicationLink;
import org.polarsys.capella.core.data.interaction.Execution;
import org.polarsys.capella.core.data.interaction.InstanceRole;
import org.polarsys.capella.core.data.interaction.MessageKind;
import org.polarsys.capella.core.data.interaction.SequenceMessage;
import org.polarsys.capella.core.data.la.CapabilityRealization;
import org.polarsys.capella.core.data.la.LaFactory;
import org.polarsys.capella.core.data.la.LogicalComponent;
import org.polarsys.capella.core.data.la.LogicalComponentPkg;
import org.polarsys.capella.core.data.oa.CommunicationMean;
import org.polarsys.capella.core.data.oa.Entity;
import org.polarsys.capella.core.data.oa.OaFactory;
import org.polarsys.capella.core.data.pa.PaFactory;
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.model.helpers.naming.NamingConstants;
/**
* Component helpers
*/
public class ComponentExt {
/**
* This method adds an interface implementation.
*
* @param component
* the component who will implement the interface
* @param interf
* the implemented interface
*/
public static void addImplementedInterface(Component component, Interface interf) {
if ((component != null) && (interf != null)) {
if (!getImplementedInterfaces(component).contains(interf)) {
InterfaceImplementation impl = CsFactory.eINSTANCE.createInterfaceImplementation();
impl.setImplementedInterface(interf);
component.getOwnedInterfaceImplementations().add(impl);
}
}
}
/**
* This method adds an provide interface. If the StandardPort doesn't exist, it will be create.
*/
public static void addProvidedInterface(Component component, Interface interf) {
if (component != null && interf != null) {
ComponentPort stdPort = getOrCreateFirstContainedComponentPort(component);
stdPort.getProvidedInterfaces().add(interf);
}
}
/**
* This method adds an required interface. If the StandardPort doesn't exist, it will be create.
*/
public static void addRequiredInterface(Component component, Interface interf) {
if (component != null && interf != null) {
ComponentPort stdPort = getOrCreateFirstContainedComponentPort(component);
stdPort.getRequiredInterfaces().add(interf);
}
}
/**
* This method returns the first contained ComponentPort, or create a StandardPort otherwise
*
* @param component
* the component who contains the componentPort
* @return the componentPort
*/
public static ComponentPort getOrCreateFirstContainedComponentPort(Component component) {
if (!component.getContainedComponentPorts().isEmpty()) {
return component.getContainedComponentPorts().get(0);
}
ComponentPort stdPort = PortExt.createStandardPort();
component.getOwnedFeatures().add(stdPort);
return stdPort;
}
/**
* This method adds an interface usage.
*
* @param component
* the component who will use the interface
* @param interf
* the used interface
*/
public static void addUsedInterface(Component component, Interface interf) {
if ((component != null) && (interf != null)) {
if (!getUsedInterfaces(component).contains(interf)) {
InterfaceUse use = CsFactory.eINSTANCE.createInterfaceUse();
use.setUsedInterface(interf);
component.getOwnedInterfaceUses().add(use);
}
}
}
/**
* This method adds a communication link.
*
* @param component
* the component who will use the interface
* @param communicationLink
* the communication link
* @param isGenerated
* true if usage is generated, false otherwise
*/
public static void addCommunicationLink(Component component, CommunicationLink communicationLink) {
if ((component != null) && (communicationLink != null)) {
if (!getExchangeItems(component).contains(communicationLink.getExchangeItem())) {
CommunicationLink lnk = CommunicationFactory.eINSTANCE.createCommunicationLink();
lnk.setKind(communicationLink.getKind());
lnk.setProtocol(communicationLink.getProtocol());
lnk.setExchangeItem(communicationLink.getExchangeItem());
component.getOwnedCommunicationLinks().add(lnk);
}
}
}
/**
*
*/
public static List<AbstractExchangeItem> getExchangeItems(Component component) {
List<AbstractExchangeItem> result = new ArrayList<AbstractExchangeItem>();
if (component != null) {
for (CommunicationLink lnk : component.getOwnedCommunicationLinks()) {
ExchangeItem exchangeItem = lnk.getExchangeItem();
if (null != exchangeItem) {
result.add(exchangeItem);
}
}
}
return result;
}
/**
* Return all exchange items allocated by all functions port of the given component
*
* @param component
* @return componentFunctionExchangeItems
*/
public static Set<AbstractExchangeItem> getAllocatedFunctionExchangeItems(Component component) {
Set<AbstractExchangeItem> componentFunctionExchangeItems = new HashSet<AbstractExchangeItem>();
Set<AbstractFunction> allocatedFunctions = (Set<AbstractFunction>) ComponentExt.getAllocatedFunctions(component);
for (AbstractFunction abstractFunction : allocatedFunctions) {
componentFunctionExchangeItems = (Set<AbstractExchangeItem>) AbstractFunctionExt
.getAllExchangeItems(abstractFunction);
}
return componentFunctionExchangeItems;
}
/**
* This method removes an exchange item realization.
*
* @param component
* the component who is using the interface
* @param communicationLink
* the communication link
*/
public static void removeCommunicationLink(Component component, CommunicationLink communicationLink) {
CommunicationLink link = null;
if (null != communicationLink) {
ListIterator<CommunicationLink> it = component.getOwnedCommunicationLinks().listIterator();
while (it.hasNext()) {
CommunicationLink lnk = it.next();
if (lnk.getExchangeItem().equals(communicationLink.getExchangeItem())
&& lnk.getProtocol().equals(communicationLink.getProtocol())
&& lnk.getKind().equals(communicationLink.getKind())) {
link = lnk;
}
}
}
if (link != null) {
link.destroy();
}
}
/**
* @param message
* @param selectedOperation
* @param portStrategie
* @param targetOnExchangeItem
*/
public static void ensureUseAndImplementsForOperation(SequenceMessage message,
ExchangeItemAllocation selectedOperation, boolean portStrategie, EObject targetOnExchangeItem) {
AbstractInstance roleSource = message.getSendingEnd() == null ? null
: message.getSendingEnd().getCovered().getRepresentedInstance();
AbstractInstance roleTarget = message.getReceivingEnd() == null ? null
: message.getReceivingEnd().getCovered().getRepresentedInstance();
Component src = null;
Component tgt = null;
// Get source component (if any).
if ((roleSource != null) && (roleSource.getAbstractType() instanceof Component)) {
src = (Component) roleSource.getAbstractType();
}
// Get target component (if any).
if ((roleTarget != null) && (roleTarget.getAbstractType() instanceof Component)) {
tgt = (Component) roleTarget.getAbstractType();
}
// communication pattern : if the message has an EIAllocation which is a READ, there is a logical
// inversion of use/implements. On departure of the message, it is the component which implements the interface
// allocating the shared data and not its use.
if ((src == null) || (tgt == null)) {
// To be a READ a message must have the following features:
// - Its target must be an ExchangeItemInstance (on an Execution or directly on the InstanceRole),
// - And it must be a SYNCHRONOUS CALL.
if ((roleTarget instanceof ExchangeItemInstance)
&& ((targetOnExchangeItem instanceof Execution) || (targetOnExchangeItem instanceof InstanceRole))
&& (MessageKind.SYNCHRONOUS_CALL == message.getKind())) {
Component temp = src;
src = tgt;
tgt = temp;
}
}
Interface interf = selectedOperation.getAllocatingInterface();
// computing all interfaces used by the source component
Collection<Interface> allUsedInterfaces = new HashSet<Interface>();
for (Component comp : getAllAncestors(src)) {
allUsedInterfaces.addAll(comp.getUsedInterfaces());
for (Feature f : comp.getOwnedFeatures()) {
if (f instanceof ComponentPort) {
ComponentPort port = (ComponentPort) f;
allUsedInterfaces.addAll(port.getRequiredInterfaces());
}
}
}
// computing all interfaces implemented by the target component
Collection<Interface> allImplementedInterfaces = new HashSet<Interface>();
for (Component comp : getAllAncestors(tgt)) {
allImplementedInterfaces.addAll(comp.getImplementedInterfaces());
for (Feature f : comp.getOwnedFeatures()) {
if (f instanceof ComponentPort) {
ComponentPort port = (ComponentPort) f;
allImplementedInterfaces.addAll(port.getProvidedInterfaces());
}
}
}
// propagation to interface hierarchy
allUsedInterfaces.addAll(InterfaceExt.getAllSuperGeneralizableElements(allUsedInterfaces));
allImplementedInterfaces.addAll(InterfaceExt.getAllSuperGeneralizableElements(allImplementedInterfaces));
if ((src != null) && !allUsedInterfaces.contains(interf)) {
if (portStrategie) {
ComponentPort port = FaFactory.eINSTANCE.createComponentPort("Requires " + interf.getName()); //$NON-NLS-1$
port.getRequiredInterfaces().add(interf);
src.getOwnedFeatures().add(port);
} else {
// missing usage ling from src
InterfaceUse usage = CsFactory.eINSTANCE.createInterfaceUse();
src.getOwnedInterfaceUses().add(usage);
usage.setUsedInterface(interf);
}
}
if ((tgt != null) && !allImplementedInterfaces.contains(interf)) {
if (portStrategie) {
ComponentPort port = FaFactory.eINSTANCE.createComponentPort("Provides " + interf.getName()); //$NON-NLS-1$
port.getProvidedInterfaces().add(interf);
tgt.getOwnedFeatures().add(port);
} else {
// missing implementation ling from tgt
InterfaceImplementation impl = CsFactory.eINSTANCE.createInterfaceImplementation();
tgt.getOwnedInterfaceImplementations().add(impl);
impl.setImplementedInterface(interf);
}
}
}
public static List<Component> getAllAncestors(Component component) {
List<Component> result = new ArrayList<Component>();
GeneralizableElement container = component;
while (container != null) {
if (container instanceof Component) {
result.add((Component) container);
}
List<GeneralizableElement> parents = container.getSuper();
if (parents.size() == 0) {
container = null;
} else {
container = parents.get(0);
}
}
return result;
}
/**
* @param cpnt
* @return
*/
static public List<Part> getAllComponentInstances(Component cpnt) {
List<Part> instList = new ArrayList<Part>();
for (EObject obj : EObjectExt.getAll(cpnt, CsPackage.Literals.PART)) {
instList.add((Part) obj);
}
return instList;
}
/**
* This method retrieves all the implemented interfaces.
*
* @param cpnt
* the component whose implemented AND provided (by StandardPorts) interfaces will be retrieved (and all its
* inherited components)
* @return List<Interface> the implemented interfaces
*/
public static List<Interface> getAllImplementedAndProvidedInterfaces(Component cpnt) {
List<Interface> implementedAndProvidedItf = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (InterfaceImplementation impl : ((Component) element).getImplementedInterfaceLinks()) {
Interface itf = impl.getImplementedInterface();
if ((itf != null) && !implementedAndProvidedItf.contains(itf)) {
implementedAndProvidedItf.add(itf);
for (GeneralizableElement elt : GeneralizableElementExt.getAllSuperGeneralizableElements(itf)) {
if (!implementedAndProvidedItf.contains(elt)) {
implementedAndProvidedItf.add((Interface) elt);
}
}
}
}
for (Interface abstractItf : ((Component) element).getProvidedInterfaces()) {
if (!implementedAndProvidedItf.contains(abstractItf)) {
implementedAndProvidedItf.add(abstractItf);
for (GeneralizableElement elt : GeneralizableElementExt.getAllSuperGeneralizableElements(abstractItf)) {
if (!implementedAndProvidedItf.contains(elt)) {
implementedAndProvidedItf.add((Interface) elt);
}
}
}
}
}
}
}
return implementedAndProvidedItf;
}
/**
* This method retrieves all the implemented interfaces.
*
* @param cpnt
* the component whose implemented interfaces will be retrieved (and all its inherited components)
* @return List<Interface> the implemented interfaces
*/
public static List<Interface> getAllImplementedInterfaces(Component cpnt) {
List<Interface> implementedInterfaces = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (InterfaceImplementation impl : ((Component) element).getImplementedInterfaceLinks()) {
Interface itf = impl.getImplementedInterface();
if ((itf != null) && !implementedInterfaces.contains(itf)) {
implementedInterfaces.add(itf);
}
}
}
}
}
return implementedInterfaces;
}
/**
* obtains all relationships ending to cpnt
*
* @param cpnt
* @return
*/
static public List<Relationship> getAllIncomingRelationships(Component cpnt) {
List<Relationship> ret = new ArrayList<Relationship>();
return ret;
}
/**
* obtains all relationships starting from cpnt
*
* @param cpnt
* @return
*/
static public List<Relationship> getAllOutgoingRelationships(Component cpnt) {
List<Relationship> ret = new ArrayList<Relationship>();
ret.addAll(cpnt.getUsedInterfaceLinks());
ret.addAll(cpnt.getImplementedInterfaceLinks());
return ret;
}
/**
* This method retrieves all the provided interfaces recursively
*
* @param component
* @return
*/
public static List<Interface> getAllProvidedInterfaces(Component cpnt) {
List<Interface> requiredItf = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (Interface abstractItf : ((Component) element).getProvidedInterfaces()) {
if (!requiredItf.contains(abstractItf)) {
requiredItf.add(abstractItf);
}
}
}
}
}
return requiredItf;
}
/**
* Return all the connection related to given Component filter by Connection kind Doesn't include CE linked to ports
*/
public static Collection<ComponentExchange> getAllRelatedComponentExchange(Part part) {
return getAllRelatedComponentExchange(part, false);
}
/**
* Return all the connection related to given Component filter by Connection kind According to given
* includePortOfType, returns also CE from ports of the type
*/
public static Collection<ComponentExchange> getAllRelatedComponentExchange(Part part, boolean includePortOfType) {
List<ComponentExchange> result = new ArrayList<>();
if (part.getAbstractType() instanceof Component) {
EList<AbstractInformationFlow> informationFlows = part.getInformationFlows();
for (AbstractInformationFlow abstractInformationFlow : informationFlows) {
if (abstractInformationFlow instanceof ComponentExchange) {
result.add((ComponentExchange) abstractInformationFlow);
}
}
for (ComponentExchangeEnd end : getCache(FunctionalExt::getRelatedComponentExchangeEnds, part)) {
EObject owner = end.eContainer();
if (owner instanceof ComponentExchange) {
result.add((ComponentExchange) owner);
}
}
if (includePortOfType) {
List<ComponentPort> ownedComponentPort = getOwnedComponentPort((Component) part.getAbstractType());
// retrieve connections from owned component Ports
for (ComponentPort componentPort : ownedComponentPort) {
result.addAll(componentPort.getComponentExchanges());
}
}
}
return result;
}
/**
* Return all the connection related to given Component filter by Connection kind
*
* @param component
* @return
*/
public static Collection<ComponentExchange> getAllRelatedComponentExchange(Component component) {
return getAllRelatedComponentExchange(component, true);
}
/**
* Return all the connection related to given Component filter by Connection kind
*
* @param component
* @return
*/
public static Collection<ComponentExchange> getAllRelatedComponentExchange(Component component,
boolean addRelatedOfParts) {
List<ComponentExchange> result = new ArrayList<ComponentExchange>();
List<ComponentPort> ownedComponentPort = getOwnedComponentPort(component);
// retrieve connections from owned component Ports
for (ComponentPort componentPort : ownedComponentPort) {
result.addAll(componentPort.getComponentExchanges());
}
// returns also communication means
if (component instanceof Entity) {
for (AbstractInformationFlow flow : ((Entity) component).getInformationFlows()) {
if (flow instanceof CommunicationMean) {
result.add((CommunicationMean) flow);
}
}
}
if (addRelatedOfParts) {
// retrieve connection from typed elements (today only Part are valid)
EList<AbstractTypedElement> abstractTypedElements = component.getAbstractTypedElements();
for (AbstractTypedElement abstractTypedElement : abstractTypedElements) {
if (abstractTypedElement instanceof Part) {
Part part = (Part) abstractTypedElement;
result.addAll(getAllRelatedComponentExchange(part, false));
}
}
}
return result;
}
/**
* Return all the connection related to given Component filter by Connection kind
*
* @param component
* @return
*/
public static Collection<ComponentExchange> getAllRelatedConnectionByKind(Component component,
ComponentExchangeKind kind) {
List<ComponentExchange> result = new ArrayList<>();
if (null == component) {
return null;
}
// filter connection by kind
Collection<ComponentExchange> allRelatedConnection = getCache(ComponentExt::getAllRelatedComponentExchange,
component);
result.addAll(allRelatedConnection);
if (null != kind) {
for (ComponentExchange connection : allRelatedConnection) {
if (connection.getKind() != kind) {
result.remove(connection);
}
}
}
return result;
}
/**
* This method retrieves all the required interfaces recursively
*
* @param component
* @return
*/
public static List<Interface> getAllRequiredInterfaces(Component cpnt) {
List<Interface> providedItf = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (Interface abstractItf : ((Component) element).getRequiredInterfaces()) {
if (!providedItf.contains(abstractItf)) {
providedItf.add(abstractItf);
}
}
}
}
}
return providedItf;
}
/**
* get all the States and Modes from current Component
*
* @param ele
* @param comp
* @return
*/
public static List<CapellaElement> getAllStatesAndModesFromComponent(Component comp) {
List<CapellaElement> result = new ArrayList<CapellaElement>(1);
// collect all the modes form Component -> StateMachin
if (comp != null) {
EList<StateMachine> ownedStateMachines = comp.getOwnedStateMachines();
for (StateMachine stateMachine : ownedStateMachines) {
TreeIterator<Object> allContents = EcoreUtil.getAllContents(stateMachine, false);
while (allContents.hasNext()) {
Object object = allContents.next();
if (object instanceof State) {
result.add((CapellaElement) object);
}
}
}
}
return result;
}
public static Collection<Part> getAllSubUsedParts(Component component, boolean useDeploymentLinks) {
Collection<Part> result = new HashSet<Part>();
for (Part part : component.getRepresentingParts()) {
result.add(part);
}
return getAllSubUsedParts(result, useDeploymentLinks);
}
public static Collection<Part> getAllSubUsedParts(Part part, boolean useDeploymentLinks) {
return getAllSubUsedParts(Collections.singletonList(part), useDeploymentLinks);
}
private static Collection<Part> getAllSubUsedParts(Collection<Part> parts, boolean useDeploymentLinks) {
Collection<Part> comps = new HashSet<Part>();
Collection<Part> visited = new ArrayList<Part>();
LinkedList<Part> subs = new LinkedList<Part>();
List<Part> initial = new LinkedList<Part>();
initial.addAll(parts);
subs.addAll(parts);
while (subs.size() > 0) {
Part sub = subs.removeFirst();
if (!visited.contains(sub)) {
visited.add(sub);
if ((sub.getAbstractType() != null) && (sub.getAbstractType() instanceof Component)) {
List<Part> internal = ComponentExt.getSubParts((Component) sub.getAbstractType(), false);
comps.addAll(internal);
subs.addAll(internal);
}
if (useDeploymentLinks) {
List<Part> internal = PartExt.getDeployedParts(sub);
comps.addAll(internal);
subs.addAll(internal);
}
}
}
return comps;
}
/**
* Returns sub components of the component. (include also the given component)
*/
public static Collection<Component> getAllSubUsedComponents(Component component) {
// Collection<Component> rsult = PartExt.getComponentsOfParts(getAllSubUsedParts(component, false));
Collection<Component> comps = new HashSet<Component>();
Collection<Component> visited = new ArrayList<Component>();
LinkedList<Component> subs = new LinkedList<Component>();
subs.add(component);
while (subs.size() > 0) {
Component sub = subs.removeFirst();
if (!visited.contains(sub)) {
visited.add(sub);
List<Component> internal = ComponentExt.getSubUsedComponents(sub);
comps.addAll(internal);
subs.addAll(internal);
}
}
return comps;
}
/**
* This method retrieves all the used interfaces.
*
* @param cpnt
* the component whose used AND required (by StandardPorts) interfaces will be retrieved (and all its
* inherited components)
* @return List<Interface> the used and required interfaces
*/
public static List<Interface> getAllUsedAndRequiredInterfaces(Component cpnt) {
List<Interface> usedAndRequiredItf = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (InterfaceUse use : ((Component) element).getUsedInterfaceLinks()) {
Interface itf = use.getUsedInterface();
if ((itf != null) && !usedAndRequiredItf.contains(itf)) {
usedAndRequiredItf.add(itf);
for (GeneralizableElement elt : GeneralizableElementExt.getAllSuperGeneralizableElements(itf)) {
if (!usedAndRequiredItf.contains(elt)) {
usedAndRequiredItf.add((Interface) elt);
}
}
}
}
for (Interface abstractItf : ((Component) element).getRequiredInterfaces()) {
if (!usedAndRequiredItf.contains(abstractItf)) {
usedAndRequiredItf.add(abstractItf);
for (GeneralizableElement elt : GeneralizableElementExt.getAllSuperGeneralizableElements(abstractItf)) {
if (!usedAndRequiredItf.contains(elt)) {
usedAndRequiredItf.add((Interface) elt);
}
}
}
}
}
}
}
return usedAndRequiredItf;
}
/**
* This method retrieves all the used interfaces.
*
* @param cpnt
* the component whose used interfaces will be retrieved (and all its inherited components)
* @return List<Interface> the used interfaces
*/
public static List<Interface> getAllUsedInterfaces(Component cpnt) {
List<Interface> usedInterfaces = new ArrayList<Interface>();
if (cpnt != null) {
List<GeneralizableElement> superComponents = GeneralizableElementExt.getAllSuperGeneralizableElements(cpnt);
superComponents.add(cpnt);
for (GeneralizableElement element : superComponents) {
if (element instanceof Component) {
for (InterfaceUse use : ((Component) element).getUsedInterfaceLinks()) {
Interface itf = use.getUsedInterface();
if ((itf != null) && !usedInterfaces.contains(itf)) {
usedInterfaces.add(itf);
}
}
}
}
}
return usedInterfaces;
}
/**
* Returns all components which are available by namespace.
*/
static public Collection<Component> getAvailableComponentsByNamespace(EObject component) {
Collection<Component> components = new java.util.HashSet<Component>();
EObject rootComponent = component;
// Add components accessible by namespace
while (rootComponent != null) {
if (rootComponent instanceof BlockArchitecture) {
components.addAll(getSubDefinedComponents((BlockArchitecture) rootComponent));
} else if (rootComponent instanceof Component) {
components.addAll(getSubDefinedComponents((Component) rootComponent));
}
rootComponent = rootComponent.eContainer();
}
return components;
}
/**
* Returns parents and brothers of parents components of parts of the given object
*/
public static Collection<EObject> getAvailableComponentsByNamespaceOfParts(Deque<EObject> ownerParts) {
Collection<EObject> components = new HashSet<>();
// Access from all hierarchy of components and blockarchitectures, all sub components and actors
while (!ownerParts.isEmpty()) {
EObject owner = ownerParts.removeFirst();
EObject parent = owner;
if ((parent != null) && !components.contains(parent)) {
if (parent instanceof BlockArchitecture) {
BlockArchitecture parentArchitecture = (BlockArchitecture) parent;
components.addAll(getSubDefinedComponents(parentArchitecture));
components.addAll(getSubDefinedActors(parentArchitecture));
} else if (parent instanceof Component) {
Component parentComponent = (Component) parent;
components.add(parentComponent);
components.addAll(getSubDefinedComponents(parentComponent));
}
ownerParts.addLast(parent.eContainer());
}
}
return components;
}
/**
* Returns parents and brothers of parents components of parts of the given object
*/
static public Collection<EObject> getAvailableComponentsByNamespaceOfParts(Part part) {
LinkedList<EObject> ownerParts = new LinkedList<EObject>();
ownerParts.add(part);
return getAvailableComponentsByNamespaceOfParts(ownerParts);
}
/**
* This method is deprecated: cycles can occur in multi part mode.
*
* @param c1
* @param c2
* @return
*/
@Deprecated
public static Component getCommonComponentAncestor(Component c1, Component c2) {
Component ancestor = c1;
if (isComponentAncestor(c2, ancestor)) {
return c2;
}
while (!isComponentAncestor(ancestor, c2)) {
if (ancestor.getAbstractTypedElements().get(0) instanceof Part) {
Part ancestorPart = (Part) ancestor.getAbstractTypedElements().get(0);
EObject parentAncestor = getDirectParent(ancestorPart);
if (parentAncestor instanceof Component) {
ancestor = (Component) parentAncestor;
} else {
break;
}
} else {
break;
}
}
return ancestor;
}
/**
* Gets all the components contained in a component
*
* @param component
* the parent component
* @return list of components
*/
static public List<Component> getComponentsFromComponent(Component component) {
List<Component> list = new ArrayList<Component>();
for (Object obj : component.eContents()) {
if (obj instanceof Component) {
list.add((Component) obj);
} else if (obj instanceof Structure) {
for (Object content : ((Structure) obj).eContents()) {
if (content instanceof Component) {
list.add((Component) content);
list.addAll(getComponentsFromComponent((Component) content));
}
}
}
}
return list;
}
/**
* @return the list of Component direct common parent for componentA and componentB.
* @return null if not have a direct common parent.
*/
public static List<Component> getDirectCommonParent(Component componentA, Component componentB) {
List<Component> lstDirCommonParents = new ArrayList<Component>();
List<Component> parentsComponents_A = ComponentExt.getDirectParents(componentA);
for (Component parentB : ComponentExt.getDirectParents(componentB)) {
if (parentsComponents_A.contains(parentB)) {
lstDirCommonParents.add(parentB);
}
}
return lstDirCommonParents;
}
/**
* @return component owning the the part given in parameter
*/
public static Component getDirectParent(Part part) {
return (Component) EcoreUtil2.getFirstContainer(part, CsPackage.Literals.COMPONENT);
}
/**
* @return component owning the component parts given in parameter
*/
public static List<Component> getDirectParents(Component component) {
List<Component> parentsComponents = new ArrayList<Component>();
for (Part part : component.getRepresentingParts()) {
Component partitionanableElt = (Component) EcoreUtil2.getFirstContainer(part, CsPackage.Literals.COMPONENT);
parentsComponents.add(partitionanableElt);
}
return parentsComponents;
}
/**
* Retrieve the first common component ancestor for two elements with eClass = Port part or component return the
* common Component or ComponentPkg ancestor between source and target if source is target, return the c1 unlike
* getCommonComponentAncestor
*/
public static EObject getFirstCommonComponentOrPkgAncestor(EObject source, EObject target,
boolean useDeployementLinks) {
if ((source == null) || (target == null)) {
return null;
}