How does update function works in drools? Does it cause the same rules fire again automatically?
I think you need to read the manual: http://docs.jboss.org/drools/release/5.4.0.Final/drools-expert-docs/html_single/
Using update
makes the rules engine aware that a fact has been modified. Therefore rules which depend upon that fact must be re-evaluated. This does have the effect that a rule may fire in an endless cycle. For instance, given the following DRL, you will see that the "Infinite loop" rule will activate continuously:
declare AreWeThereYet
answer : boolean
end
rule "Are we there yet?"
when
not AreWeThereYet()
then
insert(new AreWeThereYet());
end
rule "Infinite loop"
no-loop
when
$question: AreWeThereYet()
then
$question.setAnswer(false);
update($question);
end
This is because the rules engine has been instructed by update($question)
, that $question
has changed and that it needs to re-evaluate it.
There are ways of preventing this though. Just put no-loop
on the line between the rule name and when
to prevent rules from re-activating as a result of their own consequences. Another rule attribute which can control this is lock-on-active
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With