Artwork implying World Wide Web Architecture

Part 3: Create Rules Engine and Execute Rules

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 Load and parse a ruleset EmbeddedRulesEngine.java Other JRules Articles

Once you have loaded and parsed a ruleset, the next step is executing the rules. In order to do this, you need to:

  1. Create an IlrContext. This object contains the rules engine itself, an agenda of rules to execute, and working memory with the data that the rules need to analyze.
  2. Create an IlrParameterMap. This object must hold all the input parameters that the ruleset requires. If the ruleset has no required input parameters, this step is optional.
  3. Populate working memory with any data that the ruleset requires. If the ruleset gets all its data from input parameters, this step is optional.
  4. Order the rules engine to execute.

Creating the context is straight forward. You need a parsed ruleset that we showed you how to create earlier.

IlrContext context = new IlrContext(ruleset);

The ruleset you just passed to the rules engine is useless without data. That data can come from one of two places. It can be passed as a parameter to the rules engine or it can placed directly into working memory (see below). Either technique is legal. The technique you choose depends on how the ruleset is written. If the ruleset requires input parameters and the rules in that ruleset operate only on those input parameter values, they you have to pass input parameters. Any data you write directly into working memory will probably be ignored. If the ruleset does not accept input paremeters and the rules in that ruleset make use of data in working memory, then any data you pass as an input parameter will be ignored and you will have to write data

If you want to pass parameters to the rules engine, you must create a map of values and pass that map to the rules engine. A map is a collection of name / value pairs. The name is a string. The value can be any Java object or any built in Java data type. The name you use must be the same as a name that the person who created the ruleset specified as a ruleset parameter. Names are case sensitive. If the name you put in the map does not match a name that was specified as a ruleset parameter, that name and its associated value is ignored. There is no error message. No exceptions are thrown.

IlrParameterMap inputs = new IlrParameterMap(); Report r = new Report(); inputs.setParameter("report", r); context.setParameters(inputs);

If you look at the complete sample code, you will notice that the code above is commented out. That is because the ruleset we used does not require this parameter. It is optional.

As we mentioned earlier, you can choose to place data directly into working memory. The IlrContext object you created earlier has a method called insert() for just this purpose. This method does not throw any checked exceptions and it does not report any error conditions.

TruckDrivers t = new TruckDrivers(); Driver[] array = t.getBoundarySample(); for ( int x = 0; x < array.length; x++) { context.insert( array[x] ); }

The rules engine executes when you tell it to. The IlrContext object that you created earlier contains the rules engine. That context object has a method called execute() that does not throw any checked exceptions. It returns an IlrParameterMap that contains any output parameters that the ruleset specified. We show one possible way to print out the values that the output map contains. You can use this method without knowing what values are in the map.

IlrParameterMap outputs = context.execute(); Set s = outputs.keySet(); Iterator i = s.iterator(); while( i.hasNext() ) { String key = ( String)i.next(); System.out.println( key + ": " + outputs.getObjectValue(key).toString() ); } // end loop

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