Samples > Rule-Based Programming > How to Run a Large Number of Rules

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

Key features include:

Importing this Sample

To import the manyrules sample:

  1. Click File > Import.
  2. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials and click Next.
  3. Select the samples > engine > manyrules sample.
  4. Click Finish.
  5. If one or more files already exist in your workspace, a warning message is displayed indicating that the corresponding file(s) were not re-imported. Close the message window.

The imported sample project files are displayed in the Rule Explorer.

Running this Sample

Make sure you are in the Rule perspective before running this sample.

To run the manyrules-application project:

  1. Click Run > Open Run Dialog.
  2. Select the Java Application > manyrules-application launch configuration.
  3. Click Run.
  4. Output is displayed in the Console view and a ruleset archive, ruleset.jar, is created in the manyrules-application > generated folder.
  5. Refresh the Rule Explorer and expand the 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:

  1. Click Run > Open Run Dialog.
  2. Select the Java Application > manyrules-application launch configuration.
  3. In the Arguments tab, change the value in the Program Arguments field from SEQUENTIAL to RETEPLUS.
  4. Click Run.
  5. Output is displayed in the Console view.

To run the manyrules-application project using the Fastpath Algorithm:

  1. Click Run > Open Run Dialog.
  2. Select the Java Application > manyrules-application launch configuration.
  3. In the Arguments tab, change the value in the Program Arguments field from RETEPLUS to FASTPATH.
  4. Click Run.
  5. Output is displayed in the Console view.

Highlights

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.

Related Concepts

Technical Rules
Related Concepts
Fastpath

Related Tasks

Working with Technical Rules

Related Reference

ILOG Rule Language
dynamicselect

Related Samples and Tutorials

Import Samples and Tutorials Wizard