Skip to main content
summaryrefslogtreecommitdiffstats
blob: ddaa96e2ba009b6740c2071ea4c3db459efe036b (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
/*******************************************************************************
 * Copyright (c) 2016 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.internal.conflict;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.instanceOf;
import static org.eclipse.emf.compare.ConflictKind.PSEUDO;
import static org.eclipse.emf.compare.ConflictKind.REAL;
import static org.eclipse.emf.compare.DifferenceKind.ADD;
import static org.eclipse.emf.compare.DifferenceKind.CHANGE;
import static org.eclipse.emf.compare.DifferenceKind.MOVE;
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.utils.EMFComparePredicates.ofKind;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.onFeature;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.possiblyConflictingWith;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.valueMatches;

import com.google.common.collect.Iterables;

import java.util.Collection;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;

/**
 * Search conflicts for containment {@link ReferenceChange}s.
 * 
 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
 */
public class ContainmentRefChangeConflictSearch {

	/**
	 * Search conflicts for containment {@link ReferenceChange} of kind {@link DifferenceKind#ADD}.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	public static class Add extends AbstractConflictSearch<ReferenceChange> {

		/**
		 * Constructor.
		 * 
		 * @param diff
		 *            The diff to search for conflicts
		 * @param index
		 *            Comparison index, must not be null
		 * @param monitor
		 *            the monitor to report progress to, must not be null
		 */
		public Add(ReferenceChange diff, ComparisonIndex index, Monitor monitor) {
			super(diff, index, monitor);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void detectConflicts() {
			EObject value = diff.getValue();
			EReference feature = diff.getReference();

			// First let's see if non-containment diffs point to the EObject added
			Collection<ReferenceChange> refChanges = index.getReferenceChangesByValue(value);
			for (ReferenceChange candidate : Iterables.filter(refChanges,
					and(possiblyConflictingWith(diff), ofKind(ADD, CHANGE)))) {
				if (candidate.getReference().isContainment()) {
					if (candidate.getReference() == feature && candidate.getMatch() == diff.getMatch()
							&& matchingIndices(diff.getMatch(), feature, value, candidate.getValue())) {
						conflict(candidate, PSEUDO);
					} else {
						conflict(candidate, REAL);
					}
				}
			}

			// Can conflict with other ADD or SET if isMany() == false
			EList<Diff> diffsInSameMatch = diff.getMatch().getDifferences();
			if (!feature.isMany()) {
				for (Diff candidate : Iterables.filter(diffsInSameMatch, and(possiblyConflictingWith(diff),
						instanceOf(ReferenceChange.class), onFeature(feature), ofKind(ADD, CHANGE)))) {
					if (comparison.getEqualityHelper().matchingValues(((ReferenceChange)candidate).getValue(),
							diff.getValue())) {
						conflict(candidate, PSEUDO);
					} else {
						conflict(candidate, REAL);
					}
				}
			}
		}
	}

	/**
	 * Search conflicts for containment {@link ReferenceChange} of kind {@link DifferenceKind#CHANGE}.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	public static class Change extends AbstractConflictSearch<ReferenceChange> {

		/**
		 * Constructor.
		 * 
		 * @param diff
		 *            The diff to search for conflicts
		 * @param index
		 *            Comparison index, must not be null
		 * @param monitor
		 *            the monitor to report progress to, must not be null
		 */
		public Change(ReferenceChange diff, ComparisonIndex index, Monitor monitor) {
			super(diff, index, monitor);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void detectConflicts() {
			EObject value = diff.getValue();
			EReference feature = diff.getReference();

			// First let's see if non-containment diffs point to the EObject added
			Collection<ReferenceChange> refChanges = index.getReferenceChangesByValue(value);
			for (ReferenceChange candidate : Iterables.filter(refChanges,
					and(possiblyConflictingWith(diff), ofKind(ADD, CHANGE)))) {
				if (candidate.getReference().isContainment()) {
					if (candidate.getReference() == feature && candidate.getMatch() == diff.getMatch()) {
						conflict(candidate, PSEUDO);
					} else {
						conflict(candidate, REAL);
					}
				}
			}

			// Can conflict with other ADD or SET if isMany() == false
			EList<Diff> diffsInSameMatch = diff.getMatch().getDifferences();
			if (!feature.isMany() && isAddOrSetDiff(diff)) {
				for (Diff candidate : Iterables.filter(diffsInSameMatch, and(possiblyConflictingWith(diff),
						instanceOf(ReferenceChange.class), onFeature(feature)))) {
					if (comparison.getEqualityHelper().matchingValues(((ReferenceChange)candidate).getValue(),
							diff.getValue())) {
						conflict(candidate, PSEUDO);
					} else {
						conflict(candidate, REAL);
					}
				}
			} else if (!isDeleteOrUnsetDiff(diff)) {
				for (Diff candidate : Iterables.filter(diffsInSameMatch, and(possiblyConflictingWith(diff),
						instanceOf(ReferenceChange.class), onFeature(feature)))) {
					if (!isDeleteOrUnsetDiff(candidate)
							&& diff.getReference() == ((ReferenceChange)candidate).getReference()) {
						// Same value added in the same container/reference couple
						if (matchingIndices(diff.getMatch(), diff.getReference(), value,
								((ReferenceChange)candidate).getValue())) {
							conflict(candidate, PSEUDO);
						}
					}
				}
			}
		}
	}

	/**
	 * Search conflicts for containment {@link ReferenceChange} of kind {@link DifferenceKind#DELETE}.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	public static class Delete extends AbstractConflictSearch<ReferenceChange> {

		/**
		 * Constructor.
		 * 
		 * @param diff
		 *            The diff to search for conflicts
		 * @param index
		 *            Comparison index, must not be null
		 * @param monitor
		 *            the monitor to report progress to, must not be null
		 */
		public Delete(ReferenceChange diff, ComparisonIndex index, Monitor monitor) {
			super(diff, index, monitor);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void detectConflicts() {
			EObject value = diff.getValue();

			// First let's see if non-containment diffs point to the EObject deleted from its parent
			Collection<ReferenceChange> refChanges = index.getReferenceChangesByValue(value);
			for (ReferenceChange candidate : Iterables.filter(refChanges, possiblyConflictingWith(diff))) {
				if (isDeleteOrUnsetDiff(candidate)) {
					// No conflict here
				} else {
					conflict(candidate, REAL);
				}
			}

			// Now let's look for conflits with containment ReferenceChanges
			EList<Diff> diffsInSameMatch = diff.getMatch().getDifferences();
			for (Diff candidate : Iterables.filter(diffsInSameMatch,
					and(possiblyConflictingWith(diff), instanceOf(ReferenceChange.class),
							valueMatches(comparison.getEqualityHelper(), value)))) {

				if (isDeleteOrUnsetDiff(candidate)) {
					conflict(candidate, PSEUDO);
				} else {
					conflict(candidate, REAL);
				}
			}

			// [381143] Every Diff "under" a containment deletion conflicts with it.
			final DiffTreeIterator diffIterator = new DiffTreeIterator(comparison.getMatch(value));
			diffIterator.setFilter(possiblyConflictingWith(diff));
			diffIterator.setPruningFilter(isContainmentDelete());

			while (diffIterator.hasNext()) {
				Diff extendedCandidate = diffIterator.next();
				if (isDeleteOrUnsetDiff(extendedCandidate)) {
					// We do not want to create a pseudo conflict between a deleted container and its
					// deleted content, since that would prevent us from merging the container deletion
					// altogether (since pseudo conflicts usually mean that no action is needed).
					// conflict(extendedCandidate, PSEUDO);
				} else {
					conflict(extendedCandidate, REAL);
				}
			}

		}
	}

	/**
	 * Search conflicts for containment {@link ReferenceChange} of kind {@link DifferenceKind#MOVE}.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	public static class Move extends AbstractConflictSearch<ReferenceChange> {

		/**
		 * Constructor.
		 * 
		 * @param diff
		 *            The diff to search for conflicts
		 * @param index
		 *            Comparison index, must not be null
		 * @param monitor
		 *            the monitor to report progress to, must not be null
		 */
		public Move(ReferenceChange diff, ComparisonIndex index, Monitor monitor) {
			super(diff, index, monitor);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void detectConflicts() {
			EObject value = diff.getValue();
			EReference feature = diff.getReference();

			// First let's see if non-containment diffs point to the EObject added
			Collection<ReferenceChange> refChanges = index.getReferenceChangesByValue(value);
			for (ReferenceChange candidate : Iterables.filter(refChanges,
					and(possiblyConflictingWith(diff), ofKind(MOVE)))) {
				if (candidate.getReference().isContainment()) {
					if (candidate.getReference() == feature && candidate.getMatch() == diff.getMatch()
							&& matchingIndices(diff.getMatch(), feature, value, candidate.getValue())) {
						conflict(candidate, PSEUDO);
					} else {
						conflict(candidate, REAL);
					}
				}
			}

			EList<Diff> diffsInSameMatch = diff.getMatch().getDifferences();
			for (Diff candidate : Iterables.filter(diffsInSameMatch,
					and(possiblyConflictingWith(diff), valueMatches(comparison.getEqualityHelper(), value),
							instanceOf(ReferenceChange.class), onFeature(feature)))) {
				if (matchingIndices(diff.getMatch(), diff.getReference(), value,
						((ReferenceChange)candidate).getValue())) {
					conflict(candidate, PSEUDO);
				} else {
					conflict(candidate, REAL);
				}
			}
		}
	}
}

Back to the top