Good $time_of_day!
I'm a Drools newbie trying to use it in production. So I want to share
some of my frustration and some of my solutions to this frustration :)
First, let's start with insertLogical - it's useless. It can easily
mislead user into thinking that changed objects are retracted. Of
course, other users bumped into it before me:
http://drools-java-rules-engine.46999.n3.nabble.com/insertLogical-and-mod...
, but the proposed resolution is ugly.
So I wrote a wrapper around the 'insertLogical' function which
introduces the notion of 'fact key', so only one fact with the given
key can exist in the working memory at a time. To borrow the example
in the referenced mail thread:
============
rule "fact = 1"
when
$f : UserFact(fact1 == 1);
then
insertLogical(new CreatedFact($f.getFact2()));
end
============
Becomes:
============
global ConsequenceHelper ch; # Code of the ConsequenceHelper is attached
rule "fact = 1" language "mvel"
when
$f : UserFact(fact1 == 1);
then
ch.insertGuarded(drools, new CreatedFact($f.getFact2()),
{$f.getFact2()}); # Here {$f.getFact2()} is the CreatedFact's key.
end
============
So if this rule fires again, the stale object will be retracted and
the newly created object will be asserted. It would be nice to see
something like this integrated into the Drools core...
Next, I'd like to say something about Drools LHS language. Well, it's
VERY confusing. I spent two hours debugging internals of MVEL to
understand why this doesn't work:
============
rule "Double Shift OT"
dialect "mvel" agenda-group "OT Calculation"
when
$at : AcctTransaction()
exists(AcctTransaction(this!=$at, acct.plannedEndTimestamp
coincides[2h] $at.plannedStartTimestamp))
then
...
end
============
Turns out, I can't use these nice Drools Fusion predicates to for deep
properties. It would be nice, if it was at least mentioned somewhere
in the documentation. No workaround for this one, I had to write
global helper functions.
And the final problem (so far). Drools has this nice 'from' feature
which allows to use objects not in the working memory. However, it's
not possible to do this:
============
rule "Blah"
when
$l : Long() from new Long(42L)
then
...
end
============
I had to write a helper function which just returns its parameter to
work around this one.
And the final note - DRL is too cut&pasty. There's no _good_ way to
reuse predicates used in the LHS (there's rule inheritance, but it's
not always applicable) and actions in the RHS.