Effect of Engine Control Functions on Business Rule Execution – Part2
In current series, we have already gone through rule execution algorithm and effect of “Assert” and “Update” on rule execution cycle. If you wish to recap, please go through these articles again @
Now, we are going to talk about rest of the engine control functions - Retract, RetractByType and Reassert.
Retract
Retract is used to remove facts from working memory of rule engine. Removing facts causes rule execution cycle to repeat again because some of the earlier supplied facts are removed and it might cause action agenda to change. During re-evaluation of action agenda, those rules are ignored whose predicates are based on retracted facts. Those actions are also removed / ignored which use retracted facts.
Let’s understand this with following example.
Consider a policy with following rules and facts –
Rule1 |
Rule2 |
Rule3 |
If Fact1.A = 2 Then Fact1.B=10 Fact2.D=2 |
If Fact2.C=1 Then Fact2.D=5 |
If Fact3.E>0 Then Fact2.C=1 Retract(Fact2) |
When policy is called for execution, following facts are supplied –
Fact1 |
Fact2 |
Fact3 |
A = 2 B |
C = 1 D |
E = 9 |
Rules execution loads following action agenda into working memory for execution –
Fact1.B=10
Fact2.D=2
Fact2.D=5
Fact2.C=1
Retract (Fact2)
Now when “Retract” function is called, Fact2 is removed from working memory of rule engine. Rule execution cycle repeats and action agenda is re-evaluated. During re-evaluation, Rule2 is completely ignored because it is based on Fact2. Similarly, Fact2.D=2, Fact2.C=1 and Retract (Fact2) actions are also ignored because they use Fact2.
After re-evaluation, Next execution cycle produces following action agenda –
Fact1.B=10
RetractByType
RetractByType is similar to Retract. Only difference is that it removes all facts of a given type.
Consider a policy using following .net objects as facts – A1, A2, B, C and A3.
Suppose A1, A2 and A3 are of the same type – Either they are object of the same class type or they inherit from same base class.
In such case, if a RetractByType function is called on any of the A1, A2 or A3, then during rule re-evaluation those rules and actions are ignored / excluded which use either of them.
RetractByType does blanket removal of given fact type from rule execution working memory.
Reassert
Reassert is combination of – Retract and Assert.
Reassert also causes repeat of rule execution cycle and re-evaluation of action agenda. It happens in two stages –
- First Facts are removed from working memory. It forces those rules and action to be excluded from agenda which use these facts.
- Then new facts are inserted again in working memory with updated states. As appropriate, Rules and actions involving new facts are added or removed from agenda.
Exclusive union of agenda produced at these two stages becomes final agenda for rule re-execution. These two stages are passed as part of same re-evaluation cycle.
I am not taking any example here because it is simple combination of Retract and Assert. And we have already covered these functions in previous sections with example.
Conclusion
I hope my entries about rule execution algorithm and engine control functions were of use and information. If you have any query, please feel free to send me. Thanks!