Xpand is shipped with a special UML2 profiles metamodel implementation. The implementation maps Stereotypes to Types and Tagged Values to simple properties. It also supports Enumerations defined in the profile and Stereotype hierarchies.
To define a profile, you can use a variety of UML2-based modelling tools. Assuming they do actually correctly create profile definitions (which is not always the case, as we had to learn painfully), creating a profile and exporting it correctly is straight forward.
In this section, we explain the "manual way", which is good for explaining what happens, but completely useless for practical use. You do not want to build models of realistic sizes using the mechanisms explained below.
You start be creating a new UML2 file (as shown above). In the
example we will call it test.profile.uml
. The root
element, however, will be a
Profile
, not a
Package
. Don't forget to actually assign a name to
the profile! It should be test
, too.
The created
Profile
we call
test
. In our case, we want to make the stereotype be
applicable to UML classes – they are defined as part of the UML2
metamodel. So we have to import that metamodel first. So what you do is
to select your profile object, and then go to the UML2 Editor menu (in
the Eclipse menu bar) and select
Profile -> Reference
Metaclass
. Select uml::Class
. Then,
add a stereotype to your profile (right mouse click on the profile ->
New Child
->
Owned
Stereotype
->
Stereotype).
Now you
can define your stereotype: select
Stereotype -> Create
Extension
from the UML2 Editor menu. Select
uml::Class
. This should lead to the following
model. Save it and you are done with the profile definition.
To make any use of the profile, we have to apply it to some kind
of model. To do that, we copy the example.uml
model
to a example-profiled.uml
. We then open that file
and load a resource, namely the profile we just defined. This then looks
somewhat like this:
Now, to make the following stuff work, you first have to select the profile and select the Profile -> Define operation from the UML2 Editor menu. This creates all kinds of additional model elements, about which you should not care for the moment.
Now, finally, you can select your cars
package
(the one from the example model) and select
Package ->
Apply Profile
from the UML2 Editor menu. Select your test
profile to be applied.
For the purpose of this example, you should now apply the test
stereotype to the PersonCar
class. Select the
class, and the select
Element -> Apply
Stereotype
from the UML2 Editor menu. This should result in
the following model:
Note that all the stuff above was not in any way related to Xpand, it was just the "bare bones" means of creating and applying a profile to a UML2 model. Having an UML2 tool capable of storing models as EMF UML2 XMI would make the creation of the model far more easier. Since we cannot assume which UML2 tool you are using this tutorial shows you this way, which would always work without further tooling installed.
There are two things we have to change: The workflow
(specifically, the configuration of the generator component) needs to
know about the profile, and the template needs to generate different
code if a class has the test stereotype applied. Let us look at the
second aspect first. Here is the modified template (in
RootWithProfile.xpt
):
«DEFINE Root FOR uml::Model» «EXPAND PackageRoot FOREACH allOwnedElements().typeSelect(uml::Package)» «ENDDEFINE» «DEFINE PackageRoot FOR uml::Package» «EXPAND ClassRoot FOREACH ownedType.typeSelect(uml::Class)» «ENDDEFINE» «DEFINE ClassRoot FOR uml::Class» «FILE name+".java"» public class «name» {} «ENDFILE» «ENDDEFINE» «DEFINE ClassRoot FOR test::test» «FILE name+".java"» public class «name» {} // stereotyped «ENDFILE» «ENDDEFINE»
As you can see,
the stereotype acts just
like a type
, and even the polymorphic dispatch between the
base type (uml::Class
) and the stereotype
works!
Adapting the workflow file is also straight forward
(workflowWithProfile.mwe
). Here is the modified
model component with the new model
example-profiled.uml
and a extended setup:
<?xml version="1.0"?> <workflow> <!-- set up EMF for standalone execution --> <bean class="org.eclipse.emf.mwe.utils.StandaloneSetup" platformUri=".." /> <!-- prepare for performing uml --> <bean class="org.eclipse.xtend.typesystem.uml2.Setup" standardUML2Setup="true" /> <!--UML2 Profile - Metamodell--> <bean id="mm_profile" class="org.eclipse.xtend.typesystem.uml2.profile.ProfileMetaModel"> <profile value="platform:/resource/xpand.uml2.generator/src/test.profile.uml"/> </bean> <!-- load model and store it in slot 'model' --> <component class="org.eclipse.emf.mwe.utils.Reader"> <uri value="platform:/resource/xpand.uml2.generator/src/example-profiled.uml" /> <modelSlot value="model" /> </component>
And here is the modified generator component:
<component id="generator" class="org.eclipse.xpand2.Generator" skipOnErrors="true"> <metaModel idRef="mm_profile"/> <expand value="templates::Root::Root FOR model"/> <fileEncoding value="ISO-8859-1"/> <outlet path="src-gen"> <postprocessor class="org.eclipse.xpand2.output.JavaBeautifier"/> </outlet> </component> </workflow>
The only thing, we have to do is to add a new metamodel that references the profile we just created.