2.5.7. Agenda

2.5.7. Agenda

Two Phase Execution
Figure 2.23. Two Phase Execution

The Agenda is a RETE feature. During a Working Memory Action rules may become fully matched and eligible for execution. A single Working Memory Action can result in multiple eligible rules. When a rule is fully matched an Activation is created, referencing the Rule and the matched facts, and placed onto the Agenda. The Agenda controls the execution order of these Activations using a Conflict Resolution strategy.

The engine operates in a "2 phase" mode which is recursive:

  1. Working Memory Action Phase

    This is where most of the work takes place, in either the Consequence or the main java application process. Once the Consequence has finished or the main Java application process calls fireAllRules() the engine switches to the Agenda Evaluation phase.

  2. Agenda Evaluation Phase

    The engine attempts to select a rule to fire. If a rule is not found it exits, otherwise it attempts to fire the rule, switching the phase back to Working Memory Actions. The process repeats again until the Agenda is empty.

Two Phase Execution
Figure 2.24. Two Phase Execution

The process recurses until the agenda is clear, in which case control returns to the calling application. When Working Memory Actions are taking place, no rules are being fired.

2.5.7.1. Conflict Resolution

Conflict resolution is required when there are multiple rules on the agenda. As firing a rule may have side effects on working memory, the rule engine needs to know in what order the rules should fire. For instance, firing ruleA may cause ruleB to be removed from the agenda.

The default conflict resolution strategies emplyed by Drools are: Salience and LIFO (last in, first out).

The most visible one is "salience" or priority. A user can specify that a certain rule has a higher priority by giving it a higher number then other rules. In that case, the higher salience rule will always be preferred.

LIFO prioritises based on the assigned Working Memory Action counter value. Multiple rules created from the same action have the same value and execution of these are considered arbitrary.

As a general rule, it is a good idea not to count on the rules firing in any particular order, and try and author the rules without worrying about a "flow" where the order matters.

Custom conflict resolution strategies can be specified by setting the Class in the RuleBaseConfiguration method setConflictResolver, or using the property "drools.conflictResolver".