Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05166fceef2928f6493d5eae0d5f583136de4ea0 (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
/*******************************************************************************
 * Copyright (c) 2012 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.equi;

import java.util.Set;

import org.eclipse.emf.compare.CompareFactory;
import org.eclipse.emf.compare.ComparePackage;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.Equivalence;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.utils.ReferenceUtil;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * The requirements engine is in charge of actually computing the equivalences 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.
 * 
 * @author <a href="mailto:cedric.notot@obeo.fr">Cedric Notot</a>
 */
public class DefaultEquiEngine implements IEquiEngine {

	/**
	 * Cross referencer which links business model objects to the related differences.
	 */
	private EcoreUtil.CrossReferencer crossReferencerModelObjectsToDiffs;

	/**
	 * Constructor.
	 */
	public DefaultEquiEngine() {
	}

	/**
	 * Constructor with a cross referencer.
	 * 
	 * @param crossReferencer
	 *            The cross referencer.
	 */
	public DefaultEquiEngine(EcoreUtil.CrossReferencer crossReferencer) {
		this.crossReferencerModelObjectsToDiffs = crossReferencer;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.compare.equi.IEquiEngine#computeEquivalences(org.eclipse.emf.compare.Comparison)
	 */
	public void computeEquivalences(Comparison comparison) {
		if (crossReferencerModelObjectsToDiffs == null) {
			crossReferencerModelObjectsToDiffs = ReferenceUtil.initializeCrossReferencer(comparison);
		}

		for (Diff difference : comparison.getDifferences()) {
			checkForEquivalences(comparison, difference);
		}
	}

	/**
	 * Checks the potential equivalence 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 checkForEquivalences(Comparison comparison, Diff difference) {
		if (difference instanceof ReferenceChange) {
			ReferenceChange diff = (ReferenceChange)difference;
			if (diff.getReference().getEOpposite() != null) {
				Equivalence equivalence = diff.getEquivalence();
				if (equivalence == null) {
					equivalence = CompareFactory.eINSTANCE.createEquivalence();
					comparison.getEquivalences().add(equivalence);
					equivalence.getDifferences().add(difference);
					// Find the opposite difference to reference it from the equivalence:
					EReference oppositeReference = diff.getReference().getEOpposite();
					Set<Diff> equivalentDifferences = ReferenceUtil.getCrossReferences(
							crossReferencerModelObjectsToDiffs, oppositeReference, ComparePackage.eINSTANCE
									.getReferenceChange_Reference(), Diff.class);
					for (Diff eqDiff : equivalentDifferences) {
						equivalence.getDifferences().add(eqDiff);
					}
				}
			}
		}
	}

}

Back to the top