Skip to main content
summaryrefslogtreecommitdiffstats
blob: 87c86aea24390df08f1fc0d17d306acdd50c0521 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2016, 2017 Obeo and others.
 * 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
 *     Christian W. Damus - bug 522080
 *******************************************************************************/
package org.eclipse.emf.compare.internal.conflict;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Sets.newLinkedHashSet;
import static org.eclipse.emf.compare.ConflictKind.PSEUDO;
import static org.eclipse.emf.compare.ConflictKind.REAL;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.compare.AttributeChange;
import org.eclipse.emf.compare.CompareFactory;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Conflict;
import org.eclipse.emf.compare.ConflictKind;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.EMFCompareMessages;
import org.eclipse.emf.compare.Equivalence;
import org.eclipse.emf.compare.FeatureMapChange;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.MatchResource;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.diff.FeatureFilter;
import org.eclipse.emf.compare.internal.FeatureFilterAdapter;
import org.eclipse.emf.compare.internal.ThreeWayTextDiff;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * Class in charge of finding conflicting diffs for a given diff of type T.
 * 
 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
 * @param <T>
 *            The type of diff for which conflict are researched
 */
public abstract class AbstractConflictSearch<T extends Diff> {

	/** The difference, never <code>null</code>. */
	protected final T diff;

	/** The comparison that contains diff. */
	protected final Comparison comparison;

	/** The index of the comparison. */
	protected final ComparisonIndex index;

	/** The monitor to report progress to. */
	protected final Monitor monitor;

	/**
	 * Constructor.
	 * 
	 * @param diff
	 *            The diff to search conflicts with, must not be <code>null</code> and have a non-null match
	 *            that belongs to a non-null comparison. It must also have a non-null {@link DifferenceKind}
	 *            and {@link DifferenceSource}.
	 * @param index
	 *            Comparison index, must not be null
	 * @param monitor
	 *            the monitor to report progress to, must not be null
	 */
	public AbstractConflictSearch(T diff, ComparisonIndex index, Monitor monitor) {
		checkNotNull(diff);
		if (diff.getMatch() == null || diff.getMatch().getComparison() == null) {
			throw new IllegalArgumentException();
		}
		comparison = diff.getMatch().getComparison();
		checkArgument(diff.getKind() != null && diff.getSource() != null);
		this.diff = diff;
		this.index = checkNotNull(index);
		this.monitor = checkNotNull(monitor);
	}

	/**
	 * Detect conflicts with {@link AbstractConflictSearch#diff} in its comparison. This will add or update
	 * conflicts in <code>diff</code>'s comparison.
	 */
	public abstract void detectConflicts();

	/**
	 * Get the diffs in the same {@link Match} as diff.
	 * 
	 * @return A never-null EList of differences in the same {@link Match} as diff, including diff.
	 */
	protected EList<Diff> getDiffsInSameMatch() {
		return diff.getMatch().getDifferences();
	}

	/**
	 * Specifies whether the given {@code diff1} and {@code diff2} are either {@link FeatureMapChange feature
	 * map changes} or mergeable {@link AttributeChange attribute changes} of String attributes.
	 * 
	 * @param diff1
	 *            One of the diffs to check.
	 * @param diff2
	 *            The other diff to check.
	 * @return <code>true</code> if it is a {@link FeatureMapChange} or a mergeable {@link AttributeChange},
	 *         <code>false</code> otherwise.
	 */
	protected boolean isFeatureMapChangeOrMergeableStringAttributeChange(Diff diff1, Diff diff2) {
		return isFeatureMapChange(diff1) || areMergeableStringAttributeChanges(diff1, diff2);
	}

	/**
	 * Specifies whether the given {@code diff} is a {@link FeatureMapChange}.
	 * 
	 * @param toCheck
	 *            The diff to check.
	 * @return <code>true</code> if it is a {@link FeatureMapChange}, <code>false</code> otherwise.
	 */
	protected boolean isFeatureMapChange(Diff toCheck) {
		return toCheck instanceof FeatureMapChange;
	}

	/**
	 * Specifies whether the two given diffs, {@code diff1} and {@code diff2}, are both {@link AttributeChange
	 * attribute changes} of String attributes and can be merged with a line-based three-way merge.
	 * 
	 * @see org.eclipse.emf.compare.internal.ThreeWayTextDiff
	 * @param diff1
	 *            One of the diffs to check.
	 * @param diff2
	 *            The other diff to check.
	 * @return <code>true</code> if the diffs are mergeable changes of a string attribute, <code>false</code>
	 *         otherwise.
	 */
	protected boolean areMergeableStringAttributeChanges(Diff diff1, Diff diff2) {
		final boolean mergeableStringAttributeChange;
		if (isStringAttributeChange(diff1)) {
			final AttributeChange attributeChange1 = (AttributeChange)diff1;
			final AttributeChange attributeChange2 = (AttributeChange)diff2;
			mergeableStringAttributeChange = isMergeable(attributeChange1, attributeChange2);
		} else {
			mergeableStringAttributeChange = false;
		}
		return mergeableStringAttributeChange;
	}

