[rules-users] Creating objects in WHEN clause

Greg Barton greg_barton at yahoo.com
Thu Feb 26 10:54:54 EST 2009


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


      



More information about the rules-users mailing list