Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ec8b8879af716891188576023405ef4f4d7f1e3f (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
package org.eclipse.emf.edapt.declaration.delegation;

import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.edapt.declaration.EdaptConstraint;
import org.eclipse.emf.edapt.declaration.EdaptOperation;
import org.eclipse.emf.edapt.declaration.EdaptParameter;
import org.eclipse.emf.edapt.declaration.OperationImplementation;
import org.eclipse.emf.edapt.spi.migration.Instance;
import org.eclipse.emf.edapt.spi.migration.Metamodel;
import org.eclipse.emf.edapt.spi.migration.Model;

/**
 * {@description}
 *
 * @author herrmama
 * @author $Author$
 * @version $Rev$
 * @levd.rating YELLOW Hash: 3786A9B7666DDCE8867BE87A43066803
 */
@EdaptOperation(identifier = "combineFeature", label = "Combine Features over References", description = "In the metamodel, a number of features are combined in to a single feature by moving it over references to the same class. In the model, the values of the features are moved accordingly.")
public class CombineFeature extends OperationImplementation {

	/** {@description} */
	@EdaptParameter(main = true, description = "The features to be combined")
	public List<EStructuralFeature> features;

	/** {@description} */
	@EdaptParameter(description = "The references over which the features are moved (in the same order)")
	public List<EReference> references;

	/** {@description} */
	@EdaptConstraint(description = "All references must have the same class as type")
	public boolean checkReferenceSameType() {
		return hasSameValue(references, EcorePackage.eINSTANCE.getETypedElement_EType());
	}

	/** {@description} */
	@EdaptConstraint(description = "There must be an equal number of features and references")
	public boolean checkFeatureSize() {
		return features.size() == references.size();
	}

	/** {@description} */
	@EdaptConstraint(description = "Each feature has to belong to its reference's class")
	public boolean checkFeatureParent() {
		for (final EReference reference : references) {
			if (reference.getEContainingClass() != features.get(
				references.indexOf(reference)).getEContainingClass()) {
				return false;
			}
		}
		return true;
	}

	/** {@inheritDoc} */
	@Override
	public void execute(Metamodel metamodel, Model model) {
		final EClass eClass = references.get(0).getEReferenceType();
		final EStructuralFeature mainFeature = features.get(0);

		// metamodel adaptation
		eClass.getEStructuralFeatures().add(mainFeature);
		for (final EStructuralFeature feature : features) {
			if (feature != mainFeature) {
				metamodel.delete(feature);
			}
		}

		// model migration
		for (int i = 0; i < references.size(); i++) {
			final EReference reference = references.get(i);
			final EStructuralFeature feature = features.get(i);
			for (final Instance instance : model.getAllInstances(reference
				.getEContainingClass())) {
				final Object value = instance.unset(feature);
				final Instance ref = instance.get(reference);
				if (ref != null) {
					ref.set(mainFeature, value);
				}
			}
		}
	}
}

Back to the top