	/**
	 * Specifies whether the given {@code diff} is a {@link AttributeChange} of a String attribute.
	 * 
	 * @param toCheck
	 *            The diff to check.
	 * @return <code>true</code> if it is a {@link AttributeChange} of a String attribute, <code>false</code>
	 *         otherwise.
	 */
	protected boolean isStringAttributeChange(Diff toCheck) {
		return toCheck instanceof AttributeChange && ((AttributeChange)toCheck).getAttribute()
				.getEAttributeType().getInstanceClass() == String.class;
	}

	/**
	 * Specifies whether the two given attribute changes, {@code diff1} and {@code diff2}, can be merged with
	 * a line-based three-way merge.
	 * 
	 * @see org.eclipse.emf.compare.internal.ThreeWayTextDiff
	 * @param diff1
	 *            One of the attribute changes to check.
	 * @param diff2
	 *            The other attribute change to check.
	 * @return <code>true</code> if the attribute changes are mergeable, <code>false</code> otherwise.
	 */
	protected boolean isMergeable(final AttributeChange diff1, final AttributeChange diff2) {
		final String changedValue1 = getChangedValue(diff1);
		final String changedValue2 = getChangedValue(diff2);
		final EObject originalContainer = diff1.getMatch().getOrigin();
		final EAttribute changedAttribute = diff1.getAttribute();
		final String originalValue;

		if (originalContainer == null) {
			// Owner is added to both sides
			originalValue = null;
		} else {
			originalValue = (String)ReferenceUtil.safeEGet(originalContainer, changedAttribute);
		}

		return isMergeableText(changedValue1, changedValue2, originalValue);
	}

	/**
	 * Specifies whether the given three versions of a text {@code left}, {@code right}, and {@code origin}
	 * are mergeable with a line-based three-way merge.
	 * 
	 * @param left
	 *            The left version.
	 * @param right
	 *            The right version.
	 * @param origin
	 *            The original version.
	 * @return <code>true</code> if they are mergeable, false otherwise.
	 * @since 3.2
	 */
	protected boolean isMergeableText(final String left, final String right, final String origin) {
		ThreeWayTextDiff textDiff = new ThreeWayTextDiff(origin, left, right);
		return !textDiff.isConflicting();
	}

	/**
	 * Returns the changed attribute value denoted by the given {@code diff}.
	 * 
	 * @param attributeChange
	 *            The attribute change for which the changed value is requested.
	 * @return The changed attribute value.
	 */
	protected String getChangedValue(final AttributeChange attributeChange) {
		final String changedValue;
		Match match = attributeChange.getMatch();
		if (DifferenceSource.LEFT.equals(attributeChange.getSource())) {
			changedValue = (String)ReferenceUtil.safeEGet(match.getLeft(), attributeChange.getAttribute());
		} else if (DifferenceSource.RIGHT.equals(attributeChange.getSource())) {
			changedValue = (String)ReferenceUtil.safeEGet(match.getRight(), attributeChange.getAttribute());
		} else {
			changedValue = (String)attributeChange.getValue();
		}
		return changedValue;
	}

	/**
	 * This will be called whenever we detect a new conflict in order to create (or update) the actual
	 * association.
	 * 
	 * @param other
	 *            Second of the two differences for which we detected a conflict.
	 * @param kind
	 *            Kind of this conflict.
	 */
	protected void conflict(Diff other, ConflictKind kind) {
		// Pre-condition: diff and other are not already part of the same conflict
		if (diff.getConflict() != null && diff.getConflict().getDifferences().contains(other)) {
			return;
		}

		Conflict conflict = null;
		Conflict toBeMerged = null;
		if (diff.getConflict() != null) {
			conflict = diff.getConflict();
			if (conflict.getKind() == PSEUDO && conflict.getKind() != kind) {
				conflict.setKind(kind);
			}
			if (other.getConflict() != null) {
				// Merge the two
				toBeMerged = other.getConflict();
			}
		} else if (other.getConflict() != null) {
			conflict = other.getConflict();
			if (conflict.getKind() == PSEUDO && conflict.getKind() != kind) {
				conflict.setKind(kind);
			}
		} else if (diff.getEquivalence() != null) {
			Equivalence equivalence = diff.getEquivalence();
			for (Diff equ : equivalence.getDifferences()) {
				if (equ.getConflict() != null) {
					conflict = equ.getConflict();
					if (other.getConflict() == conflict) {
						// See initial pre-condition
						return;
					}
					if (conflict.getKind() == PSEUDO && conflict.getKind() != kind) {
						conflict.setKind(kind);
					}
					if (other.getConflict() != null) {
						// Merge the two
						toBeMerged = other.getConflict();
					}
					break;
				}
			}
		} else if (other.getEquivalence() != null) {
			Equivalence equivalence = other.getEquivalence();
			for (Diff equ : equivalence.getDifferences()) {
				if (equ.getConflict() != null) {
					conflict = equ.getConflict();
					if (conflict.getKind() == PSEUDO && conflict.getKind() != kind) {
						conflict.setKind(kind);
					}
					break;
				}
			}
		}

		if (conflict == null) {
			conflict = CompareFactory.eINSTANCE.createConflict();
			conflict.setKind(kind);
			comparison.getConflicts().add(conflict);
		}

		final EList<Diff> conflictDiffs = conflict.getDifferences();
		if (toBeMerged != null) {
			// These references are opposite. We can't simply iterate
			for (Diff aDiff : Lists.newArrayList(toBeMerged.getDifferences())) {
				conflictDiffs.add(aDiff);
			}
			if (toBeMerged.getKind() == REAL && conflict.getKind() != REAL) {
				conflict.setKind(REAL);
			}
			EcoreUtil.remove(toBeMerged);
			toBeMerged.getDifferences().clear();
		}

		Set<Diff> toAdd = newLinkedHashSet();
		for (Diff conflicting : ImmutableSet.of(diff, other)) {
			if (conflicting.getEquivalence() == null) {
				toAdd.add(conflicting);
			} else {
				toAdd.addAll(conflicting.getEquivalence().getDifferences());
			}
		}
		conflict.getDifferences().addAll(toAdd);
	}

