Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5de6dc199a6b13451342df8ca9866be84e47422 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*****************************************************************************
 * 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 java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.FCM.Assembly;
import org.eclipse.papyrus.FCM.ContainerRule;
import org.eclipse.papyrus.FCM.RuleApplication;
import org.eclipse.papyrus.FCM.Singleton;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.qompass.designer.core.preferences.QompassPreferenceConstants;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.NamedElement;
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.Type;
import org.eclipse.uml2.uml.util.UMLUtil;

public class Utils {

	/**
	 * Retrieve an element from a list of named elements via its name
	 * 
	 * @param namedElementList
	 * @param name
	 * @return
	 */
	public static NamedElement getNamedElementFromList(
		EList<? extends NamedElement> namedElementList, String name) {
		Iterator<? extends NamedElement> namedElements = namedElementList
			.iterator();
		while(namedElements.hasNext()) {
			NamedElement namedElement = namedElements.next();
			if(namedElement.getName().equals(name)) {
				return namedElement;
			}
		}
		return null;
	}

	/**
	 * return the top-level owner of an element. This function returns the same
	 * value as getModel, if the top-level element is a model. While this is the
	 * case for models, model libraries have a top-level package (not a model).
	 * In this case, getTop returns the top-level package whereas getModel would
	 * return null.
	 * 
	 * @param element
	 * @return the top-level owning package
	 */
	public static Package getTop(Element element) {
		while(element != null) {
			Element owner = element.getOwner();
			if(owner == null) {
				if(element instanceof Package) {
					return (Package)element;
				}
			}
			element = owner;
		}
		return null;
	}

	/**
	 * Return package at first level. Useful, since last model transformation 
	 * will put elements under a new "root"
	 * 
	 * @param element
	 * @return
	 */
	public static Package getFirstLevel(Element element) {
		Element lastElement = element;
		while(element != null) {
			Element owner = element.getOwner();
			if(owner == null) {
				if(lastElement instanceof Package) {
					return (Package)lastElement;
				}
			}
			lastElement = element;
			element = owner;
		}
		return null;
	}

	/**
	 * @param element
	 *        an element which is owned by a model.
	 * @param subfolder
	 *        the name of a sub-folder within root (created, if not
	 *        existent)
	 * @return a reference to the sub folder within the root of the model that
	 *         belongs to the passed element.
	 */
	public static Package getRoot(Element element, String subfolder) {
		Package root = getTop(element);
		if(root.getNestedPackage(subfolder) != null) {
			return root.getNestedPackage(subfolder);
		} else {
			return root.createNestedPackage(subfolder);
		}
	}

	/**
	 * Convenience method enabling to apply getOwnedOperation equally to classes
	 * and interfaces
	 * 
	 * @param cl
	 * @param name
	 * @param paramNames
	 * @param paramTypes
	 * @return
	 */
	public static Operation createOwnedOperation(Classifier cl, String name,
		EList<String> paramNames, EList<Type> paramTypes, Type retType) {
		if(cl instanceof Class) {
			return ((Class)cl).createOwnedOperation(name, paramNames,
				paramTypes, retType);
		} else if(cl instanceof Interface) {
			return ((Interface)cl).createOwnedOperation(name, paramNames,
				paramTypes, retType);
		} else {
			return null;
		}
	}

	public static Operation getOwnedOperation(Classifier cl, String name,
		EList<String> paramNames, EList<Type> paramTypes) {
		if(cl instanceof Class) {
			return ((Class)cl).getOwnedOperation(name, paramNames, paramTypes);
		} else if(cl instanceof Interface) {
			return ((Interface)cl).getOwnedOperation(name, paramNames,
				paramTypes);
		} else {
			return null;
		}
	}

	/**
	 * this method returns the component type of an implementation. It is based
	 * on the modeling convention that implementations inherit from types.
	 */
	public static Class componentType(Class implementation) {
		if(Utils.isCompImpl(implementation)) {
			Iterator<Class> superclasses = implementation.getSuperClasses()
				.iterator();
			while(superclasses.hasNext()) {
				Class componentType = superclasses.next();
				if(Utils.isCompType(componentType)) {
					return componentType;
				}
			}
		}
		return null;
	}

	/**
	 * return true, if a component (implementation) is a composite, i.e. has
	 * inner parts
	 */
	public static boolean isComposite(Class implementation) {
		return Utils.isCompImpl(implementation)
			&& (getParts(implementation).size() > 0);
	}

	public static EList<Property> getParts(Class implementation) {
		if(treatNoneAsComposite()) {
			EList<Property> parts = new BasicEList<Property>();
			for(Property part : implementation.getAttributes()) {
				if(part.getAggregation() != AggregationKind.SHARED_LITERAL) {
					parts.add(part);
				}
			}
			return parts;
		}
		else {
			return implementation.getParts();
		}
	}


