Archive for the 'Programming' Category

Drools 3.0.6 - Serialize compiled Rules

Friday, April 25th, 2008

I’ve finally figured a way to serialize the compiled rules for real time deployment. I’ve been struggling with serialization using Drools 3.0.6 for a while now. I’ve been told that many of bugs surrounding compiled rule object serialization has been fixed for later versions of Drools. Matter a fact, the process of serializtion completely changed from Drools 4.x and above.

For those of you still using Drools 3.0.6, (I wouldn’t be surprised If I am the only one) here is a sample way of going about it.

First, serializing RuleBase object is still out in Drools 3.0.6. It still complains about a class that doesn’t exists with release of Drools 3.

Way to go about it is to serialize the package. I’ve experienced class loader issues when serializing Package objects for prior releases of Drools, specifically Drools 3.0.5 and below. Drools 3.0.6 seem to have fixed these class loader issues.

When you compile rules, DRL or DRL/DSL, it produces a package object. These packages are added to RuleBase object through RuleBaseFacotry and finally, an instance of working memory is created. Out of the three step process, compiling the rules is the most memory intensive.  So serializing packages is where you will save most time and enhance performance. 

Before you start writing codes, be sure all your rules related java objects implements java.io.Serialize. But I’m sure you knew that already. =)

Using Eclipse IDE with Drools plugin, compile the rules and serialize the packages to Direcotry of your liking. Here is an example:

FileInputStream fsDrl = new FileInputStream(”myDrl.drl”);
FileInputStream fsDsl = new FileInputStream(”myDsl.dsl”);

Reader drl = new InputStreamReader(fsDrl);
Reader dsl = new InputStreamReader(fsDsl);

PackageBuilder buildPkg = new PackageBuilder();
buildPkg.addPackageFromDrl(drl, dsl);

Package pkg = buildPkg.getPackage();

The above code will give you the compiled package object of DRL and DSL. You can proceed on to serializing this package object using standard java serialize process. Here is an example:

File pkgFile = new File(”myPkgOut.out”); //the file name can be anything of your choice.
FileOutputStream fOut = new FileOutputStream(pkgFile);
ObjectOutputStream objOut = new ObjectOutputStream(fOut);
objOut.writeObject(pkg);
objOut.flush();
objOut.close();

Simple isn’t it? Reading in the serialized package is can be accomplished by using FileInputStream and ObjectInputStream available in standard Java.

Hope this helps anyone Drooling~

Drools 3.0.6 - API compiler gone crazy.

Monday, April 14th, 2008

I’ve become very friendly with Drools 3.0.6. So friendly that I am beginning to have fights about eachothers faults.
This one is the newest one I found.

Long story short, I implemented usage of DSL with DRL.  This method made it easier on the non-technical people to read the rule files.  Syntactically, DRL needs to identify a proper DSL in order to work properly.  When I say “proper”, I mean existing file. Check out this example:

package rule.sample.anjoe

import anjoe.test.pojo1;

expander anjoeDsl.dsl

Above is the first three lines in my DRL file.  The “expander” keyword identifies which dsl mapping file is used for this DRL. Have you ever tried to put wrong DSL file name as the expander value? The Eclipse IDE will immidiately tell you that there is an error.  But what about when compiling the rules using the API in online application environment?

The standard java code is used to read in the DRL and DSL files from the file location using Reader object. You create new PackageBuilder object and call addPackageFromDRL method with two parameters; DrlReader and DslReader. As far as compiler is concerned, both DRL and DSL files needs to be valid without any errors.  If errors exists, like wrong DSL file name as expander value, the method call should fail. RIGHT? apparently NOT!!!!

When compiling the rules through JAVA API, as long as you pass in proper DRL and DSL reader object into the addPackageFromDRL method, it compiles and everything is honkie-dorie~

In Eclipse IDE, this is a no no for built in IDE rules compiler will bitch at you right away.

Has anyone come across this? Shoot me an email. joe@anjoedesign.com

JBoss Drools - Infinite Recurrsion

Saturday, March 8th, 2008

Drools, interesting name for a software isn’t it? Clever and catchy.

When JBoss changed their name from Drools to Rules Engine and back to Drools with the release of 4.0. I personally like this name. So anywayz, today I wanted to talk about something I’ve been struggling with. I’ve been using Drools 3.0.6 with FileNet Workflow system.  The rule I’ve implemented requires to modify certain value of an objects’ field within the working memory several times before spitting out the result.  With Drools 3.0.6, I’ve encountered infinite recurrsion when modifying the objects’ value.

I’ll step back for a bit and explain my situation.

The rule I have consists of multiple Agenda groups to control the rules flow.  Each agenda group contains its’ own activation group to fire one and only one rule from its’ group.  The salience is provided to control the execution order.  At any given point in the agenda groups, based on the business need, rules may need to modify the value of the fact and re-enter the working memory. The working memory does not automagically know of this changed values unless you implement the Property Change Listener.  So, in such case, after you set the value to an object, you must call modify(object); in the consequence (THEN) portion of the rule.

In the inner workings of the working memory, when Modify helper method is called, working memory will cancel any activated rule and re-enter the working memory.  Now, technically, you could set rules attribute, NO-LOOP to true and have the rule which called the Modify method not to be re-evaluated.  However, after looking at the audit log, I’ve noticed something interesting.  Everytime a Modify method is called from a rule, it does infact cancels any activated rule before re-entering the working memory. Once re-entered, the rule which called the Modify is infact not activated. However, if other rules call modify, it re-enters the working memory and reactivates any rules that matches.  NO-LOOP isn’t taken into account when reactived. 

