[rules-users] declare variable

Scott Reed sreed at spamcop.net
Mon May 14 16:03:57 EDT 2007


fakhfakh ismail's message received 5/14/2007 3:48 PM:
> Hello,
> first sorry for my bad english.
> I want to declare a variable of type List
> 
> rule "userrole"
>  
> when
>   $user1: User (userid1: userId)
>   $user2: User (userId == userid1)
>   then
>      List L = new ArrayList();
>         if (L.contains($user1.getname())== false)
>         {
>             L.add($user1.getname());
>             System.out.println("hello");
>         }
>     end
> 
> the problem is everytime the List don't save all element every execution 
> the variable  list is intialised as empty is there other place to 
> declare List?
> (I try after when but error of parsing)

You need to declare the List in your Java code, set it as a Drools global, and declare it in the DRL 
file:

In yourjava.java:
-----------
workingMemory.setGlobal("L", new java.util.List());
-----------

in yourdrl.drl:
-----------
global java.util.List L;

rule "userrole"

when
   $user1: User (userid1: userId)
   $user2: User (userId == userid1)
   then
         if (L.contains($user1.getname())== false)
         {
             L.add($user1.getname());
             System.out.println("hello");
         }
end
-----------

Wouldn't it be great to have a "static" section in the DRL syntax for temporaries like this?



More information about the rules-users mailing list