Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b12037baa29deb45829540a11aa3cd2ba6c64f39 (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
package FCM.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.ValueSpecification;

import FCM.Connector;
import FCM.ContainerRule;
import FCM.InteractionComponent;
import FCM.RuleApplication;


public class FCMUtil {

	/**
	 * @param specification
	 * @param configPropertyName
	 * @return
	 */
	public static EList<ValueSpecification> getConfigurationValue(InstanceSpecification specification, String configPropertyName) {
		if(specification != null) {
			for(Iterator<Slot> i = specification.getSlots().iterator(); i.hasNext();) {
				Slot currentSlot = i.next();
				if(currentSlot.getDefiningFeature() != null &&
					currentSlot.getDefiningFeature().getName().equals(configPropertyName))
					return currentSlot.getValues();
			}
		}
		return null;
	}

	/**
	 * @param interfaces
	 * @param interfaceName
	 * @return
	 */
	public static Interface findInterfaceByName(EList<Interface> interfaces, String interfaceName) {
		for(Iterator<Interface> i = interfaces.iterator(); i.hasNext();) {
			Interface currentInterface = i.next();
			if(currentInterface.getName().equals(interfaceName))
				return currentInterface;
		}
		return null;
	}

	/**
	 * A data structure to be used for the specification of how the roles of the collaboration associated with
	 * the type of param "connector" are bound to actual elements of the composite containing param "connector".
	 * This data structure contains an hashmap<ConnectableElement, List<Element>>:
	 * - keys must be roles of a Collaboration
	 * - values must be list of elements playing this role in the context of a particular composite
	 * 
	 * @author ac221913
	 * 
	 */
	public static final class RoleBindingTable {

		private HashMap<org.eclipse.uml2.uml.ConnectableElement, List<org.eclipse.uml2.uml.NamedElement>> table;

		public RoleBindingTable() {
			this.table = new HashMap<org.eclipse.uml2.uml.ConnectableElement,
				List<org.eclipse.uml2.uml.NamedElement>>();
		}

		public void addEntry(org.eclipse.uml2.uml.ConnectableElement role,
			List<org.eclipse.uml2.uml.NamedElement> playedBy) {
			table.put(role, playedBy);
		}

		public List<org.eclipse.uml2.uml.NamedElement> getEntry(org.eclipse.uml2.uml.ConnectableElement role) {
			return table.get(role);
		}

		public Iterator<org.eclipse.uml2.uml.ConnectableElement> iterator() {
			return table.keySet().iterator();
		}

		public HashMap<org.eclipse.uml2.uml.ConnectableElement, List<org.eclipse.uml2.uml.NamedElement>> getTable() {
			return table;
		}

		public ConnectableElement getRoleKeyByName(String roleName) {
			for(ConnectableElement role : table.keySet()) {
				if(role.getName().equals(roleName)) {
					return role;
				}
			}
			return null;
		}
	}

	public static void generateDefaultConnectionPattern(InteractionComponent connectorComp) {
		org.eclipse.uml2.uml.Collaboration connectionPattern =
			UMLFactory.eINSTANCE.createCollaboration();

		//((org.eclipse.uml2.uml.Package)connectorComp.getBase_Class().getOwner()).getPackagedElements().add(connectionPattern) ;
		connectorComp.getBase_Class().getNestedClassifiers().add(connectionPattern);
		connectionPattern.setName(connectorComp.getBase_Class().getName() + "ConnectionPattern");

		org.eclipse.uml2.uml.Property connectorRole =
			connectionPattern.createOwnedAttribute("connector", connectorComp.getBase_Class(), 1, 1);
		for(Iterator<org.eclipse.uml2.uml.Port> i = connectorComp.getBase_Class().getOwnedPorts().iterator(); i.hasNext();) {
			org.eclipse.uml2.uml.Port port = i.next();
			org.eclipse.uml2.uml.Property role;
			org.eclipse.uml2.uml.Connector connector;
			role = connectionPattern.createOwnedAttribute(port.getName(), null);
			connector = connectionPattern.createOwnedConnector("");
			org.eclipse.uml2.uml.ConnectorEnd source, target;
			source = connector.createEnd();
			source.setRole(port);
			source.setPartWithPort(connectorRole);
			target = connector.createEnd();
			target.setRole(role);
		}

		connectorComp.setConnectionPattern(connectionPattern);
	}

	/**
	 * TODO Move this method in ConnectorReification.java (once Manel has finished to modify the file)
	 * 
	 * @author ac221913
	 * 
	 */
	public static void generateCollaborationUse(Connector connector) {

		InteractionComponent connectorCompGen = connector.getIc();

		if(!(connectorCompGen instanceof InteractionComponent)) {
			return;
		}
		InteractionComponent connectorComp = (InteractionComponent)connectorCompGen;
		org.eclipse.uml2.uml.Collaboration connectionPattern =
			connectorComp.getConnectionPattern();

		if(connectionPattern == null) {
			FCMUtil.generateDefaultConnectionPattern(connectorComp);
			connectionPattern = connectorComp.getConnectionPattern();
		}

		Class composite = (Class)connector.getBase_Connector().getOwner();
		CollaborationUse collaborationUse;
		RoleBindingTable bindingTable = getConnectorRoleBindings(connector);

		if(bindingTable == null)
			return;

		collaborationUse = composite.createCollaborationUse("useOf" + connectionPattern.getName());
		collaborationUse.setType(connectionPattern);
		for(Iterator<org.eclipse.uml2.uml.ConnectableElement> i = bindingTable.iterator(); i.hasNext();) {
			org.eclipse.uml2.uml.ConnectableElement role = i.next();
			org.eclipse.uml2.uml.Dependency roleBinding;
			roleBinding = collaborationUse.createRoleBinding(role.getName() + "RoleBinding");
			roleBinding.getSuppliers().add(role);
			for(Iterator<org.eclipse.uml2.uml.NamedElement> j = bindingTable.getEntry(role).iterator(); j.hasNext();) {
				roleBinding.getClients().add(j.next());
			}
		}
	}

	/**
	 * Computes a RoleBindingTable for a given ConnectorComp
	 * Implies that a java class has been defined in FCMProfile.util for this ConnectorComp,
	 * and that it encapsulates corresponding role binding rule
	 * 
	 * @author ac221913
	 * 
	 */
	public static RoleBindingTable getConnectorRoleBindings(Connector connector) {
		InteractionComponent connectorCompGen = connector.getIc();

		if(!(connectorCompGen instanceof InteractionComponent)) {
			return null;
		}
		InteractionComponent type = (InteractionComponent)connectorCompGen;

		IExtensionRegistry reg = Platform.getExtensionRegistry();
		IConfigurationElement[] configElements = reg.getConfigurationElementsFor("fcmEmbeddingRule");
		for(IConfigurationElement configElement : configElements) {
			try {
				final String extConnName = configElement.getAttribute("connectorName");
				if(extConnName.equals(type.getBase_Class().getName())) {
					final Object obj = configElement.createExecutableExtension("class");
					if(obj instanceof IEmbeddingRule) {
						return ((IEmbeddingRule)obj).getRoleBindings(connector);
					}
				}
			} catch (CoreException exception) {
				exception.printStackTrace();
			}
		}
		return null;
	}

	/*
	 * return a list of all rules based on containment packages of rules
	 * from owning elements
	 */
	public static EList<ContainerRule> getAllContainerRules(Element element) {
		EList<ContainerRule> list = new UniqueEList<ContainerRule>();
		if(element != null) {
			for(EObject eObj : element.getStereotypeApplications()) {
				if(eObj instanceof RuleApplication) {
					list.addAll(((RuleApplication)eObj).getContainerRule());
					break;
				}
			}
			element = element.getOwner();
			if(element != null) {
				list.addAll(getAllContainerRules(element));
			}
		}
		return list;
	}

}

Back to the top