Example of Rule Engine Execution

To better illustrate how the rule engine works let us consider the following example. Suppose that our Policy object has the following rules defined (again it does not matter whether this example is very realistic):

  1. Rule 1 (initialization of State attribute):
     IF Policy.State IS UNDEFINED Then Policy.State = 'NEW' 
  2. Rule 2 (initialization of Excess):
     IF Policy.Excess IS UNDEFINED Then Policy.Excess = 0 
  3. Rule 3 (calculation of excess for young drivers)
    IF Policy.State = 'NEW' AND Policy.MyDriver.Age < 70 THEN Policy.Excess = 100 
  4. Rule 4 (calculation of excess for elderly drivers)
    IF Policy.State = 'NEW' AND Policy.MyDriver.Age >= 70 THEN Policy.Excess = 200 
  5. Rule 5 (calculation of final state)
    IF Policy.Excess > 0 THEN Policy.State = 'CALCULATED' 

Let us assume that an instance of the Policy object is being created, Policy is initialized with a reference to the Driver, whose age is 30 and all other attributes of the Policy object are initially undefined. Now let us see what happens when the rule engine starts evaluating the rules for this new instance of the Policy object.

Step 1: all rules are evaluated. The conditions are met for Rule 1 (State is initially undefined) and Rule 2 (Excess is initially undefined) only. Conditions of Rule 3 and Rule 4 are not met because the State is not ‘NEW’; conditions of Rule 5 are not met because the Excess is undefined. The agenda is formed and it initially contains actions blocks of Rule 1 and Rule 2 (each action block has one action):

Agenda:

  • Policy.State = 'New'         /*from Rule 1*/ 
  • Policy.Excess = 0.           /*from Rule 2*/ 

Step 2: an action block is randomly taken off the agenda. Let us assume that it is the action block from Rule 2. After it is executed the value of Excess attribute becomes 0. The agenda now only has the action block from Rule 1.

Agenda:

  • Policy.State = 'New'         /*from Rule 1*/ 

Step 3: Rules that depend on the changed attribute Excess are re-evaluated. Rule 2 and Rule 5 only depend on the value of Excess attribute (Rule 3 and Rule 4 set this value but do not depend on it). The conditions of Rule 2 and Rule 5 are re-evaluated. Both conditions are not met (Excess is defined and equal to 0). Therefore their actions are not placed on the agenda. Existing agenda is reviewed as well – its only action is from Rule 1, which does not depend on the changed attribute and so is left intact. The agenda therefore still has one action block from Rule 1.

Agenda:

  • Policy.State = 'New'        /*from Rule 1*/ 

Step 4: an action block is randomly taken off the agenda. There is only one action block on the agenda so it is executed. The value of the State attribute becomes ‘NEW’

Agenda: (empty)

Step 5: Rules that depend on the changed attribute are re-evaluated. Rule 1, Rule 3 and Rule 4 depend on the value of the State attribute, so their conditions are re-evaluated. Only conditions of Rule 3 are met because the State is now defined and the age of the driver is 30. So the action of Rule 3 is placed on the agenda.

Agenda:

  • Policy.Excess = 100          /*from Rule 3*/ 

Step 6: the only action block is taken off the agenda and is executed. The value of Excess becomes 100.

Agenda: (empty)

Step 7: The conditions of Rule 2 and Rule 5 that depend on the Excess attribute are re-evaluated. The condition of Rule 5 is met and the corresponding action is placed on the agenda.

Agenda:

  • Policy.State = 'CALCULATED'  /*from Rule 5*/ 

Step 8: the only action block is taken off the agenda and is executed. The value of State is now CALCULATED.

Agenda: (empty)

Step 9: The conditions of Rule 1, Rule 3 and Rule 4 that depend on the State attribute are re-evaluated. Neither condition is met; nothing is placed on the agenda.

Agenda: (empty)

Step 10: The agenda is empty; the algorithm finishes. The value of Excess is 100, the value of State is CALCULATED

Agenda: (empty)

Let us see now what would happen if a different action block was taken off the agenda at Step 2.

Step 1 is the same as before.

Step 1: all rules are evaluated. The conditions are met for Rule 1 (State is initially undefined) and Rule 2 (Excess is initially undefined) only. The agenda has the corresponding actions

Agenda:

  • Policy.State = 'New'         /*from Rule 1*/ 
  • Policy.Excess = 0            /*from Rule 2*/ 

Now we take the action from Rule 1 off the agenda.

Step 2: The action block from Rule 1 is taken off the agenda. It is executed and the value of State attribute becomes ‘NEW’. The agenda now only has the action block from Rule 2.