	/**
	 * Returns the MatchResource corresponding to the given <code>resource</code>.
	 * 
	 * @param resource
	 *            Resource for which we need a MatchResource.
	 * @return The MatchResource corresponding to the given <code>resource</code>.
	 */
	protected MatchResource getMatchResource(Resource resource) {
		final List<MatchResource> matchedResources = comparison.getMatchedResources();
		final int size = matchedResources.size();
		MatchResource soughtMatch = null;
		for (int i = 0; i < size && soughtMatch == null; i++) {
			final MatchResource matchRes = matchedResources.get(i);
			if (matchRes.getRight() == resource || matchRes.getLeft() == resource
					|| matchRes.getOrigin() == resource) {
				soughtMatch = matchRes;
			}
		}
		checkState(soughtMatch != null, EMFCompareMessages
				.getString("ResourceAttachmentChangeSpec.MissingMatch", resource.getURI().lastSegment())); //$NON-NLS-1$
		return soughtMatch;
	}

	/**
	 * Provide the model element the given diff applies to.
	 * 
	 * @param rac
	 *            The change
	 * @return The model element of the given diff, or null if it cannot be found.
	 */
	protected EObject getRelatedModelElement(ResourceAttachmentChange rac) {
		Match m = rac.getMatch();
		EObject o;
		switch (rac.getSource()) {
			case LEFT:
				o = m.getLeft(); // null if DELETE
				break;
			case RIGHT:
				o = m.getRight(); // null if DELETE
				break;
			default:
				o = null;
		}
		return o;
	}

	/**
	 * Provide the non-null model element the given diff applies to.
	 * 
	 * @param rac
	 *            The change
	 * @return The model element of the given diff, cannot be null.
	 */
	protected EObject getValue(ResourceAttachmentChange rac) {
		Match m = rac.getMatch();
		EObject o;
		switch (rac.getKind()) {
			case ADD:
				// Voluntary pass-through
			case CHANGE:
				// Voluntary pass-through
			case MOVE:
				switch (rac.getSource()) {
					case LEFT:
						o = m.getLeft();
						break;
					case RIGHT:
						o = m.getRight();
						break;
					default:
						o = null;
				}
				break;
			case DELETE:
				o = m.getOrigin();
				break;
			default:
				throw new IllegalStateException();
		}
		checkState(o != null);
		return o;
	}

	// FIXME Move this elsewhere
	/**
	 * This predicate will be <code>true</code> for any Match which represents a containment deletion.
	 * 
	 * @return A Predicate that will be met by containment deletions.
	 */
	protected Predicate<? super Match> isContainmentDelete() {
		return new Predicate<Match>() {
			public boolean apply(Match input) {
				return input.getOrigin() != null && (input.getLeft() == null || input.getRight() == null);
			}
		};
	}

	/**
	 * Returns the feature filter attached to the given comparison if any.
	 * 
	 * @param comp
	 *            The comparison.
	 * @return The feature filter attached to the given comparison if any.
	 */
	protected FeatureFilter getFeatureFilter(Comparison comp) {
		Object adapter = EcoreUtil.getExistingAdapter(comparison, FeatureFilterAdapter.class);
		if (adapter instanceof FeatureFilterAdapter) {
			return ((FeatureFilterAdapter)adapter).getFeatureFilter();
		}
		return null;
	}
}

Back to the top