Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 247efdba3963eee97d5ce0db5c64d13d3d030818 (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
/*****************************************************************************
 * Copyright (c) 2013 PROTEUS Project Consortium.
 *
 *
 * 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:
 * Sebastien BONNET (EFFIDENCE) - Initial API and implementation
 * Nicolas DU LAC (INTEMPORA) - Additional methods and a few updates.
 *
 *****************************************************************************/

package org.eclipse.papyrus.robotml.generators.common.mmqueries;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.papyrus.RobotML.Allocate;
import org.eclipse.papyrus.RobotML.DeploymentPlan;
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.Model;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.util.UMLUtil;

public class DeploymentQueries {

	/**
	 * Return the list of instance specifications in a model
	 *
	 * @param model
	 * @return A list of InstanceSpecification of all instance specifications
	 */
	static public List<InstanceSpecification> getInstanceSpecificationsInModel(Model model) {
		LinkedList<InstanceSpecification> found_elts = new LinkedList<InstanceSpecification>();
		for (Element ne : model.getOwnedElements()) {
			if (ne instanceof NamedElement) {
				if (ne instanceof org.eclipse.uml2.uml.InstanceSpecification) {
					found_elts.add((InstanceSpecification) ne);
					// System.out.println("Instance : "+((NamedElement) ne).getName());
				} else if (ne instanceof org.eclipse.uml2.uml.Package) {
					found_elts.addAll(getInstanceSpecificationsInPackage((org.eclipse.uml2.uml.Package) ne));
				}
			} else if (ne instanceof Model) {
				found_elts.addAll(getInstanceSpecificationsInModel((Model) ne));
			}
		}
		// if(found_elts.isEmpty()){
		// System.out.println("Instances list empty");
		// }
		return found_elts;
	}

	/**
	 * Return the list of instance specifications in a package
	 *
	 * @param pack
	 * @return A list of InstanceSpecification of all instance specifications
	 */
	static public List<InstanceSpecification> getInstanceSpecificationsInPackage(org.eclipse.uml2.uml.Package pack) {
		LinkedList<InstanceSpecification> found_elts = new LinkedList<InstanceSpecification>();
		for (Element ne : pack.getOwnedElements()) {
			if (ne instanceof NamedElement) {
				if (ne instanceof org.eclipse.uml2.uml.InstanceSpecification) {
					found_elts.add((InstanceSpecification) ne);
				} else if (ne instanceof org.eclipse.uml2.uml.Package) {
					found_elts.addAll(getInstanceSpecificationsInPackage((org.eclipse.uml2.uml.Package) ne));
				}
			} else if (ne instanceof Model) {
				found_elts.addAll(getInstanceSpecificationsInModel((Model) ne));
			}
		}
		return found_elts;
	}

	/**
	 * Return the list of instance specifications allocated to given platform
	 *
	 * @param model
	 * @param
	 * @return A list of InstanceSpecification of all instance specifications
	 */
	static public List<InstanceSpecification> getInstanceSpecificationsForPlatform(org.eclipse.uml2.uml.Model model, java.lang.String pfn) {
		List<InstanceSpecification> found_elts = new LinkedList<InstanceSpecification>();
		for (InstanceSpecification instanceSpecification : getInstanceSpecificationsInModel(model)) {
			if (isAllocatedTo(instanceSpecification, pfn)) {
				found_elts.add(instanceSpecification);
			}
		}
		return found_elts;
	}

	/**
	 * Return the list of instance specifications allocated to given platform. Should be equivalent to getInstanceSpecificationsForPlatform, but
	 * different implementation.
	 *
	 * @param model
	 * @param targetPlatformName
	 *            Name of the requested target platform type.
	 * @return A list of InstanceSpecification of all instance specifications.
	 */
	static public List<InstanceSpecification> getInstanceSpecificationsForPlatform2(org.eclipse.uml2.uml.Model model, String targetPlatformName) { // org.eclipse.papyrus.RobotML.Platform targetPlatform) {
		// The list to return in the end.
		List<InstanceSpecification> found_elts = new LinkedList<InstanceSpecification>();
		for (org.eclipse.uml2.uml.Package dpPck : getDeploymentPlanPackagesInModel(model)) {
			found_elts.addAll(getDeploymentPlanInstanceSpecsForPlatform(dpPck, targetPlatformName));
		}
		return found_elts;
	}


	/**
	 * Return the parent name of an instance
	 *
	 * @param inst
	 * @return Parent name of an instance
	 */
	static public java.lang.String getParentName(InstanceSpecification inst) {
		String name = inst.getName();

		Integer idx = name.lastIndexOf('.');
		if (idx != -1) {
			return name.substring(0, idx);
		} else {
			return null;
		}
	}


	/**
	 * Get real instance from InstanceSpecification object
	 *
	 * @param model
	 * @param instanceSpecification
	 * @return the instance if found, or else null
	 */
	static public Property getInstance(Model model, InstanceSpecification instanceSpecification) {
		List<org.eclipse.uml2.uml.Property> l = ArchitectureQueries.getAllSubComponentsInClass(ArchitectureQueries.getRootClassForModel(model));

		for (Property p : l) {
			String[] str = instanceSpecification.getName().split("\\.");

			if (str.length > 0 && p.getName().equals(str[str.length - 1])) {
				return p;
			}
		}

		return null;
	}


	/**
	 * Function to know if the instance must be generated by the platform or not.
	 *
	 * @param instanceSpecification
	 * @param platformName
	 * @return true if the instance is allocated to the platform which name is given in parameter
	 */
	static public boolean isAllocatedTo(InstanceSpecification instSpec, String platformName) {
		if (instSpec == null || platformName == null) {
			return false;
		}
		for (Dependency dep : instSpec.getClientDependencies()) {
			Allocate alloc = UMLUtil.getStereotypeApplication(dep, Allocate.class);
			if (alloc == null) {
				continue;
			}
			for (NamedElement nelt : dep.getSuppliers()) {
				if (isPlatformInstanceSpecificationFromPlatform(nelt, platformName)) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * Retrieves the deployment plans defined in a given model.
	 *
	 * @param model
	 * @return A list of DeploymentPlans defined in the model.
	 */
	static public List<DeploymentPlan> getDeploymentPlansInModel(org.eclipse.uml2.uml.Model model) {
		List<DeploymentPlan> dep_plans = new LinkedList<DeploymentPlan>();
		for (Element elt : model.getOwnedElements()) {
			if (elt instanceof org.eclipse.uml2.uml.Package) {
				DeploymentPlan dp = UMLUtil.getStereotypeApplication(elt, DeploymentPlan.class);
				if (dp != null) {
					dep_plans.add(dp);
				} else {
					dep_plans.addAll(getDeploymentPlansInPackage((org.eclipse.uml2.uml.Package) elt));
				}
			}
		}
		return dep_plans;
	}

	/**
	 * Retrieves the deployment plans defined in a given package.
	 *
	 * @param pck
	 * @return
	 */
	static protected List<DeploymentPlan> getDeploymentPlansInPackage(org.eclipse.uml2.uml.Package pck) {
		List<DeploymentPlan> dep_plans = new LinkedList<DeploymentPlan>();
		for (Element elt : pck.getOwnedElements()) {
			if (elt instanceof org.eclipse.uml2.uml.Package) {
				DeploymentPlan dp = UMLUtil.getStereotypeApplication(elt, DeploymentPlan.class);
				if (dp != null) {
					dep_plans.add(dp);
				} else {
					dep_plans.addAll(getDeploymentPlansInPackage((org.eclipse.uml2.uml.Package) elt));
				}
			}
		}
		return dep_plans;
	}

	/**
	 * Retrieves the Packages in which Deployment plans are defined in the model.
	 *
	 * @param model
	 * @return
	 */
	static public List<org.eclipse.uml2.uml.Package> getDeploymentPlanPackagesInModel(Model model) {
		List<DeploymentPlan> dps = getDeploymentPlansInModel(model);
		LinkedList<org.eclipse.uml2.uml.Package> pcks = new LinkedList<org.eclipse.uml2.uml.Package>();
		for (DeploymentPlan dp : dps) {
			pcks.add(dp.getBase_Package());
		}
		return pcks;
	}

	/**
	 * Retrieves the InstanceSpecficiations allocated to a given Platform within a given DeploymentPlan.
	 *
	 * @param deploymentPlan
	 * @param targetPlatformName
	 * @return
	 */
	static public List<InstanceSpecification> getDeploymentPlanInstanceSpecsForPlatform(org.eclipse.uml2.uml.Package deploymentPlan, String targetPlatformName) {
		// The list to return in the end.
		List<InstanceSpecification> found_elts = new LinkedList<InstanceSpecification>();

		// Is the first argument really a Deployment plan ? (a Package with a DeploymentPlan StereoType)
		DeploymentPlan dp = org.eclipse.uml2.uml.util.UMLUtil.getStereotypeApplication(deploymentPlan, org.eclipse.papyrus.RobotML.DeploymentPlan.class);
		if (dp == null) {
			return found_elts;
		}

		// Let's get all the Allocate elements in the DeploymentPlan, and check if the supplier (the target platform) corresponds to the second argument.
		for (Element elt : deploymentPlan.getOwnedElements()) {
			if (elt instanceof Dependency) {
				// Is it a Dependency with an "Allocate" stereotype ?
				Dependency dep = (Dependency) elt;
				Allocate alloc = org.eclipse.uml2.uml.util.UMLUtil.getStereotypeApplication(elt, org.eclipse.papyrus.RobotML.Allocate.class);
				if (alloc == null) {
					continue;
				}

				// Let's store the potential client (the component instance) of the Allocate.
				// for future addition to the list to return.
				InstanceSpecification client_instance_spec = null;
				for (NamedElement client_elt : dep.getClients()) {
					if (false == (client_elt instanceof InstanceSpecification)) {
						continue;
					}
					client_instance_spec = (InstanceSpecification) client_elt;
					break;
				}
				if (client_instance_spec == null) {
					continue;
				}

				// Is it allocated to the platform provided as argument ?
				for (NamedElement nelt : dep.getSuppliers()) {
					if (isPlatformInstanceSpecificationFromPlatform(nelt, targetPlatformName)) {
						found_elts.add(client_instance_spec);
					}
				}
			}
		}
		return found_elts;
	}

	/**
	 * Function to know the content of allocation string.
	 *
	 * @param instanceSpecification
	 * @return the allocation string
	 */
	static public String getAllocationName(InstanceSpecification instanceSpecification) {
		if (instanceSpecification.getClientDependencies().size() == 0) {
			return new String();
		}

		// il n'y a toujours qu'un element
		Dependency d = instanceSpecification.getClientDependencies().get(0);

		return d.getName();
	}

	/**
	 * Function to know which class must be generated by the platform or not.
	 *
	 * @param model
	 * @param platformName
	 * @return all classes that are instanciated for the platform "pfn"
	 */
	static public List<org.eclipse.uml2.uml.Class> getDefinedComponentsForPlatform(Model model, String platformName) {
		// System.out.println("getDefinedComponentsForPlatform");
		LinkedList<org.eclipse.uml2.uml.Class> found_classes = new LinkedList<org.eclipse.uml2.uml.Class>();

		List<InstanceSpecification> instances = getInstanceSpecificationsForPlatform(model, platformName);
		for (InstanceSpecification instanceSpecification : instances) {
			// System.out.println("instance : " + instanceSpecification.getName());

			for (org.eclipse.uml2.uml.Classifier classifier : instanceSpecification.getClassifiers()) {
				found_classes.add(GeneralQueries.findClassInModel(model, classifier.getName()));

				// org.eclipse.uml2.uml.Class theClass = GeneralQueries.findClassInModel(model, classifier.getName());
				// System.out.println("\tclass : " + theClass.getName());
			}
		}
		return found_classes;
	}

	/**
	 * Function to know which class must be generated by the platform or not. Should be equivalent to getDefinedComponentsForPlatform but with a
	 * different implementation.
	 *
	 * @param model
	 * @param platformName
	 * @return all classes that are instanciated for the platform "pfn"
	 */
	static public List<org.eclipse.uml2.uml.Class> getDefinedComponentsForPlatform2(Model model, String platformName) // Platform platform)
	{
		LinkedList<org.eclipse.uml2.uml.Class> found_classes = new LinkedList<org.eclipse.uml2.uml.Class>();
		List<InstanceSpecification> inst_specs = getInstanceSpecificationsForPlatform2(model, platformName);
		for (InstanceSpecification inst_spec : inst_specs) {
			for (org.eclipse.uml2.uml.Classifier classifier : inst_spec.getClassifiers()) {
				found_classes.add(GeneralQueries.findClassInModel(model, classifier.getName()));
			}
		}

		// remove duplicates
		Set<org.eclipse.uml2.uml.Class> new_set = new HashSet<org.eclipse.uml2.uml.Class>(found_classes);
		List<org.eclipse.uml2.uml.Class> found_classes_2 = new LinkedList<org.eclipse.uml2.uml.Class>(new_set);

		return found_classes_2;
	}

	/**
	 * Checks whether or not a Platform instance specification has a given platform type.
	 *
	 * @param platformInstanceSpecification
	 * @param expectedPlatformName
	 * @return
	 */
	protected static boolean isPlatformInstanceSpecificationFromPlatform(NamedElement platformInstanceSpecification, String expectedPlatformName) // Platform expectedPlatform)
	{
		if (platformInstanceSpecification instanceof InstanceSpecification) {
			InstanceSpecification platformInstance = (InstanceSpecification) platformInstanceSpecification;
			for (Classifier classifier : platformInstance.getClassifiers()) {
				if (classifier.getName().toLowerCase().compareTo(expectedPlatformName) == 0) {
					return true;
					/*
					 * RoboticMiddleware mdw = UMLUtil.getStereotypeApplication(classifier, org.eclipse.papyrus.RobotML.RoboticMiddleware.class);
					 * RoboticSimulator sim = UMLUtil.getStereotypeApplication(classifier, org.eclipse.papyrus.RobotML.RoboticSimulator.class);
					 * if (mdw != null) {
					 * //The target platform seems to be a middleware.
					 * //Middlewares are identified by their "kind".
					 * if (expectedPlatform instanceof RoboticMiddleware) {
					 * if (mdw.getKind() == ((RoboticMiddleware)expectedPlatform).getKind()) {
					 * //YEEEEHAAAA !!!
					 * return true;
					 * }
					 * }
					 * }
					 * if (sim != null && expectedPlatform instanceof RoboticSimulator) {
					 * //The target platform seems to be a simulator.
					 * //Simulators are just derived classes.
					 * if (sim.getClass().getName().compareTo(((RoboticSimulator)expectedPlatform).getClass().getName()) == 0) {
					 * //YEEEEEHAAAAAA !!!
					 * return true;
					 * }
					 * }
					 */
				}
			}
		}
		return false;
	}
}

Back to the top