Code templates

 

Introduction to code templates

Code templates are used to generate code or text from model elements. The code template allows you to specify skeleton files containing the structure of the files to be generated, to automatically indent code and most importantly of all, to retrieve model information through scan methods.

The main functions of code templates are as follows:

·         to insert text evaluated by the J interpretor. This function is represented by expression nodes.

·         to generate the file according to a model. This function is represented by skeleton nodes.

 

For a complete example of the construction of a code template, please see “Quick tour of MDA Modeler - Building a generator”.

 

 

Creating a code template

To create a new code template, click on the  icon from a J profile implementation in the "Behavior profiles" folder of a J MDA component. The “Code template” window then appears (for details on this window, see “General principles of template design”).

 

Note:      Code templates can only be created on J profile implementations in J MDA components.  It is not possible to create a code template for a Java MDA component.

 

Figure 138. The code template editor

 

Toolbar

On the top-right of the template design window, the icons (which are presented on the top-right of the template design window as shown in Figure 138) available on a code template to create nodes are detailed below.

 

Icon

Description

This icon is used to create a navigation node. It is available on the template root, on a navigation node and on skeleton node.

This icon is used to create an expression node. It is available on the template root, on a navigation node and on skeleton node.

This icon is used to create a skeleton node. It is available on the template root, on a navigation node and on skeleton node.

This icon is used to reference an internal node in order to execute an existing node. It is available on the template root, on a navigation node and on skeleton node.

 

 

Template properties

The properties specific to code templates are as follows:

 

Property

Description

Indentation character

This defines the indentation characters used when the “Indentation” property has the “increase” value.

 

 

Adding an expression

To add a new expression to your code template, simply click on the  icon.

 

The properties available on an expression are detailed below.

 

Figure 139. The properties of an expression

 

Property

Description

Value

This defines the code which will be evaluated.

This code must allow information on the current element to be retrieved.

For example, “Name”, allows the element name to be retrieved.

The wizard allows information to be easily retrieved from the UML metamodel.

For further information, please see the “Inserting J code”.

Indentation

This indicates whether or not the string generated by the node must be indented.

Two values are possible:

- remain: the string is not indented. This value is the default value.

- increase: the string is indented with the indentation characters defined in the template properties.

Marker

This determines whether or not markers will be generated before and after the expression.

These markers allow the content to be automatically retrieved after an edition.

In this way, the UML model is updated after the editor is closed.

 

 

Adding a skeleton node

To add a new skeleton node to your code template, simply click on the  icon.

 

The properties available on a skeleton node are detailed below.

 

Figure 140. The properties of a skeleton node

 

Property

Description

Association skeleton

This is the name of the file containing the skeleton of the file which is to be generated.

Indentation

This indicates whether or not the string generated by the node must be indented.

Two values are possible:

- remain: the string is not indented. This value is the default value.

- increase: the string is indented with the indentation characters defined in the template properties.

 

The second table defines the translation of the aliases contained in the skeleton file. For each alias, it is possible to specify Objecteering markers which will frame the result of the expression defined in “Replacement content”. For further information, please see the “Inserting J code”.

 

Figure 141. The “alias” table of a skeleton node

 

Example of a skeleton file

A skeleton file can contain:

·         J expressions: the syntax is “<%= Jexpression %>”

·         node names defined in the template inside the skeleton node: the syntax is “<% call NodeName %>”

·         alias names: the syntax is “<% alias %>”

 

Note:      It is important to leave a space after the “<%” marker and before the “%>” marker. If a space is not left, then J errors can appear when the template is run.

 

// joperation.skel

<%$ returnParameter %> <%= OwnerClassifier.Name %>::<%= Name %> (<%$ ioParameters %>) {

/*

// Summary

<% Summary %>

// Description

<% Description %>

// Pre-condition

<% Precondition %>

// Post-condition

<% Postcondition %>*/

 

// J code

<% JCodeNote %>

} // method <% Name %>

 

In this example, we find:

·         the “<%= Name %> J expression which is used to insert the name of the current element

·         a call to one of the skeleton node's sub-nodes: “<% Summary %>. This element will be replaced by the code generated by the “Summary” node

·         an alias: “<%$ ioParameters %>. The “ioParameters” alias will be replaced by the code generated by the evaluation of “Replacement content” defined in the template on the skeleton node.