I hope this explains my situation a bit.

Based on this, I’ve decided to come up with a “Hack”.  The business requirements I am using know when to end the rules evaluation.  If an incoming object contains certain values, rules engine should fired that rule and end the rules evaluation.  In FileNets’ term, it’s called “Dispatch Work Item”, as in send it to next step in the workflow process. 

I got lukcy in this sense.  So, in order to get rid of this, “POSSIBLE DEFECT” in drools, I implemented a very simple solution.  When the working memory is ready to Dispatch the incoming object, you “Retract” the object from the working memory.  The action of Retract cancels the entire activated list of rules.

If any of you “Droolers” encountered such behavior with Drools 3.0.6, please let me know of your solution.

 Happy Drooling!

Accessing Hibernate Table Mapping Document

Tuesday, December 4th, 2007

My current job decided to build web based database maintenance system using Struts and Hibernate. The table mapping document xml file, which I will refer to it as “table config file”, basically contains most of the information you’d want about your database. So instead of creating another configuration, I’ve decided to find out if there is a way to access them programmatically in Java. XML configuration elements that I was interested in was the column name and Javabean method name.  Here is an example of table config file:

<hibernate-mapping>
   <class name=”com.myPojo” table=”MY_TABLE_NAME”>
       <property name=”FirstName” type=”string” column=”FIRST_NAME”/>
       <property name=”LastName” type=”string” column=”LAST_NAME”/>
   </class>
</hibernate-mapping>

the property “name” attribute refers to your mapping POJO’s method. In my example above, myPojo.java will have getter and setter methods defined as “getFirstName(), setFirstName(), getLastName(), setLastName()”.

What I’m interested in is the value of name attribute and value of column attribute. Here are the Java code that will access the values

I’ve created a Configuration object called config by calling “new Configuration().configure();”

Value of “column” attribute:

  1. Get the PersistentClass object:
    PersistentClass pc = config.getClassMapping(myPojo.class.getName());

  2. Get the Table object from the PersistentClass object you just got:
    Table tbl = pc.getTable();

Once you have the table object, you can get the list of defined column names using this syntax:
Iterator it = tbl.getColumnIterator();

Value of “name” attribute:

  1. Get the Component object out using similar code syntax:
    Component cmp = config.getClassMapping(myPojo.class.getName()).getKey();

  2. Get the Property iterator:
    Iterator it = cmp.getPropertyIterator();

  3. You can access the value of name attribute by casting each items in the iteratory with Property calss.
    while (it.hasNext()) {
         ( (Property) it.next() ).getName();
    }

I know this is all that of a new information but I found this extremely helpful. I hope you guys find this info helpful as well.

As always, using Hibernate API documentation is awesome!

Happy coding guys.

FileNet P8, Software testers’ Reference

Wednesday, October 10th, 2007

I’ve been asked to lead a three day training class talking about the basics of FileNet P8 Platform. This training will be geared towards software testers who are brand new to the software.  So in this guide, I’ve decided to start from the very beginning and cover areas which are relevant to software testers.

FileNet is a Java based, enterprise workflow management system that helps manage contents and business processes.  The P8 platform comes in two flavors, Process Engine and Content Engine.  Process Engine handles the workflow elements such as work flow map, queue definitions, work item field definitions (many more) while the content engine handles the document management of the system.  As a tester of FileNet P8 based software, you will find all the tools required to support your testing. 

  • Lets start by exploring “Workflow Map”.  Workflow map is a visual representation of business process.  Usually, it’s a job of BA (Business Analysts) to gather requirements on business processes and translate it into workflow map.  As a FileNet based software tester, it is important to understand the basic components of workflow map. 
  • Work Item: This is an object representation of a task a user is performing.  It carries with it, the copy of work flow map at the time of its’ creation and work item index fields defined in the work flow map. It moves from one step to next depending on user actions.
  • Queues: This is an object representation of different departments or areas in the business. It is where work items go to wait for user interaction.
  • Routes: Paths which work items can take from one step to another.  There are condition based so that multiple routes can be defined.  To make it more obvious, it is represented by an arrow. (* har har har *)
  • Submaps: As the name indicates, this is maps within map.  It has access to all fields, queues defined in the main work flow maps.  A good example of usage of submap would be, creating business process map of HR department within a company wide process map. It is also very useful because once created, it can be added anywhere in the main work flow map. (Code reuse!!!)
  • Steps: This is a very generic term given for each points of destination in the work flow map.  Steps can be more specific like System step, queue step, general step, collection step . 
  • A work item always starts from “Launch step” or “Start step”.

I’d say that when it comes to testing, there two most important tools available to testers. The all knowing, all powerful Process Administrator (PA) and the wise Process Tracker (PT).

Why I love PA and PT:

  • PA gives you the ability to find the work item quick.  It almost looks like User Friendly SQL editor. Well actually, it is a user friendly SQL editor. You define the area of your search, (By roster? or by Queue?). Then you provide additionial criteria such as “Name of your work item fields” = “value”.
  • If you have the right access, PA gives you the ability to change values and even complete the work item!
  • PT can be accessed directly from PA results.  PT gives you a historical look at the work items’ trail and it’s current location.  This is sometimes developers’ best friend but also good friends with testers. In PT, check marks indicate where the work item stopped, arrow down symbol indicates which submap in went into and sand timer symbol indicates where it is currently awaiting progress.

So, I know this wasn’t all that helpful if you are looking for an answer on how to install fileNet or fix performance issues.  However, there arent’ that much useful links out there on FileNet p8 products.  If you know of any, please let me know. If you have any questions, leave a comment. =)