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
|
/*****************************************************************************
* Copyright (c) 2014 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:
* CEA LIST - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.dev.types.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.uml2.uml.UMLPackage;
public class GenerateElementTypesConfigurationsUtils {
static public Collection<EReference> findAmbiguousContainments(EReference eReference, Collection<EReference> possibleContainmentsEReference) {
HashSet<EReference> result = new HashSet<EReference>();
for (EReference ownerContainment : eReference.getEContainingClass().getEAllContainments()) {
for (EReference possibleContainement : possibleContainmentsEReference) {
if (eReference != possibleContainement) {
if (ownerContainment.equals(possibleContainement)) {
result.add(possibleContainement);
}
}
}
}
return result;
}
static public Collection<EReference> findPossibleContainmentsEReference(EClass ownedEClass, Collection<EClass> ownerTypes) {
HashSet<EReference> result = new HashSet<EReference>();
for (EClass candidateOwner : ownerTypes) {
for (EReference eReference : candidateOwner.getEAllContainments()) {
if (isKindOf(ownedEClass, eReference.getEReferenceType())) {
result.add(eReference);
}
}
}
return result;
}
static public boolean isKindOf(EClass a, EClassifier b) {
if (a.getEAllSuperTypes().contains(b)) {
return true;
} else {
return a.equals(b);
}
}
static public Collection<EClass> getAllEClass(EPackage ePackage) {
List<EClass> eClasses = new ArrayList<EClass>();
for (EClassifier eClassifier : ePackage.getEClassifiers()) {
if (eClassifier instanceof EClass) {
eClasses.add((EClass) eClassifier);
}
}
Collections.sort(eClasses, new EClassComparator());
return eClasses;
}
static public String getIdentifier(EClass eClass) {
return "org.eclipse.papyrus.uml." + eClass.getName();
}
static public String getAsName(EReference containmentEReference, EClass container) {
return "_As_" + ((ENamedElement) container.eContainer()).getName().toUpperCase() + "::" + container.getName() + "::" + containmentEReference.getName();
}
static public boolean isSpecializedASpecialization(EClass eClass, EReference containmentEReference) {
Collection<EReference> possibleContainmentsEReference = findPossibleContainmentsEReference(eClass, getAllEClass(UMLPackage.eINSTANCE));
if (!findAmbiguousContainments(containmentEReference, possibleContainmentsEReference).isEmpty()) {
// The SpecialiazedType is SpecializationType
return true;
} else {
// The SpecialiazedType is MetamodelType
return false;
}
}
static public String findSpecializedTypesIDs(EClass eClass, EReference containmentEReference) {
if (isSpecializedASpecialization(eClass, containmentEReference)) {
// The SpecialiazedType is SpecializationType
return GenerateElementTypesConfigurationsUtils.getIdentifier(eClass) + GenerateElementTypesConfigurationsUtils.getAsName(containmentEReference, eClass);
} else {
// The SpecialiazedType is MetamodelType
return getIdentifier(eClass);
}
}
}
|