Creating a code template to generate classes

 

Creating a code template

To create your new code template, select the “JavaProfileImplementation” J profile implementation previously created and click on the  icon.  The “Code template” window then appears.

 

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.

 

For the purposes of our example, we are going to change the name of the code template we have just created, by carrying out the following steps:

1.      Select the name of the code template on the top left of the window shown in Figure 14.

2.      Click once on it (or press F2).

3.      Enter the new name (in our example, “ClassJavaTemplate”) for the code template.

 

Note:      This operation can also be carried out in the MDA Modeler explorer.

 

Defining the metaclass on which your template will run

The very first step is to define the metaclass on which the code template you are about to build will be run, in our example, the “Class” metaclass. To do this, carry out the steps shown in Figure 14.

 

Figure 14. Defining the metaclass in the “Code template creation” window

 

Steps:

1.      Click on the browse button on the right of the “Metaclass” field. The “Select a metaclass” window then appears. Select the “Class” metaclass (located in the “Objecteering UML/Metamodel” package) and click on “OK” to confirm.

 

Note:      You can either select a metaclass directly in the metamodel tree or simply type the name of the metaclass in the field. If you choose to enter the metaclass name, you will see that even if you only enter the first few letters of the name, MDA Modeler will select the first available metaclass corresponding to this entry (for example, if you enter “pack”, the “Package” metaclass is automatically selected in the metamodel tree). The field is not case sensitive.

 

 

Creating a skeleton node for the class

Skeleton nodes are used to reference the skeleton file containing the structure of the file to be generated.

 

Figure 15. Creating a skeleton node

 

Steps:

1.      Select the template node you created in Figure 14.

2.      Click on the  icon. The new skeleton node then appears. Name it “skeletonClass”.

 

Continue by creating a "class.skel" file whose contents are as follows:

class <%= Name %> {

 

<% Attributes %>

<% Operations %>

}

 

This file should be saved in the “OBJING_PATH/mdaplugs/JavaGenerator/1.0/templates” directory. This directory, which should be manually created, corresponds to the workspace of the MDA component.

 

When the template is evaluated:

·         <%= Name %> will be replaced by the name of the class. It is a J expression.

·         <% Attributes %> will be replaced by the text generated by the “Attributes” navigation node that will be created in the following sections.

·         <% Operations %> will be replaced by the text generated by the “Operations” navigation node that will be created in the following sections.

 

The "class.skel" file you have just created must now be associated with the skeleton node.  To do this, simply select the skeleton node and then click in the "Associated skeleton" field on the right of the window.  Next, click on the  button to open a file browser, in which you can select the "class.skel" file in the ""OBJING_PATH/mdaplugs//JavaGenerator/1.0/templates" directory.

 

 

 

For more information on creating skeleton nodes, please see “Code templates”.

 

 

Creating a navigation node to retrieve attributes

We are now going to create a navigation node, which will be used to retrieve the attributes of the class, as well as to process each attribute.

 

Figure 16. Creating the “Attributes” navigation node

 

Steps:

1.      Select the “skeletonClass” skeleton node you have just created.

2.      Click on the  icon. The “Create a navigation node” window then appears.

3.      Select “Attributes” from the list.

4.      Click on “OK” to confirm.

 

For more information on creating navigation nodes, please see “Creating navigation nodes”.

 

 

Creating a skeleton node for the attribute

We are now going to create a skeleton node for the attribute.  To do this, carry out the steps shown in Figure 17 below.

 

Figure 17. Creating a skeleton node for the attribute

 

Steps:

1.      Select the “Attributes” navigation node you created in Figure 19.

2.      Clicking on the  icon. The new skeleton node then appears. Name it “skeletonAttribute”.

3.      Define its “Indentation” by giving it the following value: “increase”.

4.      Enter "type" in the "Alias" field, and copy the following code into the "Replacement content" :

{

    TypeGeneralClass {

        return = Name;

    }

    if (IsSet) return.strcat ("[]");

    if (return == "") return = "void";

}

 

Next, create an “attribute.skel” file whose contents are as follows:

// Attribute: <%= Name %>

<%$ type %> <%= Name %>;

 

This file must be saved in the “OBJING_PATH/mdaplugs/JavaGenerator/1.0/templates” directory. This directory corresponds to the workspace of the MDA component.

 

When the template is evaluated:

·         <%= Name %> will be replaced by the name of the class. It is a J expression.

·         <%$ type %> will be replaced by the text generated by the “type” alias

 

The "attribute.skel" file you have just created must now be associated with the skeleton node.  To do this, simply select the "skeletonAttribute" skeleton node and then click in the "Associated skeleton" field on the right of the window.  Next, click on the  button to open a file browser, in which you can select the "attribute.skel" file in the ""OBJING_PATH/mdaplugs//JavaGenerator/1.0/templates" directory.

 

