Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 226b2b7b52a6a742cd41b1b284297b1903b393e9 (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.compare.diff.check;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.diff.engine.IMatchManager;
import org.eclipse.emf.compare.diff.engine.check.ReferencesCheck;
import org.eclipse.emf.compare.diff.metamodel.DiffGroup;
import org.eclipse.emf.compare.match.metamodel.Match2Elements;
import org.eclipse.emf.compare.match.metamodel.Match3Elements;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;

/**
 * 
 * This reference checker allows to ignore differences on features for the comparison
 * 
 */
public class PapyrusReferencesCheck extends ReferencesCheck {

	/**
	 * the checker used to take in account the ignored features for the Diff
	 */
	private final FeaturesCheck checker;

	/**
	 * 
	 * Constructor.
	 * 
	 * @param matchManager
	 * @param checker
	 *        the checker used to take in account the ignored features for the Diff
	 */
	public PapyrusReferencesCheck(final IMatchManager matchManager, final FeaturesCheck checker) {
		super(matchManager);
		this.checker = checker;
	}

	/**
	 * 
	 * @see org.eclipse.emf.compare.diff.engine.check.ReferencesCheck#shouldBeIgnored(org.eclipse.emf.ecore.EReference)
	 * 
	 * @param reference
	 * @return
	 */
	@Override
	protected boolean shouldBeIgnored(final EReference reference) {
		boolean shouldBeIgnored = super.shouldBeIgnored(reference);
		return shouldBeIgnored || this.checker.shouldBeIgnored(reference);
	}

	/**
	 * return <code>true</code> if the attribute should be ignored in this context
	 * 
	 * @param reference
	 *        a reference
	 * @param context
	 *        the context of this reference
	 * @return
	 */
	protected boolean shouldBeIgnored(final EReference reference, final EObject context) {
		return this.checker.shouldBeIgnored(reference, context);
	}

	/**
	 * Checks if there's been references updates in the model.<br/>
	 * <p>
	 * A reference is considered updated if its value(s) has been changed (either removal or addition of an element if the reference is multi-valued
	 * or update of a single-valued reference) between the left and the right model.
	 * </p>
	 * 
	 * @param root
	 *        {@link DiffGroup root} of the {@link DiffElement} to create.
	 * @param mapping
	 *        Contains informations about the left and right model elements we have to compare.
	 * @throws FactoryException
	 *         Thrown if we cannot fetch the references' values.
	 */
	public void checkReferencesUpdates(DiffGroup root, Match2Elements mapping) throws FactoryException {
		final EClass eClass = mapping.getLeftElement().eClass();
		final List<EReference> eclassReferences = eClass.getEAllReferences();

		final Iterator<EReference> it = eclassReferences.iterator();
		while(it.hasNext()) {
			final EReference next = it.next();
			if(!shouldBeIgnored(next, mapping.getLeftElement())) {
				if(!shouldBeIgnored(next)) {
					checkReferenceUpdates(root, mapping, next);
				} else if(next.isContainment() && next.isOrdered()) {
					checkContainmentReferenceOrderChange(root, mapping, next);
				}
			}
		}
	}

	/**
	 * Checks if there's been references updates in the model.<br/>
	 * <p>
	 * A reference is considered updated if its value(s) has been changed (either removal or addition of an element if the reference is multi-valued
	 * or update of a single-valued reference) between the left and the ancestor model, the right and the ancestor or between the left and the right
	 * model.
	 * </p>
	 * 
	 * @param root
	 *        {@link DiffGroup root} of the {@link DiffElement} to create.
	 * @param mapping
	 *        Contains informations about the left, right and origin model elements we have to compare.
	 * @throws FactoryException
	 *         Thrown if we cannot fetch the references' values.
	 */
	public void checkReferencesUpdates(DiffGroup root, Match3Elements mapping) throws FactoryException {
		// Ignores matchElements when they don't have origin (no updates on these)
		if(mapping.getOriginElement() == null)
			return;
		final EClass eClass = mapping.getOriginElement().eClass();
		final List<EReference> eclassReferences = eClass.getEAllReferences();

		final Iterator<EReference> it = eclassReferences.iterator();
		while(it.hasNext()) {
			final EReference next = it.next();
			if(!shouldBeIgnored(next, mapping.getLeftElement())) {
				if(!shouldBeIgnored(next)) {
					checkReferenceUpdates(root, mapping, next);
				} else if(next.isContainment() && next.isOrdered()) {
					checkContainmentReferenceOrderChange(root, mapping, next);
				}
			}
		}
	}


}

Back to the top