Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ebb7a74c4e1ddbb6ee335eea0a48f57b6149e06 (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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.profile.definition;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.papyrus.uml.tools.Activator;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;


/**
 * <p>
 * this class is used to manage the redefinition of profiles:
 * </p>
 * The normal definition in UML is like this:
 * <ul>
 * <li>Stereotype-->Eclass</li>
 * <li>Enumeration-->EEnumeration :local copy
 * <li>Datatype-->EClass</li>
 * <li>Property--> EReference or EAttribute with ref on local copy</li>
 * <li>PrimitiveType-> Edatatype : local copy</li>
 * </ul>
 * In papyrus:
 * <ul>
 * <li>Stereotype-->Eclass
 * <li>Enumeration-->EEnumeration:local copy
 * <li>Datatype-->EClass
 * <li>Property--> EReference or EAttribute with ref on direct definition
 * <li>PrimitiveType-> Edatatype : local copy
 * </ul>
 */
public class ProfileRedefinition {

	/**
	 * this method is used to redefine profile even if it contains subpackages
	 * or subprofiles
	 *
	 * @param thepackage
	 *            the given profile that we want to define
	 * @param definitionAnnotation
	 *            the definition annotation that is used to create the version
	 *            annotation
	 */
	public static void redefineProfile(Package thepackage, PapyrusDefinitionAnnotation definitionAnnotation) {
		// he wants to define
		if (thepackage instanceof Profile) {
			Profile profile = (Profile) thepackage;
			// get profile definition
			EPackage profileDefinition = profile.getDefinition();
			// collect all EClassifier of the definition
			ArrayList<EClassifier> tempList = new ArrayList<EClassifier>();
			for (int i = 0; i < profileDefinition.getEClassifiers().size(); i++) {
				tempList.add(profileDefinition.getEClassifiers().get(i));
			}

			// for all EClass
			Iterator<EClassifier> eClassIter = tempList.iterator();
			while (eClassIter.hasNext()) {
				EClassifier eclassifier = eClassIter.next();
				if (eclassifier instanceof EClass) {
					// redefine Eclass
					redefineEclass((EClass) eclassifier);
				}
			}

			// add profile definition annotation
			if (definitionAnnotation != null) {
				profile.getDefinition().getEAnnotations().add(definitionAnnotation.convertToEAnnotation());
			}
		}
		Iterator<Package> it = thepackage.getNestedPackages().iterator();
		while (it.hasNext()) {
			Package p = it.next();
			ProfileRedefinition.redefineProfile(p, definitionAnnotation);
		}
	}

	/**
	 * redefine only real definition or do nothing
	 *
	 * @param eclass
	 *            the given eclass that we want to redefine
	 */
	public static void redefineEclass(EClass eclass) {
		if (isADirectDefinition(eclass)) {
			// 1. redefine inheritances
			EList<EClass> eSuperTypes = eclass.getESuperTypes();

			/* copy in order to avoid concurrent access */
			ArrayList<EClass> superTypesList = new ArrayList<EClass>();
			for (int j = 0; j < eSuperTypes.size(); j++) {
				superTypesList.add(eSuperTypes.get(j));
			}
			// for each super types :we test if this is a direct definition
			// if not we remove the local copy and replace by direct definition
			Iterator<EClass> superIter = superTypesList.iterator();
			while (superIter.hasNext()) {
				EClass currentSuperClass = superIter.next();
				if (!isADirectDefinition(currentSuperClass)) {
					EClass directSuperClass = (EClass) lookForDirectDefinitionFrom(currentSuperClass);
					eclass.getESuperTypes().remove(currentSuperClass);
					eclass.getESuperTypes().add(directSuperClass);
				}
			}
			// 2.redefine eReferences
			// temp copy of the list
			Iterator<EReference> iterReference = eclass.getEReferences().iterator();
			ArrayList<EReference> referenceList = new ArrayList<EReference>();
			while (iterReference.hasNext()) {
				referenceList.add(iterReference.next());
			}
			// for each reference of the EClass
			Iterator<EReference> refIterator = referenceList.iterator();
			while (refIterator.hasNext()) {
				redefineEReference(refIterator.next(), eclass.getEPackage());
			}

		}
	}

	/**
	 * this class is used to redefine EReference with direct definition
	 *
	 * @param eReference
	 *            the given EReference that we want to redefine
	 */
	public static void redefineEReference(EReference eReference, EPackage profileDefinition) {
		EReference oldEOpposite = eReference.getEOpposite();
		EClassifier oldType = eReference.getEType();

		// 2.1 the type is an EClass
		if (oldType instanceof EClass) {
			// redefine type
			eReference.setEType(lookForDirectDefinitionFrom(oldType));
			// redefine the Opposite
			if (oldEOpposite != null) {
				eReference.setEOpposite(lookForEquivalentEreference((EClass) eReference.getEType(), oldEOpposite));
			}
		}
	}


	/**
	 * return id this Eclass is the real Definition
	 *
	 * @param eclass
	 *            the eclass that we want to test
	 * @return true if this is the real definition or not is this is a local
	 *         copy
	 */
	public static boolean isADirectDefinition(EClass eclass) {
		if (eclass.getEAnnotations().size() > 0) {
			EAnnotation eAnnotation = eclass.getEAnnotations().get(0);
			if (eAnnotation.getReferences().size() > 0) {
				if (!(eAnnotation.getReferences().get(0) instanceof org.eclipse.uml2.uml.Classifier)) {
					String errMsg = "Problem because of the definition of " + eclass.getName() + " in " + eclass.getEPackage().getNsURI();
					Activator.log.error(errMsg, null);
				}
				org.eclipse.uml2.uml.Classifier theClassifier = (org.eclipse.uml2.uml.Classifier) eAnnotation.getReferences().get(0);
				Package nearestPackage = theClassifier.getNearestPackage();

				if (nearestPackage instanceof Profile) {
					if (eclass.equals(((Profile) nearestPackage).getDefinition(theClassifier))) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * look for the real definition of the stereotype where the EClass may be
	 * the definition
	 *
	 * @param eclass
	 *            that maybe the real definition (maybe a local copy of
	 *            definition)
	 * @return the real definition or the itself
	 */
	public static EClassifier lookForDirectDefinitionFrom(EClassifier eClassifier) {
		if (eClassifier.getEAnnotations().size() > 0) {
			EAnnotation eAnnotation = eClassifier.getEAnnotations().get(0);
			if (eAnnotation.getReferences().size() > 0) {
				org.eclipse.uml2.uml.Classifier theClassifier = (org.eclipse.uml2.uml.Classifier) eAnnotation.getReferences().get(0);
				Package nearestPackage = theClassifier.getNearestPackage();
				if (nearestPackage instanceof Profile) {
					return (EClassifier) ((Profile) nearestPackage).getDefinition(theClassifier);
				}
				return eClassifier;
			}
		}
		return eClassifier;
	}

	/**
	 * this method is used to look for an EReference equivalent to a given
	 * EReference same name and type
	 *
	 * @param eclass
	 *            the given EClass where we look for the equivalent EReference
	 * @param eReference
	 *            the given EReference
	 * @return the return EReference or null
	 */
	private static EReference lookForEquivalentEreference(EClass eclass, EReference eReference) {
		Iterator<EReference> refIterator = eclass.getEReferences().iterator();
		while (refIterator.hasNext()) {
			EReference currentEReference = refIterator.next();
			if (currentEReference.getName().equals(eReference.getName())) {
				if (currentEReference.getEType().getName().endsWith(eReference.getEType().getName())) {
					return currentEReference;
				}
			}
		}
		return null;
	}

	/**
	 * this method is used to suppress all local copy of EClass in each Profile.
	 *
	 * @param thePackage
	 *            that we want to clean
	 */
	public static void cleanProfile(Package thePackage) {
		if (thePackage instanceof Profile) {
			Profile profile = (Profile) thePackage;
			// get profile definition
			EPackage profileDefinition = profile.getDefinition();

			// collect all EClassifier of the definition
			ArrayList<EClassifier> tempList = new ArrayList<EClassifier>();
			for (int i = 0; i < profileDefinition.getEClassifiers().size(); i++) {
				tempList.add(profileDefinition.getEClassifiers().get(i));
			}

			// for all EClass
			Iterator<EClassifier> eClassIter = tempList.iterator();
			while (eClassIter.hasNext()) {
				EClassifier eclassifier = eClassIter.next();

				if (eclassifier instanceof EClass) {

					// this is a direct Definition?
					if (!isADirectDefinition((EClass) eclassifier)) {
						// no, so it is removed
						profileDefinition.getEClassifiers().remove(eclassifier);
					}

				}
			}
		}
		Iterator<Package> it = thePackage.getNestedPackages().iterator();
		while (it.hasNext()) {
			Package p = it.next();
			ProfileRedefinition.cleanProfile(p);
		}
	}


	/**
	 * this method is used to created an EAttribute from an Ereference
	 *
	 * @param container
	 *            the Eclass that will contain the eattribute
	 * @param eReference
	 *            from this, the eattribute will be created
	 * @return the created Eattribute
	 */
	public static EAttribute createEAttribute(EClass container, EReference eReference) {

		EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
		eAttribute.setEType(eReference.getEType());
		eAttribute.setName(eReference.getName());
		eAttribute.setChangeable(eReference.isChangeable());
		eAttribute.setLowerBound(eReference.getLowerBound());
		eAttribute.setUpperBound(eReference.getUpperBound());
		eAttribute.setOrdered(eReference.isOrdered());
		eAttribute.setDerived(eReference.isDerived());
		eAttribute.setTransient(eReference.isTransient());
		eAttribute.setUnique(eReference.isUnique());
		eAttribute.setUnsettable(eReference.isUnsettable());
		eAttribute.setVolatile(eReference.isVolatile());
		container.getEStructuralFeatures().add(eAttribute);
		return eAttribute;
	}



	/**
	 * this method is used to obtain the classifier from its definition
	 *
	 * @param eclass
	 *            that is a definition
	 * @return the classifier that produce this definition
	 */
	public static Classifier getUMLClassifierFromDefinition(EClassifier eclass) {
		if (eclass.getEAnnotations().size() > 0) {
			EAnnotation eAnnotation = eclass.getEAnnotations().get(0);
			if (eAnnotation.getReferences().size() > 0) {
				org.eclipse.uml2.uml.Classifier theClassifier = (org.eclipse.uml2.uml.Classifier) eAnnotation.getReferences().get(0);
				return theClassifier;
			}
		}
		return null;
	}
}

Back to the top