Skip to content

defining rules engine

Mahmoud Ben Hassine edited this page May 17, 2020 · 9 revisions

Easy Rules provides two implementations of the RulesEngine interface:

  • DefaultRulesEngine: applies rules according to their natural order (which is priority by default).
  • InferenceRulesEngine: continuously applies rules on known facts until no more rules are applicable.

Create a rules engine

To create a rules engine, you can use the constructor of each implementation:

RulesEngine rulesEngine = new DefaultRulesEngine();

// or

RulesEngine rulesEngine = new InferenceRulesEngine();

You can then fire registered rules as follows:

rulesEngine.fire(rules, facts);

Rules engine parameters

Easy Rules engine can be configured with the following parameters:

Parameter Type Required Default
rulePriorityThreshold int no MaxInt
skipOnFirstAppliedRule boolean no false
skipOnFirstFailedRule boolean no false
skipOnFirstNonTriggeredRule boolean no false
  • The skipOnFirstAppliedRule parameter tells the engine to skip next rules when a rule is applied.
  • The skipOnFirstFailedRule parameter tells the engine to skip next rules when a rule fails.
  • The skipOnFirstNonTriggeredRule parameter tells the engine to skip next rules when a rule is not triggered.
  • The rulePriorityThreshold parameter tells the engine to skip next rules if priority exceeds the defined threshold.

You can specify these parameters using the RulesEngineParameters API:

RulesEngineParameters parameters = new RulesEngineParameters()
    .rulePriorityThreshold(10)
    .skipOnFirstAppliedRule(true)
    .skipOnFirstFailedRule(true)
    .skipOnFirstNonTriggeredRule(true);

RulesEngine rulesEngine = new DefaultRulesEngine(parameters);

If you want to get parameters from your engine, you can use the following snippet:

RulesEngineParameters parameters = myEngine.getParameters();

This allows you to reset the engine parameters after its creation.