[rules-users] Creating objects in WHEN clause

David Boaz davidb at dbmotion.com
Tue Mar 3 04:42:57 EST 2009


Thanks Edson, 
Indeed my Quantity class implements Comparable, and the described problem
was mysteriously solved. So, the expression Person( height < $2m ) does
work. BTW, this feature is not documented in "drools-docs-expert.pdf".
David

Edson Tirelli-3 wrote:
> 
>    Just a comment:
> 
> "There is no operator overloading in java, so the "<" operator must act on
> a
> numerical primitive. (int, double, etc.) "
> 
>     This is not true. Comparison operators will work on any "comparable"
> type. So if Quantity implements Comparable interface, it must work.
> Otherwise it is a bug.
> 
>     Although, remember that Drools always makes type coercion from the
> right
> to the left value, so, for that to actually work:
> 
> Person( height < $2m )
> 
>     "height" must be comparable to $2m.
> 
>      []s
>      Edson
> 
> 
> 2009/2/26 Greg Barton <greg_barton at yahoo.com>
> 
>> 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
>>
> 
> 
> 
> -- 
>  Edson Tirelli
>  JBoss Drools Core Development
>  JBoss, a division of Red Hat @ www.jboss.com
> 
> _______________________________________________
> 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-tp22207616p22305377.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list