-
Notifications
You must be signed in to change notification settings - Fork 85
/
PhysicalLinkExt.java
289 lines (256 loc) · 9 KB
/
PhysicalLinkExt.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
/*******************************************************************************
* 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.data.helpers.cs.services;
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.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.polarsys.capella.common.data.modellingcore.AbstractInformationFlow;
import org.polarsys.capella.common.helpers.EObjectExt;
import org.polarsys.capella.core.data.cs.AbstractPhysicalLinkEnd;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.CsPackage;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.cs.PhysicalLink;
import org.polarsys.capella.core.data.cs.PhysicalLinkEnd;
import org.polarsys.capella.core.data.cs.PhysicalPort;
import org.polarsys.capella.core.data.information.Port;
/**
*/
public class PhysicalLinkExt {
public static Collection<PhysicalLinkEnd> getRelatedPhysicalLinkEnds(Port element) {
HashSet<PhysicalLinkEnd> result = new HashSet<>();
List<EReference> refs = new ArrayList<>();
refs.add(CsPackage.Literals.PHYSICAL_LINK_END__PORT);
for (Object objectRef : EObjectExt.getReferencers(element, refs)) {
result.add((PhysicalLinkEnd) objectRef);
}
return result;
}
public static Collection<PhysicalLinkEnd> getRelatedPhysicalLinkEnds(Part element) {
HashSet<PhysicalLinkEnd> result = new HashSet<>();
List<EReference> refs = new ArrayList<>();
refs.add(CsPackage.Literals.PHYSICAL_LINK_END__PART);
for (Object objectRef : EObjectExt.getReferencers(element, refs)) {
result.add((PhysicalLinkEnd) objectRef);
}
return result;
}
/**
* Returns all PhysicalLink involving the port
*
* @param element
* @return
*/
public static Collection<PhysicalLink> getAllRelatedPhysicalLinks(PhysicalPort element) {
HashSet<PhysicalLink> result = new HashSet<>();
result.addAll(element.getInvolvedLinks());
for (PhysicalLinkEnd end : getRelatedPhysicalLinkEnds(element)) {
result.addAll(end.getInvolvedLinks());
}
return result;
}
public static Collection<PhysicalLink> getAllRelatedPhysicalLinks(Component element) {
HashSet<PhysicalLink> result = new HashSet<>();
for (PhysicalPort port : element.getContainedPhysicalPorts()) {
result.addAll(getCache(PhysicalLinkExt::getAllRelatedPhysicalLinks, (PhysicalPort) port));
}
return result;
}
/**
* Retrieve the helper part.componentExchanges returns all component exchanges directly connected to part, or by a
* component exchange end.
*
* @param part
* @return
*/
public static final List<PhysicalLink> getPhysicalLinks(Part part) {
List<PhysicalLink> result = new ArrayList<>();
for (AbstractInformationFlow flow : part.getInformationFlows()) {
if (flow instanceof PhysicalLink) {
result.add((PhysicalLink) flow);
}
}
for (PhysicalLinkEnd end : getRelatedPhysicalLinkEnds(part)) {
result.addAll(end.getInvolvedLinks());
}
return result;
}
/**
* Returns all PhysicalLink involving the part. (include physical link linked through physical Ports)
*
* @param element
* @return
*/
public static Collection<PhysicalLink> getAllRelatedPhysicalLinks(Part element) {
HashSet<PhysicalLink> result = new HashSet<>();
if (element.getAbstractType() instanceof Component) {
Component component = ((Component) element.getAbstractType());
result.addAll(getCache(PhysicalLinkExt::getAllRelatedPhysicalLinks, component));
}
for (PhysicalLinkEnd end : getRelatedPhysicalLinkEnds(element)) {
result.addAll(end.getInvolvedLinks());
}
return result;
}
public static EObject getSource(PhysicalLink link) {
if (!link.getLinkEnds().isEmpty()) {
return link.getLinkEnds().get(0);
}
return null;
}
public static EObject getTarget(PhysicalLink link) {
if (link.getLinkEnds().size() > 1) {
return link.getLinkEnds().get(1);
}
return null;
}
public static Port getSourcePort(PhysicalLink link) {
EObject source = getSource(link);
if (source instanceof PhysicalLinkEnd) {
return ((PhysicalLinkEnd) source).getPort();
} else if (source instanceof Port) {
return (Port) source;
}
return null;
}
public static Port getTargetPort(PhysicalLink link) {
EObject target = getTarget(link);
if (target instanceof PhysicalLinkEnd) {
return ((PhysicalLinkEnd) target).getPort();
} else if (target instanceof Port) {
return (Port) target;
}
return null;
}
public static Part getSourcePart(PhysicalLink link) {
EObject source = getSource(link);
if (source instanceof PhysicalLinkEnd) {
return ((PhysicalLinkEnd) source).getPart();
} else if (source instanceof Port) {
EObject eContainer = source.eContainer();
if (eContainer instanceof Component) {
for (Part part : ((Component) eContainer).getRepresentingParts()) {
return part;
}
}
}
return null;
}
/**
* Returns source Parts of the physical link If physical link is related to one part, returns a singleton of related
* part. If physical link is related to a component, returns representing parts of the component
*/
public static Collection<Part> getSourceParts(PhysicalLink link) {
Part part = getSourcePart(link);
if (part != null) {
return Collections.singletonList(part);
}
Component sourceComponent = getSourceComponent(link);
if (sourceComponent != null) {
ArrayList<Part> result = new ArrayList<>();
for (Part aPart : sourceComponent.getRepresentingParts()) {
result.add(aPart);
}
return result;
}
return Collections.emptyList();
}
public static Part getTargetPart(PhysicalLink link) {
EObject target = getTarget(link);
if (target instanceof PhysicalLinkEnd) {
return ((PhysicalLinkEnd) target).getPart();
} else if (target instanceof Port) {
EObject eContainer = target.eContainer();
if (eContainer instanceof Component) {
for (Part part : ((Component) eContainer).getRepresentingParts()) {
return part;
}
}
}
return null;
}
/**
* Returns target Parts of the physical link If physical link is related to one part, returns a singleton of related
* part. If physical link is related to a component, returns representing parts of the component
*/
public static Collection<Part> getTargetParts(PhysicalLink link) {
Part part = getTargetPart(link);
if (part != null) {
return Collections.singletonList(part);
}
Component targetComponent = getTargetComponent(link);
if (targetComponent != null) {
ArrayList<Part> result = new ArrayList<>();
for (Part aPart : targetComponent.getRepresentingParts()) {
result.add(aPart);
}
return result;
}
return Collections.emptyList();
}
public static Component getSourceComponent(PhysicalLink link) {
Port sourcePort = getSourcePort(link);
if (null != sourcePort) {
EObject eContainer = sourcePort.eContainer();
if (eContainer instanceof Component) {
return (Component) eContainer;
}
}
return null;
}
public static Component getTargetComponent(PhysicalLink link) {
Port sourcePort = getTargetPort(link);
if (null != sourcePort) {
EObject eContainer = sourcePort.eContainer();
if (eContainer instanceof Component) {
return (Component) eContainer;
}
}
return null;
}
/**
* @param link1
* @param link2
* @return the common Part of link1 and link2
*/
public static Part getCommonPart(PhysicalLink link1, PhysicalLink link2) {
Part sourceLink1Part = getSourcePart(link1);
Part targetLink1Part = getTargetPart(link1);
Part sourceLink2Part = getSourcePart(link2);
Part targetLink2Part = getTargetPart(link2);
if (sourceLink1Part.equals(sourceLink2Part) || sourceLink1Part.equals(targetLink2Part)) {
return sourceLink1Part;
}
if (targetLink1Part.equals(sourceLink2Part) || targetLink1Part.equals(targetLink2Part)) {
return targetLink1Part;
}
return null;
}
/**
* @param end
* @return
*/
public static PhysicalPort getRelatedPort(AbstractPhysicalLinkEnd end) {
if (end instanceof PhysicalPort) {
return (PhysicalPort) end;
} else if (end instanceof PhysicalLinkEnd) {
return ((PhysicalLinkEnd) end).getPort();
}
return null;
}
}