Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 00cfdf7a4783a250ebbb43028a79a7eec2586252 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Ansgar Radermacher  ansgar.radermacher@cea.fr
 *
 *****************************************************************************/

package org.eclipse.papyrus.qompass.designer.core.transformations;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.FCM.ContainerRule;
import org.eclipse.papyrus.FCM.InterceptionRule;
import org.eclipse.papyrus.FCM.Template;
import org.eclipse.papyrus.qompass.designer.core.Messages;
import org.eclipse.papyrus.qompass.designer.core.StUtils;
import org.eclipse.papyrus.qompass.designer.core.deployment.DepCreation;
import org.eclipse.papyrus.qompass.designer.core.deployment.DepUtils;
import org.eclipse.papyrus.qompass.designer.core.extensions.InstanceConfigurator;
import org.eclipse.papyrus.qompass.designer.core.templates.TemplateInstantiation;
import org.eclipse.papyrus.qompass.designer.core.templates.TemplateUtils;
import org.eclipse.papyrus.qompass.designer.core.templates.TextTemplateBinding;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateSignature;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * A light-weight container transformation. Unlike the standard component-based transformation, this trafo does add a new class.
 * It is target for standard object-oriented architecture, enabling in particular to intercept the methods of a class (instead of
 * adding interceptors to ports).
 *
 * @author ansgar
 *
 */
public class LWContainerTrafo extends AbstractContainerTrafo {

	public final String interceptor =
			"[import org::eclipse::papyrus::qompass::designer::core::acceleo::utils_cpp/]\n" + //$NON-NLS-1$
					"[template public dummy(operation : Operation)]\n" + //$NON-NLS-1$
					"[returnCppCall()/];\n" + //$NON-NLS-1$
					"[/template]\n"; //$NON-NLS-1$

	public final String origOpPrefix = "orig_"; //$NON-NLS-1$

	/**
	 * Constructor
	 *
	 * @param sat
	 *            information about source and target model
	 * @param tmCDP
	 *            deployment plan within target model
	 */
	public LWContainerTrafo(LazyCopier copy, Package tmCDP) {
		this.copier = copy;
		this.tmCDP = tmCDP;
		interceptionOpMap = new HashMap<Operation, Operation>();
	}

	/**
	 * creates the executor. Needs to be called *before* the other operations of
	 * this class.
	 *
	 * @param tmComponent
	 *            the implementation of a component
	 *
	 * @throws TransformationException
	 */
	@Override
	public void createContainer(Class smClass, Class tmClass) throws TransformationException {

		// for non-components: create a delegation operation for all operations that are provided by a class (excluding those derived by ports)
		// Calls on model level use CallOperationAction, will point to existing operation, unless changed.
		// If deployed dynamically (no static component deployment), need to change factories as well.
		// Pragmatic: rename/add existing operations (as Accord has done), do some renaming and clever model handling (assure
		// that code referencing classes via name automatically uses the new class.
		// => container with name of existing class, rename existing class.
		// possible: move operations into container, existing class gets copy (and update behaviors)

		// TODO: need something simple ...
		// requirements:
		// - existing creation operations create container (separation activity CreateAction: needs to change.)
		// trivially in this case case, since container is no separate entity.
		// - => references could be exchanged during copy operation with a suitable copyFilter (=> container transfo becomes a copy filter)
		// [in case of ports: quite difficult to handle: if port belongs to abstract components, it may be inherited by multiple components that
		// might or might-not have a container => only some references need to be changed]
		// => clarify, how container handles super-classes, i.e. if it inherits ports as well (from a container of the abstract component) or not (not trivial at all!)
		// TODO: don't copy derived operations

		this.tmClass = tmClass;
		// create a copy of all operations
		operations = new BasicEList<Operation>(smClass.getAllOperations());
	}

	public void createInstance() {
	}

	/**
	 * original operation => operation gets interception prefix
	 *
	 * Objectives: existing call operations call interception operation. Existing operations include CallOperationActions as well as calls within
	 * opaque behavior
	 * This is assured by keeping same model reference for the operation, but associating a new interception behavior, existing behavior will be
	 * associated
	 * with a new renamed operation
	 *
	 * @param operation
	 */
	protected Operation createInterceptionOperation(Operation operation) throws TransformationException {
		// create copy of operation
		Operation copiedOperation = EcoreUtil.copy(operation);
		tmClass.getOwnedOperations().add(copiedOperation);
		StUtils.copyStereotypes(operation, copiedOperation);

		copiedOperation.setName(origOpPrefix + operation.getName());

		// create interception code
		// TODO: this is Java/C++ specific!
		OpaqueBehavior b = (OpaqueBehavior) tmClass.createOwnedBehavior(operation.getName(), UMLPackage.eINSTANCE.getOpaqueBehavior());
		String body = TextTemplateBinding.bind(interceptor, copiedOperation);
		// TODO: solution is specific to C++
		b.getLanguages().add("C/C++"); //$NON-NLS-1$
		b.getBodies().add(body);
		// copy existing methods into new operation, copy method list,
		// since adding the method to copied operation will remove these from original operation)
		EList<Behavior> methods = new BasicEList<Behavior>(operation.getMethods());
		for (Behavior behavior : methods) {
			copiedOperation.getMethods().add(behavior);
		}
		// new behavior is associated with existing call.
		b.setSpecification(operation);
		return copiedOperation;
	}

