Rule chaining allows the outcome of one rule to define a fact that subsequent rules can use.
Steps to implement:
- Define a rule with a high
priority. - In its
onSuccess or onFailure handler, use almanac.addFact() (formerly addRuntimeFact) to set a new fact value. - Define subsequent rules with a lower
priority that include the newly created fact in their conditions.
Note: almanac.addRuntimeFact() is deprecated. Use almanac.addFact() instead.
engine.addRule({
conditions,
event,
onSuccess: function (event, almanac) {
almanac.addFact('rule-1-passed', true) // track that the rule passed
},
onFailure: function (event, almanac) {
almanac.addFact('rule-1-passed', false) // track that the rule failed
},
priority: 10 // a higher priority ensures this rule will be run prior to subsequent rules
})
// in a later rule:
engine.addRule({
conditions: {
all: [{
fact: 'rule-1-passed',
operator: 'equal',
value: true
}]
},
priority: 1 // lower priority ensures this is run AFTER its predecessor
})