	/**
	 * @param attribute
	 *        an attribute
	 * @return true, if the aggregation kind is considered as a composition
	 */
	public static boolean isComposition(Property attribute) {
		if(treatNoneAsComposite()) {
			return (attribute.getAggregation() != AggregationKind.SHARED_LITERAL);
		}
		else {
			return (attribute.getAggregation() == AggregationKind.COMPOSITE_LITERAL);
		}
	}

	/**
	 * return true, if an instance specification is a composite, i.e. has > 1
	 * slots TODO: distinguish parts and configuration attributes
	 */

	public static boolean isComposite(InstanceSpecification is) {
		return (is.getSlots().size() > 0);
	}

	/**
	 * Get an element via its qualified name. Will find elements from the root
	 * model and elements in imported models. Also supports target model in
	 * which imports have been copied (while keeping the top-level name)
	 * 
	 * @param root
	 * @param qualifiedName
	 * @return
	 */
	public static NamedElement getQualifiedElement(Package root,
		String qualifiedName) {
		NamedElement namedElement = null;
		int index = qualifiedName.indexOf("::"); //$NON-NLS-1$
		if(index != -1) {
			// first try using a path without top element (since
			// getQualifiedElement is typically used for
			// imported elements)
			String remainder = qualifiedName.substring(index + 2);
			namedElement = getQualifiedElement(root, remainder, qualifiedName);
		}
		if(namedElement == null) {
			// try with complete name as path name, but assume that the element
			// has been copied into the model,
			// i.e. qualifiedName is prefixed by model name
			namedElement = getQualifiedElement(root, qualifiedName,
				root.getName() + "::" + qualifiedName); //$NON-NLS-1$
		}
		return namedElement;
	}

	/**
	 * Retrieve an element via its qualified name within a package The segments
	 * of the package may be non unique due to imports
	 * 
	 * @return the found element, if it exists
	 */
	public static NamedElement getQualifiedElement(Package root,
		String remainingPath, String qualifiedName) {
		if(root == null) {
			return null;
		}
		if(!remainingPath.contains("::")) {
			for(NamedElement candidate : root.getMembers()) {
				String name = candidate.getName();
				if((name != null) && name.equals(remainingPath)) {
					if(candidate.getQualifiedName().equals(qualifiedName)) {
						return candidate;
					}
				}
			}
		} else {
			String segment = remainingPath.split("::")[0];
			String remainder = remainingPath.substring(segment.length() + 2);
			for(Element element : root.getMembers()) {
				if(element instanceof Package) {
					if(((NamedElement)element).getName().equals(segment)) {
						NamedElement foundElement = getQualifiedElement(
							(Package)element, remainder, qualifiedName);
						// return, if not found
						if(foundElement != null) {
							return foundElement;
						}
					}
				}
			}
		}
		return null;
	}

	/**
	 * Simple check whether an element is in a different model than the passed
	 * package It will return true, whenever the the top elements do not match.
	 * This is always true, if the 2nd belongs to a different model, whether
	 * imported or not. This distinction is however not required in our context.
	 */
	public static boolean isElementInDifferentModel(Package model,
		NamedElement namedElement) {
		return model != getTop(namedElement);
	}

	/**
	

	/**
	 * Check whether a class contains a non-port attribute of a given name
	 * 
	 * @param cl
	 * @return
	 */
	public static boolean hasNonPortOwnedAttribute(Class cl, String name) {
		for(Property p : cl.getOwnedAttributes()) {
			if(!(p instanceof Port)) {
				if(p.getName().equals(name)) {
					return true;
				}
			}
		}
		return false;
	}

	public static <T extends EObject> EList<T> getAllElementsOfType(Element examineElement, java.lang.Class<T> clazz)
	{
		EList<Element> visitedPackages = new BasicEList<Element>();
		return getAllElementsOfType(examineElement, clazz, visitedPackages);
	}

	@SuppressWarnings("unchecked")
	public static <T extends EObject> EList<T> getAllElementsOfType(Element examineElement, java.lang.Class<T> clazz, EList<Element> visitedPackages)
	{
		EList<T> list = new UniqueEList<T>();
		for(Element element : examineElement.allOwnedElements()) {
			if(element instanceof Package) {
				if(!visitedPackages.contains(element)) {
					visitedPackages.add(element);
					list.addAll(getAllElementsOfType((Package)element, clazz,
						visitedPackages));
				}
			} else if(clazz.isInstance(element)) {
				list.add((T)element);
			}
		}
		return list;
	}

