Visual software modelling languages, specifically UML, receive a lot of criticism from software analysts/developers for their poor choice of symbols, insanely complex specification, and incomprehensible (meta)-metamodel. But the underlying ideas are very powerful. Tools like Eclipse Sirius let you define your own custom modelling language to allow users to model a particular domain and to automatically generate code/data from these models.

Sirius already provides tutorials to get started defining your own modelling language here. But what do we do with the models that users create? Are they just pretty pictures? In this post, I’ll provide a tutorial on how to generate code/data from the models created in Eclipse Sirius by using the Acceleo plugin. It’s closely based on the Acceleo getting started tutorial here, but I’ll show how to use models defined in your own custom modelling language rather than assuming that you want to generate code from a UML model. The tutorial will assume you are using the ObeoDesigner Community 11.3.2 Eclipse distribution, which comes with Sirius, example projects, and the Acceleo plugin pre-installed.

The first step is to define the metamodel that you will use. For this tutorial I’ll use the sample Basic Family Tree Metamodel Definition (file > new > example) that defines the concepts such as Person, Man, Woman, etc.

You will also need a concrete model defined using the Family Tree Metamodel. Let’s use the sample family tree for Grandpa Paul & Grandma Isa:

Note: To create/edit/view the model you would usually run the family editor project which launches as a separate Eclipse session for model editing with its own workspace. However, for this tutorial, it’s easiest if everything is all imported into the same workspace.

We now need to switch to the Acceleo perspective. If using ObeoDesigner, Acceleo should already be installed by default.

Create a new Acceleo Project (File > New > Acceleo Project). In contrast to the Acceleo gettting started tutorial that uses UML as the metamodel, we will provide our own. To find the Metamodel URI, have a look at the eNS_URI variable in the metamodel source code, or open a model file in a text editor and check the xmlns namespace used. For the Basic Family Tree Metamodel, the Metamodel URI used to identify it is: http://www.eclipse.org/sirius/sample/basicfamily

Note: The Metamodel URI is just a way to uniquely identify the metamodel and doesn’t have to actually exist on the web. E.g. if you try to go there in a browser, you will just get a 404 error.

Eclipse will generate a sample project for us with the skeleton to perform a model to text transformation. This file allows us to do code/data generation for any models (e.g. family trees) defined in terms of the metamodel.

For this example, we will transform the concrete family model for Grandpa Paul & Grandma Isa:

Note: If you don’t see the model in the list, make sure that the concrete model you want to transform is imported into the workspace.

You can try to run the project now, but will probably get an error. As we are using a custom metamodel, there’s a little extra work needed to register the metamodel implementation corresponding to the metamodel URI we provided.

First add the metamodel to the dependencies:

Then modify the registerPackages function to register the metamodel:

resourceSet.getPackageRegistry().put(
    org.eclipse.sirius.sample.basicfamily.BasicfamilyPackage.eNS_URI,
    org.eclipse.sirius.sample.basicfamily.BasicfamilyPackage.eINSTANCE);

The default model transformation will generate a blank file for each Family (or may crash as the example family leaves the name field for the Family blank). Here’s a more complete example that generates a JSON file containing a list of people in the family:

[comment encoding = UTF-8 /]
[module generate('http://www.eclipse.org/sirius/sample/basicfamily')]


[template public generateElement(aFamily : Family)]
[comment @main/]
[file (aFamily.toString().concat('.json'), false, 'UTF-8')]
{
  "family": "[aFamily.name/]",
  "people": ['['/]
  [for (p:Person | aFamily.members) separator(',\n')]
    "[p.name/]"[/for]

  [']'/]
}
[/file]
[/template]

Note: See the Acceleo User Guide for an explanation of the syntax. Acceleo also supports autocomplete based on the metamodel.

Generated result:

In this case we just generated JSON. However, it demonstrates the power of model driven development. For example, we could use Sirius to define our own metamodel for apps with concepts such as Data Source, View, etc., ask customers to graphically model their app ideas using models defined in terms of the metamodel, then use Acceleo to automatically generate Android code from the models.