Artwork implying World Wide Web Architecture

Part 2: Load and Parse a Ruleset

Part of the IBM / iLOG JRules Series
Most recently modified on 2010-03-12
Back to home page Back to articles page WAS architecture articles page Script articles page .
See also these related articles:
Overview: invoke standalone rules engine Create rules engine and execute rules EmbeddedRulesEngine.java Other JRules Articles

Rulesets are just ZIP files. The Java code to load a ruleset looks almost exactly like the code to load a ZIP file. The first step is knowing the name of the ruleset file and knowing where that file is located. You can use either a relative file name or an absolute file name. We use a relative name.

String rulesetPath = "ruleArchive1.jar";

We open this binary file using a very standard Java programming technique. We create a File from our ruleset file name. We use that File object to create a FileInputStream. We use that FileInputStream to create a JarInputStream. In your code, you have to remember to surround this line with a try / catch block. Any attempt to open a ruleset has the potential to throw an IOException.

is = new JarInputStream( new FileInputStream( new File(rulesetPath) ) );

Once you have opened -- created -- a JarInputStream, you pass that stream to some JRules code -- an IlrRulesetArchiveLoader. You will not do anything with the IlrRulesetArchiveLoader object. Instead, you will pass this object to the code that actually parses the ruleset. (See the IlrRulesetArchiveParser below.)

IlrRulesetArchiveLoader rulesetloader = new IlrJarArchiveLoader(is);

Once you have created a ruleset loader, the next step is to attempt to parse the ruleset. You need three objects to successfully parse a ruleset.

  • The IlrRulesetArchiveLoader that you just created.
  • An IlrRuleset - a wrapper object for the ruleset itself.
  • An IlrRulesetArchiveParser that does the actual parsing.

Create the ruleset parser and the ruleset wrapper class. Associate the ruleset wrapper class with the ruleset parser. Neither of these objects throw any checked exceptions, so this code does not have to be in a try / catch block in order to compile. At this point, the IlrRuleset object is essentially empty. It will not have anything interesting inside it until you actually parse the ruleset archive.

IlrRulesetArchiveParser rulesetparser = new IlrRulesetArchiveParser(); IlrRuleset ruleset = new IlrRuleset(); rulesetparser.setRuleset(ruleset);

Attempt to parse the ruleset archive. Once again, this code does not throw any checked exceptions. You should always check the return value of the parseArchive() method. If it did not return true, you should probably exit with some kind of error message.

boolean parsed = rulesetparser.parseArchive(rulesetloader);

Once you have successfully parsed a ruleset archive, the IlrRuleset object has some interesting things in it. One of those things is an array of all the rules it found in the ruleset archive. Sometimes, this has a value when you are trying to troubleshoot a problem.

IlrRule[] ruleArray = ruleset.getAllRules();

This is a bogus pix

Arthur Kevin McGrath

Bio:

The author is an engineer with the consulting firm, Contract Engineers. He has consulted and lectured extensively since 1987 about the infrastructure that makes electronic commerce possible. His publications include Leading Practices for WebSphere Dynamic Process Edition V6.2 (SG24-7776-00) and Websphere Application Server Administration Using Jython (ISBN 0137009526), the definitive book on WAS scripting.

Photo of the author
To schedule a speaker at your location, write speakers@edu4eng.com
To inquire about consulting for your company, write consultants@edu4eng.com
To inquire about training for your company, write training@edu4eng.com