	/**
	 * Return the top element of the model that is currently edited. This function is based on the
	 * assumption that the user model is the first resource that is loaded into the model set.
	 * Use this function instead of Utils.getTop (or getModel) if you want to avoid navigating to the
	 * root of an imported model.
	 * 
	 * @return the top level package of the model currently loaded into an editor.
	 */
	public static Package getUserModel() {
		ServiceUtilsForActionHandlers serviceUtils = ServiceUtilsForActionHandlers.getInstance();
		try {
			// IPath fn = serviceUtils.getModelSet().getFilenameWithoutExtension();
			EList<Resource> resources = serviceUtils.getModelSet().getResources();
			if(resources.size() >= 3) {
				// check first three resources (di, notation, uml)
				for(int i = 0; i < 3; i++) {
					Resource userResource = resources.get(i);
					if(userResource.getContents().size() > 0) {
						EObject topEObj = userResource.getContents().get(0);
						if(topEObj instanceof Package) {
							return (Package)topEObj;
						}
					}
				}
			}
		} catch (ServiceException e) {
		}
		return null;
	}

	/**
	 * @param a
	 *        potential implementation
	 * @return true, if passed classifier is an implementation (i.e. declared
	 *         via stereotypes as component or connector implementation (a sub-type of component implementations,
	 *         therefore no additional check is required)
	 */
	public static boolean isCompImpl(Classifier implementation) {
		return !implementation.isAbstract();
	}

	/**
	 * Check whether the passed class is a component type (and not a component implementation).
	 * This includes connector and container types as well.
	 * 
	 * @param component
	 * @return
	 */
	public static boolean isCompType(Class component) {
		return component.isAbstract();
	}

	public static boolean isSingleton(Class component) {
		return StereotypeUtil.isApplied(component, Singleton.class);
	}

	public static boolean isAssembly(Class component) {
		return StereotypeUtil.isApplied(component, Assembly.class);
	}

	public static boolean treatNoneAsComposite() {
		IPreferenceStore store = org.eclipse.papyrus.qompass.designer.core.Activator.getDefault().getPreferenceStore();
		return store.getBoolean(QompassPreferenceConstants.P_TREAT_NONE_AS_COMPOSITE);
	}

	public static boolean allAttributesAreConfigAttributs() {
		IPreferenceStore store = org.eclipse.papyrus.qompass.designer.core.Activator.getDefault().getPreferenceStore();
		return store.getBoolean(QompassPreferenceConstants.P_ALL_ATTRIBUTES_ARE_CONFIG_ATTRIBUTES);
	}

	/**
	 * True, if either a component implementation or a type
	 * 
	 * @param component
	 * @return
	 */
	public static boolean isComponent(Class component) {
		return (isCompType(component) || isCompImpl(component));
	}

	/**
	 * Return the rules associated with a component
	 * 
	 * @param component
	 * @return
	 */
	public static EList<ContainerRule> getRules(Class component) {
		RuleApplication ruleApplication = UMLUtil.getStereotypeApplication(component, RuleApplication.class);
		if(ruleApplication != null) {
			return ruleApplication.getContainerRule();
		}
		return null;
	}

	/**
	 * Return the absolute file name to a file name given e.g. in the form platform:/plugin/xxx
	 * 
	 * @param fileName
	 *        the file name using eclipse elements such as platform:/plugin
	 * @return the absolute file name
	 */
	public static String getAbsoluteFN(String fileName) {
		try {
			URL absoluteURL = FileLocator.toFileURL(new URL(fileName));
			return absoluteURL.getFile();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		return null;
	}

	// TODO: need a generic utility function for getting all elements obeying a certain
	// criteria from a model 
	public static EList<ContainerRule> getAllRules(Package pkg) {
		EList<Package> visitedPackages = new BasicEList<Package>();
		EList<ContainerRule> contRuleList = new BasicEList<ContainerRule>();
		getAllRules(pkg, visitedPackages, contRuleList);
		return contRuleList;
	}

	public static void getAllRules(Package pkg, EList<Package> visitedPackages, EList<ContainerRule> contRuleList) {
		for(Element el : pkg.getMembers()) {
			if(el instanceof Package) {
				if(!visitedPackages.contains(el)) {
					visitedPackages.add((Package)el);
					getAllRules((Package)el, visitedPackages, contRuleList);
				}
			}
			else if(el instanceof Class) {
				if(StereotypeUtil.isApplied(el, ContainerRule.class)) {
					ContainerRule rule = UMLUtil.getStereotypeApplication((Class)el, ContainerRule.class);
					contRuleList.add(rule);
				}
			}
		}
	}
}

Back to the top