Wednesday, August 27, 2008

Software Designing - Code Generation

We design software using UML. The tools used will most likely generate the .uml file. If you are using open source tools like eclipse, you can generate the code from this .uml file. This is because this .uml file is an xml file which follows the standard

The information for this is best available in Acceleo site at http://www.acceleo.org/pages/home/en. The code generation is usually done by chaining the script and the model(.uml) file. There are lot of scripts available for code generation under modules section. The beginners can start by downloading the eclipse bundle eclipse-ganymede-win32-modeling-topcased-acceleo-.zip.

A typical script file looks like

<% metamodel http://www.eclipse.org/uml2/2.1.0/UML %>
<%script type="uml.Model" name="model" file="/config/<%name%>-config.xml"%>
<%recursion%>
>%script type="Element" name="recursion" post="trim()"%<
>%for ( descendant() ){%<>%if ( eClass.name == "Class" ){%<>%classes%<>%}%<>%}%<
>%script type="uml.Class" name="classes"%<
>%if ( name.endsWith("ServiceImpl") ){%<
>%}else if( name.endsWith("DaoHibernate") ){%<

>%}else if( name.endsWith("Controller") ){%<
>%if (superClass.nGet(0).name.endsWith("FormController")){%<
>%for ( ownedOperation.ownedParameter ){%<
>%if ( type.name.endsWith("Command") ){%<
>%}%<
>%}%<
>%}%<
>%}%<
>%if (name.endsWith("ServiceImpl") || name.endsWith("DaoHibernate") || name.endsWith("Controller") ){%<
>%for (ownedAttribute){%<
>%if ( name.endsWith("Dao") || name.endsWith("Service") ){%<

>%}%<
>%}%<
>%}%<
>%script type="Element" name="packagePath" post="trim()"%<
>%ancestor.select("eClass.name != 'Model'").reverse.name.replaceAll("\.",args(0)).sep(args(0))%<

This script parses the model chained to it. When it encounters type="uml.Model" it will execute script named model. The model script end when it encounters another script tag which is script named "recursion". The model script also gives a call to to the script recursion by "<%recursion%>".
The recursion script executes for all the elements in model due to for loop "<%for ( descendant() ){%>". If this loop encounters the class element in UML (eClass.name == "Class"). It gives the call to classes script.
The class script checks certain properties and generates the spring config file.