Skip to content

defining rules engine

Mahmoud Ben Hassine edited this page May 20, 2017 · 9 revisions

Easy Rules default engine applies rules according to their natural order (which is priority by default).

Create a rules engine

To create a rules engine, you can use the static method RulesEngineBuilder.aNewEngineBuilder():

RulesEngine rulesEngine = aNewEngineBuilder().build();

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
silentMode 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 rulePriorityThreshold parameter tells the engine to skip next rules if priority exceeds the defined threshold.
  • The skipOnFirstNonTriggeredRule parameter tells the engine to skip next rules a rule is not triggered.
  • The silentMode allows you to mute all loggers when needed.

You can specify these parameters through the RulesEngineBuilder API:

RulesEngine rulesEngine = aNewRulesEngine()
    .withRulePriorityThreshold(10)
    .withSkipOnFirstAppliedRule(true)
    .withSkipOnFirstFailedRule(true)
    .withSkipOnFirstNonTriggeredRule(true)
    .withSilentMode(true)
    .build();

All parameters are encapsulated in the RulesEngineParameters class. 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.