A Tutorial on Using JET for Model to Text transformations
JET is an plug-in for Eclipse that is used to implement a "code generator".
It uses
JSP-like template files that can be edited to any kind of source code to which a given
model is to be transformed. For this tutorial we will be transformning a model represented
as a set of prolog facts.
The following are useful links to help you get started:
JET plugin
You can also run an instance of Eclipse using a feature that we developed to implement the model to text transformation with fewer steps.
These are the steps to get started with the plugin:
Install JET
- In Eclipse go to Install New Software
- In "Work with" enter the following link: http://download.eclipse.org/modeling/m2t/updates/releases/
- Select Java Emitter Templates (JET)
- Click next
- Read the EULA
- Click finish
- Restart eclipse
This video illustrates the above steps
Create a JET project
Consider downloading the project for this tutorial
here. This is to make it easier for you to copy the code instead of writing it from what we show in the videos.
- Create new Java project
- Add a new file called FruitInput.prolog
- Add the prolog facts to the input file
This video illustrates the above steps
- Create a new package called "fruit" which will store the necessary data structures
- Create a class file called Fruit
- Add the attributes and methods of the class Fruit
This video illustrates the above steps
Create the parser for the input model file
- Create a package called io
- Create a class called InputReader to read and parse the Prolog input file
- Add the code from the InputReader.java file located inside the src folder
This video illustrates the above steps
Convert the project to a JET project
- Right click the project folder
- In "New" select "Other"
- Under Java Emitter Templates select "Convert Projects to JET Projects
- Click next
- Select the Fruit folder which is our project folder
- Click finish
This video illustrates the above steps
- Set the JET code directory
- Right click the JET project folder and select "Properties"
- Scroll to JET Settings
- In this example we are using src, so specify "src" in the "Source Container" option
- Click OK
This video illustrates the above steps
Create the JET Template
- Righ-click the "templates" folder
- Go to New and select File
- It is considered good practice to give the template an extension name of the form: typejet, where
type is the type of text you will output and jet is "jet". In this case we call it "Fruit2Text.txtjet"
- Ignore the error message that appears, it is a normal error since our template is empty
- Add the body of the template
- We recommend that you watch the video because it has a detailed explanation of the parts of the template
This video illustrates the above steps
Run the JET template and Generate Source Code
To run our template and generate the source code from the prolog model, we need to create a Java class that will contain a main method.
It is recommended that you watch the videos since they contain explanations of the code and its meaning.
- Right click the project folder
- Create a new package called "test" (you can use any other name you like
- Right click the test package and add a new Class file called "Test"
- Add the corresponding imports and the code to the main. The code is in the src folder in the test file.
This video illustrates the above steps
- Click the Run button on eclipse
- The source code that was generated will appear in std output since that's what we specified in our Test.java
This video illustrates the above steps
Run Eclipse with plugin
- Open eclipse
- Download the project to run an instance of eclipse here
.
- File->Import->General->Existing Projects into Workspace
- Select the root: the exampleWizard directory
- Click Finish
- Click the exampleWizard project and click run.
This video illustrates the above steps
Create a JET project (plugin eclipse version)
Use the eclipse instance that was creating in the previous step (Run Eclipse with plugin).
- Select File->New->Other->Prolog to Java Wizards->Create Java Project from Prolog Input
- Click next
- Select the prolog input file from the Fruit project
- Click finish
Convert the project to a JET project (plugin eclipse version)
- Right click the project folder
- In "New" select "Other"
- Under Java Emitter Templates select "Convert Projects to JET Projects
- Click next
- Select the fruit folder which is our project folder
- Click finish
- Right click the project and select Properties
- Select JET Settings
- Type "src" (without quotes) in Source Container
- Click apply then OK
- Delete the folder "generators"
This video illustrates the above steps
Edit Template (plugin eclipse version)
- Open file in templates folder
- Append your own custom code below the header and data initializations
This video illustrates the above steps
Run project (plugin eclipse version)
- Save the project and click run
This video illustrates the above steps