[rules-users] Creating objects in WHEN clause

Greg Barton greg_barton at yahoo.com
Sun Mar 1 16:42:28 EST 2009


Oh, you dun thrown down the gauntlet now. :)

The following will not compile with java6:

public class ComparableFu {

	static class Foo implements Comparable<Foo> {

		private int foo;
		
		public Foo(int foo) {
			this.foo = foo;
		}
		
		public int compareTo(Foo other) {
			if(other.foo < foo) {
				return -1;
			} else if(other.foo == foo) {
				return 0;
			} else {
				return 1;
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println(new Foo(1) < new Foo(2)); //Baaaaad!
	}
}

Methinks the overloading of comparison operators for Comparables is an mvel feature.

--- On Sun, 3/1/09, Edson Tirelli <tirelli at post.com> wrote:

> From: Edson Tirelli <tirelli at post.com>
> Subject: Re: [rules-users] Creating objects in WHEN clause
> To: greg_barton at yahoo.com, "Rules Users List" <rules-users at lists.jboss.org>
> Date: Sunday, March 1, 2009, 12:46 PM
> 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


      



More information about the rules-users mailing list