Example of generator template
The next templates are used to generate simle Java class with automatic
loading from the database.
- The first template is macro template used to decode UML datatype
to Java type
The template body is included between <begin>
and <endmacro> keywords.
The %1%
is replaced by 1st parameter provided by <call> procedure.
JavaType.mac
<macro>JavaType<begin>%decode<%1%:string:String:Integer:Int:%1%><endmacro>
- The second one is our primary template, it is given the "main"
name to identify it is start point of generation. Lines are enumerated
for demonstration purpose only. Template uses
JavaType template so you have to include file where this template is
defined (line 00). Line 01
begins definition of template with name "main". You
can see outer loop (line 02) for all classes and
several inner loops for class attributes (lines 04, 11, 14, 20 and 25). <For>
commands on line 11 uses filter
to generate code only for attributes matching to filtering expression
"-M+P" (wich means "no mapping info" and "include attributes from
parent classes"). <For>
command on line 20 in addition to filter "+PK"
(means "include attributes from parent classes" and "include primary
key attributes") has iteration divider element " and " (it will be
generated between results of generation iterations. <Call> command on line 05 calls JavaType.mac macro with parameter %.type% wich
will be replaced by attribute type from object model.
Note 1. Variable %.map%
has different meanning on lines 19 and 20
- in first case it is applicable to class (returns table name the class
is mapped to) and in second case it is applicable to attibute (returns
column name the attribute is mapped to).
Note 2. Generation
is always executed in context of package. Even if you parametrize
generator with class name generator will create temporary pseudo
package containing single class and apply it to template. It does mean
to generate class code you always must to use <for> command for class
iteration in you main template.
JavaDemo.mac
00#include<JavaType.mac>
01<macro>main<begin>
02 <for>c classes<begin>%\%
03 public class %.name%
{
04 <for>x attributes<begin>
05 <call>JavaType<,>%.type%<endcall> %.name%;<endfor>
06
07 public %.name%()
{
08 }
09
10 public load(java.sql.ResultSet rs) {
11 // not-mapped attributes<for>x attributes<where>-M+P<begin>
12 //%.name%
= ?;<endfor>
13
14 // mapped attributes<for>x attributes<where>+MP<begin>
15 %.name% = rs.Get<call>JavaType<,>%.type%<endcall>("%.map%");<endfor>
16 }
17
18 private String getLoadString) {
19 return "select * from %.map% "+
20 "where <for>x attributes<where>+PK<div> and <begin>%.map%=?<endfor>";
21 }
22
23 public load() {
24 java.sql.PreparedStatement stmt =
connection.prepareStatement(getLoadString());%\%
25<for>x attributes<where>+PK<div> and <begin>
26 stmt.Set<call>JavaType.mac<,>%.type%<endcall>(%.name%);<endfor>
27 java.sql.ResultSet rs = stmt.executeQuery();
28 load(rs);
29 rs.close();
30 }
31}
32<endfor>
33<endmacro>
Macro JavaType can be changed to simplify it's call -
JavaType1.mac
<macro>JavaType1<begin>%decode<%.type%:string:String:Integer:Int:%.type%><endmacro>
It explicitly uses %.type% value of
current element (class attribute)
After that the call procedure has to be modified as follow
<call>JavaType1<endcall>
It is shorter but less
obviouse variant. In typicall case it make sence to
create your command constructions as short as possible becouse it will
simplify the understanding of whole template structure.
Semantically equivalent (normalized) form of JavaType1 macro is
<macro>JavaType1<begin><decode>%.type%<,>string<=>String<,>Integer<=>Int<,>%.type%<enddecode><endmacro>
To compile templates run command
Comp.exe JavaDemo.mac
JavaDemo.gen file will be created.
Now it is time to create class model in UML tool like Rational
Rose or Enterprise Architecht and export UML package to XMI file
with name Test1.xml.

Now run MapTool to map your classes to database tables.
... not documented yet ...
Finally generate your source code
Gen.exe XMI=Test1.xml GEN=JavaDemo.gen REPO=scott/tiger
OBJ=\test1\MyClass FILE=MyClass.java