Jump to: navigation, search

MDT/UML2/Getting Started with UML2

< MDT‎ | UML2
Revision as of 23:41, 11 October 2011 by Kenn.Hussey.gmail.com (Talk | contribs)

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. Right-click and 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