[rules-users] Creating objects in WHEN clause

David Boaz davidb at dbmotion.com
Thu Feb 26 11:16:55 EST 2009


Thanks for your excellent remarks and information!
I must pass through the Quantity.compareTo() method because I have to do
quantity conversion checks (e.g., 2m > 180 cm).
I understood the drawback of using FROM instead of using working memory
objects. 
My goal is to make the rules easy to read and maintain. I want to avoid
defining "setup-rules" for inserting objects to WM, and other "real" rules.
My Quantity objects are immutable value objects. just like the String
literal "abc", or a Date. My rule should behave exactly like Person(name ==
"abc")

Can I instruct Drools not to check for changes on that objects?
Im sure that Im not the first facing this issue.

Thanks again, David


Greg Barton wrote:
> 
> The problem is the "Person( height < $2m )" part.  There is no operator
> overloading in java, so the "<" operator must act on a numerical
> primitive. (int, double, etc.)  $2m is a Quantity.  As for why this
> doesn't error out, I'm not sure, but it certainly won't execute. (Unless
> mvel has some capabilities I'm not aware of.)
> 
> Try 
> 
> Person( height < $2m.height )
> 
> ...as long as Person.height and Quantity.height are both numerical types.
> 
> If you want to specifically call compareTo() you should use an eval block:
> 
> WHEN 
>   $meter:UoM( ) from UoMs.get("length","m")
>   $2m:Quantity() from Quantities.get(2,$meter)
>   $p: Person()
>   eval($p.getHeight().compareTo($2m))
> 
> However, even if this works, I hope you're not going to be putting too
> much data through rules like this.  The way it's currently constructed it
> completely circumvents all of drools' indexing ability. :)  The power of
> rules comes from tracking the changes in a set of objects, and firing
> rules based on only the changes observed.  (That set is the working
> memory.) The "from" keyword allows you to have rules that draw objects
> from outside working memory, but you pay for that convenience: the cost is
> not being able to track changes.  As a result, objects gathered using
> "from" must be reconsidered even if they haven't changed (i.e. every time
> the condition is encountered) because drools has no way of knowing if
> they've changed or not.
> 
> So, after this long winded spiel, here's my suggestion: get the rule(s) to
> work using "from" but also try inserting the Quantity object in working
> memory.  If you're processing enough data with the rules you will notice a
> difference.
> 
> --- On Thu, 2/26/09, David Boaz <davidb at dbmotion.com> wrote:
> 
>> From: David Boaz <davidb at dbmotion.com>
>> Subject: Re: [rules-users] Creating objects in WHEN clause
>> To: rules-users at lists.jboss.org
>> Date: Thursday, February 26, 2009, 9:13 AM
>> Thanks for your all help.
>> with your help, my rule looks now as:
>> WHEN $meter:UoM( ) from
>> UoMs.get("length","m")
>> 	 $2m:Quantity() from Quantities.get(2,$meter)
>> 	Person( height < $2m )
>> 
>> My Quantity class implements Comparable interface. When
>> running the rule on
>> a good dataset (where I expect the rule to fire), the rule
>> fails with no
>> error. in addition, the compareTo(Object o) method is not
>> called (the
>> debugger doesn't stop in this method).
>> 
>> Can you please help?
>> Thanks, David
> 
> 
>       
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
> 
> 

-- 
View this message in context: http://www.nabble.com/Creating-objects-in-WHEN-clause-tp22207616p22227554.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list