| ILOG JRules User Guide > Executing Rules > Tasks > Executing a Ruleset Using a Native Rule Engine > Writing a Ruleset Execution Method |
Writing a Ruleset Execution Method |
PREVIOUS NEXT |
This section describes how to write a ruleset execution method. It also describes how you can execute rules and get the number of rules fired, get the output ruleset parameters for all output parameter pairs, set an exception handler to treat user code exceptions that may be thrown during rule execution and add the root object of an XML data file.
To write a ruleset execution method, you need to create a main class file then edit it.
To write a ruleset execution method:
You can execute the rules and get the number of rules fired.
To get the number of rules fired:
You can get the output ruleset parameters for all output parameter pairs (String key, <type>).
To get the output ruleset parameters for all output parameter pair:
You can set an exception handler to treat user code exceptions that may be thrown during rule execution.
To set an exception handler:
This exception handler silently catches all exceptions raised during condition evaluation. All other cases (actions, and ruleflow execution) are thrown back to the engine caller, as if there were no exception handler.
If the exception handler is set for the ruleflow. For example:
?context.ruleflowExceptionHandler = new MyExceptionHandler();
the exception handler is also available in the Sequential and Fastpath algorithms.
To can also add the root object of an XML data file.
To add the root object of an XML data file:
By default, adding the root object does not do a recursive add. If you want to process rules on the other objects, therefore, you must use the in and from keywords in your rules to reach the non-added objects.
Here is an extract from a rule named MatchSession1 to illustrate the use of these keywords:
rule MatchSession1{when{?c: Conference();?s: Session(?t: title.toLowerCase()) in ?c.sessionList;?p: Participant() in ?c.participantList;collect String(?t.indexOf(toLowerCase()) != -1) in ?p.interestListwhere (size() >= 2);}...
Using an IlrXmlObjectAsserter object, you can add the following lines:
IlrXmlObjectAsserter asserter = new IlrXmlObjectAsserter(engine);asserter.registerAllXmlClasses();// Add all the objectsasserter.exploreObject (obj);
Using an IlrXmlObjectAsserter, it is possible to `filter' the objects added in the working memory. Here, all the objects have been added so there is no need to use the in or from in your rules.
Here is an extract from a rule that does not use these keywords:
rule processValidItem{priority=high;when {item: Item ( "waiting".equals(state) );product: Product ( id.equals(item.product); stock >= item.quantity );}...
Use the ILOG utility classes as follows to pass a ruleset parameter as an instance of the class .w3c.dom.Element:
helper.addXMLDocumentAsParameter("Data", string, IlrRuleSessionHelper.getXmlBindingType());
The string representation can be built from an XML file by using the static method IlrUtil.getContentsAsText(FileReader file).
The following code provides an example of a complete ruleset execution class. This class is used in the code sample: samples > architecture > xmlbinding:
import ilog.rules.archive.IlrJarArchiveLoader;
import ilog.rules.archive.IlrRulesetArchiveLoader;
import ilog.rules.engine.IlrContext;
import ilog.rules.engine.IlrParameterMap;
import ilog.rules.engine.IlrRulesetArchiveParser;
import ilog.rules.xml.IlrXmlErrorException;
import ilog.rules.xml.IlrXmlObject;
import ilog.rules.xml.binding.IlrXmlDefaultDataDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.jar.JarInputStream;
public class SimpleRuleEngineRunner {
public static void main(String[] args) throws Exception {
// args[0] stores the ruleset path
// args[1] stores the xmlFileName
String rulesetPath = args[0];
String xmlFileName = args[1];
System.out.println("financial information is " + xmlFileName);
System.out.println("ruleset archive is " + rulesetPath);
File rulesetFile = new File(rulesetPath);
JarInputStream is = new JarInputStream (new
FileInputStream(rulesetFile));
IlrRulesetArchiveLoader rulesetloader = new IlrJarArchiveLoader(is);
IlrRulesetArchiveParser rulesetparser = new IlrRulesetArchiveParser();
boolean parsed = rulesetparser.parseArchive(rulesetloader);
if (!parsed)
return;
// Create the engine
IlrContext engine = new IlrContext(rulesetparser.getRuleset());
try {
// Read the input XML data
IlrXmlDefaultDataDriver driver = new IlrXmlDefaultDataDriver
(engine.getRuleset().getReflect());
IlrXmlObject xmlObject = driver.readObject (new
FileReader(xmlFileName));
// Set the input parameters
IlrParameterMap inputs = new IlrParameterMap();
inputs.setParameter("eventBatch", xmlObject);
engine.setParameters(inputs);
// Execute the ruleset
IlrParameterMap outputs = engine.execute();
// Write the modified XML data
driver.writeObject(xmlObject, new FileWriter("results.xml"));
// Get the number of fired rules
int nrules = outputs.getIntValue("ilog.rules.firedRulesCount");
System.out.println(nrules + " rules fired");
// Clean the engine
engine.retractAll();
engine.end();
}
catch ( IlrXmlErrorException e ) {
e.printStackTrace();
}
}
};
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |