Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Losing the Coding Millstone


March 2002: Losing the Coding Millstone

Losing the Coding Millstone

March 2002

Codagen lightens your load with Gen-it Architect, a code generation tool that applies architectural and design choices to your model to produce as much source code as possible early on.

By David Dossot

Let's face it: Some day, you'll have to generate code from your UML diagrams. This might sound obvious, but each of us knows at least one project where the analysts would never take a coffee break at the same time as the programmers or the architects.

Preaching the value of UML and code reuse is one thing, but having the proper tool to apply them is the real challenge. For this purpose, Codagen offers Gen-it, a powerful and versatile code generation tool that applies architectural and design choices to a model in order to produce as much code as possible before it's handed off to the programmers. Gen-it allows you to prepare templates that will transform technical frameworks and design patterns into the reality of code without cluttering up the model with extra classes—and your analysts will love you for it.

What's the Trick?
Gen-it uses projects containing templates to generate classes, interfaces, attributes and methods. These templates are applied according to an iteration scope and desired conditions (a.k.a. "filters"). Any property of the generated object is constructed according to templates, creating values such as class, attribute or method names according to a rule. For example, this class name template:

generated object name = "gen" + name of the object in the model

will create a class name genfoo for an object named foo in the model.

The iteration's scope depends on the type of object you generate. You can iterate on source objects such as the objects in your model, or on generated objects (also called derived objects): That is, your templates can generate objects that will be used to generate other objects. The object hierarchy is also available in the iteration scope, leading to many options. For example, for a class template, you can choose to iterate on a model or a generated class, attribute or method; or a model or generated super—or subclasses of a specified class.

The conditions you create for each template can be very complex, with multiple and and or operators grouped with brackets. They're applied on UML objects from your model or from other templates, using criteria like class type, position in inheritance, or even external properties you can define and set in your UML modeling tool. In addition, you can use the same iteration/filter/value features to generate code inside a method—a powerful advantage.

Gen-it Under Fire
I tested the Java version of Gen-it Architect on Windows 2000 Professional, using Rational Rose 2000 Enterprise Edition as the modeling tool. (According to the documentation, I also could have used TogetherSoft's Together ControlCenter.) Gen-it also supports Visual Basic, C++, C# and XML, but I didn't evaluate those versions. The installation was quick and easy.

I was pleasantly surprised by the numerous and well-documented sample templates. If you want to go deeper, detailed contextual help is always available in the system.

My series of tests consisted first of simple code generation applying Codagen's sample templates for "basic code generation" and "singleton pattern code generation," among others. In your modeling tool, after selecting the classes that are candidates for code generation, Gen-it features are conveniently accessible from a contextual menu, including:

  • features to define and set external properties on your classes,
  • a shortcut to open Gen-it Organizer (the templates management tool that inherits the definition of the selected objects),
  • a shortcut to quickly generate code directly from an existing Gen-it project on the selected classes (this feature is called "Batch-it for Java").

Code can be preserved at generation time via a mechanism called a "pocket," in which the developer inserts business logic code. This crucial feature allows iterative and incremental development, as specified in the Unified Process.

Then I decided to create my code implementation of one of the most famous Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) patterns: the factory.

This simple model demonstrates that a PlaneFactory will instantiate a subclass of Plane according to a parameter; say, typeOfPlane).

To generate a factory, I went through the following steps:

First, in Rose, I set an external property named needFactory on each main class for which I wanted a factory to be created.

Then, in Gen-it Organizer, I created a class template and an associated filter that iterates on each class in which needFactory equals true. The name of the newly generated classes is: Original Name + "Factory".

Finally, I added to the factory class the getter method that returns an instance of a Plane object depending on the type of plane desired. To generate the code in the most generic way, I created an operation template that iterates on the subclasses of each class needing a factory.

This can really bake your noodle, but Gen-it will replace the variables in blue with their expected content and iterate over the subclasses in the grey background block of code. All it takes now is a click on the Generate button, and ... Voila!, the file PlaneFactory.java appears, containing the expected code:

public class PlaneFactory extends Object {

public static Plane getPlane (java.lang.String typeOfPlane)
{
if (typeOfPlane.equals("Jet")) return new Jet();
if (typeOfPlane.equals("Prop")) return new Prop();
// no type of plane recognized : return null
return null;
}
} //PlaneFactory

After saving this project, I could then use Batch-it for Java from my modeling tool to directly generate Java code without having to open Gen-it Organizer. It's interesting to note that Batch-it can be licensed independently, allowing programmers to generate code without having to install the full product (and with no temptation to alter the templates!).

Of course, this is a simple pattern example, but you can use Gen-it to generate complete frameworks like those required in persistence layers or EJBs.

Overall, a positive experience. On the downside, I found the constant display of available filters a bit confusing. I'd also like to see a tree view, organized by category, of the templates and some better way of managing a large number of templates across distinct projects.

Icing on the Cake
Gen-it offers several other cool features. One is that you can check the model you're working on at any time, without switching back to your UML tool, as the Organizer offers an embedded hierarchical view of the current objects, showing related characteristics (types, visibility, external properties ...) and documentation (see "Minimize Application Switching").

