Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Use Eclipse JET to Automate Model-Driven Development Aspects

Get the lowdown on the Eclipse JET code generator and learn how to use it to automate your development processes.


advertisement
he continuously evolving Eclipse platform aids software developers in all phases of their work activities from modeling to coding to testing and deploying software artifacts. It is an aggregation of many collaborating components, each one performing a specific role and targeting a specific audience. This article demonstrates how to use one of these components, Eclipse JET, a specialized code generator tool that plays an important role in the modeling phase of a software project.

JET is one of the 21 projects bundled in the Eclipse Europa release. More precisely, it belongs to the Eclipse project Model To Text (M2T), which provides ready-to-use engines that perform model-to-text transformations. You can use JET to perform the following tasks:

  • Introduce or strengthen Model Driven Development (MDD) practices in your project (see Sidebar 1. Model Driven Development and Code Generation)
  • Simplify and automate mundane tasks that require conversions and transformations between models (usually from a more abstract to a more detailed one)
  • Enforce best practices and standards by automatically generating software artifacts designed to respect them

Before you get started with your first transformation, you must install JET into your Eclipse IDE from the update manager (See Sidebar 2. Starting with JET in Your Eclipse IDE).



JET Basics
At the heart of JET code generation resides a template, a plain text file that drives the JET engine to transform an input model into a software artifact. The input model and output artifact do not have to match any constraint (as you will see shortly). You can use JET to generate Java source files from an XML model, as well as C (or PHP or Ruby or whatever) code from an Eclipse EMF model.

The template is a mixture of static sections, which JET will reproduce unmodified in the generated output, and XML-like directives, which perform transformations on the input model. If you are familiar with Java Server Pages (JSP), PHP, ASP, or any other templating engine, these concepts should be familiar.

The other important concept to grasp is JET's use of XPath. By default, JET expects models to be represented by XML structures or EMF models. Therefore, its processing directives rely on XPath selectors and functions to identify and isolate the parts of the model upon which it will act. See Sidebar 3. Essential XPath for a quick intro to the language.

However, you are not limited to XML or EMF input models. Since JET is packaged and distributed as an Eclipse plugin, it offers various extension points to augment its capabilities, including the definition of additional input formats. The documentation bundled with JET includes full specifications for the available extension points.

JET processing directives can be expressed in various forms, including:

  • Java snippets such as <% out.write(mySampleData); %> – The engine interprets these snippets when it encounters them within a template during model processing.
  • XML tags such as <c:if /> and <c:when /> – They define control flows in the processing engine.
  • XML tags such as <f:formatNow /> – They represent a formatting directive.

Pretty much in the same way as defined by the JSP syntax, XML tags are provided to the engine in the form of tag libraries. A number of them, for the most common tasks, are bundled with the engine, but you can create additional custom tag libraries for your specific needs (again note the similarity with the contribution mechanism for custom tag libraries in the JSP world).

Given the sample XML model in Listing 1, you can easily understand the JET template in Listing 2, which transforms the model into the usual HelloWorld class.

Listing 1. Sample XML Model That Describes a Phrase
<class name="HelloClass"> <phrase>Hello,World!</phrase> </class> Listing 2. JET Template That Converts the Model into a Working Java Source File
public class <c:get select="/class/@name" /> { public static void main(String[] args) { System.out.println("<c:get select="/class/phrase" />"); } }

You can identify both the static sections and the XML directives. In particular, <c:get /> prints the result of an XPath selector passed as parameter (such as /class/@name, which isolates the name attribute of the class tag).

You are not limited to generating Java files either. For example, the following template transforms the same model into an equivalent Ruby class:

class <c:get select="/class/@name" /> def sayPhrase puts "<c:get select="/class/phrase" />" end end

These examples offer a glimpse of the power behind code generation and MDD: if the model is sufficiently robust, it is easy to adapt the final product to different environments and to migrate it to a new software architecture.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap