/
GettingStartedWithUML2.java
421 lines (348 loc) · 14.2 KB
/
GettingStartedWithUML2.java
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
/*
* Copyright (c) 2014, 2018 CEA and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Christian W. Damus (CEA) - initial API and implementation
* Kenn Hussey - 535301
*
*/
package org.eclipse.uml2.examples.gettingstarted;
import java.io.File;
import java.io.IOException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.uml2.uml.resources.util.UMLResourcesUtil;
/**
* A Java program that may be run stand-alone (with the required EMF and UML2
* bundle JARs on the classpath) to create the example model illustrated in the
* <em>Getting Started with UML2</em> article on the Wiki.
*
* @see http://wiki.eclipse.org/MDT/UML2/Getting_Started_with_UML2
*/
public class GettingStartedWithUML2 {
public static boolean DEBUG = true;
private static File outputDir;
/**
* The main program. It expects one argument, which is the local filesystem
* path of a directory in which to create the <tt>ExtendedPO2.uml</tt> file.
*
* @param the
* program arguments, which must consist of a single filesystem
* path
*/
public static void main(String[] args)
throws Exception {
if (!processArgs(args)) {
System.exit(1);
}
banner("Creating root model package and primitive types.");
// Create the root package (a model).
Model epo2Model = createModel("epo2");
// Create primitive types to be used as types of attributes in our
// classes.
PrimitiveType intPrimitiveType = createPrimitiveType(epo2Model, "int");
PrimitiveType stringPrimitiveType = createPrimitiveType(epo2Model,
"String");
PrimitiveType datePrimitiveType = createPrimitiveType(epo2Model, "Date");
PrimitiveType skuPrimitiveType = createPrimitiveType(epo2Model, "SKU");
// Create enumerations to be used as types of attributes in our classes.
Enumeration orderStatusEnumeration = createEnumeration(epo2Model,
"OrderStatus");
createEnumerationLiteral(orderStatusEnumeration, "Pending");
createEnumerationLiteral(orderStatusEnumeration, "BackOrder");
createEnumerationLiteral(orderStatusEnumeration, "Complete");
banner("Creating model classes.");
// Create the classes.
org.eclipse.uml2.uml.Class supplierClass = createClass(epo2Model,
"Supplier", false);
org.eclipse.uml2.uml.Class customerClass = createClass(epo2Model,
"Customer", false);
org.eclipse.uml2.uml.Class purchaseOrderClass = createClass(epo2Model,
"PurchaseOrder", false);
org.eclipse.uml2.uml.Class itemClass = createClass(epo2Model, "Item",
false);
org.eclipse.uml2.uml.Class addressClass = createClass(epo2Model,
"Address", true);
org.eclipse.uml2.uml.Class usAddressClass = createClass(epo2Model,
"USAddress", false);
org.eclipse.uml2.uml.Class globalAddressClass = createClass(epo2Model,
"GlobalAddress", false);
org.eclipse.uml2.uml.Class globalLocationClass = createClass(epo2Model,
"GlobalLocation", false);
// Create generalization relationships amongst our classes.
createGeneralization(usAddressClass, addressClass);
createGeneralization(globalAddressClass, addressClass);
createGeneralization(globalAddressClass, globalLocationClass);
banner("Creating attributes of model classes.");
// Create attributes in our classes.
createAttribute(supplierClass, "name", stringPrimitiveType, 0, 1);
createAttribute(customerClass, "customerID", intPrimitiveType, 1, 1);
createAttribute(purchaseOrderClass, "comment", stringPrimitiveType, 0,
1);
createAttribute(purchaseOrderClass, "orderDate", datePrimitiveType, 1,
1);
createAttribute(purchaseOrderClass, "status", orderStatusEnumeration,
1, 1);
createAttribute(purchaseOrderClass, "totalAmount", intPrimitiveType, 0,
1);
createAttribute(itemClass, "productName", stringPrimitiveType, 0, 1);
createAttribute(itemClass, "quantity", intPrimitiveType, 0, 1);
createAttribute(itemClass, "usPrice", intPrimitiveType, 0, 1);
createAttribute(itemClass, "comment", stringPrimitiveType, 0, 1);
createAttribute(itemClass, "shipDate", datePrimitiveType, 0, 1);
createAttribute(itemClass, "partNum", skuPrimitiveType, 1, 1);
createAttribute(addressClass, "name", stringPrimitiveType, 0, 1);
createAttribute(addressClass, "country", stringPrimitiveType, 0, 1);
createAttribute(usAddressClass, "street", stringPrimitiveType, 1, 1);
createAttribute(usAddressClass, "city", stringPrimitiveType, 1, 1);
createAttribute(usAddressClass, "state", stringPrimitiveType, 1, 1);
createAttribute(usAddressClass, "zip", stringPrimitiveType, 1, 1);
createAttribute(globalAddressClass, "location", stringPrimitiveType, 1,
1);
createAttribute(globalLocationClass, "countryCode", intPrimitiveType,
1, 1);
banner("Creating associations between model classes.");
// Create associations between our classes.
createAssociation(supplierClass, true,
AggregationKind.COMPOSITE_LITERAL, "orders", 0,
LiteralUnlimitedNatural.UNLIMITED, purchaseOrderClass, false,
AggregationKind.NONE_LITERAL, "", 1, 1);
createAssociation(supplierClass, true, AggregationKind.NONE_LITERAL,
"pendingOrders", 0, LiteralUnlimitedNatural.UNLIMITED,
purchaseOrderClass, false, AggregationKind.NONE_LITERAL, "", 0, 1);
createAssociation(supplierClass, true, AggregationKind.NONE_LITERAL,
"shippedOrders", 0, LiteralUnlimitedNatural.UNLIMITED,
purchaseOrderClass, false, AggregationKind.NONE_LITERAL, "", 0, 1);
createAssociation(supplierClass, true,
AggregationKind.COMPOSITE_LITERAL, "customers", 0,
LiteralUnlimitedNatural.UNLIMITED, customerClass, false,
AggregationKind.NONE_LITERAL, "", 1, 1);
createAssociation(customerClass, true, AggregationKind.NONE_LITERAL,
"orders", 0, LiteralUnlimitedNatural.UNLIMITED, purchaseOrderClass,
true, AggregationKind.NONE_LITERAL, "customer", 1, 1);
createAssociation(purchaseOrderClass, true,
AggregationKind.NONE_LITERAL, "previousOrder", 0, 1,
purchaseOrderClass, false, AggregationKind.NONE_LITERAL, "", 0, 1);
createAssociation(purchaseOrderClass, true,
AggregationKind.COMPOSITE_LITERAL, "items", 0,
LiteralUnlimitedNatural.UNLIMITED, itemClass, true,
AggregationKind.NONE_LITERAL, "order", 1, 1);
createAssociation(purchaseOrderClass, true,
AggregationKind.COMPOSITE_LITERAL, "billTo", 1, 1, addressClass,
false, AggregationKind.NONE_LITERAL, "", 1, 1);
createAssociation(purchaseOrderClass, true,
AggregationKind.COMPOSITE_LITERAL, "shipTo", 1, 1, addressClass,
false, AggregationKind.NONE_LITERAL, "", 1, 1);
// Save our model to a file in the user-specified output directory
URI outputURI = URI.createFileURI(outputDir.getAbsolutePath())
.appendSegment("ExtendedPO2")
.appendFileExtension(UMLResource.FILE_EXTENSION);
banner("Saving the model to %s.", outputURI.toFileString());
save(epo2Model, outputURI);
}
//
// Model-building utilities
//
protected static Model createModel(String name) {
Model model = UMLFactory.eINSTANCE.createModel();
model.setName(name);
out("Model '%s' created.", model.getQualifiedName());
return model;
}
protected static org.eclipse.uml2.uml.Package createPackage(
org.eclipse.uml2.uml.Package nestingPackage, String name) {
org.eclipse.uml2.uml.Package package_ = nestingPackage
.createNestedPackage(name);
out("Package '%s' created.", package_.getQualifiedName());
return package_;
}
protected static PrimitiveType createPrimitiveType(
org.eclipse.uml2.uml.Package package_, String name) {
PrimitiveType primitiveType = package_.createOwnedPrimitiveType(name);
out("Primitive type '%s' created.", primitiveType.getQualifiedName());
return primitiveType;
}
protected static Enumeration createEnumeration(
org.eclipse.uml2.uml.Package package_, String name) {
Enumeration enumeration = package_.createOwnedEnumeration(name);
out("Enumeration '%s' created.", enumeration.getQualifiedName());
return enumeration;
}
protected static EnumerationLiteral createEnumerationLiteral(
Enumeration enumeration, String name) {
EnumerationLiteral enumerationLiteral = enumeration
.createOwnedLiteral(name);
out("Enumeration literal '%s' created.",
enumerationLiteral.getQualifiedName());
return enumerationLiteral;
}
protected static org.eclipse.uml2.uml.Class createClass(
org.eclipse.uml2.uml.Package package_, String name,
boolean isAbstract) {
org.eclipse.uml2.uml.Class class_ = package_.createOwnedClass(name,
isAbstract);
out("Class '%s' created.", class_.getQualifiedName());
return class_;
}
protected static Generalization createGeneralization(
Classifier specificClassifier, Classifier generalClassifier) {
Generalization generalization = specificClassifier
.createGeneralization(generalClassifier);
out("Generalization %s --|> %s created.",
specificClassifier.getQualifiedName(),
generalClassifier.getQualifiedName());
return generalization;
}
protected static Property createAttribute(
org.eclipse.uml2.uml.Class class_, String name, Type type,
int lowerBound, int upperBound) {
Property attribute = class_.createOwnedAttribute(name, type,
lowerBound, upperBound);
out("Attribute '%s' : %s [%s..%s] created.", //
attribute.getQualifiedName(), // attribute name
type.getQualifiedName(), // type name
lowerBound, // no special case for multiplicity lower bound
(upperBound == LiteralUnlimitedNatural.UNLIMITED)
? "*" // special case for unlimited bound
: upperBound);
return attribute;
}
protected static Association createAssociation(Type type1,
boolean end1IsNavigable, AggregationKind end1Aggregation,
String end1Name, int end1LowerBound, int end1UpperBound,
Type type2, boolean end2IsNavigable,
AggregationKind end2Aggregation, String end2Name,
int end2LowerBound, int end2UpperBound) {
Association association = type1.createAssociation(end1IsNavigable,
end1Aggregation, end1Name, end1LowerBound, end1UpperBound, type2,
end2IsNavigable, end2Aggregation, end2Name, end2LowerBound,
end2UpperBound);
out("Association %s [%s..%s] %s-%s %s [%s..%s] created.", //
UML2Util.isEmpty(end1Name)
// compute a placeholder for the name
? String.format("{%s}", type1.getQualifiedName()) //
// user-specified name
: String.format("'%s::%s'", type1.getQualifiedName(), end1Name), //
end1LowerBound, // no special case for this
(end1UpperBound == LiteralUnlimitedNatural.UNLIMITED)
? "*" // special case for unlimited upper bound
: end1UpperBound, // finite upper bound
end2IsNavigable
? "<" // indicate navigability
: "-", // not navigable
end1IsNavigable
? ">" // indicate navigability
: "-", // not navigable
UML2Util.isEmpty(end2Name)
// compute a placeholder for the name
? String.format("{%s}", type2.getQualifiedName()) //
// user-specified name
: String.format("'%s::%s'", type2.getQualifiedName(), end2Name), //
end2LowerBound, // no special case for this
(end2UpperBound == LiteralUnlimitedNatural.UNLIMITED)
? "*" // special case for unlimited upper bound
: end2UpperBound);
return association;
}
//
// Program control
//
private static boolean processArgs(String[] args)
throws IOException {
if (args.length != 1) {
err("Expected 1 argument.");
err("Usage: java -jar ... %s <dir>",
GettingStartedWithUML2.class.getSimpleName());
err("where");
err("<dir> - path to output folder in which to save the UML model");
return false;
}
outputDir = new File(args[0]).getCanonicalFile();
if (!outputDir.exists()) {
err("No such directory: %s", outputDir.getAbsolutePath());
return false;
}
if (!outputDir.isDirectory()) {
err("Not a directory: %s", outputDir.getAbsolutePath());
return false;
}
if (!outputDir.canWrite()) {
err("Cannot create a file in directory: %s",
outputDir.getAbsolutePath());
return false;
}
return true;
}
protected static void save(org.eclipse.uml2.uml.Package package_, URI uri) {
// Create a resource-set to contain the resource(s) that we are saving
ResourceSet resourceSet = new ResourceSetImpl();
// Initialize registrations of resource factories, library models,
// profiles, Ecore metadata, and other dependencies required for
// serializing and working with UML resources. This is only necessary in
// applications that are not hosted in the Eclipse platform run-time, in
// which case these registrations are discovered automatically from
// Eclipse extension points.
UMLResourcesUtil.init(resourceSet);
// Create the output resource and add our model package to it.
Resource resource = resourceSet.createResource(uri);
resource.getContents().add(package_);
// And save
try {
resource.save(null); // no save options needed
out("Done.");
} catch (IOException ioe) {
err(ioe.getMessage());
}
}
//
// Logging utilities
//
protected static void banner(String format, Object... args) {
System.out.println();
hrule();
System.out.printf(format, args);
if (!format.endsWith("%n")) {
System.out.println();
}
hrule();
System.out.println();
}
protected static void hrule() {
System.out.println("------------------------------------");
}
protected static void out(String format, Object... args) {
if (DEBUG) {
System.out.printf(format, args);
if (!format.endsWith("%n")) {
System.out.println();
}
}
}
protected static void err(String format, Object... args) {
System.err.printf(format, args);
if (!format.endsWith("%n")) {
System.err.println();
}
}
}