Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 211b146d70612675f2eb964f76e6ef97d9dc3c9f (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 * 
 * 		Mauricio Alférez (mauricio.alferez@cea.fr) CEA LIST - Initial API and implementation based on UML2 tutorials.
 *
 *****************************************************************************/
package org.eclipse.papyrus.req.domainModel2Profile.utils;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.InstanceValue;
import org.eclipse.uml2.uml.LiteralBoolean;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Utility methods to create a profile
 *
 */
public class Utils {
	/**
	 * Creates a new UML model with a specific name
	 * 
	 * @param name
	 *            The name of the model
	 * @return the UML model
	 */
	public static Model createModel(String name) {
		Model model = UMLFactory.eINSTANCE.createModel();
		model.setName(name);
		return model;
	}

	/**
	 * Refresh the project that contains a file without user intervention (e.g.,
	 * by using the F5 key)
	 * 
	 * @param file
	 *            The file contained in the project to be refreshed
	 */
	public static void refreshProject(File file) {
		IProject iproject = ResourcesPlugin.getWorkspace().getRoot().getProject(file.getParentFile().getName());
		try {
			iproject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Creates an enumeration in a UML package
	 * 
	 * @param package_
	 *            The UML package that will owns the enumeration
	 * @param name
	 *            The name of the enumeration contained in package_
	 * @return a new Enumeration
	 */
	public static Enumeration createEnumeration(org.eclipse.uml2.uml.Package package_, String name) {
		Enumeration enumeration = package_.createOwnedEnumeration(name);
		return enumeration;
	}

	/**
	 * Creates an enumeration literal
	 * 
	 * @param enumeration
	 *            The enumeration that will own the literal
	 * @param name
	 *            The name of the literal
	 * @return a new enumeration literal
	 */
	public static EnumerationLiteral createEnumerationLiteral(Enumeration enumeration, String name) {
		EnumerationLiteral enumerationLiteral = enumeration.createOwnedLiteral(name);
		return enumerationLiteral;
	}

	/**
	 * Creates a extension of the specified metaclass with this stereotype.
	 * 
	 * @param metaclass
	 *            The UML metaclass that will be extended, for example Class or
	 *            Association
	 * @param stereotype
	 *            The stereotype that will extend the metaclass
	 * @param required
	 *            Whether the new extension should be required
	 * @return the new extension
	 */
	public static Extension createExtension(org.eclipse.uml2.uml.Class metaclass, Stereotype stereotype,
			boolean required) {
		Extension extension = stereotype.createExtension(metaclass, required);
		return extension;
	}

	/**
	 * Creates a new {@link org.eclipse.uml2.uml.Generalization}, with the
	 * specified '<em><b>General</b></em>', and appends it to the
	 * '<em><b>Generalization</b></em>' containment reference list.
	 * 
	 * @param specificClassifier
	 *            The '<em><b>Specific</b></em>' for the new
	 *            {@link org.eclipse.uml2.uml.Generalization}
	 * @param generalClassifier
	 *            The '<em><b>General</b></em>' for the new
	 *            {@link org.eclipse.uml2.uml.Generalization}
	 * @return the new generalization
	 */
	public static Generalization createGeneralization(Classifier specificClassifier, Classifier generalClassifier) {
		Generalization generalization = specificClassifier.createGeneralization(generalClassifier);
		return generalization;
	}

	/**
	 * Creates a new UML package inside another package
	 * 
	 * @param nestingPackage
	 *            The package that will contain the new package
	 * @param name
	 *            The name of the new package
	 * @return the new UML package
	 */
	public static org.eclipse.uml2.uml.Package createPackage(org.eclipse.uml2.uml.Package nestingPackage, String name) {
		org.eclipse.uml2.uml.Package package_ = nestingPackage.createNestedPackage(name);
		return package_;
	}

	/**
	 * Creates a new primitive type inside a Package
	 * 
	 * @param package_
	 *            The name of the package
	 * @param name
	 *            The name of the primitive type
	 * @return the primitive type
	 */
	public static PrimitiveType createPrimitiveType(org.eclipse.uml2.uml.Package package_, String name) {
		PrimitiveType primitiveType = package_.createOwnedPrimitiveType(name);
		return primitiveType;
	}

	/**
	 * Creates a UML profile with a specific name and nsURI
	 * 
	 * @param name
	 *            The new of the new profile
	 * @param nsURI
	 *            The nsURI of the new package
	 * @return the new UML profile
	 */
	public static Profile createProfile(String name, String nsURI) {
		Profile profile = UMLFactory.eINSTANCE.createProfile();
		profile.setName(name);
		profile.setURI(nsURI);
		return profile;
	}

	/**
	 * Creates a new stereotype in a specific profile
	 * 
	 * @param profile
	 *            The profile that will contain the stereotype
	 * @param name
	 *            The name of the stereotype
	 * @param isAbstract
	 *            Whether the stereotype is abstract
	 * @return
	 */
	public static Stereotype createStereotype(Profile profile, String name, boolean isAbstract) {
		Stereotype stereotype = profile.createOwnedStereotype(name, isAbstract);
		return stereotype;
	}

	/**
	 * Defines a profile
	 * 
	 * @param profile
	 *            the profile to be defined
	 */
	public static void defineProfile(Profile profile) {
		profile.define();
	}

	/**
	 * Applies a profile to a package
	 * 
	 * @param package_
	 *            The package where the profile will be applied
	 * @param profile
	 *            The profile that will be applied to the package
	 */
	protected static void applyProfile(org.eclipse.uml2.uml.Package package_, Profile profile) {
		package_.applyProfile(profile);
	}

	/**
	 * Creates a property with the specified name, type, lower bound, upper
	 * bound and default value as an owned attribute of a class
	 * 
	 * @param class_
	 *            The class that will have the attribute
	 * @param name
	 *            The name of the attribute
	 * @param type
	 *            The type of the attribute
	 * @param lowerBound
	 *            The lower bound for the new attribute
	 * @param upperBound
	 *            The upper bound for the new attribute
	 * @param defaultValue
	 *            The default value of the new attribute
	 * @return the new attribute
	 */
	public static Property createAttribute(org.eclipse.uml2.uml.Class class_, String name, Type type, int lowerBound,
			int upperBound, Object defaultValue) {
		Property attribute = class_.createOwnedAttribute(name, type, lowerBound, upperBound);
		if (defaultValue instanceof Boolean) {
			LiteralBoolean literal = (LiteralBoolean) attribute.createDefaultValue(null, null,
					UMLPackage.Literals.LITERAL_BOOLEAN);
			literal.setValue(((Boolean) defaultValue).booleanValue());
		} else if (defaultValue instanceof String) {
			if (type instanceof Enumeration) {
				InstanceValue value = (InstanceValue) attribute.createDefaultValue(null, null,
						UMLPackage.Literals.INSTANCE_VALUE);
				value.setInstance(((Enumeration) type).getOwnedLiteral((String) defaultValue));
			} else {
				LiteralString literal = (LiteralString) attribute.createDefaultValue(null, null,
						UMLPackage.Literals.LITERAL_STRING);
				literal.setValue((String) defaultValue);
			}
		}
		return attribute;
	}

	/**
	 * Obtains a java.io.File in a workspace based on a URI
	 * @param uri The URI
	 * @return the file
	 */
	public static File getFileFromURI(URI uri) {
		if (uri != null) {
			IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
			IPath path = new Path(uri.toPlatformString(true));
			IFile file = myWorkspaceRoot.getFile(path);
			if (file != null && file.getLocation() != null) {
				File f = file.getLocation().toFile();
				return f;
			}
		}
		return null;
	}
}

Back to the top