Agenda:

  • Policy.Excess = 0            /*from Rule 2*/ 

Step 3: The conditions of Rule 1, Rule 3 and Rule 4 are re-evaluated because they depend on the value of the changed attribute State. The condition of Rule 3 only is met (state is ‘NEW’ and driver age is 30) so its action is placed on the agenda. The agenda is reviewed as well – the rule of its existing action block does not depend on the State attribute and so the action is left intact. The agenda now has 2 actions.

Agenda:

  • Policy.Excess = 0            /*from Rule 2*/ 
  • Policy.Excess = 100          /*from Rule 3*/ 

Step 4: an action block is randomly taken off the agenda. Let us assume that it is the action block from Rule 3. After it is executed the value of the Excess attribute becomes 100. The agenda now only has the action block from Rule 2.

Agenda:

  • Policy.Excess = 0            /*from Rule 2*/ 

Step 5: The conditions of Rule 2 and Rule 5 are re-evaluated because they depend on the value of the changed attribute Excess. The condition of Rule 5 only is met so its action (Policy.State = 'CALCULATED') is placed on the agenda. The existing action on the agenda is reviewed as well. The action comes from Rule 2 that depends on the changed attribute so the condition is re-evaluated – it no longer holds (Policy.Excess is no longer undefined as its value is now 100)! This means that the action is removed from the agenda! Therefore the only action that remains on the agenda is the action from Rule 5.

Agenda:

  • Policy.State = 'CALCULATED'  /*from Rule 5*/ 

Further steps are equivalent to steps 8, 9 and 10 in the previous scenario. At the end of the algorithm we will also have the value of State='CALCULATED' and the value of Excess=100.

Finally let us consider what would have happened if a different action was taken off the agenda at step 4 of the second scenario.

The first 3 steps are the same:

Step 1: all rules are evaluated. The conditions are met for Rule 1 (State is initially undefined) and Rule 2 (Excess is initially undefined) only. The agenda has the corresponding actions

Agenda:

  • Policy.State = 'New'         /*from Rule 1*/ 
  • Policy.Excess = 0            /*from Rule 2*/ 

Step 2: The action block from Rule 1 is taken off the agenda. After it is executed the value of the State attribute becomes ‘NEW’. The agenda now only has the action block from Rule 2.

Agenda:

  • Policy.Excess = 0            /*from Rule 2*/ 

Step 3: The conditions of Rule 1, Rule 3 and Rule 4 get re-evaluated because they depend on the value of the changed attribute State. The condition of Rule 3 only is met (State is ‘NEW’ and driver age is 30) so its action is placed on the agenda. The agenda is reviewed as well – the rule of its action does not depend on the State attribute and so the action is left intact. So the agenda now has 2 actions.

Agenda:

  • Policy.Excess = 0            /*from Rule 2*/ 
  • Policy.Excess = 100          /*from Rule 3*/ 

Now we take the action from Rule 2 off the agenda.

Step 4: The action block from Rule 2 is taken off the agenda. After it is executed the value of the Excess attribute becomes 0. The agenda now only has action block from Rule 3.

Agenda:

  • Policy.Excess = 100         /*from Rule 3*/ 

Step 5: The conditions of Rule 2 and Rule 5 are re-evaluated because these rules depend on the changed attribute Excess. The conditions of neither rule are met (Excess is defined and is equal to 0). The existing action on the agenda is reviewed as well – its rule depends on the changed attribute, but its conditions still hold, so the action remains on the agenda.

Agenda:

  • Policy.Excess = 100          /*from Rule 3*/ 

Step 6: the only action block is taken off the agenda and is executed. The value of Excess is now 100.

Agenda: (empty)

Step 7: The conditions of Rule 2 and Rule 5 are re-evaluated because they depend on the value of the changed attribute Excess. The condition of Rule 5 only is met so its action is placed on the agenda.

Agenda:

  • Policy.State = 'CALCULATED'  /*from Rule 5*/ 

Further steps are equivalent to steps 8, 9 and 10 of the scenario 1. At the end of the algorithm we get the value of State=’CALCULATED’ and the value of Excess=100 again.

Interestingly, no matter in which order we take the actions off the agenda the end result is the same! This is only possible, though, if every single rule in the un-ordered collection of rules is self-contained (independent of how and when other rules are evaluated) and consistent (makes sense from the business point of view) and this is what business rules are supposed to be! This is worth stressing again – business rules must be self-contained and consistent!

  • Last modified: 2022/09/13 18:15