Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does update function works in drools?

Tags:

drools

How does update function works in drools? Does it cause the same rules fire again automatically?

like image 807
Nicky Jaidev Avatar asked Dec 06 '22 09:12

Nicky Jaidev


1 Answers

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.

like image 142
Steve Avatar answered Jan 27 '23 21:01

Steve