[rules-users] Problem passing objects to a method via the eval statement

Mark Proctor mproctor at codehaus.org
Fri Mar 7 06:45:27 EST 2008


The 'or' must be used for classes of the same type.

Mark
Joshua Undesser wrote:
> I am curious if anyone else has run into this problem.....It seems like there
> is something wrong in the Drools Logic....My issue can be very easily be
> recreated in about 5 minutes....
>
> >From eclipse I set up a new drools project.    Where DroolTest.java is
> defined I create 4 new classes,   ClassA, ClassB extends ClassA, ClassC
> extends ClassA, and ClassD.
>
> Each of these classes has a default constructor that does nothing, there are
> no attributes or anything in these classes just to make the example simple
> to reproduce.   Ie ClassA looks like this....
>
>
>
> package com.sample;
>
> public class ClassA {
> 	
> 	public ClassA() {
> 	}
> }
>
>
>
> Now within the DroolsTest.java source code at line 29 (ie right after the
> workingMemory.insert( message ) method call I added 4 more lines which
> creates and inserts two objects...
>
> ClassB classB = new ClassB();
> ClassD classD = new ClassD();
> workingMemory.insert( classB );
> workingMemory.insert( classD );
>
> and then the next line would be the             
> workingMemory.fireAllRules();   
> as normal.
>
>
> Then in the sample.drl file I add the following rule and functions
>
> 1 rule "Test"
> 2	 when
> 3
> 4		$classA : ClassA()
> 5		$classD : ClassD()
> 6	
> 7		(or
> 8			$classB : ClassB(eval($classB == $classA))
> 9			$classC : ClassC(eval($classC == $classA))
> 10		)	
> 11	
> 12		eval(retB($classA, $classD))	
> 13
> 14	then
> 15		System.out.println("\n\n<<FIRE RULE>>TEST");		
> 16 end		
> 17
> 18 function boolean retA(Object obj) {
> 19   System.out.println("Ret Val = " + obj);
> 20   return true;
> 21 }
> 22
> 23 function boolean retB(Object obj1, Object obj2) {
> 24	 System.out.println("Ret Val 1 = " + obj1);
> 25	 System.out.println("Ret Val 2 = " + obj2);
> 26	 return true;
> 27 }
>
>
>
> So what I am doing is very quick and very simple to reproduce.   What is
> happening is that when i run the program I am getting a class cast exception
> which is very very strange.
>
>
> At first I thought it was tied only to the OR statement because if I break
> the rule into two rules (one for each of the or conditions) it works fine.
>
> But after playing with it a little further I get a lot of other strange
> class cast exceptions.
>
> For instance if I replace line 12 (eval(retB($classA, $classD))) with
> eval(retA($classA))  it works fine, but if I replace it with
> eval(retA($classD)) I get another class cast exception.    Another strainge
> issue....if switch lines 4 and 5 and keep line 12 as eval(retA($classD))
> that previously failed, it now passes, but if I change line 12 back to
> eval(retA($classA)) which previously passed it now fails.....
>
> There are a few other combinations that produce errors as well.  Just seems
> to be a fundamental flaw in the logic and I'm curious if anyone else has
> seen this?
>
> Thanks!
>
> Joshua
>
> P.S.
>
> Also I've noticed an issue with the manual example 6.36 
>
>
> This does not seem to work for me
> 	pensioner : (or Person( sex == "f", age > 60 ) 
>                     Person( sex == "m", age > 65 ) )
>
>
> But this does
> (or pensioner : Person( sex == "f", age > 60 ) 
>     pensioner : Person( sex == "m", age > 65 ) )
>
>
>
>
> Although both formats are stated as working in the manual.
>
>
>
>
>
>
>   




More information about the rules-users mailing list