Getting Started Code Generation With Epsilon
While evaluating code generation tools under Eclipse, I tried Xpand and Acceleo but I didn’t succeed with them for Papyrus UML models. So recently, I decided to try Epsilon Modeling Tools. I tried it for GMF tools generation with Emfatic and EuGENia. Epsilon is a family of languages and tools for code generation, model-to-model transformation, model validation, comparison, migration and refactoring that work out of the box with EMF and other types of models. The example below is inspired from this article. In this example, we’ll try to generate an HTML Form model which is prepared with Papyrus UML. Papyrus is a tool for modeling with UML, SysML and ModelicaML. Modeling HTML Form We assumed that you are familiar wit Papyrus. An HTML form Class is shown in the image below. It is a Papyrus Class Diagram. Model has a Form Class with two type of fields as TextField and MultiLİneTextField which is derived from TextFiled.
In the image below, an a HTML form design with the instance of the Class elements in Class diagram. The form has three instance, a Name TextField, an Email TextField, and a Message MultiLineTextField.
Creating Epsilon Generator Language Templates We have two EGL file to generate HTML code, “create.egl” and “uml2html.egl” as follow. The first one is used to call converter, and the second one is used to generate. create.egl
[% var t := TemplateFactory.load('uml2html.egl'); t.populate ('dummy', null); t.generate ('../out/contact.html'); %]
uml2html.egl
[% var FormAttr : Set; var FormFields : Set; var name : String; var label : String; for (p in Package.all.select(t|t.type.name.equals("Package"))) { if (p.name.equals("System")==true){ for (i in p.packagedElement.select(e|e.isTypeOf(InstanceSpecification))) { if (i.classifier.first.type.name == "Class") { if (i.classifier.first.name == "Form") { for (s in i.slot) { if (s.value.first.isTypeOf(LiteralString)) { FormAttr.add(s); } else if (s.value.first.isTypeOf(InstanceValue)) { FormFields.add(s); } } } } } } } %] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>[% for (attr in FormAttr) { if (attr.definingFeature.name.equals("title")) { %][%=attr.value.first.value %][%}}%]</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <div id="page-wrap"> <form method="post" action="form.php"> [% for (field in FormFields) { for (s in field.value.first.instance.slot) { if (s.definingFeature.name.equals("name")) { name = s.value.first.value; } else if (s.definingFeature.name.equals("label")) { label = s.value.first.value; } }%] <label for="[%=name%]">[%=label%]:</label> [% if (field.definingFeature.name.equals("textField")) {%] <input type="text" name="[%=name%]" id="[%=name%]" /> [%} else {%] <textarea name="[%=name%]" id="[%=name%]" rows="20" cols="20"></textarea> [%}}%] <input type="submit" name="submit" value="Submit" class="submit-button" /> </form> <div style="clear: both;"></div> </div> </div> </body> </html>
Launcher Configuration To initiate launcher is done as follow.
Rigt-click on the file “create.egl” in “Project Explorer” and select “Run As > Run Configuration…”. You’ll reach the window below.
Click “Models” tab. Add the new one as shown the image below.
To save your run configuration in your project, click the tab “Common”, then click “Shared file:” radio button and select your project location as shown in the image below
Then click Run, then you’ll have your HTML file in folder “out”. The form looks like as follow.
Please feel free to ask any question. The complete Eclipse project can be downloaded from here.
References
Eclipse
Papyrus UML
ModelicaML
Epsilon
Xpand
Acceleo
The Example with Expand
All rights reserved. COPYRIGHT 2014 StarGate Inc.












