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

Edson Tirelli tirelli at post.com
Fri Mar 7 11:34:22 EST 2008


   Joshua,

   The limitation is on binding variables on different fact types on
different logical branches and using them in the consequence. If you don't
use the bindings in the consequence, you are fine.

   This is a temporary limitation.

   []s
   Edson

2008/3/7, Joshua Undesser <jundesse at cdmtech.com>:
>
>
> Thank you Mark for the quick reply.   I am curious if this is only
> temporary
> or if this is the protocol that Drools is going to use for "or"
> statements?
> The reason I ask is that i come from a jess/clips background, where "or"
> can
> be used across multiple classes as shown in my example and it is very
> useful
> when you have two huge rules that are identical except for one LHS class
> pattern difference.
>
> Obviously the fix for now is to break up the rule into two rules, but I
> was
> curious if this was only a temporary restriction or if this is going to be
> the standing protocol?
>
> I'll also grab the new 4.0.5 branch as you mentioned and give that a
> whirl.
>
> Thanks again Mark!
>
> Joshua
>
>
>
>
>
>
> Mark Proctor wrote:
> >
> > 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.
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> > _______________________________________________
> > 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/Problem-passing-objects-to-a-method-via-the-eval-statement-tp15885523p15898856.html
>
> Sent from the drools - user mailing list archive at Nabble.com.
>
> _______________________________________________
>
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
  Edson Tirelli
  JBoss Drools Core Development
  Office: +55 11 3529-6000
  Mobile: +55 11 9287-5646
  JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080307/9e6d215a/attachment.html 


More information about the rules-users mailing list