I'm learning drools and trying to understand best practices. Is there any practical difference between:
when
$event : Event(Helper.notNull(foo),...)
And
when
$event : Event(foo != null,...)
assuming that Helper.notNull is a static method in java that simply checks if object is not null.
I've tested this on two identical rules and one event that matches all conditions and helper method was called just once so it seems like drools was able to cache value. (my worst expectation was that it will eagerly evaluate it for every rule)
I feel that there might be some specifics with using helper methods versus native drools operations.
If you generalize my question then it will sound like: "Is it safe to call helpers that return boolean values in when clause?" (for example if I would use Objects.equals(a, b) instead of writing a == b)
Drools (in 6.x) evaluates Boolean expressions. Whether there is a "practical difference" between a simple comparison operator and a method implementing the same: both will evaluate to the same result, and the simple operator avoids the overhead of the call.
What you call "caching" is the result of Drools combining identical constraints as far as possible, but there's no guarantee that all such expressions can be cached all of the time.
With simple comparisons there is no gain in writing methods evaluating the same. Drools calls equals
for ==
and !=
whenever necessary.