Skip to main content
summaryrefslogtreecommitdiffstats
blob: f7632e8ec47f9acd7314f3324847d024df5ca231 (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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*******************************************************************************
 * Copyright (c) 2013 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 com.google.common.base.Predicates.instanceOf;

import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory;
import org.eclipse.emf.compare.uml2.internal.StereotypeApplicationChange;
import org.eclipse.emf.compare.uml2.internal.UMLDiff;
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.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.Switch;
import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.uml.util.UMLSwitch;

/**
 * Factory of UML difference extensions.
 * 
 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
 */
public abstract class AbstractUMLChangeFactory extends AbstractChangeFactory {

	/**
	 * UML Switch to get the discriminants (if they exist) related to the given business object.
	 * 
	 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
	 */
	protected class DiscriminantsGetter extends UMLSwitch<Set<EObject>> {

		@Override
		public Set<EObject> defaultCase(EObject object) {
			return defaultCaseForDiscriminantsGetter(this, object);
		}
	}

	/**
	 * Setting to define a candidate to the seeking of refining differences.
	 * 
	 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
	 */
	private class RefiningCandidate implements Setting {

		/** The business object containing the structural feature. */
		private EObject holdingObject;

		/** The structural feature. */
		private EStructuralFeature eStructuralFeature;

		/**
		 * Constructor to use if the origin of the target value is unknown (neither holding object nor
		 * structural feature). This candidate will be considered as a containment reference to the target
		 * value.
		 */
		public RefiningCandidate() {
		}

		/**
		 * Constructor.
		 * 
		 * @param holdingObject
		 *            The business object containing the structural feature to the target value.
		 * @param feature
		 *            the structural feature to the target value.
		 */
		public RefiningCandidate(EObject holdingObject, EStructuralFeature feature) {
			this.holdingObject = holdingObject;
			this.eStructuralFeature = feature;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#getEObject()
		 */
		public EObject getEObject() {
			return holdingObject;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#getEStructuralFeature()
		 */
		public EStructuralFeature getEStructuralFeature() {
			return eStructuralFeature;
		}

		/**
		 * {@inheritDoc}<br>
		 * No use. Return null.
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#get(boolean)
		 */
		public Object get(boolean resolve) {
			return null;
		}

		/**
		 * {@inheritDoc}<br>
		 * No use. Do nothing.
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#set(java.lang.Object)
		 */
		public void set(Object newValue) {
		}

		/**
		 * {@inheritDoc}<br>
		 * No use. Return false.
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#isSet()
		 */
		public boolean isSet() {
			return false;
		}

		/**
		 * {@inheritDoc}<br>
		 * No use. Do nothing.
		 * 
		 * @see org.eclipse.emf.ecore.EStructuralFeature.Setting#unset()
		 */
		public void unset() {
		}

	}

	/**
	 * It defines the predicate to keep only the differences which match with the given settings (refining
	 * candidates).
	 * 
	 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
	 */
	private class DifferencesOnRefiningCandidates implements Predicate<Diff> {

		/** The comparison. */
		private final Comparison fComparison;

		/** The specified settings. */
		private final HashMultimap<Object, RefiningCandidate> fRefiningCandidates;

		/**
		 * Constructor.
		 * 
		 * @param comparison
		 *            The comparison.
		 * @param refiningCandidates
		 *            The specified settings.
		 */
		public DifferencesOnRefiningCandidates(Comparison comparison,
				HashMultimap<Object, RefiningCandidate> refiningCandidates) {
			fComparison = comparison;
			fRefiningCandidates = refiningCandidates;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see com.google.common.base.Predicate#apply(java.lang.Object)
		 */
		public boolean apply(final Diff input) {
			boolean result = false;
			Object value = MatchUtil.getValue(input);
			Set<RefiningCandidate> settings = fRefiningCandidates.get(value);
			if (settings.size() > 0) {
				// Keep the current difference if one specified candidate setting match with it at least.
				result = Iterables.any(settings, new Predicate<EStructuralFeature.Setting>() {
					public boolean apply(EStructuralFeature.Setting setting) {
						boolean res = true;
						if (setting.getEObject() != null) {
							// Keep if match of the current difference is the same as the match of the
							// specified
							// holding object in the setting...
							res = input.getMatch() == fComparison.getMatch(setting.getEObject());
						}
						if (setting.getEStructuralFeature() != null) {
							// ... and the structural feature is the same as the specified one in the
							// setting.
							res = res
									&& MatchUtil.getStructuralFeature(input) == setting
											.getEStructuralFeature();
						} else {
							// If no structural feature specified, check that the reference of the
							// difference is containment.
							EStructuralFeature diffFeature = MatchUtil.getStructuralFeature(input);
							res = res && diffFeature instanceof EReference
									&& ((EReference)diffFeature).isContainment();
						}
						return res;
					}
				});
			}
			return result;
		}
	}

	/**
	 * {@inheritDoc}<br>
	 * It checks that the given difference concerns the creation of an UML macroscopic change. <br>
	 * It verifies this difference is not a part of a macroscopic ADD or DELETE not to have a macroscopic
	 * ADD/DELETE plus a macroscopic CHANGE.<br>
	 * At last, the first matching difference allows to create a complete macroscopic change (with all the
	 * refining differences. So, the next matching ones will not be held to avoid to create duplicated
	 * macroscopic changes.
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#handles(org.eclipse.emf.compare.Diff)
	 */
	@Override
	public boolean handles(Diff input) {
		return super.handles(input) && !isChangeOnAddOrDelete(input) && input.getRefines().isEmpty();
	}

	/**
	 * {@inheritDoc}<br>
	 * It creates the macroscopic change and builds it. It sets its discriminant (main business object to
	 * focus). <br>
	 * For a macroscopic ADD/DELETE, it sets its eReference (the reference of the main refining difference (on
	 * the discriminant)).
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.IChangeFactory#create(org.eclipse.emf.compare.Diff)
	 */
	@Override
	public Diff create(Diff input) {
		Diff ret = super.create(input);
		if (ret instanceof UMLDiff) {
			((UMLDiff)ret).setDiscriminant(getDiscriminant(input));
			setEReference(input, (UMLDiff)ret);
		}
		return ret;
	}

	/**
	 * {@inheritDoc}<br>
	 * During the building process, it sets the differences refining the macroscopic one.
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#setRefiningChanges(org.eclipse.emf.compare.Diff,
	 *      org.eclipse.emf.compare.DifferenceKind, org.eclipse.emf.compare.Diff)
	 */
	@Override
	public void setRefiningChanges(Diff extension, DifferenceKind extensionKind, Diff refiningDiff) {
		HashMultimap<Object, RefiningCandidate> refiningCandidates = HashMultimap.create();

		Comparison comparison = refiningDiff.getMatch().getComparison();
		// From each discriminant business object, ...
		Set<EObject> discriminants = getDiscriminants(refiningDiff);
		for (EObject discriminant : discriminants) {
			// ... define all the business objects which may be impacted by refining differences specifying
			// the
			// settings (holding object and structural feature) which link them.
			defineRefiningCandidates(discriminant, refiningCandidates);
			// For each of these business objects, find the impacted differences, keeping only the ones
			// matching the defined settings.
			for (Object elt : refiningCandidates.keys()) {
				beRefinedByCrossReferences(comparison, elt, (UMLDiff)extension,
						new DifferencesOnRefiningCandidates(comparison, refiningCandidates));
			}
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.IChangeFactory#getParentMatch(org.eclipse.emf.compare.Diff)
	 */
	@Override
	public Match getParentMatch(Diff input) {
		return getParentMatch(input.getMatch().getComparison(), input);
	}

	/**
	 * Get the discriminant business objects concerned by the given difference.<br>
	 * 
	 * @param input
	 *            The given difference.
	 * @return The set of discriminant business objects.
	 */
	protected Set<EObject> getDiscriminants(Diff input) {
		EObject value;
		// Get the business object to focus as starting point to find all the discriminant objects.
		if (input instanceof ReferenceChange && ((ReferenceChange)input).getReference().isContainment()) {
			value = ((ReferenceChange)input).getValue();
		} else {
			value = MatchUtil.getContainer(input.getMatch().getComparison(), input);
		}
		return getDiscriminants(value);
	}

	/**
	 * Get the cross referenced object (setting) matching with given predicate, from the given business
	 * object.
	 * 
	 * @param object
	 *            The given object from which we seek a cross referenced object.
	 * @param predicate
	 *            The predicate.
	 * @return the setting containing the cross referenced object.
	 */
	protected Setting getInverseReferences(EObject object, Predicate<EStructuralFeature.Setting> predicate) {
		final Iterator<EStructuralFeature.Setting> crossReferences = UML2Util.getInverseReferences(object)
				.iterator();
		return Iterators.find(crossReferences, predicate, null);
	}

	/**
	 * {@inheritDoc}<br>
	 * The given reference change is related to a macroscopic ADD if... <br>
	 * - Its reference is a containment one<br>
	 * - Its value is the specified discriminant<br>
	 * - Its kind is ADD
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#isRelatedToAnExtensionAdd(org.eclipse.emf.compare.ReferenceChange)
	 */
	@Override
	protected boolean isRelatedToAnExtensionAdd(ReferenceChange input) {
		return input.getReference().isContainment() && input.getValue() == getDiscriminant(input)
				&& input.getKind() == DifferenceKind.ADD;
	}

	/**
	 * {@inheritDoc}<br>
	 * The given reference change is related to a macroscopic DELETE if... <br>
	 * - Its reference is a containment one<br>
	 * - Its value is the specified discriminant<br>
	 * - Its kind is DELETE
	 * 
	 * @see org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#isRelatedToAnExtensionDelete(org.eclipse.emf.compare.ReferenceChange)
	 */
	@Override
	protected boolean isRelatedToAnExtensionDelete(ReferenceChange input) {
		return input.getReference().isContainment() && input.getValue() == getDiscriminant(input)
				&& input.getKind() == DifferenceKind.DELETE;
	}

	// No UML macroscopic change anymore for any changes on discriminants.
	// /**
	// * {@inheritDoc}<br>
	// * The given reference change is related to a macroscopic CHANGE if discriminants are found from the
	// * business impacted object.
	// *
	// * @see
	// org.eclipse.emf.compare.internal.postprocessor.factories.AbstractChangeFactory#isRelatedToAnExtensionChange(org.eclipse.emf.compare.ReferenceChange)
	// */
	// @Override
	// protected boolean isRelatedToAnExtensionChange(ReferenceChange input) {
	// return !getDiscriminants(MatchUtil.getContainer(input.getMatch().getComparison(), input)).isEmpty();
	// }

	// TODO: Add isRelatedToAnExtensionChange(AttributeChange input)

	/**
	 * Get the switch which allows to return the set of discriminants from any business object.<br>
	 * 
	 * @return The switch to get the discriminants.
	 */
	protected abstract Switch<Set<EObject>> getDiscriminantsGetter();

	/**
	 * Get the main discriminant business object concerned by the given difference. Usually, it will be one of
	 * the ones returned by {@link AbstractUMLChangeFactory#getDiscriminants(Diff)}
	 * 
	 * @param input
	 *            The difference.
	 * @return The discriminant.
	 */
	protected abstract EObject getDiscriminant(Diff input);

	/**
	 * Default method in discriminant getters ({@link DiscriminantsGetter}) to find discriminants.
	 * 
	 * @param discriminantsGetter
	 *            The specific discriminant getter.
	 * @param object
	 *            The current object as starting point to the seeking of discriminants.
	 * @return The set of discriminants.
	 */
	protected static Set<EObject> defaultCaseForDiscriminantsGetter(Switch<Set<EObject>> discriminantsGetter,
			EObject object) {
		Set<EObject> result = new HashSet<EObject>();
		EObject parent = object.eContainer();
		if (parent != null) {
			result.addAll(discriminantsGetter.doSwitch(parent));
		}
		return result;
	}

	/**
	 * Get the discriminant business objects from the given one.
	 * 
	 * @param value
	 *            The given business object.
	 * @return The set of discriminants.
	 */
	private Set<EObject> getDiscriminants(EObject value) {
		Switch<Set<EObject>> discriminantGetter = getDiscriminantsGetter();
		return discriminantGetter.doSwitch(value);
	}

	/**
	 * It defines the business objects to scan and their settings to find the matching refining differences,
	 * from the given discriminant business object.
	 * 
	 * @param discriminant
	 *            The given discriminant.
	 * @param refiningCandidates
	 *            Map business object to the list of settings to keep during the research of refining
	 *            differences. This map must not be null.
	 */
	private void defineRefiningCandidates(EObject discriminant,
			HashMultimap<Object, RefiningCandidate> refiningCandidates) {
		// The discriminant itself is a candidate, only on an incoming containment reference.
		refiningCandidates.put(discriminant, new RefiningCandidate());
		// Delegation to a recursive method to find the other candidates.
		defineRefiningCandidatesFrom(discriminant, refiningCandidates);
	}

	/**
	 * It defines the business objects to scan and their settings to find the matching refining differences,
	 * from the given discriminant business object. It ignores the given object and searches candidates in all
	 * the referenced objects from it and all the attributes. A recursion is made between containment
	 * references, to scan all the children. Opposite references are taken into account for the candidates.
	 * 
	 * @param discriminant
	 *            The given discriminant.
	 * @param refiningCandidates
	 *            Map business object to the list of settings to keep during the research of refining
	 *            differences. This map must not be null.
	 */
	private void defineRefiningCandidatesFrom(EObject discriminant,
			HashMultimap<Object, RefiningCandidate> refiningCandidates) {
		Iterator<EStructuralFeature> outgoingFeatures = discriminant.eClass().getEAllStructuralFeatures()
				.iterator();
		while (outgoingFeatures.hasNext()) {
			EStructuralFeature outgoingFeature = outgoingFeatures.next();
			// For each referenced objects and attributes...
			Iterator<Object> values = ReferenceUtil.getAsList(discriminant, outgoingFeature).iterator();
			while (values.hasNext()) {
				Object value = values.next();
				// ... register it with its setting.
				refiningCandidates.put(value, new RefiningCandidate(discriminant, outgoingFeature));
				if (outgoingFeature instanceof EReference && value instanceof EObject) {
					if (((EReference)outgoingFeature).isContainment()) {
						// Recursion on children
						defineRefiningCandidatesFrom((EObject)value, refiningCandidates);
					} else if (((EReference)outgoingFeature).getEOpposite() != null) {
						// Take opposite references
						refiningCandidates.put(discriminant, new RefiningCandidate((EObject)value,
								((EReference)outgoingFeature).getEOpposite()));
					}
				}
			}

		}
	}

	/**
	 * Get the match in which the macroscopic change should be added.<br>
	 * Take the same match of the given difference. If it is related to a macroscopic CHANGE, take the match
	 * of the discriminant.
	 * 
	 * @param comparison
	 *            The current comparison.
	 * @param input
	 *            The difference to locate.
	 * @return The containing match.
	 */
	private Match getParentMatch(Comparison comparison, Diff input) {
		if (getRelatedExtensionKind(input) == DifferenceKind.CHANGE) {
			return comparison.getMatch(getDiscriminant(input));
		} else {
			return input.getMatch();
		}
	}

	/**
	 * Fill the refining link of the given refined extension (macroscopic change) with the found differences
	 * on the given object (lookup), according to the given predicate.
	 * 
	 * @param comparison
	 *            The comparison.
	 * @param lookup
	 *            The object on which differences have to be found.
	 * @param refinedExtension
	 *            The macroscopic change to set (refinedBy link)
	 * @param p
	 *            The predicate.
	 */
	private void beRefinedByCrossReferences(Comparison comparison, Object lookup, UMLDiff refinedExtension,
			Predicate<Diff> p) {
		if (lookup instanceof EObject) {
			List<Diff> crossReferences = findCrossReferences(comparison, (EObject)lookup, p);
			refinedExtension.getRefinedBy().addAll(crossReferences);
		}
	}

	/**
	 * Set the eReference link in the given UML difference, from the given unit difference.
	 * 
	 * @param input
	 *            The unit difference.
	 * @param umlDiff
	 *            The UML difference (macroscopic change).
	 */
	private void setEReference(Diff input, UMLDiff umlDiff) {
		if (getRelatedExtensionKind(input) == DifferenceKind.ADD
				|| getRelatedExtensionKind(input) == DifferenceKind.DELETE) {
			if (input instanceof ReferenceChange) {
				umlDiff.setEReference(((ReferenceChange)input).getReference());
			} else if (input instanceof ResourceAttachmentChange
					&& umlDiff instanceof StereotypeApplicationChange) {
				// the resource attachment concerns the stereotype application itself.
				// The reference is located "below" that.
				final List<Diff> candidates = input.getMatch().getDifferences();
				// Little chance that there is more is that the input ... and what we seek.
				for (Diff candidate : candidates) {
					if (candidate instanceof ReferenceChange) {
						umlDiff.setEReference(((ReferenceChange)candidate).getReference());
					}
				}
			}
		}
	}

	/**
	 * It checks if the given difference concerns is related to a macroscopic CHANGE within a macroscopic
	 * ADD/DELETE.
	 * 
	 * @param input
	 *            The difference.
	 * @return True if it is related to a CHANGE in an ADD/DELETE.
	 */
	private boolean isChangeOnAddOrDelete(Diff input) {
		if (getRelatedExtensionKind(input) == DifferenceKind.CHANGE) {
			final Comparison comparison = input.getMatch().getComparison();
			final EObject discriminant = getDiscriminant(input);
			if (discriminant != null) {
				return isChangeOnAddOrDelete(input, comparison, discriminant);
			}
		}
		return false;
	}

	/**
	 * It checks if the given difference concerns is related to a macroscopic CHANGE within a macroscopic
	 * ADD/DELETE.
	 * 
	 * @param input
	 *            The given difference.
	 * @param comparison
	 *            the related comparison.
	 * @param discriminant
	 *            The discriminant found for the given difference.
	 * @return True if it is related to a CHANGE in an ADD/DELETE.
	 */
	private boolean isChangeOnAddOrDelete(Diff input, final Comparison comparison, final EObject discriminant) {
		boolean result = false;
		if (Iterables.any(comparison.getMatch(discriminant).getDifferences(),
				instanceOf(ResourceAttachmentChange.class))) {
			result = true;
		}
		if (!result) {
			final List<Diff> candidates = comparison.getDifferences(discriminant);
			for (Diff diff : candidates) {
				if (diff == input) {
					// ignore this one
				} else {
					DifferenceKind relatedExtensionKind = getRelatedExtensionKind(diff);
					if ((relatedExtensionKind == DifferenceKind.ADD || relatedExtensionKind == DifferenceKind.DELETE)
							&& getDiscriminant(diff) == discriminant) {
						result = true;
						break;
					}
				}
			}
		}
		return result;
	}

}

Back to the top