For more information on creating skeleton nodes, please see “Code templates”.

 

 

Creating a navigation node to retrieve operations

We are now going to create a navigation node, which will be used to retrieve the operations of the class, as well as to process each operation.

 

Figure 18. Creating the “Operations” navigation node

 

Steps:

1.      Select the “skeletonClass” skeleton node you created in Figure 18.

2.      Click on the  icon. The “Create a navigation node” window then appears.

3.      In this window, select “Operations” from the list.

4.      Click on “OK” to confirm.

 

For more information on creating navigation nodes, please see “Creating navigation nodes”.

 

 

Creating a skeleton node for the operation

 

Figure 19. Creating a skeleton node for the operation

 

Steps:

1.      Select the “Operations” navigation node you created in Figure 21.

2.      Click on the  icon. The new skeleton node then appears. Name it "skeletonOperation".

3.      Define its “Indentation” by giving it the following value: “increase”.

4.      Enter "returnParameter" in the "Alias" field and then copy the following code into the "Replacement content" field:

{

    ReturnParameter {

        TypeGeneralClass {

            return = Name;

        }

        if (IsSet) return.strcat ("[]");

    }

    if (return == "") return = "void";

}

5.      Define an “ioParameters” alias, with the following in the “Replacement content” field:

{

    String indent = "    ";

    IOParameter {

        if (ParameterPassing == In) return.strcat ("in ");

        else if (ParameterPassing == Out) return.strcat ("out ");

        else if (ParameterPassing == Inout) return.strcat ("inout ");

        return.strcat (TypeGeneralClass.Name);

        if (IsSet) return.strcat ("[]");

        return.strcat (" ", Name, ",", NL, indent);

    }

    return.strip (", " + NL);

    if (IOParameter.size() > 1) {

        return.prepend (indent);return.prepend (NL);

    }

}

 

Continue by creating an “operation.skel” file whose contents are as follows:

// Operation "<%= Name %>"

<%= {if (isTaggedValue ("synchronized")) return.strcat ("synchronized ");} %><%$ returnParameter %> <%= Name %> (<%$ ioParameters %>) {

    // Code

    <% CodeNote %>

} // operation <%= Name %>

 

This file must be saved in the “OBJING_PATH/mdaplugs/JavaGenerator/1.0/templates” directory. This directory corresponds to the workspace of the MDA component.

 

When the template is evaluated:

·         <%= Name %> will be replaced by the name of the class. It is a J expression.

·         <%= {if (isTaggedValue ("synchronized")) return.strcat ("synchronized ");} %> will be replaced by “synchronized”, if the operation has the {synchronized} tagged value. It is a J expression.

·         <% returnParameter %> will be replaced by the text generated by the “returnParameter” alias.

·         <% ioParameters %> will be replaced by the text generated by the “ioParameters” alias.

·         <% CodeNote %> will be replaced by the text generated by the “CodeNote” navigation node that will be created next.

 

The "operation.skel" file you have just created must now be associated with the skeleton node.  To do this, simply select the "skeletonOperation" skeleton node and then click in the "Associated skeleton" field on the right of the window.  Next, click on the  button to open a file browser, in which you can select the "operation.skel" file in the ""OBJING_PATH/mdaplugs/JavaGenerator/1.0/templates" directory.

 

For more information on creating skeleton nodes, please see “Code templates”.

 

 

Creating a navigation node to retrieve notes

We are now going to create a navigation node, which will be used to retrieve notes.

 

Figure 20. Creating the "Notes" navigation node

 

Steps:

1.      Select the “skeletonOperation” skeleton node you have just created.

2.      Click on the  icon. The “Create a navigation node” window then appears.

3.      In this window, select “Notes” from the list, and click on “OK” to confirm.

4.      Change the name of the new navigation node to “CodeNote” (by selecting it, pressing F2 and entering the new name).

5.      Define the “Filter” property of the navigation node by entering the following: "(ModelNoteType != null) && (ModelNoteType.Name == "JavaCode")".

 

For more information on creating navigation nodes, please see “Creating navigation nodes”.

 

 

Creating an expression to generate the contents of the note

We are now going to create an expression, which will be used to generate the contents of the note.

 

Figure 21. Creating an expression to generate the contents of the note

 

Steps:

1.      Select the “CodeNote” navigation node you have just created.

2.      Click on the  icon. The new expression then appears.

3.      Define the “Value” property of the expression node by entering the following: “Content”

 

For more information on creating navigation nodes, please see “Code templates”.

 

Click on the “OK” button to validate the template. At any time, you can save your modifications without closing the window by clicking on the “Apply” button.