Jump to: navigation, search

Difference between revisions of "MDT/UML2/Getting Started with UML2"

< MDT‎ | UML2
Line 106: Line 106:
  
 
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a package with a specified name in a specified nesting package.  
 
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a package with a specified name in a specified nesting package.  
<pre>    protected static org.eclipse.uml2.uml.Package createPackage(
+
<pre>    protected static org.eclipse.uml2.uml.Package createPackage(org.eclipse.uml2.uml.Package nestingPackage, String name) {
                    org.eclipse.uml2.uml.Package nestingPackage, String name) {
+
 
         org.eclipse.uml2.uml.Package package_ = nestingPackage.createNestedPackage(name);
 
         org.eclipse.uml2.uml.Package package_ = nestingPackage.createNestedPackage(name);
  
Line 125: Line 124:
  
 
#Select a package (i.e.,&nbsp;'''&lt;Model&gt; epo2''') in the UML editor.  
 
#Select a package (i.e.,&nbsp;'''&lt;Model&gt; epo2''') in the UML editor.  
#Right-click and select the '''New Child &gt; Owned Type &gt; Primitive Type''' option from the context menu.  
+
#Select the '''New Child &gt; Owned Type &gt; Primitive Type''' option from the context menu.  
 
#Enter a value (i.e., “int”) for the '''Name''' property in the '''Properties''' view.
 
#Enter a value (i.e., “int”) for the '''Name''' property in the '''Properties''' view.
  
Line 135: Line 134:
  
 
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a primitive type with a specified name in a specified package.  
 
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a primitive type with a specified name in a specified package.  
<pre>    protected static PrimitiveType createPrimitiveType(
+
<pre>    protected static PrimitiveType createPrimitiveType(org.eclipse.uml2.uml.Package package_, String name) {
                    org.eclipse.uml2.uml.Package package_, String name) {
+
 
         PrimitiveType primitiveType = (PrimitiveType) package_.createOwnedPrimitiveType(name);
 
         PrimitiveType primitiveType = (PrimitiveType) package_.createOwnedPrimitiveType(name);
 
   
 
   
Line 151: Line 149:
 
[[Image:GSWU2 tryit.gif]] Write code to programmatically create the remaining primitive types from the ExtendedPO2 model.  
 
[[Image:GSWU2 tryit.gif]] Write code to programmatically create the remaining primitive types from the ExtendedPO2 model.  
  
= Creating Enumerations =
+
= Creating Enumerations =
 +
 
 +
An enumeration is a kind of data type whose instances may be any of a number of user-defined enumeration literals. To create an enumeration using the UML editor, follow these steps:
 +
 
 +
#Select a package (i.e., '''&lt;Model&gt; epo2''') in the UML editor.
 +
#Select the '''New Child &gt; Owned Type &gt; Enumeration''' option from the context menu.
 +
#Enter a value (i.e., “OrderStatus”) for the '''Name''' property in the '''Properties''' view.
 +
 
 +
At this point your workspace should look something like this:
 +
 
 +
[GSWU2_CreatingEnumerations.png]
 +
 
 +
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns an enumeration with a specified name in a specified package.
 +
<pre>    protected static Enumeration createEnumeration(org.eclipse.uml2.uml.Package package_, String name) {
 +
        Enumeration enumeration = (Enumeration) package_.createOwnedEnumeraton(name);
 +
 +
        out("Enumeration '" + enumeration.getQualifiedName() + "' created.");
 +
 +
        return enumeration;
 +
    }
 +
</pre>
 +
Here we call the '''createOwnedEnumeration(String)''' convenience factory method to ask the package to create a primitive type with the specified name as one of its packaged elements.
 +
 
 +
OK, let’s see this method in action. For example, we could create an enumeration named ‘OrderStatus’ in model ‘epo2’ as follows:
 +
<pre>        Enumeration orderStatusEnumeration = createEnumeration(epo2Model,&nbsp;"OrderStatus");
 +
</pre>
 +
= Creating Enumeration Literals  =
 +
 
 +
An enumeration literal is a user-defined data value for an enumeration. To create an enumeration literal using the UML editor, follow these steps:
 +
 
 +
#Select an enumeration (i.e., '''&lt;Enumeration&gt; OrderStatus''') in the UML editor.
 +
#Select the '''New Child &gt; Owned Literal &gt; Enumeration Literal''' option from the context menu.
 +
#Enter a value (i.e., “Pending”) for the '''Name''' property in the '''Properties''' view.
 +
 
 +
[[Image:GSWU2 tryit.gif]] Create the remaining enumeration literals from the ExtendedPO2 model using the UML editor.
 +
 
 +
At this point your workspace should look something like this:
 +
 
 +
[[Image:GSWU2 CreatingEnumerationLiterals.png]]
 +
 
 +
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns an enumeration literal with a specified name in a specified enumeration.<br>
 +
<pre>    protected static EnumerationLiteral createEnumerationLiteral(Enumeration enumeration, String name) {
 +
        EnumerationLiteral enumerationLiteral = enumeration.createOwnedLiteral(name);
 +
 +
        out("Enumeration literal '" + enumerationLiteral.getQualifiedName() + "' created.");
 +
 +
        return enumerationLiteral;
 +
    }
 +
</pre>
 +
Here we call a '''createOwnedLiteral(String)''' convenience factory method to ask the enumeration to create an enumeration literal with the specified name as one of its owned literals.
 +
 
 +
OK, let’s see this method in action. For example, we could create an enumeration literal named ‘Pending’ in enumeration ‘OrderStatus’ as follows:
 +
<pre>        createEnumerationLiteral(orderStatusEnumeration, "Pending");
 +
</pre>
 +
[[Image:GSWU2 tryit.gif]] Write code to programmatically create the remaining enumeration literals from the ExtendedPO2 model.
 +
 
 +
= Creating Classes  =

Revision as of 23:14, 11 October 2011

Copyright © 2004, 2011 International Business Machines Corp. and CEA

Summary

This article describes how to get started with the UML2 plug-ins for Eclipse. In particular, it gives an overview of how to create models (and their contents) both programmatically and by using the sample UML editor.

Kenn Hussey and James Bruck Last Updated: October 12, 2011

Prerequisites

To start using UML2 (and to follow along with the example in this article), you must have Eclipse, EMF, and UML2 installed. You can either download the Modeling Tools Package or follow these steps:

  1. Download and run Eclipse.
  2. Select the Help > Install New Software… menu item.
  3. Select a software site to work with, e.g., Indigo - http://download.eclipse.org/releases/indigo.
  4. Expand the Modeling tree item.
  5. Select UML2 Extender SDK and press the Next > button.
  6. Review the install details and press the Next > button.
  7. Accept the terms of the license agreement and press the Finish button.
  8. Restart Eclipse when prompted to do so.

GSWU2 Prerequisites.png

At this stage, UML2 and all dependencies should be installed.

Introduction

This article will walk you through the basics of creating models using UML2. Using a simple model (the ExtendedPO2 model, shamelessly “borrowed” from the EMF “bible” [1]) as an example, we’ll look at what’s involved in creating some of the more common elements that make up a model. For each type of element, we’ll first explain the creation process using the sample UML editor and then explore how to accomplish the same thing using Java code. The ExtendedPO2 model is shown below.

GSWU2 Introduction.gif

Getting Started

Before getting started, you’ll need to create a simple project in your workspace. This project will serve as the container for the model that we’ll create using the UML editor. To create a simple project for this article, follow these steps:

  1. Select the Window > Open Perspective > Other… menu item.
  2. Select the Resource perspective and press the OK button.
  3. Select the File > New > Project... menu item.
  4. Select the Project wizard from the General category and press the Next > button.
  5. Enter a project name (i.e. “Getting Started with UML2”) and press the Finish button.

At this point your workspace should look something like this:

GSWU2 GettingStarted.png

OK, that should be enough to get us going with the UML editor. Now, to follow along with the programmatic approach to creating models, we’ll assume that you’ve created a class (named, say, “GettingStartedWithUML2”) in which you can write some code to construct our sample model. The code snippets we’ll show assume you’ve defined the following utility methods to give the user information on the program’s status:

     public static boolean DEBUG = true;

     protected static void out(String output) {

         if (DEBUG) {
             System.out.println(output);
         }
     }

     protected static void err(String error) {
         System.err.println(error);
     }

A static debug flag can be used to enable or disable verbose information printed to the system’s output stream. Errors will always be printed to the system’s error stream.

All righty then! In each of the following subsections, we’ll look at how to create a different kind of UML element, starting with models.

Creating Models

At the root of a typical UML model is a model element. It contains a (hierarchical) set of elements that together describe the physical system being modeled. To create a model using the UML editor, follow these steps:

  1. Select a project (i.e., Getting Started with UML2) in the Project Explorer view and select the File > New > Other... menu item.
  2. Select the UML Model wizard from the Example EMF Model Creation Wizards category and press the Next > button.
  3. Enter a file name (i.e., “ExtendedPO2.uml”) and press the Next > button.
  4. Select Model for the model object and press the Finish button.
  5. Select the Window > Show View > Properties menu item.
  6. Select the <Model> element in the UML editor.
  7. Enter a value (i.e., “epo2”) for the Name property in the Properties view.

At this point your workspace should look something like this:

GSWU2 CreatingModels.png


Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a model with a specified name.

     protected static Model createModel(String name) {
         Model model = UMLFactory.eINSTANCE.createModel();
         model.setName(name);
 
         out("Model '" + model.getQualifiedName() + "' created.");
 
         return model;
     }

First, we ask the UML factory singleton to create a model, and we set its name. Then, we output information to let the user know that the model has been successfully created. Finally, we return the model. You’ll notice most, if not all, of the code snippets in this article will follow this pattern – create the element (and set some properties on it), inform the user, and return it.

GSWU2 tip.gif All named elements (a model is a type of named element) have a “simple” name and a qualified name. The qualified name is the “simple” name prefixed with the “simple” names of all of the named element’s containing namespaces. Note that the qualified name of a named element is only defined if all of its containing namespaces have non-empty “simple” names.

OK, let’s see this method in action. For example, we could create a model named ‘epo2’ as follows:

         Model epo2Model = createModel("epo2");

Creating Packages

A package is a namespace for its members (packageable elements), and may contain other packages. A package can import either individual members of other packages, or all of the members of other packages. To create a package using the UML editor, follow these steps:

  1. Select a package (e.g., <Package> foo) in the UML editor.
  2. Select the New Child > Nested Package > Package option from the context menu.
  3. Enter a value (e.g., “bar”) for the Name property in the Properties view.

We don’t actually need to create a package because our sample model doesn’t contain any… except of course for the root package (i.e., the model). That’s right – a model is a type of package.

Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a package with a specified name in a specified nesting package.

     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 '" + package_.getQualifiedName() + "' created.");
 
         return package_;
     }

Here, instead of asking the factory to create the package for us, we’re making use of one of the factory methods in the UML2 API. In UML2, a factory method exists for every feature that can contain other elements (i.e., every containment feature). In addition, more convenient factory methods exist for commonly created types (like packages). In this case, the package has a feature (packagedElement) that can contain packageable elements, so we could obtain the Ecore class of the type of (packageable) element we want to create (i.e., Package) from the UML Ecore package singleton, and pass it to the createPackagedElement(String, EClass) factory method. Instead, we use the more convenient createNestedPackage(String) factory method which implicitly creates a package and accepts the desired package name as an argument. Behind the scenes, the package will create a nested package, set its name, and add the package to its list of packaged elements.

OK, let’s see this method in action. For example, we could create a package named ‘bar’ in nesting package ‘foo’ as follows:

         org.eclipse.uml2.uml.Package barPackage = createPackage(fooPackage, "bar");

Creating Primitive Types

A primitive type defines a predefined data type, without any relevant substructure. Primitive types used in UML™ itself include Boolean, Integer, Real, String, and UnlimitedNatural. To create a primitive type using the UML editor, follow these steps:

  1. Select a package (i.e., <Model> epo2) in the UML editor.
  2. Select the New Child > Owned Type > Primitive Type option from the context menu.
  3. Enter a value (i.e., “int”) for the Name property in the Properties view.

GSWU2 tryit.gif Create the remaining primitive types from the ExtendedPO2 model using the UML editor.

At this point your workspace should look something like this:

GSWU2 CreatingPrimitiveTypes.png

Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns a primitive type with a specified name in a specified package.

     protected static PrimitiveType createPrimitiveType(org.eclipse.uml2.uml.Package package_, String name) {
         PrimitiveType primitiveType = (PrimitiveType) package_.createOwnedPrimitiveType(name);
 
         out("Primitive type '" + primitiveType.getQualifiedName() + "' created.");
 
         return primitiveType;
     }

Here we call the createOwnedPrimitiveType(String) convenience factory method to ask the package to create a primitive type with the specified name as one of its packaged elements.

OK, let’s see this method in action. For example, we could create a primitive type named ‘int’ in model ‘epo2’ as follows:

         PrimitiveType intPrimitiveType = createPrimitiveType(epo2Model, "int");

GSWU2 tryit.gif Write code to programmatically create the remaining primitive types from the ExtendedPO2 model.

Creating Enumerations

An enumeration is a kind of data type whose instances may be any of a number of user-defined enumeration literals. To create an enumeration using the UML editor, follow these steps:

  1. Select a package (i.e., <Model> epo2) in the UML editor.
  2. Select the New Child > Owned Type > Enumeration option from the context menu.
  3. Enter a value (i.e., “OrderStatus”) for the Name property in the Properties view.

At this point your workspace should look something like this:

[GSWU2_CreatingEnumerations.png]

Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns an enumeration with a specified name in a specified package.

     protected static Enumeration createEnumeration(org.eclipse.uml2.uml.Package package_, String name) {
         Enumeration enumeration = (Enumeration) package_.createOwnedEnumeraton(name);
 
         out("Enumeration '" + enumeration.getQualifiedName() + "' created.");
 
         return enumeration;
     }

Here we call the createOwnedEnumeration(String) convenience factory method to ask the package to create a primitive type with the specified name as one of its packaged elements.

OK, let’s see this method in action. For example, we could create an enumeration named ‘OrderStatus’ in model ‘epo2’ as follows:

         Enumeration orderStatusEnumeration = createEnumeration(epo2Model, "OrderStatus");

Creating Enumeration Literals

An enumeration literal is a user-defined data value for an enumeration. To create an enumeration literal using the UML editor, follow these steps:

  1. Select an enumeration (i.e., <Enumeration> OrderStatus) in the UML editor.
  2. Select the New Child > Owned Literal > Enumeration Literal option from the context menu.
  3. Enter a value (i.e., “Pending”) for the Name property in the Properties view.

GSWU2 tryit.gif Create the remaining enumeration literals from the ExtendedPO2 model using the UML editor.

At this point your workspace should look something like this:

GSWU2 CreatingEnumerationLiterals.png

Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically creates and returns an enumeration literal with a specified name in a specified enumeration.

     protected static EnumerationLiteral createEnumerationLiteral(Enumeration enumeration, String name) {
         EnumerationLiteral enumerationLiteral = enumeration.createOwnedLiteral(name);
 
         out("Enumeration literal '" + enumerationLiteral.getQualifiedName() + "' created.");
 
         return enumerationLiteral;
     }

Here we call a createOwnedLiteral(String) convenience factory method to ask the enumeration to create an enumeration literal with the specified name as one of its owned literals.

OK, let’s see this method in action. For example, we could create an enumeration literal named ‘Pending’ in enumeration ‘OrderStatus’ as follows:

         createEnumerationLiteral(orderStatusEnumeration, "Pending");

GSWU2 tryit.gif Write code to programmatically create the remaining enumeration literals from the ExtendedPO2 model.

Creating Classes