Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drools Exists vs. "normal" pattern

Tags:

drools

I've a problem understanding why the existskeyword is necessary at all. I have the following rules: 1)

rule "normal"
  when
    Bus( seats > 20 )
  then
    System.out.println("There is a 20+ bus);
end

2)

rule "with exists"
  when
    exists Bus( seats > 20 )
  then
    System.out.println("There is a 20+ bus existing...);
end

How is the first rule's LHS different from the second?

Thanks!

like image 557
Frank Avatar asked Sep 05 '15 18:09

Frank


Video Answer


1 Answers

The documentation for exists states the following

The CE exists is first order logic's existential quantifier and checks for the existence of something in the Working Memory. Think of "exists" as meaning "there is at least one..". It is different from just having the pattern on its own, which is more like saying "for each one of...". If you use exists with a pattern, the rule will only activate at most once, regardless of how much data there is in working memory that matches the condition inside of the exists pattern. Since only the existence matters, no bindings will be established.

Thus, for your examples, the first rule is fired for each bus that has more than 20 seats, but the second rule is fired only once even if there would be number of buses with seats more than 20.

like image 142
kaskelotti Avatar answered Nov 02 '22 11:11

kaskelotti