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
|
/*******************************************************************************
* Copyright (c) 2012, 2014 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.emf.compare.req;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.collect.Iterables.filter;
import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isAddOrSetDiff;
import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isDeleteOrUnsetDiff;
import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isFeatureMapContainment;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.ofKind;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.FeatureMapChange;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.utils.MatchUtil;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.FeatureMap;
/**
* The requirements engine is in charge of actually computing the requirements between the differences.
* <p>
* This default implementation aims at being generic enough to be used for any model, whatever the metamodel.
* However, specific requirements might be necessary.
* </p>
* TODO document available extension possibilities. TODO to test on XSD models for FeatureMaps
*
* @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
*/
public class DefaultReqEngine implements IReqEngine {
/**
* {@inheritDoc}
*
* @see org.eclipse.emf.compare.req.IReqEngine#computeRequirements(Comparison, Monitor)
*/
public void computeRequirements(Comparison comparison, Monitor monitor) {
for (Diff difference : comparison.getDifferences()) {
checkForRequiredDifferences(comparison, difference);
}
}
/**
* Checks the potential required differences from the given <code>difference</code>.
*
* @param comparison
* The comparison this engine is expected to complete.
* @param difference
* The difference that is to be checked
*/
protected void checkForRequiredDifferences(Comparison comparison, Diff difference) {
Set<Diff> requiredDifferences = new HashSet<Diff>();
Set<Diff> requiredByDifferences = new HashSet<Diff>();
Match match = difference.getMatch();
EObject value = getValue(comparison, difference);
DifferenceKind kind = difference.getKind();
if (value != null) {
boolean isAddition = isAddOrSetDiff(difference);
boolean isDeletion = !isAddition && isDeleteOrUnsetDiff(difference);
// ADD object
if (isAddition && isReferenceContainment(difference)) {
// -> requires ADD on the container of the object
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, value.eContainer(),
DifferenceKind.ADD));
// -> requires DELETE of the origin value on the same containment mono-valued reference
requiredDifferences.addAll(getDELOriginValueOnContainmentRefSingle(comparison, difference));
// ADD reference
} else if (isAddition && !isFeatureMapContainment(difference)) {
// -> requires ADD of the value of the reference (target object)
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, value, DifferenceKind.ADD));
// -> requires ADD of the object containing the reference
final EObject container = MatchUtil.getContainer(comparison, difference);
if (container != null) {
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, container,
DifferenceKind.ADD));
}
requiredDifferences.addAll(Collections2.filter(match.getDifferences(), and(
instanceOf(ResourceAttachmentChange.class), ofKind(DifferenceKind.ADD))));
// DELETE object
} else if (isDeletion && isReferenceContainment(difference)) {
// -> requires DELETE of the outgoing references and contained objects
requiredDifferences.addAll(getDELOutgoingReferences(comparison, difference));
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, value.eContents(),
DifferenceKind.DELETE));
// -> requires MOVE of contained objects
requiredDifferences.addAll(getMOVEContainedObjects(comparison, difference));
// The DELETE or CHANGE of incoming references are handled in the DELETE reference and CHANGE
// reference cases.
// DELETE reference
} else if (isDeletion && !isFeatureMapContainment(difference)) {
// -> is required by DELETE of the target object
requiredByDifferences.addAll(getDifferenceOnGivenObject(comparison, value,
DifferenceKind.DELETE));
// MOVE object
} else if (kind == DifferenceKind.MOVE && isReferenceContainment(difference)) {
EObject container = value.eContainer();
// -> requires ADD on the container of the object
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, container,
DifferenceKind.ADD));
// -> requires MOVE of the container of the object
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, container,
DifferenceKind.MOVE));
// CHANGE reference
} else if (kind == DifferenceKind.CHANGE && !isAddition && !isDeletion
&& !(difference instanceof FeatureMapChange)) {
// -> is required by DELETE of the origin target object
requiredByDifferences.addAll(getDifferenceOnGivenObject(comparison, MatchUtil.getOriginValue(
comparison, (ReferenceChange)difference), DifferenceKind.DELETE));
// -> requires ADD of the value of the reference (target object) if required
requiredDifferences.addAll(getDifferenceOnGivenObject(comparison, value, DifferenceKind.ADD));
}
difference.getRequires().addAll(requiredDifferences);
difference.getRequiredBy().addAll(requiredByDifferences);
}
}
/**
* From a <code>sourceDifference</code> (ADD) on a containment mono-valued reference, it retrieves a
* potential DELETE difference on the origin value.
*
* @param comparison
* The comparison this engine is expected to complete.
* @param sourceDifference
* The given difference.
* @return The found differences.
*/
private Set<Diff> getDELOriginValueOnContainmentRefSingle(Comparison comparison, Diff sourceDifference) {
Set<Diff> result = new HashSet<Diff>();
if (!(sourceDifference instanceof ReferenceChange)) {
return result;
}
EReference reference = ((ReferenceChange)sourceDifference).getReference();
if (!reference.isMany()) {
EObject originContainer = MatchUtil.getOriginContainer(comparison, sourceDifference);
if (originContainer != null) {
Object originValue = ReferenceUtil.safeEGet(originContainer, reference);
if (originValue instanceof EObject) {
result = getDifferenceOnGivenObject(comparison, (EObject)originValue,
DifferenceKind.DELETE);
}
}
}
return result;
}
/**
* Retrieve candidate reference changes based on the given <code>object</code> (value) which are from the
* given <code>kind</code>.
*
* @param comparison
* the comparison to search in.
* @param object
* The given object.
* @param kind
* The given kind.
* @return The found differences.
*/
private Set<Diff> getDifferenceOnGivenObject(Comparison comparison, EObject object, DifferenceKind kind) {
final Set<Diff> result = new LinkedHashSet<Diff>();
for (Diff diff : filter(comparison.getDifferences(object), isRequiredContainmentChange(object, kind))) {
result.add(diff);
}
return result;
}
/**
* Will be used to filter out of a list of differences all those that relate to containment changes on the
* given object (a containment reference change or a resource attachment change if the given object has no
* direct container.
*
* @param object
* The object for which we seek containmnent differences.
* @param kind
* The kind of difference we seek.
* @return The created predicate.
*/
private Predicate<? super Diff> isRequiredContainmentChange(final EObject object,
final DifferenceKind kind) {
return new Predicate<Diff>() {
public boolean apply(Diff input) {
if (input.getKind() != kind) {
return false;
}
boolean result = false;
if (input instanceof ReferenceChange
&& ((ReferenceChange)input).getReference().isContainment()) {
result = true;
} else if (input instanceof ResourceAttachmentChange && object.eContainer() == null) {
result = true;
}
return result;
}
};
}
/**
* Retrieve candidate reference changes from a list of given <code>objects</code>.
*
* @see DefaultReqEngine#getDifferenceOnGivenObject(EObject, DifferenceKind).
* @param comparison
* the comparison to search in.
* @param objects
* The given objects.
* @param kind
* The kind of requested differences.
* @return The found differences.
*/
private Set<Diff> getDifferenceOnGivenObject(Comparison comparison, List<EObject> objects,
DifferenceKind kind) {
Set<Diff> result = new HashSet<Diff>();
for (EObject object : objects) {
result.addAll(getDifferenceOnGivenObject(comparison, object, kind));
}
return result;
}
/**
* From a <code>sourceDifference</code> (DELETE) on a containment reference, it retrieves potential DELETE
* differences on the outgoing references from the value object of the <code>sourceDifference</code>.
*
* @param comparison
* The comparison this engine is expected to complete.
* @param sourceDifference
* The given difference.
* @return The found differences.
*/
private Set<ReferenceChange> getDELOutgoingReferences(Comparison comparison, Diff sourceDifference) {
Set<ReferenceChange> result = new HashSet<ReferenceChange>();
EObject value = getValue(comparison, sourceDifference);
if (value != null) {
final Match valueMatch = comparison.getMatch(value);
if (valueMatch != null) {
for (ReferenceChange candidate : filter(valueMatch.getDifferences(), ReferenceChange.class)) {
if (candidate.getKind() == DifferenceKind.DELETE || isDeleteOrUnsetDiff(candidate)) {
result.add(candidate);
}
}
}
}
return result;
}
/**
* From a <code>sourceDifference</code> (DELETE) on a containment reference, it retrieves potential MOVE
* differences on the objects contained in the value object of the <code>sourceDifference</code>.
*
* @param comparison
* The comparison this engine is expected to complete.
* @param sourceDifference
* The given difference.
* @return The found differences.
*/
private Set<ReferenceChange> getMOVEContainedObjects(Comparison comparison, Diff sourceDifference) {
Set<ReferenceChange> result = new HashSet<ReferenceChange>();
EObject value = getValue(comparison, sourceDifference);
if (value != null) {
List<EObject> contents = value.eContents();
for (EObject content : contents) {
EObject originObject = MatchUtil.getOriginObject(comparison, content);
if (originObject != null) {
for (ReferenceChange difference : filter(comparison.getDifferences(originObject),
ReferenceChange.class)) {
if (difference.getReference().isContainment()
&& difference.getSource() == sourceDifference.getSource()
&& difference.getKind() == DifferenceKind.MOVE) {
result.add(difference);
}
}
}
}
}
return result;
}
/**
* Checks whether the given diff corresponds to a containment change. This holds true for differences on
* containment references' values, but also for resource attachment changes.
*
* @param diff
* The diff to consider.
* @return <code>true</code> if the given {@code diff} is to be considered a containment change,
* <code>false</code> otherwise.
*/
private static boolean isReferenceContainment(Diff diff) {
return diff instanceof ReferenceChange && ((ReferenceChange)diff).getReference().isContainment()
|| diff instanceof ResourceAttachmentChange;
}
/**
* Retrieves the "value" of the given containment change. This will be either the "value" field of a
* ReferenceChange, or the side of the parent match for a resource attachment change.
*
* @param comparison
* The comparison during which this {@code diff} was detected.
* @param diff
* The diff which value we are to retrieve.
* @return The "value" of the given containment change.
*/
private static EObject getValue(Comparison comparison, Diff diff) {
EObject value = null;
if (diff instanceof ReferenceChange) {
value = ((ReferenceChange)diff).getValue();
} else if (diff instanceof ResourceAttachmentChange) {
value = MatchUtil.getContainer(comparison, diff);
} else if (diff instanceof FeatureMapChange) {
Object entry = ((FeatureMapChange)diff).getValue();
if (entry instanceof FeatureMap.Entry) {
Object entryValue = ((FeatureMap.Entry)entry).getValue();
if (entryValue instanceof EObject) {
value = (EObject)entryValue;
}
}
}
return value;
}
}
|