Tigerstripe M0-Based Generation
< To: Tigerstripe
While typical MDD approaches initially consider generating content out of model, it is also very useful and convenient to be able to generate content from objects of a model, i.e. using instances of classes defined in the model.
This feature was introduced in Tigerstripe since 0.3.
This article presents a brief overview of how to set up a M0-Level Generation Plugin, run it on an instance diagram to generate content.
Note: the projects used in this article can be downloaded from M0-gen-sample.zip.
We need a Model Project to start with. We will use a simple example as shown below:
Out of this model, we have created an instance diagram as shown below.
From this instance diagram, we will generate an XML snippet containing the details of each object. To do that, we need to
- Create a M0-Level Generation Plugin
- Create a global rule in this Generation Plugin to walk the content of an instance diagram and generate an XML file.
- Deploy the generation plugin
- Trigger it on our sample Instance Diagram
Creating a M0-Level Generation Plugin
Let's open the new M0-Level Generation Plugin wizard, by selecting File->New->Other->Tigerstripe M0-Level Generator Project
M0-Level Generator projects are similar to M1-Level Generators Project in that they host a descriptor (m0-generator.xml), a src/ directory that may optionally contain Java code to use in the generator, and a templates/ directory to host any template used for generation.
Let's create a template first. Right-click->New->File on the templates/ directory. Let's call it xml.vm (note that the .vm extension is required).
You can use the following for content
<root> #foreach( $cInstance in $classInstances ) <instance name="$cInstance.InstanceName" class="$cInstance.Artifact.FullyQualifiedName"> #foreach( $variable in $cInstance.Variables ) <variable name="$variable.Name" value="$variable.Value"/> #end </instance> #end #foreach( $association in $associationInstances ) <association name="$association.Name" class="$association.Artifact.FullyQualifiedName"> <aEnd name="$association.AEnd.InstanceName"/> <zEnd name="$association.ZEnd.InstanceName"/> </association> #end </root>
There are 2 additional entries in the Velocity context of an M0 Global rule compared to a M1 Global rule:
- $classInstances containing a List<ClassInstance> (see javadoc at http://www.eclipse.org/tigerstripe/documentation/javadoc/)
- $associationInstances containing a List<AssociationInstance>(see javadoc at http://www.eclipse.org/tigerstripe/documentation/javadoc/)
In the template above, we are simply iterating over the content of both these lists to populate the content of generated file.
Let's now add a global rule to the Generator by opening the m0-generator.xml descriptor and by navigating to the "Rules" tab. Add a global rule, let's name it XML. The 2 required attributes on that rule are:
- the template: xml.vm, i.e. the template we just created above.
- the output file: output.xml, as the file to create.
Save, and navigate back to the Overview tab of the m0-generator.xml descriptor to locate the "Package and Deploy this Plugin" hyperlink in order to deploy the M0 Generator Plugin. Hit it. A dialog box should confirm that the plugin was successfully deployed.
Triggering a M0 Generator Plugin
Once a M0 Generator plugin has been deployed, it needs to be enabled in the Model Project where you want to trigger it. To do so, similarly to a M1 Generator Plugin, open the corresponding Model project descriptor (tigerstripe.xml), navigate to the "Plugin Settings" tab, and enable the M0 Generator plugin.
Make sure you save the change to the tigerstripe.xml descriptor.
Finally, open the instance diagram you want to trigger this M0 Generator plugin on, right-click on the diagram and select "Generate...". A wizard will open, the M0 Plugin should be pre-selected. Click finish.
A target/tigerstripe.gen/output.xml file should have been generated with the following content (for the instance diagram above):
<root> <instance name="e1" class="com.mycompany.Equipment"> <variable name="state" value="active"/> <variable name="id" value="e1-0"/> </instance> <instance name="e2" class="com.mycompany.Equipment"> <variable name="state" value="inactive"/> <variable name="id" value="e2-100"/> </instance> <instance name="h1e1" class="com.mycompany.Holder"> </instance> <instance name="h2e1" class="com.mycompany.Holder"> </instance> <instance name="h1e2" class="com.mycompany.Holder"> </instance> <association name="contains" class="com.mycompany.contains"> <aEnd name="e1"/> <zEnd name="h1e1"/> </association> <association name="contains" class="com.mycompany.contains"> <aEnd name="e1"/> <zEnd name="h2e1"/> </association> <association name="contains" class="com.mycompany.contains"> <aEnd name="e2"/> <zEnd name="h1e2"/> </association> </root>
- As of early 0.3 builds, the meta-model presented in the velocity complex is the raw model from the instance diagram. This should be refined into a meta-model more integrated with the new EMF-based metamodel for Tigerstripe
- This article focused on generating content based on instances defined in a diagram. Once a proper EMF metamodel for instance is available, it should be possible to define bigger instance contexts, so that M0 generation can be triggered outside of an Instance Diagram.
- Only global instance rules are implemented at this stage.