Another useful feature is "macro templates," which can eliminate duplicate code across methods. This feature is useful, for example, in error handling and logging. The example shows a simple catch block of code. After creating the macro template, you can insert the piece of code in your other templates from a contextual menu, provided the iteration scope of your macro matches that of the template you want to insert the macro code into. Moreover, a macro can insert the content of another macro or call itself recursively. (In this case, you must add conditions, like hierarchical boundaries, to stop the recursion.)

The following code sample shows the usage of two macros (try and catch) in our previous operation template. I've added a code pocket to demonstrate this preservation feature: Any code entered in the pocket in an external Java editor will be preserved during a subsequent generation process.

Gen-it's companion file templates are another powerful feature. They allow you to generate external files in different formats (DOC, TXT, HTML, XML) according to a template and an iteration scope. It can be an interesting alternative to JavaDoc generation, as you can automatically build tailor-made documentation.

I wanted to check if this feature could be used in CORBA projects, where you'd have to generate class methods information files in IDL (Interface Definition Language), so I created this companion file template, associated with a filter on derived classes (classes generated by the class templates).

After running this template, Gen-it created a file named PlaneFactory.idl containing the following code:

module RootPackage  {
  interface PlaneFactory {
Plane getPlane(typeOfPlane);
};
};

It's nice to avoid the drudgery of hand-updating such files every time an associated class changes. What's more, Gen-it can eliminate a big source of runtime configuration errors by automating the generation of companion files.

When Is It Needed?
Using a code generation tool isn't an easy decision: Code generation requires the efforts of skilled professionals with thorough knowledge of analysis, design and implementation; a high level of abstraction is also necessary to produce efficient templates.

I'd recommend the following rule to determine if you need a code generation tool: Multiply the number of classes by the number of frameworks (a database data model counts as one framework). If you get 200 or more, automatic code generation is worth the trouble. Of course, if you run several small projects and want to maximize code reuse, Gen-it can also come in handy.

Another major consideration when deciding whether to use Gen-it is the cost. At $80,000 for a year's license, the product is one of the most expensive code-generation tools available.

Finally ...
Gen-it allows each team working on your project to focus on high-value-added tasks: analysis with only significant classes, design with guaranteed code reuse, and creating business logic without repetitive tasks. Moreover, because Gen-it involves all parties, it facilitates communication among your analysts, designers and programmers.

Gen-it has the main features expected from a professional code generation tool:

  • It works with any design rather than forcing a particular design on the user.
  • It can implement a complete framework from the analysis.
  • It preserves code to allow incremental development.

On top of this solid base, Gen-it offers a tight integration with your modeling tool plus advanced iteration, filtering and code generation features in a user-friendly environment. If you add that Gen-it can generate standard Java code files or export data to Visual Age (ENVY repository) and is able to import XMI documents (UML model under XML format), it's a good investment for any company that wants to improve both its methods and its quality of development.

Figure title: Minimize Application Switching Figure caption: Gen-it operations are only a right-mouse-click away from within Rational Rose (top) and TogetherSoft's Control Center. Also, if you're in Gen-it, the Organizer window (bottom) shows your model's class hierarchy so you don't have to go into your design tool.

Generation X-press
The next big jump in abstraction may be in models that code themselves.

The following is a selected list of significant products that include code generation. Gen-it extends the code generation of Rational Rose, Together ControlCenter and Microsoft Visual Modeler by allowing several meta-code constructs. Some of these products also include complete case tools.

Codagen
Gen-it Architect
www.codagen.com

Computer Associates
Paradigm Plus
www.cai.com

ILogix
Rhapsody in J
www.ilogix.com

Kennedy Carter
I-CCG
www.kc.com

Microgold
WithClass
www.microgold.com

Microsoft
Visual Modeler 2.0
www.microsoft.com

No Magic
MagicDraw
www.nomagic.com

Project Technology
BridgePoint Modeling Suite
www.projtech.com

Rational Software
Rational Rose
www.rational.com

Softeam Objecteering
Java Developer
www.softeam.fr

TogetherSoft
Together ControlCenter
www.togethersoft.com

 

Gen-it Architect
Codagen Technologies
Corporate Office
2075 University St., Ste. 1020
Montreal, Quebec
Canada H3A 2LI
Fax: (514) 288-2446
E-mail: [email protected]

Pricing Scheme:

$80,000 for an annual renewable package that incudes one architect license and 10 developer licenses. A perpetual license is $220,000.

Technical Requirements:

Windows 98, NT (SP4), ME or 2000. Modeling Tool: Rational Rose, Microsoft's Visual Modeler (a Visual Studio component) or TogetherSoft Together 4.x and 5.x.

RATING: **** The Rate Sheet
Pros:
  1. Gen-it preserves code, allowing for incremental development.
  2. It tightly integrates with leading modeling tools.
  3. The product has several useful code-generation features (iterating, filters, macro templates, companion file templates).

Cons:
  1. The constant display of available filters is confusing.
  2. The organization of templates in categories is inconvenient.
  3. It's expensive.

Minimize Application Switching



[click for larger images]
Gen-it operations are only a right-mouse-click away from within Rational Rose (top) and TogetherSoft's Control Center. Also, if you're in Gen-it, the Organizer window (bottom) shows your model's class hierarchy so you don't have to go into your design tool.

[return to text]

 


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.