| Samples > Rule-Based Programming > How to Run a Large Number of Rules |
How to Run a Large Number of Rules |
PREVIOUS NEXT |
This sample shows how the rule engine executes many rules on a few objects. In this sample, the rules require no stateful behavior of the algorithm. They can be executed using either the RetePlus algorithm or the sequential algorithm. In this case, the sequential algorithm is an appropriate choice.
Key features include:
IlrRulesetArchiveBuilder ruleset archive building API
IlrRuleset and IlrContext parsing and execution API.
ruletask main { ... } construction)
property algorithm = ... and other property constructions)
dynamicselect()... construction)
scope, dynamicselect, collect IlrRule(...) and cache to improve performance.
To import the manyrules sample:
samples > engine > manyrules sample.
The imported sample project files are displayed in the Rule Explorer.
Make sure you are in the Rule perspective before running this sample.
To run the manyrules-application project:
Java Application > manyrules-application launch configuration.
ruleset.jar, is created in the manyrules-application > generated folder.
manyrules-application > generated to locate the IRL files and the ruleset archive ruleset.jar. You can open this archive file with any JAR or ZIP archive viewer to see the ruleset that is generated and executed by the sample.
To run the manyrules-application project with the RetePlus algorithm:
SEQUENTIAL to RETEPLUS.
To run the manyrules-application project using the Fastpath Algorithm:
RETEPLUS to FASTPATH.
The Java application project manyrules-application runs a ruleset from a Java application. A main method first generates the ruleset file from Velocity template files. The generated ruleset contains many rules, organized in a ruleflow with two ruletasks. The JRules engine executes this ruleflow for a series of input objects.
This sample is designed using a template-based ruleset generator. The generated ruleset contains a large number of similar rules, plus an ilrmain function dedicated to executing the rules on a series of input objects.
The generateIRLFiles method generates IRL files for the different parts of the ruleset (ruleflow, rule packages, ruleset header). The number of generated rules is around 6000, but you can change it by expanding the number of treated cases. The generateRulesetArchive then assembles these IRL files with the BOM file and builds a ruleset archive using the IlrRulesetArchiveBuilder class. The parseArchive method is used to parse the generated ruleset archive, using the IlrRulesetArchiveParser class, and an IlrContext engine is created with this ruleset.
After initial parsing, a first dummy execution triggers either the sequential dynamic bytecode generation or the dynamic bytecode generation of the RetePlus conditions. All subsequent executions take advantage of this generated bytecode.
The generated ruleset includes a dynamic selection of the rules to be evaluated in the ruletask. This type of optimization simultaneously takes advantage of:
This sample includes a large number of rules and rule selection is therefore key to performance. In addition to the built-in rule engine optimized algorithms, we implemented a specific selection strategy, using a slicing criterion based on a rule property. The dynamic selection retrieves the appropriate rule slice based on some property of the input object to be processed.
We declared a currency property in the rules, based on the currency of the cash event they are supposed to process. As the input cash events also have a currency property, we are able to get the valid rules for each cash event, without recomputing this every time. To do so, the dynamicselect of the ruletask invokes a function called findRulesByCurrency that collects the appropriate rules and caches them in a HashMap.
The final results show that once the engine is prepared (ruleset parsing, engine construction and warm up), evaluation of around 6000 rules for more than 1000 events (potentially 6,000,000 rule instances to evaluate) takes about 1 minute, equivalent to around 100,000 rule instances evaluated per second.
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |