One of the typical workflows in using the IBM® Rational® Software Architect design and development tool for model-driven development is to create a Unified Modeling Language (UML) model to represent a particular system. This tool also provides several predefined transformations that will convert the UML model into any of the supported programming languages. However, rather than using the ones provided, you can create your own transformation that will convert the model according to rules that you define.
The extensibility features in Rational Software Architect provide tools to create Java™ Emitter Template (JET) transformations and specify your own rules for the generation of output from the transformations. JET is a template language developed by IBM. By using JET transformations, you can quickly and easily generate a template-based model-to-text transformation. With the JET toolkit, you can generate text files from a model by using templates that contain text and JET commands (tags). JET transformations automate the repetitive process of text generation.
This article guides you through the steps of authoring a new JET transformation to create a Java project that represents a book library.
Create and configure the JET transformation project
To use the JET technology to create transformations, the target domain expert must provide an Exemplar project to the transformation author. The Exemplar project represents the desired format of the output of the transformation. The best practice is to create an Exemplar project that represents as many details as possible of the format for the required output. The project must be imported into the Rational Software Architect workspace.
For the purposes of this article, a simple Java™ project (MyLibrary) that contains a Java class (BookTitle) within a package (books) is the example. When the JET transformation is created and executed, the expected output of the transformation will be a Java project that contains one or more Java classes within a package.
Figure 1 shows the exemplar Java project used in this article.
Figure 1. The exemplar Java project
In order to develop the JET transformation that will accept a UML model as a source and generate a Java project as a target, you must create a new JET Project with Exemplar Authoring in Rational Software Architect:
- Select File > New > Project from the main menu.
- From the JET Transformations category, specify JET Project with Exemplar Authoring.
- Name the project
com.test.my.transform. - Accept the defaults on the next page, and then click the Next button.
- From the Transformation Scope page, select the MyLibrary project, and then click Finish.
The created project contains the transform.tma file that you will use in this exercise mainly to develop the desired transformation. It opens in the editor view automatically, with two tabs: Schema and Overview. The Overview tab (Figure2) displays general information about the transformation.
Figure 2. Overview tab with general information about the JET project
You will work in the Schema tab (Figure 3) to develop the transformation output actions.
Figure 3. The editor of the JET with the Exemplar authoring project
As the first step, the Exemplar project has to be analyzed. It might contain artifacts that you do not plan to generate in your output. In our example we do not want the transformation to generate .class files. The generation of these classes will be performed in the target Java project automatically based on the .java files.
To delete BookTitle.class from the Exemplar schema:
- Right-click BookTitle.class in the Transform Authoring window.
- Select Delete from the menu.
In Figure 4, the Exemplar project in its final version is shown in the transformation Schema editor.
Figure 4. The final Exemplar project
Develop the input and output schema actions
Now you are ready to develop the input schema and output actions so that you can generate the transformation output automatically.
To specify the target Java project generation:
- Right-click the root entry in the "Define the transformation input schema and output actions" pane, and select New >Type from the context menu.
- Give the new type the name
projectInfo. - Add a new project named
my.projectto the projectInfo type. - To create output actions, drag the .classpath and .project files from the Exemplar pane onto the my.project node in the Transform Input and output actions pane.
- In the Transformation input schema and output actions pane, rename the created files as
.projectand.classpath.
The files are now added to the "Transformation input schema and output actions" pane. In the Exemplar pane, icons near the .project and .classpath file nodes appear. This indicates that actions have been created for these elements (see Figure 5).
Figure 5. The .classpath and .project files in the Transformation Input schema
- If you do not see the Properties view in your workspace, right-click any added element in "Transformation input schema and output actions" pane, and select Show Properties View from the context menu.
- Add an attribute called
nameto the projectInfo type.
The created transformation should be flexible so that it can generate projects with various names for different libraries. To enable this flexibility:
- Right-click the projectInfo type in the "Define the transformation input schema and output actions" pane, and select New > Attribute from the context menu.
- Call the new attribute
name. - (Optional) Right-click the name attribute, and select Edit Exemplar Text from the context menu.
- In the Edit Exemplar Text window, add the project name
MyLibraryto the name attribute (Figure 6). - Click Add and then OK.
Figure 6. Setting the exemplar text
The exemplar's project name is displayed in the Properties view under the Exemplar Text field (see Figure 7).
Figure 7. The properties of the edited name attribute
Replace the Exemplar Text string
This Exemplar Text string can be automatically replaced with the name that you want to use for the project when you are ready to run the transformation. The output project will be generated with the user-defined name specified in the source model.
Follow these steps to specify that the Exemplar Text should be replaced according to user input when the transformation is executed:
- Select the my.project project.
- In the Properties view, under the Action Parameters section, right-click the value for the Name property.
- Select Replace with model reference (see Figure 8).
Figure 8. Updating the project name parameter
- In the "Replace with Model References" dialog window, select the name attribute of the projectInfo type, and then click OK (Figure 9).
Figure 9. Selecting the "name" attribute
As a result, the value of the Action Parameter name of the project called my.project is replaced with the {$projectInfo/@name} tag (Figure 10).
Figure 10. Replaced value of the "name" action parameter
Specify transformation actions for generating artifacts
Now you are going to specify the transformation actions for generating project artifacts.
To generate a Java file for each library item, an output action needs to be created and linked with the exemplar Java file. To do this, you add a new type called "item" to the "projectInfo" type, and specify Java class generation:
- In the "Transformation input schema and output actions" pane, right-click the projectInfo type.
- From the context menu, select New > Type.
- Name the new type
item. - Drag the BookTitle.java file from the Exemplar pane onto the item type.
- Name the file
myBookTitle.java.
The new myBookTitle.java file is now added to the "Transformation input schema and output actions" pane. An icon is displayed next to BookTitle.java in the Exemplar pane, indicating that this file is linked to the new myBookTitle.java file in the other pane. Notice that, in the Properties view under the Action section, you can add conditions for generation, as well as a description of any of the generated artifacts (see Figure10).
Save the created schema and update the project:
- From the main menu, select File > Save All.
- In the editor view in the "Transformation input schema and output actions" pane, right-click the root type and select Update Project (Figure11).
Figure 11. Select Update Project from the menu
The folders for each type will be generated, containing JET templates that specify the generation of each required file (Figure 12).
Figure 12. Generated JET templates
The main.jet template file contains tags that specify the generation of the .project, .classpath, and myBookTitle.java files (Figure 13).
Figure 13. Template tags generated in the main.jet template
Replace hard-coded strings with xPath expressions
To add flexibility in generating a variety of output results, various hard-coded strings in JET templates can be updated with xPath expressions. Here, we will describe a few of the possible options as examples, as well as the recommended workflow.
- In the "Transformation input schema and output actions" pane, select the myBookTiltle.java file (Figure14).
- In the Properties view under "Action parameters," select the MyLibrary/books value of the path property, right-click it, and select the "Replace with Model Reference" menu item.
Figure 14. String selection to be replaced with model references in Properties
Figure 15 shows the resulting dialog window: Replace with Model References.
Figure 15. The Replace with Model References window
In this dialog window, you can select an existing attribute or create a new one. Here, we will demonstrate the creation of a new attribute that will describe the generation of the package name.
- Select the projectInfo type.
- Click New.
When you click the New button, the "Create New Derived Attribute" dialog window will appear (Figure 16). In this window, you can define the attribute with the value derived from the calculation. The attribute's name field should be populated by you, the user. the exemplar text field is populated with the string that you selected when the dialog was invoked, and the same string is written, by default, for the Calculation value. Leave the default value for this exercise, but, in a real-world transformation you can replace it with an XPath expression.
Figure 16. Creating a derived attribute
- Add the
pkgDirstring to the "Attribute name" text field. - Click OK to close the "Create New Derived Attribute" window.
- Click OK to close the "Replace with Model References" window.
The new derived attribute, pkgDir, is created and displayed in the "Transformation input schema and output actions" editor pane (Figure 17).
Figure 17. Newly created derived attribute, pkgDir
The value of the path property for myBookTitle.java is updated with the generated derived attribute parameter (Figure 18).
Figure 18. The path value with a replaced parameter
The transformation author should also provide the ability to generate multiple Java classes.
To enable this, create a new name attribute for the item type the same way that we described before for the projectInfo type. Replace the hard-coded file name with a created attribute for the path parameter of the myBookTitle.java file.
- Select myBookTitle.java in the "Transformation input schema and output actions" editor pane,
- Select the BookTitle file name in the path value in the Property view.
- Right-click the BookTitle file name in the path value in the Properties view.
- Select the "Replace with Model Reference" context menu item.
- In the "Replace with Model References" dialog window, select the name attribute that belongs to the item type.
As a result, the hard coded path is replaced with a string that contains an XPath expression (Figure 19). This gives flexibility for the transformation user to generate multiple Java classes with different names.
Figure 19. The path value with the file name replaced
- Save the created schema.
- In the editor view in the "Transformation input schema and output actions" pane, right-click the root type and select Update Project.
The JET templates will be updated. The following XPath expression will be added to the main.jet template:
<ws:file path="{$projectInfo/@pkgDir}/{$item/@name}.java" replace="true"
template="templates/item/myBookTitle.java.jet"/> |
Edit the templates to customize the transformation output
Up to this point, you were developing the transformation using the JET authoring editor. Now, when the template skeletons are generated, you should fine-tune them to enable generating the content of the transformation output according to your project's requirements.
Here, we will demonstrate how you can use the editing tools provided in the Rational Software Architect tool set.
To assist you with template editing, the tool indicates detected strings within the template that can be replaced with transformation artifacts. The information signs are displayed in the Project Explorer view (Figure 20). When you open the indicated template, the text to be replaced is underscored. When you hover the mouse over the underscored text, a help string with a suggested action is displayed.
Figure 20. The assistance suggestion
- Select the underscored text.
- Right-click and select Find/Replace with Model References (Figure 21).
- In the "Find/Replace with Model References" dialog window, you can select the suggested attribute or any other existing attribute, or you can create a new attribute.
For this demonstration, we select the suggested highlighted attribute, name, under the item type. - Click Replace, and then click Close.
Figure 21. Replacing the name by using an automatically generated string
The selected text in the template will be replaced with this JET tag:
<c:get select="$item/@name" /> |
You can replace any text with the JET tag at this point. In the attached example project's project.jet and main.jet templates (see the Download section), we replaced the hard-coded project name with this XPath expression:
<c:get select="$projectInfo/@name" /> |
Note:
Information signs in the Project Explorer disappear when all suggested updates are performed and any updated templates are saved.
You created a basic JET transformation, and you are ready to execute it now.
To test the transformation, create a file containing the transformation input. The IBM® Rational® Software Architect for WebSphere® edition provides tools to generate a test input file based on the XSD schema that was generated in the transformation authoring project. Follow these steps to generate the input file:
- Select File > New > Other > XML > XML File, and then Next.
Note:
You may need to select the Show All Wizards check box in the New dialog window to make this option available. - Confirm enabling the capabilities if a dialog box up requesting that you do so.
- Select the com.test.my.transform project as a container, enter
MyTest.xmlas the name, and click Next. - Select the Create XML file from an XML schema file radio button, and click Next.
- Click the Select file from Workspace radio button.
- From the com.test.my.transform project, select the schema.xsd file, and then click Next.
- Check all check boxes, and then click Finish.
- Enter the required data in the generated MyTest.xml file, and then save and close that file.
Figure 22 shows the suggested input file.
Figure 22. Suggested transformation input file
The transformation is ready to be executed.
- In the Project Explorer, right-click the MyTest.xml file, and select Run As from the menu.
- Select Run Configuration.
- In the Run Configurations window, double-click JET Transformation.
- Make sure that the MyTest.xml file is listed as Transformation Input (Figure 23).
- Click Run.
Figure 23. Defining a test transformation configuration
When the transformation runs, the new project called BookLibrary is created. It contains Java files named as specified in the MyTest.xml file (Figure 24).
Figure 24. The files generated from running the transformation
The Rational Software Architect tool set can be used in a similar way to further elaborate the transformation input schema and output actions.
This article provided step-by-step instructions on how to define a basic transformation based on JET templates. Such a powerful user-defined transformation can simplify the process of asset-based development and can assist in code reuse.
In the next article in this two-part series, we will demonstrate how to use Rational Software Architect tools to generate profiles specific to the target domain of the transformation and how to deploy the transformation into your workspace.
Be sure to check the Resources section for where you can learn more.
| Description | Name | Size | Download method |
|---|---|---|---|
| Example project files | artifacts04c.zip | 28KB | HTTP |
Information about download methods
Learn
- Read more about the JET template-based transformation and for an XPath primer, see Strategic Reuse with Asset-Based Development (IBM® Redbooks®, May 2008).
- Check these relevant developerWorks articles:
- Create more — better — code in Eclipse with JET (IBM® developerWorks®, August 2006)
- Create powerful custom tools quickly with Rational Software Architect Version 7.0: Introduction to the Transformation Authoring feature (developerWorks, January 2007)
- Get more information on JET technology from the Model-to-Text project page on the Eclipse website.
- Find out more about Rational Software Architect versions:
- Rational Software Architect Standard Edition:
- Rational Software Architect for WebSphere Software:
- Visit the Rational software area on developerWorks for technical resources and best practices for Rational Software Delivery Platform products.
- Stay current with developerWorks technical events and webcasts focused on a variety of IBM products and IT industry topics.
- Attend a free developerWorks Live! briefing to get up to speed quickly on IBM products and tools, as well as IT industry trends.
- Follow developerWorks on Twitter.
- Watch developerWorks on-demand demos, ranging from product installation and setup demos for beginners to advanced functionality for experienced developers.
- Try one of our web-based training courses, which you can take anywhere, any time.
Get products and technologies
- Download either or both version for a trial:
- Evaluate IBM software in the way that suits you best: Download it for a trial, try it online, use it in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement service-oriented architecture efficiently.
Discuss
- Join the discussion in the Rational Development Tools forum.
- Get involved in the My developerWorks community. Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups such as the Rational Café, and wikis.






