Acceleo Tutorial

Authors: Laurent Goubet
Contact: laurent.goubet@obeo.fr

Copyright © 2008, 2009 Obeo™.

New Acceleo project

The aim of an Acceleo project is to generate text from a model. For this tutorial, we will create a new Acceleo module for generating java beans from a UML model.

../images/uml_model_example.png

To create a new Acceleo project, you can right click on the package explorer view then New->Acceleo Project.

../images/new_acceleo_module_project.png

Choose a correct plugin name for the project, then click next.

../images/new_acceleo_module_project_1.png

Next you can create a new Acceleo module file.

../images/new_acceleo_module_project_2.png

You can create more than one module file in this project by using the "Add" button ont the left.

Clicking on finish will create the module file(s), and some files automatically generated from it ( more on these below ).

../images/new_acceleo_module_project_result.png

The Acceleo editor

The module editor provides the following features :

../images/acceleo_editor.png

For more information about the Acceleo syntax, please read the official OMG specification accessible from : <http://www.omg.org/spec/MOFM2T/1.0/>

Creating a module to generate java beans

We would like to create a bean for each of the classes defined in the model. Here is the code for the module file :

[module generate('http://www.eclipse.org/uml2/2.1.0/UML')/]

[template public generate(c : Class)]
        [file (c.name.concat('.java'), false)]
        public class [c.name.toUpperFirst()/] {
        [for (p : Property | c.attribute)]
        private [p.type.name/] [p.name/];
        [/for]
        [for (p : Property | c.attribute)]
        public [p.type.name/] get[p.name.toUpperFirst()/]() {
                return this.[p.name/];
        }
        [/for]
        [for (o : Operation | c.ownedOperation)]
        public [o.type.name/] [o.name/]() {
                // TODO should be implemented
        }
        [/for]
        }
        [/file]
[/template]

As shown below, the content assistant provides choices for the UML metamodel :

../images/acceleo_editor_example.png

The resulting java file generated by launching this module file will look like this :

../images/acceleo_module_java_result.png

The java file

Note : This feature may evolve in future releases.

Next to each module file containing a [comment @main] annotation, a java file with the same name is automatically generated. This allows you to launch the generation via the doGenerate() method. This can be achieved either by creating a new instance of the class, or calling its main() method.

../images/acceleo_java.png

The ant task

Note : This feature may evolve in future releases.

If you create a tasks folder at the root of our project, there will also be generated an ant file which can be used to launch a generation with the specified template. In order to use this build file, copy it inside the project containing the model to generate from, rename it to build.xml for example, then change the MODEL and TARGET properties accordingly. Launch the build via External Tools > Run As > Ant Build.

../images/acceleo_ant.png