# Acceleo Tutorial

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

Copyright © 2008, 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.

To create a new Acceleo project, you can use the Acceleo Module Project wizard.

On the Acceleo perspective, you can also right click on the package explorer view and select New->Acceleo Module Project.

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

Next you can create a new Acceleo template.

- Select the folder in witch you want to create the new template.
- Then type the template name
- Then choose the metamodel that will be used by the template ( UML in this example )
- Finally choose the metaclass that will be used to generate the file. ( Class )

Optionally you can browse an existing file, which will be copied into the template. This could be useful to create a template from an existing file.

Clicking on finish will create the template(s), and some files associated with it ( more on these below ).

# The template editor

The template editor provides the following features :

- Syntax highlighting
- Content assistant
- Error highlighting
- Dynamic outline
- Code folding

For more information about the Acceleo syntax, please read the official OMG specification : <http://www.omg.org/docs/ptc/07-08-16.pdf>

# Creating a template for generating java beans

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

    [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 :

The resulting java file generated by this template will look like this :

# The java file

Note : This feature may evolve in future releases.

Next to each template file, 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 the main() method of the file.

# The ant task

Note : This feature may evolve in future releases.

In the tasks folder can also be found 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 a project which contains the model to generate from, rename it to build.xml for example, then change the MODEL and TARGET properties. Launch the build via External Tools > Run As > Ant Build.