-
Notifications
You must be signed in to change notification settings - Fork 85
/
PortExt.java
948 lines (848 loc) · 32.8 KB
/
PortExt.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
/*******************************************************************************
* 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.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.polarsys.capella.common.data.activity.ActivityNode;
import org.polarsys.capella.common.data.modellingcore.AbstractInformationFlow;
import org.polarsys.capella.common.data.modellingcore.AbstractTrace;
import org.polarsys.capella.common.data.modellingcore.TraceableElement;
import org.polarsys.capella.common.helpers.EcoreUtil2;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.Interface;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.cs.PhysicalLink;
import org.polarsys.capella.core.data.cs.PhysicalPort;
import org.polarsys.capella.core.data.ctx.SystemComponent;
import org.polarsys.capella.core.data.fa.AbstractFunction;
import org.polarsys.capella.core.data.fa.AbstractFunctionalBlock;
import org.polarsys.capella.core.data.fa.ComponentExchange;
import org.polarsys.capella.core.data.fa.ComponentExchangeKind;
import org.polarsys.capella.core.data.fa.ComponentPort;
import org.polarsys.capella.core.data.fa.ComponentPortAllocation;
import org.polarsys.capella.core.data.fa.ComponentPortKind;
import org.polarsys.capella.core.data.fa.ExchangeSpecification;
import org.polarsys.capella.core.data.fa.FaFactory;
import org.polarsys.capella.core.data.fa.FunctionPort;
import org.polarsys.capella.core.data.fa.OrientationPortKind;
import org.polarsys.capella.core.data.helpers.fa.services.FunctionExt;
import org.polarsys.capella.core.data.information.InformationFactory;
import org.polarsys.capella.core.data.information.Port;
import org.polarsys.capella.core.data.information.PortAllocation;
import org.polarsys.capella.core.data.pa.PhysicalComponent;
/**
* The port extension.
*/
public class PortExt {
/**
* Attach with PortAllocation the given port to the given activityNode if wasn't yet linked by a portAllocation
* @param port
* @param activityNode
*/
public static void attachPort(Port port, ActivityNode activityNode) {
if (((activityNode instanceof AbstractFunction) || (activityNode instanceof FunctionPort)) && !isRelatedTo(port, activityNode)) {
PortAllocation portrealS = InformationFactory.eINSTANCE.createPortAllocation();
portrealS.setSourceElement(port);
portrealS.setTargetElement((TraceableElement) activityNode);
port.getOwnedPortAllocations().add(portrealS);
CapellaElementExt.creationService(portrealS);
}
}
/**
* Checks if two standard ports could be connected together.
* @param sourcePort The source standard port.
* @param targetPort The target standard port.
* @return <code>True</code> if the two standard ports could be connected together else <code>false</code>.
*/
public static boolean canCommunicate(ComponentPort sourcePort, ComponentPort targetPort) {
// Check if any provided interface of the source port matches with any
// required interface of the target port.
EList<Interface> providedInterfaces = sourcePort.getProvidedInterfaces();
boolean matched = false;
Iterator<?> iterator = providedInterfaces.iterator();
while (iterator.hasNext() && !matched) {
Interface providedInterface = (Interface) iterator.next();
matched = targetPort.getRequiredInterfaces().contains(providedInterface);
}
// Check if any required interface of the source port matches with any
// provided interface of the target port.
if (!matched) {
EList<Interface> requiredInterfaces = sourcePort.getRequiredInterfaces();
iterator = requiredInterfaces.iterator();
while (iterator.hasNext() && !matched) {
Interface requiredInterface = (Interface) iterator.next();
matched = targetPort.getProvidedInterfaces().contains(requiredInterface);
}
}
return matched;
}
/**
* Create an in flow port
* @return
*/
public static ComponentPort createInFlowPort() {
ComponentPort inP = FaFactory.eINSTANCE.createComponentPort();
inP.setKind(ComponentPortKind.FLOW);
inP.setOrientation(OrientationPortKind.IN);
return inP;
}
/**
* Create an in flow port
* @param name
* @return
*/
public static ComponentPort createInFlowPort(String name) {
ComponentPort inP = FaFactory.eINSTANCE.createComponentPort(name);
inP.setKind(ComponentPortKind.FLOW);
inP.setOrientation(OrientationPortKind.IN);
return inP;
}
/**
* Create an out flow port
* @return
*/
public static ComponentPort createOutFlowPort() {
ComponentPort inP = FaFactory.eINSTANCE.createComponentPort();
inP.setKind(ComponentPortKind.FLOW);
inP.setOrientation(OrientationPortKind.OUT);
return inP;
}
/**
* Create an out flow port
* @param name
* @return
*/
public static ComponentPort createOutFlowPort(String name) {
ComponentPort inP = FaFactory.eINSTANCE.createComponentPort(name);
inP.setKind(ComponentPortKind.FLOW);
inP.setOrientation(OrientationPortKind.OUT);
return inP;
}
/**
* @return
*/
public static ComponentPort createStandardPort() {
ComponentPort inP = FaFactory.eINSTANCE.createComponentPort();
inP.setKind(ComponentPortKind.STANDARD);
return inP;
}
public static final List<ComponentExchange> getAssemblyComponentExchanges(ComponentPort port) {
List<ComponentExchange> componentExchanges = new ArrayList<ComponentExchange>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.ASSEMBLY) {
componentExchanges.add(flow);
}
}
return componentExchanges;
}
/**
* Returns the list of connected ComponentPort for a given ComponentPort. Rules :<br>
* Connected ComponentPort are : </BR> Port connected to the given port directly Return :<br>
* The list does not contain the supplied flowPort, is not null but may be empty.
* @param FlowPort flowPort
* @return List<Function> the list of siblings (may be empty).
*/
public static final Collection<ComponentPort> getConnectedComponentPortsWithoutDelegation(ComponentPort port) {
Collection<ComponentPort> connected = new HashSet<ComponentPort>(1);
for (ComponentExchange ex : port.getComponentExchanges()) {
if ((ex.getKind() != ComponentExchangeKind.DELEGATION)) {
Port sourcePort = ComponentExchangeExt.getSourcePort(ex);
Port targetPort = ComponentExchangeExt.getTargetPort(ex);
if ((sourcePort != null) && (sourcePort instanceof ComponentPort)) {
connected.add((ComponentPort) sourcePort);
}
if ((targetPort != null) && (targetPort instanceof ComponentPort)) {
connected.add((ComponentPort) targetPort);
}
}
}
connected.remove(port);
return connected;
}
/**
* Returns the list of connected ComponentPort for a given ComponentPort. Rules :<br>
* Connected ComponentPort are : </BR> Port connected to the given port directly Return :<br>
* The list does not contain the supplied flowPort, is not null but may be empty.
* @param FlowPort flowPort
* @return List<Function> the list of siblings (may be empty).
*/
public static final Collection<ComponentPort> getConnectedComponentPorts(ComponentPort port) {
Collection<ComponentPort> connected = new HashSet<ComponentPort>(1);
for (ComponentExchange ex : port.getComponentExchanges()) {
Port sourcePort = ComponentExchangeExt.getSourcePort(ex);
Port targetPort = ComponentExchangeExt.getTargetPort(ex);
if ((sourcePort != null) && (sourcePort instanceof ComponentPort)) {
connected.add((ComponentPort) sourcePort);
}
if ((targetPort != null) && (targetPort instanceof ComponentPort)) {
connected.add((ComponentPort) targetPort);
}
}
connected.remove(port);
return connected;
}
public static final List<ComponentPortAllocation> getIncomingComponentPortAllocations(ComponentPort port) {
List<ComponentPortAllocation> componentExchanges = new ArrayList<ComponentPortAllocation>(1);
for (AbstractTrace flow : port.getIncomingTraces()) {
if (flow instanceof ComponentPortAllocation) {
componentExchanges.add((ComponentPortAllocation) flow);
}
}
return componentExchanges;
}
public static final List<ComponentExchange> getDelegationComponentExchanges(ComponentPort port) {
List<ComponentExchange> componentExchanges = new ArrayList<ComponentExchange>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
componentExchanges.add(flow);
}
}
return componentExchanges;
}
public static final List<ComponentExchange> getFlowComponentExchanges(ComponentPort port) {
List<ComponentExchange> componentExchanges = new ArrayList<ComponentExchange>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.FLOW) {
componentExchanges.add(flow);
}
}
return componentExchanges;
}
public static final Collection<ComponentPort> getAllLinkedDelegatedComponentPorts(ComponentPort componentPort1) {
LinkedList<ComponentPort> ports = new LinkedList<ComponentPort>();
Collection<ComponentPort> visited = new ArrayList<ComponentPort>();
ports.add(componentPort1);
while (ports.size() > 0) {
ComponentPort port = ports.removeFirst();
if (!visited.contains(port)) {
visited.add(port);
ports.addAll(getDelegatingComponentPorts(port));
}
}
visited.remove(componentPort1);
ports.add(componentPort1);
while (ports.size() > 0) {
ComponentPort port = ports.removeFirst();
if (!visited.contains(port)) {
visited.add(port);
ports.addAll(getDelegatedComponentPorts(port));
}
}
return visited;
}
public static final Collection<ComponentPort> getAllDelegatedComponentPorts(ComponentPort componentPort1) {
LinkedList<ComponentPort> ports = new LinkedList<ComponentPort>();
Collection<ComponentPort> visited = new HashSet<ComponentPort>();
ports.add(componentPort1);
while (ports.size() > 0) {
ComponentPort port = ports.removeFirst();
if (!visited.contains(port)) {
visited.add(port);
ports.addAll(getDelegatedComponentPorts(port));
}
}
visited.remove(componentPort1);
return visited;
}
public static final Collection<ComponentPort> getAllDelegatingComponentPorts(ComponentPort componentPort1) {
LinkedList<ComponentPort> ports = new LinkedList<ComponentPort>();
Collection<ComponentPort> visited = new HashSet<ComponentPort>();
ports.add(componentPort1);
while (ports.size() > 0) {
ComponentPort port = ports.removeFirst();
if (!visited.contains(port)) {
visited.add(port);
ports.addAll(getDelegatingComponentPorts(port));
}
}
visited.remove(componentPort1);
return visited;
}
public static final Collection<ComponentPort> getDelegatingComponentPorts(ComponentPort port) {
List<ComponentPort> ports = new ArrayList<ComponentPort>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
if (port.equals(ComponentExchangeExt.getTargetPort(flow))) {
Port opposite = ComponentExchangeExt.getOppositePort(flow, port);
if (opposite instanceof ComponentPort) {
ports.add((ComponentPort) opposite);
}
}
}
}
return ports;
}
public static final Collection<ComponentExchange> getDelegatingComponentExchanges(ComponentPort port) {
List<ComponentExchange> exchanges = new ArrayList<ComponentExchange>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
if (port.equals(ComponentExchangeExt.getTargetPort(flow))) {
exchanges.add(flow);
}
}
}
return exchanges;
}
public static final Collection<ComponentPort> getDelegatedComponentPorts(ComponentPort port) {
List<ComponentPort> ports = new ArrayList<ComponentPort>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
if (port.equals(ComponentExchangeExt.getSourcePort(flow))) {
Port opposite = ComponentExchangeExt.getOppositePort(flow, port);
if (opposite instanceof ComponentPort) {
ports.add((ComponentPort) opposite);
}
}
}
}
return ports;
}
public static final Collection<ComponentExchange> getDelegatedComponentExchanges(ComponentPort port) {
List<ComponentExchange> exchanges = new ArrayList<ComponentExchange>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
if (port.equals(ComponentExchangeExt.getSourcePort(flow))) {
exchanges.add(flow);
}
}
}
return exchanges;
}
public static final Collection<ComponentPort> getLinkedDelegatedComponentPorts(ComponentPort port) {
List<ComponentPort> ports = new ArrayList<ComponentPort>(1);
for (ComponentExchange flow : port.getComponentExchanges()) {
if (flow.getKind() == ComponentExchangeKind.DELEGATION) {
Port opposite = ComponentExchangeExt.getOppositePort(flow, port);
if (opposite instanceof ComponentPort) {
ports.add((ComponentPort) opposite);
}
}
}
return ports;
}
/**
* This method retrieves the provided interfaces.
* @param port the port whose provided interfaces will be retrieved
* @return List<Interface> the provided interfaces
*/
public static List<Interface> getProvidedInterfaces(ComponentPort port) {
List<Interface> providedInterfaces = new ArrayList<Interface>();
if (port != null) {
SystemComponent portType = (SystemComponent) port.getType();
if (portType != null) {
if (portType instanceof PhysicalComponent) {
PhysicalComponent pc = (PhysicalComponent) portType;
providedInterfaces.addAll(PhysicalComponentExt.getProvidedInterfaces(pc));
} else {
List<Interface> providedSet = portType.getImplementedInterfaces();
for (Interface itf : providedSet) {
providedInterfaces.add(itf);
}
}
}
}
return providedInterfaces;
}
/**
* Retrieve the owning component of the given port
* @param aFeature_p
* @return
*/
public static Component getRelatedComponent(ComponentPort port) {
if (port.eContainer() instanceof Component) {
return (Component) port.eContainer();
}
return null;
}
/**
* Retrieve the owning component of the given port
* @param aFeature_p
* @return
*/
public static Component getRelatedComponent(PhysicalPort port) {
if (port.eContainer() instanceof Component) {
return (Component) port.eContainer();
}
return null;
}
/**
* Returns the list of current non delegation exchanges for a given flowPort.</BR> Rules :<br>
* <UL>
* <LI>must be instances of Exchange.</LI>
* <LI>flowPort must be at one end of those PortCommunications.</LI>
* </UL>
* Return :<br>
* The list is not null but may be empty.
*/
public static final List<ExchangeSpecification> getRelatedExchangeSpecifications(ComponentPort port) {
List<ExchangeSpecification> exchanges = new ArrayList<ExchangeSpecification>(1);
for (AbstractInformationFlow flow : port.getInformationFlows()) {
if (flow instanceof ExchangeSpecification) {
ExchangeSpecification exchange = (ExchangeSpecification) flow;
exchanges.add(exchange);
}
}
return exchanges;
}
/**
* This method retrieves the required interfaces.
* @param port the port whose required interfaces will be retrieved
* @return List<Interface> the required interfaces
*/
public static List<Interface> getRequiredInterfaces(ComponentPort port) {
List<Interface> requiredInterfaces = new ArrayList<Interface>();
if (port != null) {
SystemComponent portType = (SystemComponent) port.getType();
if (portType != null) {
if (portType instanceof PhysicalComponent) {
PhysicalComponent pc = (PhysicalComponent) portType;
requiredInterfaces.addAll(PhysicalComponentExt.getRequiredInterfaces(pc));
} else {
List<Interface> requiredSet = portType.getUsedInterfaces();
for (Interface itf : requiredSet) {
requiredInterfaces.add(itf);
}
}
}
}
return requiredInterfaces;
}
/**
* Returns whether the given ComponentPorts have complementary orientations.</BR> Rules :</BR>
* <UL>
* <LI>an IN has complementary orientation with INOUT Or OUT.</LI>
* <LI>an OUT has complementary orientation with INOUT Or IN.</LI>
* </UL>
* Return :</BR> Defaults to true.
* @return boolean as specified above.
*/
public static final boolean haveComplementaryOrientation(ComponentPort first, ComponentPort second) {
if ((first == null) || (second == null)) {
// trivial protection
return false;
}
if (first.getOrientation() == OrientationPortKind.IN) {
return second.getOrientation() != OrientationPortKind.IN;
}
if (first.getOrientation() == OrientationPortKind.OUT) {
return second.getOrientation() != OrientationPortKind.OUT;
}
// default case
return true;
}
/**
* Returns whether the given ComponentPorts have same orientation.</BR> Rules :</BR>
* <UL>
* <LI>an IN has same orientation with IN Or INOUT.</LI>
* <LI>an OUT has same orientation with OUT Or INOUT.</LI>
* </UL>
* Return :</BR> Defaults to true.
* @return boolean as specified above.
*/
public static final boolean haveSameOrientation(ComponentPort first, ComponentPort second) {
if ((first == null) || (second == null)) {
// trivial protection
return false;
}
if (first.getOrientation() == OrientationPortKind.IN) {
return second.getOrientation() != OrientationPortKind.OUT;
}
if (first.getOrientation() == OrientationPortKind.OUT) {
return second.getOrientation() != OrientationPortKind.IN;
}
// default case
return true;
}
/**
* Check if the Component related to a ComponentPort (or one of his subcomponents) is allocating the Function related to a FunctionPort.<br>
* This method can be used to know if a PortAllocation can be created between the given ports.
* @param componentPort
* @param functionPort
* @return <ul>
* <li><code>Boolean.TRUE</code> if the related Component (or one if its subcomponents) is allocating the related Function,</li>
* <li><code>Boolean.FALSE</code> if not,</li>
* <li><code>null</code> if given ComponentPort isn't contained by a Component or if given function port is not or is not contained by an
* AbstractFunction.</li>
* </ul>
*/
public static Boolean isRelatedComponentAllocatingRelatedFunction(ComponentPort componentPort, ActivityNode functionPort) {
// Get the Component containing the given port.
Component component = PortExt.getRelatedComponent(componentPort);
if (null == component) {
return null;
}
// Get the AbstractFunction containing the given port.
AbstractFunction abstractFunction = FunctionExt.getRelatedFunction(functionPort);
if (null == abstractFunction) {
return null;
}
// Get the component and all its nested components.
List<Component> componentsHierarchy = ComponentExt.getAllSubUsedAndDeployedComponents(component);
// Get all components allocating the function.
List<AbstractFunctionalBlock> allocatingComponents = abstractFunction.getAllocationBlocks();
// Is the Function allocated by a Component which is the same as/nested in the parent Component ?
if (Collections.disjoint(componentsHierarchy, allocatingComponents)) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
/**
* @param aFeature
* @return
*/
public static boolean isFlowPort(EObject aFeature) {
return (aFeature instanceof ComponentPort) && (((ComponentPort) aFeature).getKind() == ComponentPortKind.FLOW);
}
/**
* @param aFeature
* @return
*/
public static boolean isIn(ComponentPort aFeature) {
return (aFeature.getOrientation() == OrientationPortKind.IN) || (aFeature.getOrientation() == OrientationPortKind.INOUT);
}
/**
* Checks if port is an in flow port.
* @param port the given componentPort
* @return true, if is in flow port
*/
public static boolean isInFlowPort(EObject port) {
return isFlowPort(port) && isIn((ComponentPort) port);
}
/**
* Checks if port is an (strictly) in flow port.
* @param port the given componentPort
* @return true, if is in flow port
*/
public static boolean isInStrictFlowPort(EObject port) {
return isFlowPort(port) && isInStrict((ComponentPort) port);
}
/**
* @param cp
* @return
*/
public static boolean isInStrict(ComponentPort cp) {
return OrientationPortKind.IN.equals(cp.getOrientation());
}
/**
* @param cp
* @return
*/
public static boolean isOutStrict(ComponentPort cp) {
return OrientationPortKind.OUT.equals(cp.getOrientation());
}
/**
* @param cp
* @return
*/
public static boolean isInoutStrict(ComponentPort cp) {
return OrientationPortKind.INOUT.equals(cp.getOrientation());
}
public static boolean isNotCompatibleWith(ComponentPort comparedPort, ComponentPort reference) {
if ((comparedPort == null) || (reference == null)) {
return true;
}
SystemComponent comparedType = (SystemComponent) comparedPort.getType();
SystemComponent referenceType = (SystemComponent) reference.getType();
if ((comparedType == null) || (referenceType == null)) {
return true;
}
List<Interface> comparedProvItf = comparedPort.getProvidedInterfaces();
List<Interface> comparedReqItf = comparedPort.getRequiredInterfaces();
List<Interface> referenceProvItf = reference.getProvidedInterfaces();
List<Interface> referenceReqItf = reference.getRequiredInterfaces();
for (Interface refItf : referenceReqItf) {
if (comparedProvItf.contains(refItf)) {
return false;
}
}
for (Interface refItf : referenceProvItf) {
if (comparedReqItf.contains(refItf)) {
return false;
}
}
return true;
}
/**
* @param aFeature
* @return
*/
public static boolean isOut(ComponentPort aFeature) {
return (aFeature.getOrientation() == OrientationPortKind.OUT) || (aFeature.getOrientation() == OrientationPortKind.INOUT);
}
/**
* Checks if port is an out flow port.
* @param port the given componentPort
* @return true, if is out flow port
*/
public static boolean isOutFlowPort(EObject port) {
return isFlowPort(port) && isOut((ComponentPort) port);
}
/**
* Checks if port is an (strictly) out flow port.
* @param port the given componentPort
* @return true, if is out flow port
*/
public static boolean isOutStrictFlowPort(EObject port) {
return isFlowPort(port) && isOutStrict((ComponentPort) port);
}
/**
* Checks if port is an (strictly) inout flow port.
* @param port the given componentPort
* @return true, if is in flow port
*/
public static boolean isInoutStrictFlowPort(EObject port) {
return isFlowPort(port) && isInoutStrict((ComponentPort) port);
}
/**
* Returns whether the component port is related to the function port and has a connection which allocate the functional exchange
*/
public static boolean isRelatedTo(Port cport, ActivityNode fport) {
if ((cport == null) || (fport == null)) {
return false;
}
for (AbstractTrace trace : cport.getIncomingTraces()) {
if ((trace.getSourceElement() != null) && trace.getSourceElement().equals(fport)) {
return true;
}
if ((trace.getTargetElement() != null) && trace.getTargetElement().equals(fport)) {
return true;
}
}
for (AbstractTrace trace : cport.getOutgoingTraces()) {
if ((trace.getSourceElement() != null) && trace.getSourceElement().equals(fport)) {
return true;
}
if ((trace.getTargetElement() != null) && trace.getTargetElement().equals(fport)) {
return true;
}
}
return false;
}
/**
* @param eObject_p
* @return
*/
public static boolean isStandardPort(Object aFeature) {
return (aFeature instanceof ComponentPort) && (((ComponentPort) aFeature).getKind() == ComponentPortKind.STANDARD);
}
/**
* Returns whether the port sourcePrevious is transitioned to sourceCurrent or if sourcePrevious is transitioned to one of parents ports of
* sourceCurrent (by delegation)
*/
public static boolean isTransitionedTo(ComponentPort sourcePrevious, ComponentPort sourceCurrent) {
if (RefinementLinkExt.isLinkedTo(sourceCurrent, sourcePrevious)) {
return true;
}
for (ComponentPort port : retrieveParentDelegating(sourceCurrent)) {
if (RefinementLinkExt.isLinkedTo(port, sourcePrevious)) {
return true;
}
}
return false;
}
/**
* Returns whether the port sourcePrevious is transitioned to sourceCurrent or if sourcePrevious is transitioned to one of parents ports of
* sourceCurrent (by delegation)
*/
public static boolean isTransitionedTo(FunctionPort sourcePrevious, FunctionPort sourceCurrent) {
return RefinementLinkExt.isLinkedTo(sourceCurrent, sourcePrevious);
}
public static Collection<ComponentPort> retrieveParentDelegating(ComponentPort sourceCurrent) {
LinkedList<ComponentPort> ports = new LinkedList<ComponentPort>();
HashSet<ComponentPort> visited = new HashSet<ComponentPort>();
ports.add(sourceCurrent);
while (ports.size() > 0) {
ComponentPort port = ports.removeFirst();
visited.add(port);
for (ComponentPort cp : PortExt.getLinkedDelegatedComponentPorts(sourceCurrent)) {
if (!visited.contains(cp)) {
ports.addLast(cp);
}
}
}
return visited;
}
/**
* Returns whether the activity node is defined in a transitioned function or sub function of the given container (works for port and others activity nodes)
*/
public static boolean transitionedPortIsValid(ActivityNode eObj, TraceableElement container) {
if (null != container) {
for (AbstractTrace traceFunction : container.getIncomingTraces()) {
if (traceFunction.getSourceElement() instanceof AbstractFunction) {
AbstractFunction targetFunction = (AbstractFunction) traceFunction.getSourceElement();
if (EcoreUtil2.isOrIsContainedBy(eObj, targetFunction)) {
return true;
}
}
}
}
return false;
}
/**
* @param eObj
* @return
*/
public static boolean transitionedPortIsValid(ComponentPort eObj) {
for (AbstractTrace trace : eObj.getOutgoingTraces()) {
if (trace.getTargetElement() instanceof ComponentPort) {
ComponentPort exc = (ComponentPort) trace.getTargetElement();
Component container = (Component) exc.eContainer();
if (transitionedPortIsValid(eObj, container)) {
return true;
}
}
}
return false;
}
/**
* Returns whether the port is defined in a transitioned function or sub function of the given container
*/
public static boolean transitionedPortIsValid(ComponentPort eObj, TraceableElement container) {
if (null != container) {
for (AbstractTrace traceFunction : container.getIncomingTraces()) {
if (traceFunction.getSourceElement() instanceof Component) {
Component targetFunction = (Component) traceFunction.getSourceElement();
if ((eObj.eContainer() == targetFunction) || ComponentExt.isComponentAncestor((Component) eObj.eContainer(), targetFunction)) {
return true;
}
}
}
}
return false;
}
/**
* @param eObj
* @return
*/
public static boolean transitionedPortIsValid(FunctionPort eObj) {
for (AbstractTrace trace : eObj.getOutgoingTraces()) {
if (trace.getTargetElement() instanceof FunctionPort) {
FunctionPort exc = (FunctionPort) trace.getTargetElement();
AbstractFunction container = (AbstractFunction) exc.eContainer();
if ((eObj instanceof ActivityNode) && transitionedPortIsValid((ActivityNode) eObj, container)) {
return true;
}
}
}
return false;
}
/**
* Returns whether the link can be considered as a delegation
* @param port
* @return
*/
public static Collection<PhysicalLink> getDelegatedPhysicalLinks(PhysicalPort port) {
Collection<PhysicalLink> links = new HashSet<>();
Component component = PortExt.getRelatedComponent(port);
Collection<Part> parts = getCache(ComponentExt::getRepresentingParts, component);
for (PhysicalLink link : port.getInvolvedLinks()) {
Collection<Part> sourceParts = org.polarsys.capella.core.data.helpers.cs.services.PhysicalLinkExt.getSourceParts(link);
Collection<Part> targetParts = org.polarsys.capella.core.data.helpers.cs.services.PhysicalLinkExt.getTargetParts(link);
boolean isSource = false;
boolean isTarget = false;
for (Part owningPart : parts) {
if (sourceParts.contains(owningPart)) {
isSource = true;
} else if (targetParts.contains(owningPart)) {
isTarget = true;
}
}
if (isSource && !isTarget) {
for (Part part : targetParts) {
Collection<Part> firstPartAncestors = PartExt.getFirstPartAncestors(part);
for (Part part2 : firstPartAncestors) {
if (sourceParts.contains(part2)) {
links.add(link);
break;
}
}
}
} else if (isTarget && !isSource) {
for (Part part : sourceParts) {
Collection<Part> firstPartAncestors = PartExt.getFirstPartAncestors(part);
for (Part part2 : firstPartAncestors) {
if (targetParts.contains(part2)) {
links.add(link);
break;
}
}
}
}
}
return links;
}
/**
* Returns whether the link can be considered as a delegation
* @param port
* @return
*/
public static Collection<PhysicalLink> getDelegatingPhysicalLinks(PhysicalPort port) {
Collection<PhysicalLink> links = new HashSet<>();
Component component = PortExt.getRelatedComponent(port);
Collection<Part> parts = getCache(ComponentExt::getRepresentingParts, component);
for (PhysicalLink link : port.getInvolvedLinks()) {
Collection<Part> sourceParts = org.polarsys.capella.core.data.helpers.cs.services.PhysicalLinkExt.getSourceParts(link);
Collection<Part> targetParts = org.polarsys.capella.core.data.helpers.cs.services.PhysicalLinkExt.getTargetParts(link);
boolean isSource = false;
boolean isTarget = false;
for (Part owningPart : parts) {
if (sourceParts.contains(owningPart)) {
isSource = true;
} else if (targetParts.contains(owningPart)) {
isTarget = true;
}
}
if (isSource && !isTarget) {
for (Part part : sourceParts) {
Collection<Part> firstPartAncestors = PartExt.getFirstPartAncestors(part);
for (Part part2 : firstPartAncestors) {
if (targetParts.contains(part2)) {
links.add(link);
break;
}
}
}
} else if (isTarget && !isSource) {
for (Part part : targetParts) {
Collection<Part> firstPartAncestors = PartExt.getFirstPartAncestors(part);
for (Part part2 : firstPartAncestors) {
if (sourceParts.contains(part2)) {
links.add(link);
break;
}
}
}
}
}
return links;
}
/**
* Returns whether the link can be considered as a delegation
* @param delegationTargetPort_p
* @return
*/
public static Collection<PhysicalLink> getDelegationPhysicalLinks(PhysicalPort port) {
Collection<PhysicalLink> links = new ArrayList<PhysicalLink>();
links.addAll(getDelegatedPhysicalLinks(port));
links.addAll(getDelegatingPhysicalLinks(port));
return links;
}
}