Samples > Rule-Based Programming > How to Select Rules to be Executed

This sample shows how to use strategies to select which rules are evaluated for a given task. Please note the information provided in Using the Samples.

Key Features

This sample shows how to use various strategies for selecting which rules should be evaluated for a given task. This is particularly useful when rules become numerous and specialized.

In JRules, rule selection relies on a selection pipeline that behaves like a series of filters, using rule properties instead of rule conditions.

Importing this Sample

As this sample will install a custom rule model instead of the default one, it is recommended to explore and run it in a separate workspace.

To prepare a new workspace:

  1. Click File > Switch Workspace and specify a new workspace directory. Eclipse will exit and restart automatically with a blank workspace.
  2. Switch to the Rule perspective.

To import the ruleselection sample:

  1. Click File > Import.
  2. In the Import Wizard, select ILOG Rule Studio Samples and Tutorials and click Next
  3. Select the sample: samples > engine > ruleselection.
  4. Click Finish.
  5. The required sample project files are imported and displayed in the Rule Explorer.
    If your Eclipse is using the automatic build mode, the project is built with a large number of errors. This is because the project uses an extended model and Rule Studio should be configured for it. You will do this in the next step.
  6. Click Window > Preferences.
  7. In ILOG Rule Studio > RuleModelExtension, set the Extension model to ruleselection-rules/data/ruleselection.brmx, and set the Extension data to ruleselection-rules/data/ruleselection.brdx.
  8. Click OK.
  9. If prompted to restart Eclipse, click Yes.
  10. Click File > Switch Workspace then click OK without changing the workspace location. Eclipse with shut down and immediately restart.
  11. Once Eclipse restarts and the project is rebuilt, the built errors are resolved since the correct extended model is used.

The rule model extension and its attached data file are now recognized by Rule Studio.

Running this Sample

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

To run the sample:

  1. Click Run > Open Run Dialog.
  2. Select the ruleselection-rules-case1 launch configuration.
  3. Click Run.
  4. The console displays the initial state of the contract, then lists which rules were extracted to the ruleset archive, which rules were selected in each ruletask, and which were finally fired. The final state of the contract is then displayed.
  5. Select and run the ruleselection-rules-case2 and the ruleselection-rules-case3 launch configurations.

The console trace shows that, given a different extractor and a different input parameter, different rules are extracted, selected and fired.

Highlights

The selection strategy is split into two phases:

This sample loads the ruleselection-rules rule project and the ruleselection-xom XOM project. Rules evaluate a contract status based on information about the company featured in the contract, and information about the contract itself.

A first ruletask, called assessment, evaluates a risk level based on the company revenue, a rating of the company, and the amount of the contract.

A second ruletask, called decision, determines the status of the contract (using a set of predefined status values such as ACCEPTED, REFUSED, NEGOCIATE), based on the evaluated risk level and the expected profit from the contract.

The rule model extension located in the data directory of the ruleselection-rules project, enhances the rules in the sample with the following properties:

Using the default behavior of the product and these properties, rule selection implements the following strategy:

ruleselection-rules-case1

With the ruleselection-rules-case1 launch configuration, which evaluates a contract with a software company in north California with the V1 production application, only the following rules are retained:

And only the following rules are fired:

V2-* and *-Europe-* rules are not extracted by the query.

Rules such as assessment.rating.V1-NorthAmerica-Services and assessment.rating.V1-BlackList-NorthernCalifornia-Software are in the ruletask scope, but are rejected by the dynamicselect.

Finally, assessment.rating.V1-NorthAmerica-Software is overridden by assessment.rating.V1-USA-Software, which is overridden by assessment.rating.V1-NorthernCalifornia-Software.

ruleselection-rules-case2

With the ruleselection-rules-case2 launch configuration, which evaluates a contract with a French Bank agency in Italy with the V2 beta application, only the following rules are retained:

And only the following rules are fired:

V1-* and American rules are not extracted by the query.

ruleselection-rules-case3

The ruleselection-rules-case3 launch configuration evaluates a contract with a Software company named Swindle Systems in Northern California with the V1 production application. In this case, the rule assessment.rating.V1-BlackList-NorthernCalifornia-Software is selected during the dynamic selection, and overrides the assessment.rating.V1-NorthernCalifornia-Software rule and all rules that were overridden by the latter.

Notes
  1. It is interesting to notice that rule execution is not only controlled by the engine algorithm reasoning on rule conditions but also enforced by a selection strategy based upon rule properties, prior to actual execution.
  2. The sample has been created with functions in the ruleflow and specific actions in the rules, in order to track and display rules extracted in the archive, selected in a task and fired. An IlrTool implementation could have been used instead of rule actions to display which rule is actually fired.
  3. An IlrAgendaFilter may also be used to filter out rule instances, just before being fired, even when the rule was selected and the rule condition matched.

Related Concepts

Engine Execution Modes
Extracting Artifacts to be Put in a Ruleset
Rule Overriding
Extended Properties
Rule Control Properties

Related Tasks

Defining Rule Model Extensions
Creating a Struct Property
Creating a Hierarchical Property
Querying

Related Reference

dynamicselect
select
match

Related Samples and Tutorials

Import Samples and Tutorials Wizard
How to Use Pattern Matching to Apply Rules on a Set of Objects
How to Monitor Ruleset Execution