MDT - UML2 - Introduction To UML2 Profiles - Eclipsepedia
MDT - UML2 - Introduction To UML2 Profiles - Eclipsepedia
Download (https://eclipse.org/downloads/)
(https://www.eclipse.org/)
Copyright © 2004, 2014 International Business Machines Corp., CEA, and others.
Contents
1 Summary
2 Prerequisites
3 Introduction
4 Getting Started
5 Creating Profiles
6 Importing Primitive Types
7 Creating Enumerations
8 Creating Enumeration Literals
9 Creating Stereotypes
10 Creating Stereotype Generalizations
11 Creating Stereotype Properties
12 Referencing Metaclasses
13 Creating Extensions
14 Defining Profiles
15 Saving Profiles
16 Loading Models
17 Applying Profiles
18 Applying Stereotypes
19 Accessing Stereotype Property Values
20 Conclusion
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 1/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
21 References
Summary
This article describes how to work with profiles using the UML2 plug-ins for Eclipse. In particular, it gives an overview of how to create and apply profiles (and
their contents) both programmatically and by using the sample UML editor.
Kenn Hussey and James Bruck
Last Updated: January 21, 2014
Prerequisites
To start using UML2 (and to follow along with the example in this article), you must have Eclipse (http://www.eclipse.org/eclipse/), EMF
(http://www.eclipse.org/modeling/emf/?project=emf), and UML2 (http://www.eclipse.org/modeling/mdt/?project=uml2) installed. You can either download the
Modeling Tools Package (http://www.eclipse.org/downloads/packages/eclipse-modeling-tools/lunam4) or follow these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 2/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:GSWU2_Prerequisites.png)
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 3/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
Introduction
This article will walk you through the basics of creating and applying profiles using UML2. Using a subset of the Ecore profile (see below) and the model we
described in the “Getting Started with UML2” article [1] (the ExtendedPO2 model, shamelessly “borrowed” from the EMF “bible” [2]) as an example, we’ll look at
what’s involved in creating some of the more common elements that make up a profile. For each type of element, we’ll first explain the creation process using
the sample UML editor and explore how to accomplish the same thing using Java code. Then we’ll look at what’s involved in applying profiles and stereotypes
to models. The ExtendedPO2 model is shown below.
(/File:GSWU2_Introduction.png)
The Ecore profile that we will develop is as follows, comprising only the subset of stereotypes dealing with properties.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 4/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_Ecore_Profile.png)
Getting Started
(/File:GSWU2_tip.gif) Readers who don't want to follow every step of this tutorial may install a working solution from the New → Example... wizard,
selecting the UML2 Example Projects → Introduction to UML2 Profiles sample. This will be available when Enhancement 382342
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=382342) is resolved and released in a UML2 build. This includes the finished profile, complete source code,
and a launch configuration that runs the stand-alone Java application which creates the profile in the root folder of the example project, applies it to a test
model, and manipulates stereotype attributes ("tagged values").
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 5/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_ExampleWizard.png)
Before getting started, you’ll need to create a simple project in your workspace. This project will serve as the container for the profile 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.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 6/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 7/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 8/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_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 profiles, we’ll assume that
you’ve created a Java class (named, say, “IntroductionToUML2Profiles”) in which you can write some code to construct our sample profile. The code snippets
we’ll show assume you’ve defined the following utility methods to give the user information on the program’s status:
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 right, then! In each of the following subsections, we’ll look at how to create or manipulate a different kind of UML element, starting with profiles.
Creating Profiles
At the root of every UML2 profile is a profile element. It defines limited extensions to a reference metamodel (i.e., UML) with the purpose of adapting the
metamodel to a specific platform or domain (e.g., EMF). Metaclasses from the reference metamodel are extended via stereotypes, which are defined as part of
profiles. To create a profile using the UML editor, follow these steps:
1. Select a project (e.g., Introduction to UML2 Profiles) 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 (e.g., “Ecore.profile.uml”) and press the Next > button.
4. Select Profile for the model object and press the Finish button.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 9/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
5. Select the Window > Show View > Properties menu item.
6. Select the <Profile> element in the UML editor.
7. Enter a value (e.g., “ecore”) for the Name property in the Properties view.
8. (optional) Enter a value (e.g., "http://www.eclipse.org/schema/UML2/examples/ecore") for the URI property in the Properties view.
(/File:GSWU2_tip.gif) In UML™, packages may be identified uniquely by assigning URIs. In UML2 profiles, this URI is used as the namespace URI
of the Ecore EPackage representing the profile metadata in EMF. By specifying this URI explicitly, you can establish a stable namespace URI that will be used
by all successive iterations of your profile schema as it evolves over time. If you omit the URI, one will be generated automatically by UML2 in the Ecore
definition. This default namespace URI is suffixed by an increasing version number, resulting in a different URI in each successive definition. A consequence of
this is that existing models that have an older profile definition applied will have to be migrated to the new version when it is deployed. This is useful when
incompatible changes are made in the profile from one version to the next. You may, of course, also change the explicitly set URI yourself, when required.
(/File:GSWU2_tip.gif) By convention, resources that contain profiles end with a .profile.uml file extension in UML2.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 10/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 11/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingProfiles.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 profile with a
specified name.
return profile;
}
First, we ask the UML factory singleton to create a profile, and we set its name and URI. Then, we output information to the user to let them know that the
profile has been successfully created. Finally, we return the profile. 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.
OK, let’s see this method in action. For example, we could create a profile named ‘ecore’ as follows:
(/File:GSWU2_tryit.gif) Import the other required primitive type (PrimitiveTypes::String) into the Ecore profile using the UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 12/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 13/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_ImportingPrimitiveTypes.png)
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically imports the primitive type with a
specified name from the UML primitive types library into a specified package and returns it.
package_.createElementImport(primitiveType);
return primitiveType;
}
Here we load the model library containing the UML primitive types (Boolean, Integer, Real, String, and UnlimitedNatural) using a load(...) utility method
(described later) and a URI defined on the UMLResource interface. Next, we retrieve the desired (owned) primitive type from the model by name using one of
the convenience methods defined in the UML2 API. Finally, we invoke another convenience method to create the element import relationship between the
package and the element (with default public visibility), notify the user, and return the primitive type.
(/File:GSWU2_tip.gif) The UML resources plug-in (org.eclipse.uml2.uml.resources) provides several model libraries (which by convention have a
.library.uml file extension) that contain commonly used primitive types, such as those defined by Java and EMF (in addition to those defined by UML™ itself).
These libraries can be accessed using URIs defined on the UMLResource interface, as shown above.
(/File:GSWU2_tip.gif) In UML2, a method of the form get<feature name>(String) exists for every feature that can contain or reference named
elements. In this case, the package has a feature (ownedType) that can contain types, so we pass the unqualified name of the type we are looking for to the
getOwnedType(String) convenience method. Behind the scenes, the package will iterate over all of its owned types and return the first one that it finds that
has the specified (unqualified) name.
OK, let’s see this method in action. For example, we could import the primitive type named ‘Boolean’ into profile ‘ecore’ as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically import the other required primitive type (i.e., ‘String’) into the Ecore profile.
Creating Enumerations
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 14/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
As with packages, profiles can contain 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:
(/File:GSWU2_tryit.gif) Create the other enumeration (i.e., “FeatureKind”) for the Ecore profile using the UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 15/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 16/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingEnumerations.png)
Let’s look at how to perform the same task using Java code. The code snippet below (which should be familiar from the Getting Started with UML2
(/MDT/UML2/Getting_Started_with_UML2) article) shows a method that programmatically creates and returns an enumeration with a specified name in a
specified package.
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 ‘VisibilityKind’ in profile ‘ecore’ as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically create the other enumeration (i.e., ‘FeatureKind’) for the Ecore profile.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 17/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 18/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_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.
return enumerationLiteral;
}
Here we call the 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 ‘Unspecified’ in enumeration ‘VisibilityKind’ as follows:
createEnumerationLiteral(visibilityKindEnumeration, "Unspecified");
(/File:GSWU2_tryit.gif) Write code to programmatically create the remaining enumeration literals (i.e., ‘None’, ‘ReadOnly’, ‘ReadWrite’,
‘ReadOnlyUnsettable’, and ‘ReadWriteUnsettable’ in enumeration ‘VisibilityKind’; ‘Unspecified’, ‘Simple’, ‘Attribute’, ‘Element’, ‘AttributeWildcard’,
‘ElementWildcard’, and ‘Group’ in enumeration ‘FeatureKind’) for the Ecore profile.
Creating Stereotypes
A stereotype defines how an existing metaclass may be extended, and enables the use of platform- or domain-specific terminology or notation in place of, or in
addition to, the ones used for the extended metaclass. Each stereotype may extend one or more classes through extensions as part of a profile. To create a
stereotype using the UML editor, follow these steps:
(/File:GSWU2_tryit.gif) Create the remaining stereotypes for the Ecore profile (i.e., “EAttribute” and “EReference”) using the UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 19/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 20/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingStereotypes.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(n) (abstract)
stereotype with a specified name in a specified profile.
return stereotype;
}
Here we call the createOwnedStereotype(String, boolean) convenience factory method to ask the profile to create a stereotype with the specified name as
one of its owned members, and set the isAbstract attribute of the stereotype based on the specified boolean argument.
OK, let’s see this method in action. For example, we could create an abstract stereotype named ‘EStructuralFeature’ in profile ‘ecore’ as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically create the remaining (non-abstract) stereotypes (i.e., ‘EAttribute’ and ‘EReference’) for the
Ecore profile.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 21/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 22/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingStereotypeGeneralizations.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 generalization
between specified specific and general classifiers.
return generalization;
}
Here we call a convenience factory method on the specific classifier that creates a generalization as one of its children and sets the general classifier to the
specified argument.
OK, let’s see this method in action. For example, we could create a generalization between specific stereotype ‘EAttribute’ and general stereotype
‘EStructuralFeature’ as follows:
createGeneralization(eAttributeStereotype, eStructuralFeatureStereotype);
(/File:GSWU2_tryit.gif) Write code to programmatically create the other generalization (i.e., between ‘EReference’ and ‘EStructuralFeature’) for the
Ecore profile.
selected via the Default property in the Properties view. For attributes typed by the UML™ standard primitive types or by enumeration types, the Default
property in the property sheet provides assistance in creating value specifications of the appropriate type. For other types (e.g., custom data types or classes),
it is necessary to explicitly create the Default Value value specification.
(/File:GSWU2_tryit.gif) Create the remaining stereotype properties for the Ecore profile (i.e., “isUnsettable”, “isVolatile”, “visibility”, “xmlName”,
“xmlNamespace”, and “xmlFeatureKind” for <Stereotype> EStructuralFeature; “attributeName” for <Stereotype> EAttribute; “referenceName” and
“isResolveProxies” for <Stereotype> EReference) using the UML editor.
(/File:GSWU2_tip.gif) Be sure to set the appropriate default value for each stereotype property so that it is consistent with the corresponding
property in Ecore. In particular, the default value for the “isResolveProxies” should be a literal Boolean with a value of true instead of the default (!) default
value of false. Note also that the types of the “visibility” and “xmlFeatureKind” properties should be the enumerations we created earlier (i.e., ecore::Visibility
and ecore::FeatureKind, respectively).
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 24/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 25/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingStereotypeProperties.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 attribute
with a specified upper bound, lower bound, type, name, and (optionally) default value in a specified class.
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);
return attribute;
}
Here we call the createOwnedAttribute(String, Type, int, int) convenience factory method to ask the class to create a property as one of its owned attributes,
set the type of the attribute to the specified type, and set the lower and upper bounds of the attribute (the factory method creates a literal integer and literal
unlimited natural, respectively, and sets its value to the specified integer value). If a default value is provided, we create a value specification of the appropriate
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 26/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
type (in this example, only booleans, strings, and enumeration literals are handled).
(/File:GSWU2_tip.gif) The LiteralUnlimitedNatural.UNLIMITED constant represents the unlimited value for upper bounds (-1), as it does in EMF.
OK, let’s see this method in action. For example, we could create an attribute with multiplicity 0..1 of type ‘UMLPrimitiveTypes::Boolean’ named ‘isTransient’ in
stereotype ‘EStructuralFeature’ as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically create the remaining stereotype properties (i.e., ‘isUnsettable’, ‘isVolatile’, ‘visibility’,
‘xmlName’, ‘xmlNamespace’, and ‘xmlFeatureKind’ in stereotype ‘EStructuralFeature’; ‘attributeName’ in stereotype ‘EAttribute’; ‘referenceName’ and
‘isResolveProxies’ in stereotype ‘EReference’) for the Ecore profile.
Referencing Metaclasses
A profile is a restricted form of a metamodel that must always be related to a reference metamodel (e.g., UML). A profile cannot be used without its reference
metamodel; it defines a limited capability to extend metaclasses of the reference metamodel via stereotypes. Profiles can be made to reference metaclasses
from metamodels by creating an import relationship between the profile and the reference metaclass. To reference a metaclass using the UML editor, follow
these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 27/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 28/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_ReferencingMetaclasses.png)
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically references the metaclass with a
specified name in the UML metamodel from a specified profile and returns it.
profile.createMetaclassReference(metaclass);
return metaclass;
}
Here we load the metamodel containing the UML metaclasses using a load(...) utility method (described later) and a URI defined on the UMLResource
interface. Next, we retrieve the desired (owned) metaclass from the (meta)model by name using the convenience method. Finally, we invoke another
convenience method to create the element import relationship between the profile and the metaclass, notify the user, and return the metaclass.
(/File:GSWU2_tip.gif) The UML resources plug-in (org.eclipse.uml2.uml.resources) provides two metamodels (which by convention have a
.metamodel.uml file extension), UML and Ecore. These metamodels can be accessed using URIs defined on the UMLResource interface, as shown above.
OK, let’s see this method in action. For example, we could reference the metaclass named ‘Property’ from profile ‘ecore’ as follows:
Creating Extensions
Extensions are used to indicate that the properties of metaclasses are extended through stereotypes, and give the ability to flexibly apply (and later unapply)
stereotypes to elements. An extension is a kind of association, one end of which is an ordinary property, and the other is an extension end. An extension may
be required (depending on the lower bound of the extension end), which indicates that an instance of the extending stereotype must be created whenever an
instance of the extended metaclass is created. To create an extension using the UML editor, follow these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 29/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:GSWU2_tryit.gif) Create the other extension (i.e., between UML::Property and <Stereotype> EReference) for the Ecore profile using the
UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 30/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 31/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_CreatingExtensions.png)
(/File:GSWU2_tip.gif) You’ll notice that a number of new elements have appeared in the UML editor. In particular, you’ll see a new stereotype
property with a name of the form base_<metaclass name>, an extension with a name of the form <metaclass name>_<stereotype name>, and an extension
end with a name of the form extension_<stereotype name>. It’s important that these elements not be modified, since the UML2 profile mechanism depends
on these constructs.
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(n) (required)
extension between a specified metaclass and a specified stereotype.
return extension;
}
Here we call a convenience method on the stereotype that creates an extension (and its ends) between it and a metaclass as one of its siblings (i.e., as a child
of its profile namespace). Behind the scenes, the stereotype also creates the ends of the extension, resulting in a new property on the stereotype (with a
special name) and an extension end owned by the extension (again, with a special name).
OK, let’s see this method in action. For example, we could create a non-required extension between metaclass ‘Property’ and stereotype ‘EAttribute’ in profile
‘ecore’ as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically create the other extension (i.e., between metaclass ‘Property’ and stereotype ‘EReference’)
for the Ecore profile.
Defining Profiles
There. We’ve finished creating (a scaled down version of) the Ecore profile, and we’re ready to start using it. But before we can, there’s one final thing we need
to do: define it. Since a profile effectively represents an augmentation of a reference metamodel (UML), in order for the extensions we’ve defined to appear as
though they’re part of the UML metamodel, they need to be “defined” at the meta-metamodel (i.e., Ecore) level. The implementation of profile support in UML2
supports defining both dynamic and static profile representations. In this article, we'll focus on the former.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 32/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
When defining a dynamic profile representation, the contents of a profile are converted to an equivalent Ecore format that is stored as an annotation on the
profile. Then, when a profile and its stereotypes are applied to a model and its elements, dynamic EMF (see the EMF book for details) is used to store property
values for the stereotypes. For the most part, you can ignore this complexity, as long as you remember to define your profile before using it. To define a
dynamic profile representation using the UML editor, follow these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 33/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 34/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_DefiningProfiles.png)
You’ll notice that an annotation with source "http://www.eclipse.org/uml2/2.0.0/UML" has been attached to the profile. It contains the generated Ecore
representation (an Ecore package with classes, attributes, enums, etc.) of the profile. As with extensions, it’s important that this annotation (and its contents)
not be modified, since the UML2 profile mechanism depends on these constructs.
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically defines a specified profile.
Here we call a convenience method on the profile that generates the Ecore representation of the profile and increments its version.
OK, let’s see this method in action. For example, we could define the ‘ecore’ profile as follows:
defineProfile(ecoreProfile);
Saving Profiles
Now that we’ve spent all this time creating and defining a profile, we’d better save our work. When we created our profile using the UML model wizard, a UML
resource was created for us, so now all that needs to be done is to serialize the contents of our profile as XMI to a file on disk (i.e., Ecore.profile.uml). To save
a profile using the UML editor, follow these steps:
It’s that simple. Programmatically, we have a bit more work to do because so far, we’ve been creating our profile in a vacuum, i.e., without a containing
resource. The code snippet below shows a method that saves a specified package to a resource with a specified URI.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 35/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
static {
// Create a resource-set to contain the resource(s) that we load and
// save
RESOURCE_SET = new ResourceSetImpl();
// And save.
try {
resource.save(null);
out("Done.");
} catch (IOException ioe) {
err(ioe.getMessage());
}
}
Here we use a statically initialized resource set to create a resource with the specified URI, add the package to the resource’s contents, and ask the resource
to save itself using the default options. If an exception occurs, we notify the user via our handy utility method.
(/File:GSWU2_tip.gif) The above example uses the UMLResourcesUtil.init(...) API introduced in UML2 4.0.0 for the Juno release. As commented in
the code snippet, this is not required in code running in the Eclipse Platform run-time, as in that case these registrations are discovered automatically from
extension points.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 36/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
OK, let’s see this method in action. For example, we could save the ‘ecore’ profile to a resource with URI ‘Ecore.profile.uml’ (relative to a local filesystem path
passed in as an argument) as follows:
save(ecoreProfile, URI.createFileURI(args[0]).appendSegment("Ecore").appendFileExtension(UMLResource.PROFILE_FILE_EXTENSION));
(/File:GSWU2_tip.gif) The UMLResource.PROFILE_FILE_EXTENSION constant represents the file extension for UML2 profiles (.profile.uml).
Loading Models
In order to make use of our profile, we’ll need to open a model and load the profile. We’ll use the ExtendedPO2 model that was created in the Getting Started
with UML2 (/MDT/UML2/Getting_Started_with_UML2) article (you’ll need to copy it into your project and refresh the workspace first).
To open a model using the UML editor, follow these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 37/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 38/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_LoadingModels.png)
Programmatically, we have a bit more work to do. The code snippet below shows a method that loads a package from a resource with a specified URI.
try {
// Load the requested resource
Resource resource = RESOURCE_SET.getResource(uri, true);
return package_;
}
Here we obtain a resource with the specified URI from our statically initialized resource set, asking that it be loaded on demand. Next, we use an EMF utility
method to obtain the first object of type Package from the resource’s contents. If an exception occurs, we notify the user via our handy utility method. Finally, if
all is well, we return the package.
(/File:GSWU2_tip.gif) The EcoreUtil class (provided by EMF) defines a number of utilities that you may find quite useful when working with EMF-
based resources.
OK, let’s see this method in action. For example, we could load the ‘epo2’ model from a resource with URI ‘ExtendedPO2.uml’ (relative to a filesystem path
passed in as an argument) as follows:
Applying Profiles
OK, our profile has been created, defined, saved, and loaded, and we’re ready to apply it to our model. Applying a profile means that it is allowed (but not
necessarily required) to apply the stereotypes that are defined in the profile to elements in the package. A profile application is a special type of package import
that indicates that a profile has been applied to a package. To apply a profile to a package using the UML editor, follow these steps:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 39/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:GSWU2_tip.gif) Be sure to pick the profile we’ve created instead of the built-in profile provided by the UML resources plug-in (i.e., ecore, not
Ecore).
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 40/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 41/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_ApplyingProfiles.png)
(/File:GSWU2_tip.gif) You’ll notice another annotation (with source "http://www.eclipse.org/uml2/2.0.0/UML") has been attached, in this case to keep
track of the Ecore representation for the definition of the profile that is currently applied to the package. Again, it’s important that this annotation not be
modified, since the UML2 profile mechanism depends on this construct. Note that a newer definition of the profile can be applied using the same menu item,
and a profile (along with all of its stereotypes) can be unapplied using the UML Editor > Package > Unapply Profile... menu item.
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically applies a specified profile to a
specified package.
Here we call a convenience method on the package that creates a profile application on the package and sets the profile as the imported profile.
OK, let’s see this method in action. For example, we could apply the ‘ecore’ profile to the ‘epo2’ model as follows:
applyProfile(epo2Model, ecoreProfile);
Applying Stereotypes
We’re on the home stretch now. Once a profile has been applied to a package, stereotypes defined in the profile can be applied to instances of the appropriate
metaclasses (as per the defined extensions). When a stereotype is applied to an element, that element is effectively extended with the properties that are
defined as part of the stereotype. To apply a stereotype to an element using the UML editor, follow these steps:
1. Select an element (e.g., <Property> pendingOrders : PurchaseOrder [0..*] in <Class> Supplier) in the UML editor.
2. Select the UML Editor > Element > Apply Stereotype... menu item.
3. Choose a stereotype (e.g., ecore::EReference), press the Add button, then press the OK button.
(/File:GSWU2_tryit.gif) Apply the appropriate stereotypes to other properties (<Property> shippedOrders : PurchaseOrder [0..*] in <Class>
Supplier; <Property> totalAmount : int [0..1], <Property> previousOrder : PurchaseOrder [0..1], and <Property> customer : Customer in <Class>
PurchaseOrder; <Property> orders : PurchaseOrder [0..*] in <Class> Customer) in the ExtendedPO2 model using the UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 42/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 43/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_ApplyingStereotypes.png)
(/File:GSWU2_tip.gif) A stereotype (and its tagged values) can be unapplied using the UML Editor > Element > Unapply Stereotype... menu item.
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically applies a specified stereotype to a
specified (named) element.
Here we call a convenience method on the element that creates an instance of the Ecore class representing the specified stereotype (using dynamic EMF) and
attaches it to the element using an annotation.
OK, let’s see this method in action. For example, we could apply the ‘EReference’ stereotype to the ‘pendingOrders’ property of the ‘Supplier’ class in the ‘epo2’
model as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically apply the appropriate stereotypes to other properties (i.e., ‘epo2::Supplier::shippedOrders’,
‘epo2::PurchaseOrder::totalAmount’, ‘epo2::PurchaseOrder::previousOrder’, ‘epo2::PurchaseOrder::customer’, and ‘epo2::Customer::orders’) in the
ExtendedPO2 model.
1. Select the element to which the stereotyped has been applied (i.e., <<EReference>> <Property> pendingOrders : PurchaseOrder [0..*] in <Class>
Supplier) in the UML editor.
2. Enter or select a value (i.e., false) for the property (i.e., Is Resolve Proxies) under the category named for the stereotype (i.e., EReference) in the
Properties view.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 44/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:GSWU2_tryit.gif) Get and set values for other stereotype properties (i.e., Is Transient and Is Volatile under EReference for <<EReference>>
<Property> pendingOrders : PurchaseOrder [0..*] in <Class> Supplier; Is Resolve Proxies, Is Transient, and Is Volatile under EReference for
<<EReference>> <Property> shippedOrders : PurchaseOrder [0..*] in <Class> Supplier; Is Transient and Is Volatile under EAttribute for
<<EAttribute>> <Property> totalAmount : int [0..1] in <Class> PurchaseOrder; Is Resolve Proxies under EReference for <<EReference>> <Property>
previousOrder : PurchaseOrder [0..1] in <Class> PurchaseOrder; Is Resolve Proxies under EReference for <<EReference>> <Property> customer :
Customer in <Class> PurchaseOrder; Is Resolve Proxies under EReference for <<EReference>> <Property> orders : PurchaseOrder [0..*] in <Class>
Customer) in the ExtendedPO2 model using the UML editor.
At this point your workspace should look something like this:
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 45/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 46/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
(/File:ITU2P_AccessingStereotypePropertyValues.png)
Let’s look at how to perform the same task using Java code. The code snippet below shows a method that programmatically gets and returns the value of a
specified property of a specified stereotype for a specified element.
out("Value of stereotype property '%s' on element '%s' is %s.", property.getQualifiedName(), namedElement.getQualifiedName(), val
return value;
}
Here we call a convenience method on the (named) element that retrieves the value of a property with a specified name from the dynamically created Ecore
object instance corresponding to the specified applied stereotype, notifies the user, and returns it.
OK, let’s see this method in action. For example, we could get the value of the ‘isVolatile’ property of the ‘EReference’ stereotype for the ‘pendingOrders’
property of the ‘Supplier’ class in the ‘epo2’ model as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically get the values of the other stereotype properties for elements in the ExtendedPO2 model.
The code snippet below shows a method that programmatically sets the value of a specified property of a specified stereotype for a specified element to a
specified value.
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 47/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
protected static void setStereotypePropertyValue(NamedElement namedElement, Stereotype stereotype, Property property, Object value) {
Object valueToSet = value;
Here we call a convenience method on the (named) element that sets the value of a property with a specified name in the dynamically created Ecore object
instance corresponding to the specified applied stereotype and notifies the user.
(/File:GSWU2_tip.gif) Values of primitive types are represented by the usual Java types: String, Boolean, Integer, etc. For values of enumerations,
the UML2 API accepts and returns the EnumerationLiteral instances from the profile model. For convenience, this utility method allows enumeration values to
be set using simply the name of the literal.
OK, let’s see this method in action. For example, we could set the value of the ‘isVolatile’ property of the ‘EReference’ stereotype for the ‘pendingOrders’
property of the ‘Supplier’ class in the ‘epo2’ model to Boolean.TRUE as follows:
(/File:GSWU2_tryit.gif) Write code to programmatically set the values of the other stereotype properties for elements in the ExtendedPO2 model.
Conclusion
Congratulations! If you’ve made it this far, you’ve successfully created and applied a simple profile programmatically and/or using the UML editor. There’s a
whole lot more that could be said, but the purpose of this article was just to introduce you to the concepts. Stay tuned for more articles on how to develop tools
with UML2.
For more information on UML2, visit the home page (http://www.eclipse.org/modeling/mdt/?project=uml2) or join a discussion in the forum
(http://www.eclipse.org/forums/eclipse.modeling.mdt.uml2).
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 48/49
25/04/2023 15:47 MDT/UML2/Introduction to UML2 Profiles - Eclipsepedia
References
[1] K. Hussey and J. Bruck. "Getting Started with UML2 (/MDT/UML2/Getting_Started_with_UML2)”. International Business Machines Corp., CEA, and others,
2004, 2014.
[2] F. Budinsky, D. Steinberg, E. Merks, R. Ellersick, and T. J. Grose. Eclipse Modeling Framework. Pearson Education, Inc., Boston, MA, 2003.
This page was last modified 09:36, 29 May 2014 by Christian W. Damus (/User:Give.a.damus.gmail.com). Based on work by Kenn Hussey
(/User:Kenn.Hussey.gmail.com).
(https://twitter.com/EclipseFdn)
(https://www.facebook.com/eclipse.org)
(https://www.youtube.com/user/EclipseFdn)
(https://www.linkedin.com/company/eclipse-
foundation)
https://wiki.eclipse.org/MDT/UML2/Introduction_to_UML2_Profiles 49/49