Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6ff1cb5bd876266268ab05d68e92d2ecd04dfc4 (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
/*****************************************************************************
 * 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;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.FCM.PortKind;
import org.eclipse.papyrus.FCM.TemplatePort;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.EncapsulatedClassifier;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.util.UMLUtil;

public class PortUtils {

	/**
	 * Return the provided interface associated with the UML port, i.e.
	 * the derived attribute of the FCM profile
	 * 
	 * @param port
	 *        the UML port
	 * @return the provided interface
	 */
	public static Interface getProvided(Port port) {
		org.eclipse.papyrus.FCM.Port fcmPort = getFCMport(port);
		if(fcmPort != null) {
			return fcmPort.getProvidedInterface();
		} else if(port.getProvideds().size() > 0) {
			// return first standard UML provided port
			return port.getProvideds().get(0);
		}
		return null;
	}

	/**
	 * Return the required interface associated with the UML port, i.e.
	 * the derived attribute of the FCM profile
	 * 
	 * @param port
	 *        the UML port
	 * @return the required interface
	 */
	public static Interface getRequired(Port port) {
		org.eclipse.papyrus.FCM.Port fcmPort = getFCMport(port);
		if(fcmPort != null) {
			return fcmPort.getRequiredInterface();
		} else if(port.getRequireds().size() > 0) {
			// return first standard UML required port
			return port.getRequireds().get(0);
		}
		return null;
	}

	/**
	 * Return the FCM port (static profile) from a given UML port
	 * 
	 * @param port
	 * @return
	 */
	public static org.eclipse.papyrus.FCM.Port getFCMport(Port port) {
		return UMLUtil.getStereotypeApplication(port, org.eclipse.papyrus.FCM.Port.class);
	}

	/**
	 * Returns all ports (including inherited ones) for an encapsulated classifier
	 * It will also flatten extended ports
	 *
	 * @param ec
	 * @return
	 */
	public static EList<Port> getAllPorts(EncapsulatedClassifier ec) {
		EList<Port> ports = new BasicEList<Port>();
		for(Property attribute : ec.getAllAttributes()) {
			if(attribute instanceof Port) {
				ports.add((Port) attribute);
			}
		}
		
		// TODO: for the moment, don't add aggregated ports to list.
		/*
		 * ComponentType compType = = UMLUtil.getStereotypeApplication (ec, ComponentType.class);
		 * for (ContainerRule rule : compType.getContainerRule ())
		 * {
		 * if ((rule.getExtensionKind () == ContainerExtKind.AGGREGATION) &&
		 * (rule.getExtension () != null)) {
		 * ports.addAll (getAllPorts (rule.getExtension ().getBase_Class ()));
		 * }
		 * }
		 */

		return ports;
	}

	/**
	 * Returns all ports for an encapsulated classifier. Inherited ports are
	 * included, except if the superclass is already a component implementation.
	 * The motivation for this function is that ports inherited by a
	 * component implementation have already corresponding operations/attributes,
	 * only ports inherited by types need these definitions in subclasses.
	 * TODO: support for abstract implementations that partially implement ports
	 * 
	 * @param ec
	 * @return
	 */
	public static EList<Port> getAllPorts2(EncapsulatedClassifier ec) {
		EList<Port> ports = new BasicEList<Port>();

		ports.addAll(ec.getOwnedPorts());
		
		for(Classifier general : ec.getGenerals()) {
			if((general instanceof EncapsulatedClassifier) && (!Utils.isCompImpl(general))) {
				ports.addAll(getAllPorts2((EncapsulatedClassifier)general));
			}
		}
		return ports;
	}

	/**
	 * When given a list of ports, flatten the extended ports within this list
	 * and return a list of port-infos, i.e. information about ports.
	 * @param ports A list of ports
	 * @return A list of port-infos
	 */
	public static EList<PortInfo> flattenExtendedPorts(EList<Port> ports) {
		EList<PortInfo> portInfos = new BasicEList<PortInfo>();
		for (Port port : ports) {
			portInfos.addAll (flattenExtendedPort(port));
		}
		return portInfos;
	}

	
	/**
	 * Flatten the given extended port and return a list of port-infos.
	 * @param port
	 * @return
	 */
	public static EList<PortInfo> flattenExtendedPort(Port port) {
		EList<PortInfo> portInfos = new BasicEList<PortInfo>();
		if (isExtendedPort(port)) {
			org.eclipse.papyrus.FCM.Port fcmPort = getFCMport(port);
			org.eclipse.uml2.uml.Class cl;
			if (isTemplatePort(port)) {
				TemplatePort tp = UMLUtil.getStereotypeApplication(port, TemplatePort.class);
				if (tp.getBoundType() == null) {
					System.err.println("Problems, problems"); //$NON-NLS-1$
					cl = fcmPort.getKind().getBase_Class();
				}
				else {
					cl = tp.getBoundType().getBase_Class();
				}
			}
			else {
				cl = fcmPort.getKind().getBase_Class();
			}
			if ((cl != null) && (getAllPorts(cl).size() > 0)) {
				EList<Port> extendedPorts = getAllPorts(cl);
				for (Port extendedPort : extendedPorts) {
					portInfos.add(new PortInfo(extendedPort, port));
				}
			}
		}
		else {
			portInfos.add(new PortInfo(port, null));
		}
		return portInfos;
	}
	
	/**
	 * Return true, if the passed port is an extended port
	 * @param port
	 * @return
	 */
	public static boolean isExtendedPort(Port port) {
		org.eclipse.papyrus.FCM.Port fcmPort = getFCMport(port);
		if ((fcmPort != null) && (fcmPort.getKind() != null)) {
			org.eclipse.uml2.uml.Class cl = fcmPort.getKind().getBase_Class();
			return ((cl != null) && (getAllPorts(cl).size() > 0));
		}
		return false;
	}
	
	/**
	 * Return true, if the passed port is an extended port
	 * @param port
	 * @return
	 */
	public static boolean isTemplatePort(Port port) {
		return StereotypeUtil.isApplied(port, TemplatePort.class);
	}
	
	/**
	 * Return the port kind, an element of the static profile
	 * 
	 * @param port
	 * @return
	 */
	public static PortKind getKind(Port port) {
		org.eclipse.papyrus.FCM.Port fcmPort = getFCMport(port);
		if(fcmPort != null) {
			return fcmPort.getKind();
		}
		return null;
	}

	/**
	 * Check whether two ports have the same port kind. Since different models apparently
	 * use different Java instances for the same port kind, the check is therefore based
	 * on the equality of full qualified name.
	 * 
	 * @param portA
	 * @param portB
	 * @return true, if port kinds are identical
	 */
	public static boolean sameKinds(Port portA, Port portB) {
		PortKind kindA = getKind(portA);
		PortKind kindB = getKind(portB);
		return kindA == kindB;
	}

	/**
	 * Check whether two ports match, i.e. have the same kind but different conjugation (assembly)
	 * or same (delegation)
	 * 
	 * @param portA
	 *        first port
	 * @param portB
	 *        second port
	 * @param isAssembly
	 *        true, if the ports should be connected by an assembly connector (i.e. no delegation)
	 * @return true, if ports match
	 */
	public static boolean matches(Port portA, Port portB, boolean isAssembly) {
		if(isAssembly) {
			return ((portA.getType() == portB.getType()) && sameKinds(portA, portB) && (portA.isConjugated() != portB.isConjugated()));
		} else {
			// delegation
			return ((portA.getType() == portB.getType()) && sameKinds(portA, portB) && (portA.isConjugated() == portB.isConjugated()));
		}
	}

	/**
	 * Check whether two ports are compatible. i.e. either match or are compatible interface wise
	 * 
	 * @param portA
	 * @param portB
	 * @param isAssembly
	 *        true, if the ports should be connected by an assembly connector (i.e. no delegation)
	 * @return
	 */
	public static boolean isCompatible(Port portA, Port portB, boolean isAssembly) {
		if(matches(portA, portB, isAssembly)) {
			return true;
		}
		// no match found, try weaker condition: find 1st match for provided ...
		// TODO: check not only for identical interfaces but allow a superclass on the required interface
		if(isAssembly) {
			return (PortUtils.getProvided(portA) == PortUtils.getRequired(portB)) &&
				(PortUtils.getProvided(portB) == PortUtils.getRequired(portA));
		} else {
			return (PortUtils.getProvided(portA) == PortUtils.getProvided(portB)) &&
				(PortUtils.getRequired(portB) == PortUtils.getRequired(portA));
		}
	}
}

Back to the top