	/**
	 * return the reference of the created container class
	 *
	 * @return
	 */
	public Class getContainer() {
		return tmClass;
	}

	/**
	 * apply a container rule, i.e. add either a container extension or an
	 * interceptor to the container.
	 *
	 * @param smContainerRule
	 *            An container rule
	 * @param smComponent
	 *            the application component in the source model
	 * @param tmComponent
	 *            the application component in the target model
	 * @param tmIS
	 *            the instance specification for the application component in the target model
	 * @throws TransformationException
	 */
	@Override
	public void applyRule(ContainerRule smContainerRule, Class smComponent, Class tmComponent)
			throws TransformationException
	{
		// dependencies of the rule become dependencies of he class. These dependencies must be instantiated
		for (Dependency dependency : smContainerRule.getBase_Class().getClientDependencies()) {
			//
			for (Element target : dependency.getTargets()) {
				// target may, or may not be in a template
				if (target instanceof Classifier) {
					Classifier targetCl = (Classifier) target;
					Class extClass = expandAggregationDep((Class) targetCl, smComponent);
					tmComponent.createDependency(extClass);
					// if(TemplateUtils.getSignature(targetCl) != null) {
					// }
				}
			}
		}

		// execute after expansion before, since the TransformationContext.templateBinding variable remains set
		// to the same values (TODO: calculate it properly)
		for (Operation interceptionOperation : smContainerRule.getBase_Class().getAllOperations()) {
			expandInterceptorExtension(smContainerRule, interceptionOperation);
		}

		for (Property part : smContainerRule.getBase_Class().getAllAttributes()) {
			Type type = part.getType();
			if (type == null) {
				String ruleName = (smContainerRule.getBase_Class() != null) ? smContainerRule.getBase_Class().getName() : "undefined"; //$NON-NLS-1$
				throw new TransformationException(String.format(Messages.LWContainerTrafo_CannotApplyRule, ruleName));
			}
			if (part instanceof Port) {
				Port newPort = tmClass.createOwnedPort(part.getName(), part.getType());
				StUtils.copyStereotypes(part, newPort);
			}
			else if (type instanceof Class) {
				Class extOrInterceptor = (Class) type;
				// DepUtils.chooseImplementation((Class) type,
				// new BasicEList<InstanceSpecification>(), false);

				if (StereotypeUtil.isApplied(part, InterceptionRule.class)) {
					// port.filter
					//
				}
				else {
					Property extensionPart =
							expandAggregationExtension(part.getName(), extOrInterceptor, tmComponent);
					// register relation to facilitate connector copy
					copier.setPackageTemplate(smContainerRule.getBase_Class(), tmClass);
					copier.put(part, extensionPart);
					copier.setPackageTemplate(null, null);
				}
			}
			else {
				Property newAttribute = EcoreUtil.copy(part);
				tmClass.getOwnedAttributes().add(newAttribute);
			}
		}

		// tell copy that tmcontainerImpl is associated with the smContainerRule
		// register a package template (although it is not a template) to assure that the connectors
		// get copied, although they are in a different resource (only the connectors are copied, not
		// the types of the referenced parts).
		// [main issue here: properties of container rule are not copies in the sense of identical
		// copies]

		// copy.setPackageTemplate(smContainerRule.getBase_Class(), tmClass);
		// copy.setPackageTemplate(null, null);
	}

	/**
	 * This container expansion does not create a new composite. Instead, it
	 * adds the extension as a part to the copied application component. The
	 * application component also inherits from the type of the container
	 * extension in order to avoid copying ports.
	 */
	Property expandAggregationExtension(String name, Class smContainerExtImpl, Class tmComponent) throws TransformationException {
		Class tmContainerExtImpl = expandAggregationDep(smContainerExtImpl, tmComponent);

		// add part associated with the extension to the container
		Property extensionPart = tmClass.createOwnedAttribute(name, tmContainerExtImpl);

		// problem: would not be unique in case of multiple extensions
		// Copy.copyID(tmComponent, extensionPart, "a");
		extensionPart.setIsComposite(true);

		return extensionPart;
	}

