Drools 3.0.6 - Serialize compiled Rules
Friday, April 25th, 2008I’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~
