Monday, August 3, 2009

eval in Drool

Sometimes, after looking at all of those expressions, you still can't find one that fits. eval allows any expression or formula that returns a Boolean value (that is, a true or false answer) to be used in evaluating a rule. For example, if we want to get the names of customers that are longer than 10 letters, we could use the following:

rule
when
$s : Sales()
eval ($s.getName().length >10)
then



eval is also useful when calling functions. For example, if we had a function, 'name too long', which returned a Boolean, we could call it using eval(nameTooLongFunction()).


eval is very convenient as it allows us to include pretty much any condition in a rule. However, it's considerably slower. With other conditions, Drools can cache (remember) the results because it can figure out when these results need to change. With eval, Drools doesn't have this visibility. Therefore, all eval expressions need to be rechecked every time the rule is true. If you have to use eval, it's best to add it as the last condition in a rule—meaning, it will be called less often. If any of the previous conditions return 'false', then Drools shortcuts, because there is no need to check any of the remaining conditions.

No comments:

Post a Comment