Frank
Frank

Reputation: 53

Drools Exists vs. "normal" pattern

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!

Upvotes: 5

Views: 4058

Answers (1)

kaskelotti
kaskelotti

Reputation: 4823

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.

Upvotes: 9

Related Questions