Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c385174d53025739d315933c14664c7e6ae78fa (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*****************************************************************************
 * 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;

/**
 * This file is part of Qompass GenTools
 * Copyright (C) 2008 CEA LIST (http://www-list.cea.fr/)

 * initial developer : Christophe JOUVRAY from CEA LIST 
 * Major contributions: Ansgar Radermacher from CEA LIST
 */

import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.FCM.InteractionComponent;
import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.qompass.designer.core.ConnectorUtils;
import org.eclipse.papyrus.qompass.designer.core.Log;
import org.eclipse.papyrus.qompass.designer.core.Messages;
import org.eclipse.papyrus.qompass.designer.core.PortUtils;
import org.eclipse.papyrus.qompass.designer.core.Utils;
import org.eclipse.papyrus.qompass.designer.core.deployment.AllocUtils;
import org.eclipse.papyrus.qompass.designer.core.deployment.DepUtils;
import org.eclipse.papyrus.qompass.designer.core.templates.ConnectorBinding;
import org.eclipse.papyrus.qompass.designer.core.templates.TemplateInstantiation;
import org.eclipse.papyrus.qompass.designer.core.templates.TemplateUtils;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.EncapsulatedClassifier;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * This class enables the reification of connectors, i.e. the replacement of
 * a UML connector with an interaction component. Reification is primarily
 * done on the level of a composite class, the
 */
public class ConnectorReification {

	/**
	 * Return an instance specification that corresponds to a part. This
	 * function is useful in the connector context, since it allows to retrieve
	 * the instance specification that is reference by a connection end-point
	 * (which points to the part).
	 * 
	 * @param system
	 *        the instance specification for the assembly
	 * @param part
	 *        the part within a class
	 * @return The instance specification for the passed part
	 */
	/*
	 * public static InstanceSpecification getInstanceForPart(
	 * InstanceSpecification system, Property part) {
	 * for (Slot slot : system.getSlots()) {
	 * if (slot.getDefiningFeature() == part) {
	 * for (ValueSpecification value : slot.getValues()) {
	 * // instances are accessible via ValueSpecification subclass
	 * // InstanceValue
	 * if (value instanceof InstanceValue) {
	 * return (((InstanceValue) value).getInstance());
	 * }
	 * }
	 * }
	 * }
	 * return null;
	 * }
	 */

	/**
	 * Find a port that would match a connection
	 * 
	 * @param connectorType
	 *        a connector type, i.e. a component with ports
	 * @param the
	 *        port on the other side of the connection
	 * @return the first port (of all ports owned or inherited by the type) that
	 *         is compatible with the passed otherPort.
	 */
	public static Port getConnectorPort(EncapsulatedClassifier connectorType,
		Port otherPort, boolean isAssembly) {
		EList<Port> ports = PortUtils.getAllPorts(connectorType);
		// try to find matching port
		for(Port port : ports) {
			if(PortUtils.isCompatible(port, otherPort, isAssembly)) {
				return port;
			}
		}
		throw new RuntimeException(String.format(Messages.ConnectorReification_CannotFindMatchingPort,
				connectorType.getName(), otherPort.getQualifiedName()));
	}

	/**
	 * 
	 * @param copy
	 * @param tmComponent
	 * @param smConnectorPart
	 *        Part representing the connector
	 * @param tmIS
	 *        target instance specification (required for choosing the right implementatioN)
	 * @param args
	 *        Acceleo arguments passed during instantiation
	 * @return The created part within tmComponent that represents the reified
	 *         connector
	 * @throws TransformationException
	 */
	public static Property reifyConnector(Copy copy, Class tmComponent,
		Property smConnectorPart, InstanceSpecification tmIS, Object[] args)
		throws TransformationException {
		// 1st step: create part for the connector without actually changing the type.
		// the objective is to enable the user to perform this step optionally, if the
		// connector needs n-ary connections.
		if(!(smConnectorPart.getType() instanceof Class)) {
			// can not happen since caller checks whether type is stereotyped as ConnectorComp
			// which extends class
			Log.log(Status.ERROR, Log.TRAFO_CONNECTOR, Messages.ConnectorReification_TemplateTypeNotClass);
			return null;
		}
		// choose an implementation
		Class connectorImplemTemplate = DepUtils.chooseImplementation(
			(Class)smConnectorPart.getType(), AllocUtils.getAllNodes(tmIS), null);

		TemplateBinding binding = ConnectorBinding.obtainBinding(tmComponent,
			smConnectorPart, connectorImplemTemplate, true);
		Class connectorImplem;

		if(binding != null) {
			TemplateUtils.adaptActualsToTargetModel(copy, binding);
			TemplateInstantiation ti = new TemplateInstantiation(copy, binding, args);
			connectorImplem = (Class)ti.bindNamedElement(connectorImplemTemplate);
		} else {
			// no binding, class is not a template => copy as it is
			connectorImplem = copy.getCopy(connectorImplemTemplate);
		}
		Property tmConnectorPart = copy.getCopy(smConnectorPart);
		tmConnectorPart.setType(connectorImplem);
		// now retarget connectors towards this part

		for(Connector connector : tmComponent.getOwnedConnectors()) {
			if(ConnectorUtils.connectsPart(connector, tmConnectorPart)) {
				// the connector end targets a port of a part or the composite (in case of delegation)
				ConnectorEnd connEnd = ConnectorUtils.connEndForPart(connector, tmConnectorPart);
				// redirect role, if pointing to port
				if(connEnd.getRole() instanceof Port) {
					Port connectedTemplatePort = (Port)connEnd.getRole();
					Port connectedBoundPort = (Port)Utils.getNamedElementFromList(
						PortUtils.getAllPorts(connectorImplem), connectedTemplatePort.getName());
					connEnd.setRole(connectedBoundPort);
				}
			}
		}
		return tmConnectorPart;
	}

	/**
	 * Reify a connector
	 * 
	 * @param copy
	 *        The copy obect (from source to target mode)
	 * @param tmComponent
	 *        containing composite in target target
	 * @param name
	 *        name of the connector
	 * @param smConnector
	 *        connector element within the source model
	 * @param tmIS
	 *        target model instance specification of the composite in which the reified connector
	 *        (the part typed with the instantiated interaction component) should be created.
	 *        The instance is only used to find a suitable implementation.
	 * @param args
	 *        addtion args for the Acceleo transformation
	 * @return the created part within tmComponent
	 * @throws TransformationException
	 */
	public static Property reifyConnector(Copy copy, Class tmComponent,
		String name, Connector smConnector, InstanceSpecification tmIS, Object[] args)
		throws TransformationException {

		org.eclipse.papyrus.FCM.Connector fcmConn = UMLUtil.getStereotypeApplication(smConnector, org.eclipse.papyrus.FCM.Connector.class);

		InteractionComponent connType = fcmConn.getIc();
		if(connType == null) {
			return null;
		}

		// choose an implementation
		Class connectorImplemTemplate = DepUtils.chooseImplementation(connType.getBase_Class(), AllocUtils.getAllNodes(tmIS), null);

		// ---- obtain binding & instantiate template type ...
		TemplateBinding binding = ConnectorBinding.obtainBinding(tmComponent,
			smConnector, connectorImplemTemplate, true);
		Class connectorImplem;

		if(binding != null) {
			TemplateUtils.adaptActualsToTargetModel(copy, binding);
			// make copy of bound package and restore it later. Required for nested template instantiations, in particular
			// the bound package is set within container transformations and is (by default) restored to "null" afterwards.
			// TODO: TemplateInstantiation should do this automatically
			copy.pushPackageTemplate();
			TemplateInstantiation ti = new TemplateInstantiation(copy, binding, args);
			connectorImplem = (Class)ti.bindNamedElement(connectorImplemTemplate);
			copy.popPackageTemplate();
		} else {
			// no binding, class is not a template => copy as it is
			connectorImplem = copy.getCopy(connectorImplemTemplate);
		}

		if(connectorImplem == null) {
			throw new TransformationException(
				String.format(Messages.ConnectorReification_CouldNotBind, connectorImplemTemplate.getName()));
		}

		Property tmConnectorPart = tmComponent.createOwnedAttribute(name,
			connectorImplemTemplate);
		// copy id, but prefix it with "p" (for part)
		Copy.copyID(smConnector, tmConnectorPart, "p"); //$NON-NLS-1$
		tmConnectorPart.setIsComposite(true);

		Log.log(Status.INFO, Log.TRAFO_CONNECTOR,
			String.format(Messages.ConnectorReification_InfoAddConnectorPart,
				connectorImplemTemplate.getName(), connectorImplem.getName()));

		// now create (simple) connections towards the new part
		int i = 0;
		for(ConnectorEnd smEnd : smConnector.getEnds()) {
			Connector tmConnector = tmComponent.createOwnedConnector("c " //$NON-NLS-1$
				+ name + " " + String.valueOf(i)); //$NON-NLS-1$
			Copy.copyID(smConnector, tmConnector);
			i++;
			// the new connector connects the existing end with an end of the
			// reified connector (the newly created property.)

			// --- first end, connected with the existing end (of another
			// non-connector part)
			ConnectorEnd tmEnd1 = tmConnector.createEnd();
			Property smPartWithPort = smEnd.getPartWithPort();
			Property tmPartWithPort = copy.getCopy(smPartWithPort);
			ConnectableElement smRole = smEnd.getRole();
			ConnectableElement tmRole = copy.getCopy(smRole);
			tmEnd1.setPartWithPort(tmPartWithPort);
			tmEnd1.setRole(tmRole);

			// --- 2nd end, connected with the reified connector (new part)
			ConnectorEnd tmEnd2 = tmConnector.createEnd();
			tmEnd2.setPartWithPort(tmConnectorPart);
			// inheritance between connector type and implementation (ports should be identical)
			// TODO: check whether filter condition is unique? (first returned by getConnectorPort is "good" one)
			if(tmRole instanceof Port) {
				tmEnd2.setRole(getConnectorPort(connectorImplem, (Port)tmRole, (tmPartWithPort != null)));
			}
			else {
				throw new TransformationException(
					Messages.ConnectorReification_RequiresUseOfPorts);
			}
		}

		tmConnectorPart.setType(connectorImplem);
		// updatePorts(tmComponent, connectorPart, connectorImplem);
		// connectContainerPorts(tmComponent, connectorPart);

		return tmConnectorPart;
	}

	/**
	 * Simple helper function
	 * 
	 * @param part
	 * @param connection
	 * @return the connector end that is associated with the "other" end of a
	 *         connection, i.e. the end that is not connected with the part
	 *         passed as parameter.
	 */
	public static ConnectorEnd oppositeConnEnd(Connector connection,
		ConnectorEnd myEnd) {
		// look for the other end (connectedEnd != myEnd)
		for(ConnectorEnd end : connection.getEnds()) {
			if(end != myEnd) {
				return end;
			}
		}
		return null;
	}

	/**
	 * Check, if a delegation connection targets a part that is deployed on a
	 * certain node.
	 * 
	 * @param connection
	 *        a delegation connection
	 * @param owner
	 *        the owning instance specification (instance specification for
	 *        composite containing the part that is targeted by the
	 *        connection)
	 * @param node
	 * @return
	 */
	/*
	 * public static boolean delegationForNode(Connector connection,
	 * InstanceSpecification owner, Node node) {
	 * InstanceSpecification instanceOfPart = getInstanceForPart(owner,
	 * ConnectorUtils.connEndNotPart(connection, null)
	 * .getPartWithPort());
	 * 
	 * if (instanceOfPart != null) {
	 * return (!ConnectorUtils.isAssembly(connection) && (AllocUtils
	 * .getNode(instanceOfPart) == node));
	 * } else {
	 * return false;
	 * }
	 * }
	 */

	/**
	 * Components can contain additional ports that are inherited via the
	 * container extension. These ports should typically be connected with
	 * additional ports of the (reified) connector. This connection is based on
	 * equal port types and is done automatically by this function, i.e. it
	 * cannot be done by the developer.
	 * 
	 * @param composite
	 *        the composite in which a connector has been reified.
	 * @param reifiedConnector
	 *        the part associated with the reifiedConnector
	 */
	static void connectContainerPorts(Class composite, Property reifiedConnector) {
		// This function is based on the assumption that the additional ports of
		// the reified
		// connector need to be connected with a port of a component that is
		// already
		// connected via the "normal" connectors (explicitly modeled by the
		// user).
		// For instance, in case of ACCORD-calls, the server component provides
		// the additional
		// RTU port via its container.

		// Create a subset of connectors that are connected with the
		// reified connector
		EList<Connector> connSubset = new BasicEList<Connector>();
		for(Connector connector : composite.getOwnedConnectors()) {
			if(ConnectorUtils.connectsPart(connector, reifiedConnector)) {
				connSubset.add(connector);
			}
		}

		for(Port port : PortUtils.getAllPorts((Class)reifiedConnector
			.getType())) {
			// check, if port is unconnected.
			boolean connected = false;
			// check whether a port of the reified connector is not yet
			// connected.
			for(Connector connector : connSubset) {
				if(ConnectorUtils.connectsPort(connector, port)) {
					connected = true;
				}
			}
			if(!connected) {
				// port is not connected yet. Check to find a connectable port
				// among all ports of connected parts.
				// In the moment, we assume the processes is stopped, as soon as
				// the port is connected, i.e. we do not want to connect the port to
				// potentially set of ports (todo: restriction always useful?)
				for(Connector connector : connSubset) {
					ConnectorEnd connEnd = ConnectorUtils.connEndNotPart(
						connector, reifiedConnector);
					Property otherPart = connEnd.getPartWithPort();
					// this is a part which is connected with the reified
					// connector check whether one of its ports is compatible with the
					// non-connected port.
					if(!(otherPart.getType() instanceof EncapsulatedClassifier))
						continue;

					for(Port otherPort : PortUtils.getAllPorts((EncapsulatedClassifier)otherPart.getType())) {
						Log.log(Status.INFO, Log.TRAFO_CONNECTOR, String.format(
							Messages.ConnectorReification_InfoPortTypes,
							otherPort.getType().getQualifiedName(),
							port.getType().getQualifiedName()));
						if(otherPort.getType() == port.getType()) {
							Connector newConnector = composite.createOwnedConnector("connector - container of " //$NON-NLS-1$
								+ otherPart.getName());
							ConnectorEnd end1 = newConnector.createEnd();
							ConnectorEnd end2 = newConnector.createEnd();
							end1.setPartWithPort(reifiedConnector);
							end1.setRole(port);
							end2.setPartWithPort(otherPart);
							end2.setRole(otherPort);
							connected = true;
							break;
						}
					}
					if(connected)
						break;
				}
				if(!connected) {
					if(port.getType() == null) {
						Activator.log.debug(
							String.format(Messages.ConnectorReification_CouldNotConnectPort, port.getName()));
					} else {
						Activator.log.debug(
							String.format(Messages.ConnectorReification_CouldNotConnectPortOfType, port.getName(), port.getType().getName()));
					}
				}
			}
		}
	}

	/**
	 * Propagate node allocation into a reified connector (identified via its part)
	 * 
	 * The allocation algorithm works as follows.
	 * - get a connector (simple) originating from the passed part
	 * - get the opposite end of this simple connector and identify the connected part
	 * (other part, e.g. a part typed with an application component)
	 * - obtain the nodes to which this other part is allocated:
	 * - in case of a composite, connections are followed up to a monolithic component
	 * - propagate the node allocation into the connector (propagation will finally
	 * allocate leafs within a (composite) connector.
	 * 
	 * @param composite
	 *        the composite class (e.g. system) in which a connector is reified
	 * @param compositeIS
	 *        the associated instance specification
	 * @param partSlot
	 *        the slot associated with the part for which nodes are
	 *        allocated.
	 */
	public static void propagateNodeAllocation(Class composite,
		InstanceSpecification compositeIS, Slot partSlot) {
		Property part = (Property)partSlot.getDefiningFeature();

		// loop over connectors of composite that originate from passed part.
		for(Connector connector : composite.getOwnedConnectors()) {
			ConnectorEnd myEnd = ConnectorUtils.connEndForPart(connector, part);
			if(myEnd == null) {
				// the connector does not connect this part
				continue;
			}
			ConnectorEnd otherEnd = ConnectorUtils.connEndNotPart(connector,
				part);

			Property otherPart = otherEnd.getPartWithPort();
			Port otherPort = (Port)otherEnd.getRole();
			// Property myPart = myEnd.getPartWithPort();
			Port myPort = (Port)myEnd.getRole();

			// find instance that is associated with other part.
			for(Slot slot : compositeIS.getSlots()) {
				if(slot.getDefiningFeature() == otherPart) {
					InstanceSpecification containedInstance = DepUtils.getInstance(slot);
					// TODO: too complicated, if the non-connector is a composite as well, it must be clearly allocated
					EList<InstanceSpecification> nodes = AllocUtils.getAllNodesForPort(
						containedInstance, otherPort);
					AllocUtils.propagateNodesViaPort(
							DepUtils.getInstance(partSlot), myPort, nodes);
					break;
				}
			}
		}
	}
}

Back to the top