	Class expandAggregationDep(Class smContainerExtImpl, Class tmComponent) throws TransformationException {
		Class tmContainerExtImpl = null;

		TemplateSignature signature = TemplateUtils.getSignature(smContainerExtImpl);
		if (signature == null) {
			// no template signature, just copy the container extension into the target model
			tmContainerExtImpl = copier.getCopy(smContainerExtImpl);
		} else {
			// template signature found, instantiate container extension via the
			// template binding mechanism
			TemplateBinding binding = TemplateUtils.fixedBinding(copier.target, smContainerExtImpl, tmComponent);
			Object[] args = new Object[] {};
			TemplateInstantiation ti = new TemplateInstantiation(copier, binding, args);
			tmContainerExtImpl = ti.bindNamedElement(smContainerExtImpl);
		}

		return tmContainerExtImpl;
	}

	/**
	 * Expand an interceptor definition between the container and the executor.
	 *
	 * @throws TransformationException
	 */
	EList<Property> expandInterceptorExtension(ContainerRule smContainerRule, Operation interceptionOperationInRule)
			throws TransformationException
	{
		for (Operation smOperation : operations) {
			Operation tmOperation = copier.getCopy(smOperation);
			String interceptionBody = ""; //$NON-NLS-1$
			for (Behavior behavior : interceptionOperationInRule.getMethods()) {
				if (behavior instanceof OpaqueBehavior) {
					EList<String> bodies = ((OpaqueBehavior) behavior).getBodies();
					if (bodies.size() > 0) {
						// always take first
						interceptionBody = bodies.get(0);
					}
				}
			}
			if (StereotypeUtil.isApplied(interceptionOperationInRule, Template.class)) {
				// pass operation in source model, since this enables acceleo code to check
				// for markers on model
				interceptionBody = TextTemplateBinding.bind(interceptionBody, smOperation, null);
			}
			if (interceptionBody.length() > 0) {
				// only add interception operation, if the interception is not empty
				interceptionBody = "// --- interception code from rule <" + smContainerRule.getBase_Class().getName() + "> ---\n" + //$NON-NLS-1$ //$NON-NLS-2$
						interceptionBody;
				Operation interceptionOpInClass = interceptionOpMap.get(tmOperation);
				if (interceptionOpInClass == null) {
					createInterceptionOperation(tmOperation);
					// existing operation becomes interception operation (by assigning it a new behavior and moving its behavior to a new operation
					interceptionOpInClass = tmOperation;
					interceptionOpMap.put(tmOperation, interceptionOpInClass);
				}
				for (Behavior behavior : interceptionOpInClass.getMethods()) {
					if (behavior instanceof OpaqueBehavior) {
						EList<String> bodies = ((OpaqueBehavior) behavior).getBodies();
						if (bodies.size() > 0) {
							// always take first
							String newBody = interceptionBody + "\n" + //$NON-NLS-1$
									bodies.get(0);
							((OpaqueBehavior) behavior).getBodies().set(0, newBody);
						}
					}
				}
			}
		}
		return null;
	}

	// protected InstanceSpecification tmClassIS;

	protected EList<Operation> operations;

	protected Map<Operation, Operation> interceptionOpMap;

	/**
	 * @see AbstractContainerTrafo.createContainerInstance
	 */
	@Override
	public InstanceSpecification createContainerInstance(Class tmComponent, InstanceSpecification executorIS) throws TransformationException {
		InstanceSpecification containerIS = executorIS;
		// TODO ... incomplete!
		// InstanceConfigurator.configureInstance(smContainerRule, containerIS, null, context);

		// now create instances for the contained elements
		for (Property extensionPart : tmComponent.getAttributes()) {
			Type tmContainerExtImpl = extensionPart.getType();
			if (tmContainerExtImpl instanceof Class) {
				if (DepUtils.getSlot(executorIS, extensionPart) == null) {
					// no slot for part exists => assume that the part has been added by the container and create an instance specification for it.

					InstanceSpecification containerExtIS = DepCreation.createDepPlan(tmCDP, (Class) tmContainerExtImpl, containerIS.getName() + "." + //$NON-NLS-1$
							extensionPart.getName(), false);
					// configure extension
					InstanceConfigurator.configureInstance(containerExtIS, extensionPart, null);
					DepCreation.createSlot(containerIS, containerExtIS, extensionPart);
				}
			}
		}
		return containerIS;
	}
}

Back to the top