Skip to main content
summaryrefslogtreecommitdiffstats
blob: bb421951b7b2fbbdc494ee8d54c089825604b32e (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
/*******************************************************************************
 * 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.uml2.internal.postprocessor;

import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isAddOrSetDiff;
import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isDeleteOrUnsetDiff;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.internal.postprocessor.factories.IChangeFactory;
import org.eclipse.emf.compare.postprocessor.IPostProcessor;
import org.eclipse.emf.compare.uml2.internal.UMLCompareMessages;
import org.eclipse.emf.compare.uml2.internal.UMLComparePlugin;
import org.eclipse.emf.compare.uml2.internal.UMLDiff;
import org.eclipse.emf.compare.uml2.internal.postprocessor.extension.UMLExtensionFactoryRegistry;
import org.eclipse.emf.compare.uml2.internal.postprocessor.util.UMLCompareUtil;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.ProfileApplication;

/**
 * Post-processor to create the UML difference extensions.
 * 
 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
 */
public class UMLPostProcessor implements IPostProcessor {

	/**
	 * Predicate to find the match of an annotation referencing a profile definition, within a list a matches.
	 */
	private static final Predicate<Match> ANNOTATION_REFERENCING_PROFILE_DEFINITION = new Predicate<Match>() {

		private Pattern umlNsPattern = Pattern.compile("http://www\\.eclipse\\.org/uml2/.*/UML"); //$NON-NLS-1$

		public boolean apply(Match input) {
			return input.getLeft() instanceof EAnnotation
					&& umlNsPattern.matcher(((EAnnotation)input.getLeft()).getSource()).matches();
		}
	};

	/** UML2 extensions factories. */
	private Set<IChangeFactory> uml2ExtensionFactories;

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postMatch(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postMatch(Comparison comparison, Monitor monitor) {
		// Check the version of the applied profile on each matched profile application.
		boolean isSameProfileVersion = true;

		Iterator<Match> matchesRoot = comparison.getMatches().iterator();
		while (matchesRoot.hasNext() && isSameProfileVersion) {
			Match matchRoot = matchesRoot.next();

			isSameProfileVersion = checkProfileVersion(matchRoot);

			Iterator<Match> matches = matchRoot.getAllSubmatches().iterator();
			while (matches.hasNext() && isSameProfileVersion) {
				Match match = matches.next();

				isSameProfileVersion = checkProfileVersion(match);
			}
		}
	}

	/**
	 * It checks the profile applications, matched by the given match, are based on the same profile version.<br>
	 * It adds a diagnostic (error) to the comparison as soon as a difference is met.
	 * 
	 * @param match
	 *            A match of profile applications.
	 * @return False if a couple of profile applications at least is not based on the same profile version,
	 *         True otherwise.
	 */
	private boolean checkProfileVersion(Match match) {
		EObject left = match.getLeft();
		EObject right = match.getRight();
		if (left instanceof ProfileApplication && right != null) {
			Collection<Match> annotationsMatches = Collections2.filter(match.getSubmatches(),
					ANNOTATION_REFERENCING_PROFILE_DEFINITION);
			for (Match annotationMatch : annotationsMatches) {
				EAnnotation leftAnnot = (EAnnotation)annotationMatch.getLeft();
				EAnnotation rightAnnot = (EAnnotation)annotationMatch.getRight();
				if (!checkProfileVersion(match.getComparison(), (ProfileApplication)left, leftAnnot,
						rightAnnot)) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * It checks the profile annotations reference the same profile version.<br>
	 * It adds a diagnostic (error) to the comparison as soon as an annotation does not reference the same
	 * profile version as the annotation from the other side.
	 * 
	 * @param comparison
	 *            The comparison.
	 * @param profileApplication
	 *            The profile application to compare (on the left or right side)
	 * @param leftAnnot
	 *            The annotation referencing the profile on the left side
	 * @param rightAnnot
	 *            The annotation referencing the profile on the right side
	 * @return False if the version of the referenced profile is different, True otherwise.
	 */
	private boolean checkProfileVersion(Comparison comparison, ProfileApplication profileApplication,
			EAnnotation leftAnnot, EAnnotation rightAnnot) {
		Collection<URI> leftUris = getNormalizedURIs(ReferenceUtil.getAsList(leftAnnot,
				EcorePackage.Literals.EANNOTATION__REFERENCES));
		Collection<URI> rightUris = getNormalizedURIs(ReferenceUtil.getAsList(rightAnnot,
				EcorePackage.Literals.EANNOTATION__REFERENCES));
		if (leftUris.size() != rightUris.size() || !leftUris.containsAll(rightUris)) {
			org.eclipse.uml2.uml.Package impactedPackage = profileApplication.getApplyingPackage();
			String message;
			if (impactedPackage != null) {
				message = UMLCompareMessages.getString("profile.definition.changed.on", "<" //$NON-NLS-1$//$NON-NLS-2$
						+ impactedPackage.eClass().getName() + "> " + impactedPackage.getName()); //$NON-NLS-1$
			} else {
				message = UMLCompareMessages.getString("profile.definition.changed"); //$NON-NLS-1$
			}

			addDiagnostic(comparison, new BasicDiagnostic(Diagnostic.ERROR, UMLComparePlugin.PLUGIN_ID, 0,
					message, new Object[] {}));

			return false;
		}
		return true;
	}

	/**
	 * It adds a diagnostic to the given comparison.
	 * 
	 * @param comparison
	 *            The comparison
	 * @param diagnostic
	 *            The diagnostic
	 */
	private void addDiagnostic(Comparison comparison, Diagnostic diagnostic) {
		Diagnostic currentDiag = comparison.getDiagnostic();
		if (currentDiag == null) {
			comparison.setDiagnostic(new BasicDiagnostic(UMLComparePlugin.PLUGIN_ID, 0, null, new Object[0]));
		}
		((BasicDiagnostic)comparison.getDiagnostic()).add(diagnostic);
	}

	/**
	 * Get the normalized URI of the given ecore objects.
	 * 
	 * @param eObjects
	 *            The ecore objects.
	 * @return the list of the URI.
	 */
	private Collection<URI> getNormalizedURIs(List<Object> eObjects) {
		Function<Object, URI> eObjectToURI = new Function<Object, URI>() {
			public URI apply(Object input) {
				if (input instanceof EObject) {
					URI uri = EcoreUtil.getURI((EObject)input);
					URIConverter uriConverter = getURIConverter((EObject)input);
					if (uriConverter != null) {
						uri = uriConverter.normalize(uri);
					}
					return uri;
				}
				return null;
			}

			private URIConverter getURIConverter(EObject eObject) {
				Resource resource = eObject.eResource();
				if (resource != null) {
					ResourceSet resourceSet = resource.getResourceSet();
					if (resourceSet != null) {
						return resourceSet.getURIConverter();
					}
				}
				return null;
			}
		};
		return Collections2.transform(eObjects, eObjectToURI);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postDiff(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postDiff(Comparison comparison, Monitor monitor) {
		// Not needed here.
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postRequirements(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postRequirements(Comparison comparison, Monitor monitor) {

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postEquivalences(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postEquivalences(Comparison comparison, Monitor monitor) {
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postConflicts(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postConflicts(Comparison comparison, Monitor monitor) {

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor#postComparison(org.eclipse.emf.compare.Comparison,
	 *      org.eclipse.emf.common.util.Monitor)
	 */
	public void postComparison(Comparison comparison, Monitor monitor) {

		final Map<Class<? extends Diff>, IChangeFactory> mapUml2ExtensionFactories = UMLExtensionFactoryRegistry
				.createExtensionFactories();
		uml2ExtensionFactories = new LinkedHashSet<IChangeFactory>(mapUml2ExtensionFactories.values());

		// Creation of the UML difference extensions
		for (Diff diff : comparison.getDifferences()) {
			applyManagedTypes(diff);
		}

		// Filling of the requirements link of the UML difference extensions
		for (Diff umlDiff : comparison.getDifferences()) {
			if (umlDiff instanceof UMLDiff) {
				final Class<?> classDiffElement = umlDiff.eClass().getInstanceClass();
				final IChangeFactory diffFactory = mapUml2ExtensionFactories.get(classDiffElement);
				if (diffFactory != null) {
					diffFactory.fillRequiredDifferences(comparison, umlDiff);
				}
			}
		}

		// Filling implications with subsets
		for (Diff diff : comparison.getDifferences()) {
			if (diff instanceof ReferenceChange) {
				fillImplicationsWithUMLSubsets((ReferenceChange)diff);
			}
		}

	}

	/**
	 * Creates the difference extensions in relation to the existing {@link DiffElement}s.
	 * 
	 * @param element
	 *            The input {@link DiffElement}.
	 */
	private void applyManagedTypes(Diff element) {
		for (IChangeFactory factory : uml2ExtensionFactories) {
			if (factory.handles(element)) {
				Diff extension = factory.create(element);
				// FIXME: Instantiation of UML extensions (intersections of predicates)
				if (!extension.getRefinedBy().isEmpty()) {
					final Match match = factory.getParentMatch(element);
					// FIXME: why the match may be null ? (see AddAssociation2Test.testMergeLtRA30UseCase)
					if (match != null) {
						match.getDifferences().add(extension);
					}
				} else {
					extension = null;
				}

			}
		}
	}

	/**
	 * Fill the implication links ({@link Diff#getImplies()}, {@link Diff#getImpliedBy()}) on the given
	 * reference change.
	 * 
	 * @param diff
	 *            The reference change.
	 */
	private void fillImplicationsWithUMLSubsets(ReferenceChange diff) {
		EReference reference = diff.getReference();
		// ADD implies ADD on non union supersets
		// DELETE is implied by DEL on non union supersets
		for (EReference superSet : UMLCompareUtil.getNonUnionSupersetReferences(reference)) {
			Comparison comparison = diff.getMatch().getComparison();
			for (Diff superSetDiff : comparison.getDifferences(superSet)) {
				// Only keep diffs on the same ref and value where parent matches
				if (superSetDiff instanceof ReferenceChange
						&& ((ReferenceChange)superSetDiff).getReference() == superSet
						&& ((ReferenceChange)superSetDiff).getValue() == diff.getValue()
						&& ((ReferenceChange)superSetDiff).getMatch() == diff.getMatch()) {
					if (isAddOrSetDiff(diff) && isAddOrSetDiff(superSetDiff)) {
						diff.getImplies().add(superSetDiff);
					} else if (isDeleteOrUnsetDiff(diff) && isDeleteOrUnsetDiff(superSetDiff)) {
						diff.getImpliedBy().add(superSetDiff);
					}
				}
			}
		}
	}
}

Back to the top