Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 31224c33ba4e7f303b667ab1636a793f81fd2d84 (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
package org.eclipse.papyrus.infra.table.efacet.menu.handler;

import java.io.ObjectInputStream.GetField;

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.EcorePackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.facet.efacet.core.IFacetSetCatalogManager;
import org.eclipse.emf.facet.efacet.core.IFacetSetCatalogManagerFactory;
import org.eclipse.emf.facet.efacet.core.exception.FacetManagerException;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.EFacetFactory;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.Facet;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetAttribute;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetOperation;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetReference;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetSet;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.extensible.Query;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.query.EObjectLiteralQuery;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.query.OperationCallQuery;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.query.QueryFactory;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacet.query.StringLiteralQuery;
import org.eclipse.emf.facet.efacet.metamodel.v0_2_0.efacetcatalog.EfacetcatalogFactory;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.emf.facet.efacet.core.*;

public class FacetFactory {


	private FacetFactory() {
		//to prevent instanciation
	}

	public static final FacetSet createFacetSet(final String name, final String nsPrefix, final String nsURI, final String documentation) {
		FacetSet set = EFacetFactory.eINSTANCE.createFacetSet();
		set.setName(name);
		set.setNsPrefix(nsPrefix);
		set.setNsURI(nsURI);
		set.setDocumentation(documentation);
		return set;
	}

	public static final Facet createFacet(final Stereotype stereotype) {
		Facet facet = EFacetFactory.eINSTANCE.createFacet();
		facet.setName(stereotype.getName());
		facet.setDocumentation("This facet represents the eClass " + stereotype.getName());
		//		facet.setConformanceTypedElement(value);//TODO
		facet.setExtendedMetaclass(UMLPackage.eINSTANCE.getElement());//TODO
		for(final Property attribute : stereotype.getOwnedAttributes()) {
			FacetAttribute facetAttr = createFacetAttribute2(attribute, stereotype);
			facet.getFacetElements().add(facetAttr);
		}

		//We create the query to get the reprensentedStereotype
		FacetOperation operation = EFacetFactory.eINSTANCE.createFacetOperation();
		operation.setLowerBound(1);
		operation.setUpperBound(1);
		operation.setDocumentation("Returns the qualified name of the represented stereotype");
		operation.setName("getStereotypeQualifiedName");
		//		operation.setEType(); //string
		StringLiteralQuery query = QueryFactory.eINSTANCE.createStringLiteralQuery();
		query.setValue(stereotype.getQualifiedName());
		operation.setQuery(query);
		return facet;
	}

	private static FacetOperation singleStringValueOperation;
	private static FacetOperation singleStringValueOperation2;

	private static FacetOperation multiStringValueOperation;

	private static FacetOperation singlePrimitiveTypeValueOperation;

	private static FacetOperation multiPrimitiveTypeValueOperation;

	private static Facet stereotypedElementFacet;



	private static final Facet getStereotypedElementFacet(final ResourceSet resourceSet) {
		if(stereotypedElementFacet == null) {
			IFacetSetCatalogManager manager = IFacetSetCatalogManagerFactory.DEFAULT.getOrCreateFacetSetCatalogManager(resourceSet);
			for(FacetSet set : manager.getRegisteredFacetSets()) {
				if("org.eclipse.papyrus.uml.efacet/0.9/UML_stereotype".equals(set.getNsURI())) {
					stereotypedElementFacet = (Facet)set.getEClassifier("stereotyped_element");
					break;
				}
			}
		}
		return stereotypedElementFacet;
	}

	private static FacetOperation getMultiPrimitiveTypeValueOperation(final ResourceSet resourceSet) {
		if(singleStringValueOperation == null) {
			final Facet stereotypedElementFacet = getStereotypedElementFacet(resourceSet);
			if(stereotypedElementFacet != null)
				for(final FacetOperation op : stereotypedElementFacet.getFacetOperations()) {
					if("getMultiPrimitiveTypeValue".equals(op.getName())) {
						multiPrimitiveTypeValueOperation = op;
						break;
					}
				}
		}
		return multiPrimitiveTypeValueOperation;
	}

	private static FacetOperation getSinglePrimitiveTypeValueOperation(final ResourceSet resourceSet) {
		if(singleStringValueOperation == null) {
			final Facet stereotypedElementFacet = getStereotypedElementFacet(resourceSet);
			if(stereotypedElementFacet != null)
				for(final FacetOperation op : stereotypedElementFacet.getFacetOperations()) {
					if("getSinglePrimitiveTypeValue".equals(op.getName())) {
						singlePrimitiveTypeValueOperation = op;
						break;
					}
				}
		}
		return singlePrimitiveTypeValueOperation;
	}

	private static FacetOperation getSingleStringValueOperation(final ResourceSet resourceSet) {
		if(singleStringValueOperation == null) {
			final Facet stereotypedElementFacet = getStereotypedElementFacet(resourceSet);
			if(stereotypedElementFacet != null)
				for(final FacetOperation op : stereotypedElementFacet.getFacetOperations()) {
					if("getSingleStringValue".equals(op.getName())) {
						singleStringValueOperation = op;
						break;
					}
				}
		}
		return singleStringValueOperation;
	}
	
	private static FacetOperation getSingleStringValueOperation2(final ResourceSet resourceSet) {
		if(singleStringValueOperation2 == null) {
			final Facet stereotypedElementFacet = getStereotypedElementFacet(resourceSet);
			if(stereotypedElementFacet != null)
				for(final FacetOperation op : stereotypedElementFacet.getFacetOperations()) {
					if("getSingleStringValue2".equals(op.getName())) {
						singleStringValueOperation2 = op;
						break;
					}
				}
		}
		return singleStringValueOperation2;
	}

	//the first version with the qualifiedName
	public static final FacetAttribute createFacetAttribute(Property attribute2, final Stereotype ste) {
		FacetAttribute facetAttribute = EFacetFactory.eINSTANCE.createFacetAttribute();
		facetAttribute.setName(attribute2.getName());
		facetAttribute.setDocumentation("This Facet Attribute represents the stereotype property " + attribute2.getQualifiedName());
		facetAttribute.setChangeable(!attribute2.isDerived());
		facetAttribute.setDerived(true);
		Type type = attribute2.getType();
		EClassifier eType = null;
		OperationCallQuery query;
		FacetOperation facetOperationCalled = null;
		int upperBound = facetAttribute.getUpperBound();
		if(type instanceof PrimitiveType) {
			PrimitiveType pType = (PrimitiveType)type;
			final String name = pType.getName();
			if("EString".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEString();
				if(upperBound > 1) {
					//TODO
				} else {
					facetOperationCalled = getSingleStringValueOperation(attribute2.eResource().getResourceSet());
				}
			} else if("EBoolean".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEBoolean();
			} else if("EInt".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEInt();
			}
			//else TODO to finish





			//we prepare the arguments
			query = QueryFactory.eINSTANCE.createOperationCallQuery();
			query.setCanHaveSideEffects(false);

			StringLiteralQuery qualifiedNameQuery = QueryFactory.eINSTANCE.createStringLiteralQuery();
			qualifiedNameQuery.setValue(ste.getQualifiedName());
			query.getArguments().add(qualifiedNameQuery);

			StringLiteralQuery propertyNameQuery = QueryFactory.eINSTANCE.createStringLiteralQuery();
			propertyNameQuery.setValue(attribute2.getName());
			query.getArguments().add(propertyNameQuery);
			facetAttribute.setQuery(query);

			query.setOperation(facetOperationCalled);

		}
		facetAttribute.setEType(eType);//TODO
		//		attribute.setQuery(query);////TODO
		facetAttribute.setLowerBound(attribute2.getLower());
		facetAttribute.setUpperBound(attribute2.getUpper());
		return facetAttribute;
	}
	
	//the second version with references to property and stereitype
	public static final FacetAttribute createFacetAttribute2(Property attribute2, final Stereotype ste) {
		FacetAttribute facetAttribute = EFacetFactory.eINSTANCE.createFacetAttribute();
		facetAttribute.setName(attribute2.getName());
		facetAttribute.setDocumentation("This Facet Attribute represents the stereotype property " + attribute2.getQualifiedName());
		facetAttribute.setChangeable(!attribute2.isDerived());
		facetAttribute.setDerived(true);
		Type type = attribute2.getType();
		EClassifier eType = null;
		OperationCallQuery query;
		FacetOperation facetOperationCalled = null;
		int upperBound = facetAttribute.getUpperBound();
		if(type instanceof PrimitiveType) {
			PrimitiveType pType = (PrimitiveType)type;
			final String name = pType.getName();
			if("EString".equals(name) || "String".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEString();
				if(upperBound > 1) {
					//TODO
				} else {
					facetOperationCalled = getSingleStringValueOperation2(attribute2.eResource().getResourceSet());
				}
			} else if("EBoolean".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEBoolean();
			} else if("EInt".equals(name)) {
				eType = EcorePackage.eINSTANCE.getEInt();
			}
			//else TODO to finish





			//we prepare the arguments
			query = QueryFactory.eINSTANCE.createOperationCallQuery();
			query.setCanHaveSideEffects(false);

			EObjectLiteralQuery qualifiedNameQuery = QueryFactory.eINSTANCE.createEObjectLiteralQuery();
			qualifiedNameQuery.setElement(ste);
			query.getArguments().add(qualifiedNameQuery);

			EObjectLiteralQuery propertyNameQuery = QueryFactory.eINSTANCE.createEObjectLiteralQuery();
			propertyNameQuery.setElement(attribute2);
			query.getArguments().add(propertyNameQuery);
			facetAttribute.setQuery(query);

			query.setOperation(facetOperationCalled);

		}
		facetAttribute.setEType(eType);//TODO
		//		attribute.setQuery(query);////TODO
		facetAttribute.setLowerBound(attribute2.getLower());
		facetAttribute.setUpperBound(attribute2.getUpper());
		return facetAttribute;
	}

	public static final FacetReference createFacetReference() {
		FacetReference reference = EFacetFactory.eINSTANCE.createFacetReference();
		return reference;
	}
}

Back to the top