-
Notifications
You must be signed in to change notification settings - Fork 85
/
ReconciliationTraceabilityHandler.java
310 lines (278 loc) · 14 KB
/
ReconciliationTraceabilityHandler.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
/*******************************************************************************
* 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.transition.system.handlers.traceability;
import java.util.Optional;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.polarsys.capella.common.data.modellingcore.AbstractNamedElement;
import org.polarsys.capella.common.data.modellingcore.AbstractTrace;
import org.polarsys.capella.common.data.modellingcore.TraceableElement;
import org.polarsys.capella.core.data.capellacore.Allocation;
import org.polarsys.capella.core.data.capellacore.ModellingArchitecture;
import org.polarsys.capella.core.data.capellamodeller.SystemEngineering;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.ComponentPkg;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.fa.FunctionPkg;
import org.polarsys.capella.core.data.information.DataPkg;
import org.polarsys.capella.core.model.helpers.BlockArchitectureExt;
import org.polarsys.capella.core.model.helpers.naming.NamingConstants;
import org.polarsys.capella.core.transition.common.handlers.traceability.ITraceabilityHandler;
import org.polarsys.capella.core.transition.common.handlers.traceability.LevelBasedTraceabilityHandler;
import org.polarsys.capella.core.transition.common.handlers.traceability.TraceabilityHandlerHelper;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;
/**
*
*/
public class ReconciliationTraceabilityHandler extends LevelBasedTraceabilityHandler {
public ReconciliationTraceabilityHandler(String identifier) {
super(identifier);
}
@Override
protected boolean isLevelElement(EObject object, IContext context) {
return true;
}
@Override
protected void initializeMapping(EObject source, EObject target, IContext context, LevelMappingTraceability map) {
if ((source instanceof SystemEngineering) && (target instanceof SystemEngineering)) {
initializeSystemEngineering((SystemEngineering) source, (SystemEngineering) target, context, map);
}
if ((source instanceof BlockArchitecture) && (target instanceof BlockArchitecture)) {
initializeBlockArchitecture((BlockArchitecture) source, (BlockArchitecture) target, context, map);
}
if ((source instanceof Component) && (target instanceof Component)) {
initializeComponent((Component) source, (Component) target, context, map);
}
if ((source instanceof ComponentPkg) && (target instanceof ComponentPkg)) {
initializeComponentPkg((ComponentPkg) source, (ComponentPkg) target, context, map);
}
if ((source instanceof FunctionPkg) && (target instanceof FunctionPkg)) {
initializeFunctionPkg((FunctionPkg) source, (FunctionPkg) target, context, map);
}
if ((source instanceof DataPkg) && (target instanceof DataPkg)) {
initializeDataPkg((DataPkg) source, (DataPkg) target, context, map);
}
if ((source instanceof TraceableElement) && (target instanceof TraceableElement)) {
initializeTraceableElement((TraceableElement) source, (TraceableElement) target, context, map);
}
}
/**
* Map the Data package and its Predefined Types package if possible
* @param source
* @param target
* @param context
* @param map
*/
protected void initializeDataPkg(DataPkg source, DataPkg target, IContext context, LevelMappingTraceability map) {
BlockArchitecture sourceBlockArchitecture = BlockArchitectureExt.getRootBlockArchitecture(source);
BlockArchitecture targetBlockArchitecture = BlockArchitectureExt.getRootBlockArchitecture(target);
if (sourceBlockArchitecture != null && targetBlockArchitecture != null) {
DataPkg sourceDataPkg = BlockArchitectureExt.getDataPkg(sourceBlockArchitecture);
DataPkg targetDataPkg = BlockArchitectureExt.getDataPkg(targetBlockArchitecture);
if (source == sourceDataPkg && target == targetDataPkg) {
addMapping(map, sourceDataPkg, targetDataPkg, context);
Optional<DataPkg> sourcePredefinedTypes = sourceDataPkg.getOwnedDataPkgs().stream()
.filter(pkg -> NamingConstants.PredefinedTypesCmd_predefinedDataTypePkg_name.equals(pkg.getName()))
.findFirst();
Optional<DataPkg> targetPredefinedTypes = targetDataPkg.getOwnedDataPkgs().stream()
.filter(pkg -> NamingConstants.PredefinedTypesCmd_predefinedDataTypePkg_name.equals(pkg.getName()))
.findFirst();
if (sourcePredefinedTypes.isPresent() && targetPredefinedTypes.isPresent()) {
initializeDataType(sourcePredefinedTypes.get(), targetPredefinedTypes.get(), context, map);
}
}
}
}
protected void initializeDataType(EObject source, EObject target, IContext context, LevelMappingTraceability map) {
if (source.eClass().equals(target.eClass())) {
addMapping(map, source, target, context);
for (EReference reference : source.eClass().getEAllReferences()) {
if (reference.isContainment()) {
if (!reference.isMany()) {
Object sourceValue = source.eGet(reference);
Object targetValue = target.eGet(reference);
if ((sourceValue != null) && (targetValue != null) && (sourceValue instanceof EObject) && (targetValue instanceof EObject)) {
if (((EObject) sourceValue).eClass().equals(((EObject) targetValue).eClass())) {
initializeDataType((EObject) sourceValue, (EObject) targetValue, context, map);
}
}
} else if (reference.isMany()) {
Object sourceValue = source.eGet(reference);
Object targetValue = target.eGet(reference);
if ((sourceValue != null) && (targetValue != null) && (sourceValue instanceof EList) && (targetValue instanceof EList)) {
EList<?> sourceList = (EList<?>) sourceValue;
EList<?> targetList = (EList<?>) targetValue;
for (int i = 0; i < sourceList.size(); i++) {
sourceValue = sourceList.get(i);
if ((sourceValue != null) && (sourceValue instanceof EObject)) {
EObject sourceObject = (EObject) sourceValue;
//We try to find a element with same type and same name if any
for (int j = 0; j < targetList.size(); j++) {
Object targetObject = targetList.get(j);
if ((targetObject != null) && (targetObject instanceof EObject) && ((EObject) targetObject).eClass().equals(sourceObject.eClass())) {
if ((targetObject instanceof AbstractNamedElement) && (sourceObject instanceof AbstractNamedElement)) {
AbstractNamedElement sourceNamed = (AbstractNamedElement) sourceObject;
AbstractNamedElement targetNamed = (AbstractNamedElement) targetObject;
if (((sourceNamed.getName() == null) && (targetNamed.getName() == null))
|| ((sourceNamed.getName() != null) && sourceNamed.getName().equals(targetNamed.getName()))) {
initializeDataType(sourceObject, (EObject) targetObject, context, map);
break;
}
} else {
initializeDataType(sourceObject, (EObject) targetObject, context, map);
break;
}
}
}
}
}
}
}
}
}
}
}
/**
* @param source
* @param target
* @param context
* @param map
*/
protected void initializeTraceableElement(TraceableElement source, TraceableElement target, IContext context, LevelMappingTraceability map) {
ITraceabilityHandler handler = TraceabilityHandlerHelper.getInstance(context);
for (AbstractTrace sourceTrace : source.getIncomingTraces()) {
for (AbstractTrace targetTrace : target.getIncomingTraces()) {
if ((sourceTrace.eClass() == targetTrace.eClass()) && (sourceTrace instanceof Allocation) && (targetTrace instanceof Allocation)) {
if ((sourceTrace.getSourceElement() != null) && (targetTrace.getSourceElement() != null)) {
if ((sourceTrace.getTargetElement() != null) && (targetTrace.getTargetElement() != null)) {
if (handler.retrieveTracedElements(sourceTrace.getSourceElement(), context).contains(targetTrace.getSourceElement())) {
if (handler.retrieveTracedElements(sourceTrace.getTargetElement(), context).contains(targetTrace.getTargetElement())) {
addMapping(map, sourceTrace, targetTrace, context);
}
}
}
}
}
}
}
for (AbstractTrace sourceTrace : source.getOutgoingTraces()) {
for (AbstractTrace targetTrace : target.getOutgoingTraces()) {
if ((sourceTrace.eClass() == targetTrace.eClass()) && (sourceTrace instanceof Allocation) && (targetTrace instanceof Allocation)) {
if ((sourceTrace.getSourceElement() != null) && (targetTrace.getSourceElement() != null)) {
if ((sourceTrace.getTargetElement() != null) && (targetTrace.getTargetElement() != null)) {
if (handler.retrieveTracedElements(sourceTrace.getSourceElement(), context).contains(targetTrace.getSourceElement())) {
if (handler.retrieveTracedElements(sourceTrace.getTargetElement(), context).contains(targetTrace.getTargetElement())) {
addMapping(map, sourceTrace, targetTrace, context);
}
}
}
}
}
}
}
}
/**
* @param source
* @param target
* @param context
* @param map
*/
protected void initializeFunctionPkg(FunctionPkg source, FunctionPkg target, IContext context, LevelMappingTraceability map) {
//Nothing yet
}
/**
* @param source
* @param target
* @param context
* @param map
*/
protected void initializeComponent(Component source, Component target, IContext context, LevelMappingTraceability map) {
ITraceabilityHandler handler = TraceabilityHandlerHelper.getInstance(context);
// Perform a map with parts if there is only one part with the same type
for (Part sourcePartition : source.getContainedParts()) {
if (sourcePartition.getType() != null) {
Part targetPart = null;
for (Part targetPartition : target.getContainedParts()) {
if (targetPartition.getType() != null) {
if (handler.retrieveTracedElements(sourcePartition.getType(), context).contains(targetPartition.getType())) {
if (targetPart != null) {
targetPart = null;
} else {
targetPart = targetPartition;
}
}
}
}
if (targetPart != null) {
addMapping(map, sourcePartition, targetPart, context);
}
}
}
if (source.getRepresentingParts().size() == 1) {
if (target.getRepresentingParts().size() == 1) {
Part sourcePartition = source.getRepresentingParts().get(0);
Part targetPartition = target.getRepresentingParts().get(0);
addMapping(map, sourcePartition, targetPartition, context);
}
}
}
protected void initializeComponentPkg(ComponentPkg source, ComponentPkg target, IContext context,
LevelMappingTraceability map) {
ITraceabilityHandler handler = TraceabilityHandlerHelper.getInstance(context);
// Perform a map with parts if there is only one part with the same type
for (Part sourcePart : source.getOwnedParts()) {
if (sourcePart.getType() != null) {
for (Part targetPart : target.getOwnedParts()) {
if (targetPart.getType() != null) {
if (handler.retrieveTracedElements(sourcePart.getType(), context).contains(targetPart.getType())) {
addMapping(map, sourcePart, targetPart, context);
break;
}
}
}
}
}
}
/**
* @param sourceRoot_p
* @param targetRoot_p
* @param context
* @param map
*/
protected void initializeSystemEngineering(SystemEngineering source, SystemEngineering target, IContext context, LevelMappingTraceability map) {
for (ModellingArchitecture archi : source.getOwnedArchitectures()) {
for (ModellingArchitecture archi2 : target.getOwnedArchitectures()) {
if (archi2.eClass() == archi.eClass()) {
addMapping(map, archi, archi2, context);
}
}
}
}
/**
* @param sourceRoot_p
* @param targetRoot_p
* @param context
* @param map
*/
protected void initializeBlockArchitecture(BlockArchitecture source, BlockArchitecture target, IContext context, LevelMappingTraceability map) {
addMapping(map, BlockArchitectureExt.getFunctionPkg(source, false), BlockArchitectureExt.getFunctionPkg(target, false), context);
addMapping(map, BlockArchitectureExt.getRootFunction(source, false), BlockArchitectureExt.getRootFunction(target, false), context);
addMapping(map, BlockArchitectureExt.getDataPkg(source, false), BlockArchitectureExt.getDataPkg(target, false), context);
addMapping(map, BlockArchitectureExt.getActorPkg(source, false), BlockArchitectureExt.getActorPkg(target, false), context);
addMapping(map, BlockArchitectureExt.getContext(source, false), BlockArchitectureExt.getContext(target, false), context);
addMapping(map, BlockArchitectureExt.getInterfacePkg(source, false), BlockArchitectureExt.getInterfacePkg(target, false), context);
addMapping(map, BlockArchitectureExt.getAbstractCapabilityPkg(source, false), BlockArchitectureExt.getAbstractCapabilityPkg(target, false), context);
}
}