Controlling loops in rules
So far, we've seen some ways in which we can manage our rules to trigger new rule invocations. This will help us enormously in order to be able to split our rules into simple components that interact in the background through the data in the working memory. Powerful as it is, however, it can bring us a few extra complications along the line of the rules getting fired more times than we desire. Fortunately, Drools provides us with a set of elements to control rule execution from the very syntax where we define them.
The first and the most simple case where we can get into an infinite rule execution loop happens when a rule modifies the working memory in a way that it retriggers itself. Let's see an example of this problem in the following:
rule "Apply 10% discount on notepads" when $i: Item(name == "notepad", $sp: salePrice) then modify($i) { setSalePrice($sp * 0.9); } end
In this rule, our intention is to reduce the sale price of the notepads in our inventory...