Business Object Model Transformation
I recently worked on a little piece of proof-of-concept code that converts a BOM into a target source language. The principle is very simple: recursively visit the classes and attributes in the BOM, convert them into text and then persist the results to disk. I used Apache Velocity to perform the template-based source text generation.
The generation framework adds two parameters to the VelocityContext: ‘$class’ is the instance of ilog.rules.engine.bom.IlrClass that we want to generate source text for, while ‘$util’ is a utility class to help with the source generation.
The first template I developed generates Java source code from the classes and attributes in the BOM:
#if($util.isNestedClass($class) == false )
package $class.getEnclosingNamespace().getFullyQualifiedName();
#end
/**
* Class $class.getDisplayName() was generated from the ILOG JRules BOM.
* Do not modify this generated code as it will be overwritten the
* next time the BOM is built.
* @author jrules
*/
$util.getTypeDeclaration( $class )
{
#foreach($attribute in $class.getAttributes() ) $util.getAttributeDeclaration( $attribute );
#end
#if( $class.getNestedClasses() )
#foreach($nested in $class.getNestedClasses() )
$util.generateCode( $nested )
#end
#end
}
Sample output:
package pkg;
/**
* Class pkg.Foo was generated from the ILOG JRules BOM.
* Do not modify this generated code as it will be overwritten the
* next time the BOM is built.
* @author jrules
*/
public class Foo extends java.lang.Object
{
public java.lang.String name;
/**
* Class pkg.Foo.Nested was generated from the ILOG JRules BOM.
* Do not modify this generated code as it will be overwritten the
* next time the BOM is built.
* @author jrules
*/
public class Nested extends java.lang.Object
{
public float nestedAttribute;
}
}
This might allow someone to create the BOM using Rule Studio, verbalize the BOM, author rules, then generate bootstrap XOM code from the BOM. Note that because this was just a proof-of-concept I did not handle enumerations, domains or generate generics.
I then developed a template that converts an IlrClass into an Ecore class definition, and another that renders the IlrClass into an HTML document. It’s interesting to imagine other scenarios as well, perhaps generating Hibernate annotated classes for persistence, BOM to BOM transformation, custom B2X mapping code, XML Schemas, HTML forms, or even a vocabulary for the BOM. Here I’ve used a “poor man’s” approach for model-to-text transformation, for more complex and robust generation you might want to consider a commercial or open source library — perhaps using Ecore as a pivot model for your transformations.
You can download the three Velocity templates here: velocity-templates.zip
Ecore class generated from a BOM class.
The Java class BomCodeGenerator.java implements the recursive visiting of the elements of the IlrObjectModel, sets up Velocity and manages persistence of results. You could invoke this code from the context menu for a BOM entry within Rule Studio or even from a custom Eclipse IncrementalProjectBuilder.
Let me know what you think, and where you might have a need for BOM transformation.
Tags: BOM, export, transformation








November 20th, 2008 at 9:11 pm
IBM Ascential has a tool that takes ecores and generates the sql database tables and java classes for the model. IBM’s medataserver product also generates the ORM mappings between sql database and java classes.
The real interesting bit is automatically converting a pattern for a given object to the corresponding database query without having the developer manually write it. This way, one could “lazily” load objects from the database using subgoaling techniques. Say the first join node is right activated, we can use that to execute a query on the fly against a database. The same approach could be applied to any join node in a rule to lazily load and assert data on the fly. This way, the user writes one rule, but depending on the configuration, the engine could eagerly match, or lazily query the database.
I’ve done this in the past using a business rule language and generating the query rules with a custom rule compiler I wrote.
November 21st, 2008 at 10:07 am
Peter,
The challenge/power is that the distinction between the database and the “rule engine” gets very blurred. You need to start to think about DB query optimization techniques to decide whether you want to join in the DB or join in memory. Given that the DB and DB driver are already performing a lot of this work you might need some unifying technology or data models to do this well…
Dan
November 21st, 2008 at 3:29 pm
Daniel - funny we were discussing the topic of BOM-to-fact translation in the current round of W3C RIF standards development work. It looks like its time you were conscripted by your colleagues into the RIF PRD work too ….
Cheers