[rules-users] Initialize Global

Anstis, Michael (M.) manstis1 at ford.com
Tue Oct 30 05:47:41 EDT 2007


This is in essence already catered for - you need a LHS fact.

A global is just a holder for some (immutable) set of values\functions to
make life easier for common (immutable) values\functions your rule may need.

If you want to start making the global mutable for RETE to "use" the changes
they become facts.

>> rule "Start"
>>     salience 101
>>     when
>>     then
>>         MyGlobalObject myGlobal = new MyGlobalObject();
>>         ArrayList objectList = new ArrayList() ;
>>         objectList.add("1");
>>         objectList.add("2");   
>>         myGlobal.setList(objectList);
>>         System.out.println("Start objectList:" + objectList); 
>>         insert(myGlobal);
>> end
>>
>> rule "end"
>>     salience 97
>>     when
>>         $g : MyGlobalObject( list != null )
>>     then
>>         System.out.println("End objectList: " + $g.getList());
>> end


-----Original Message-----
From: rules-users-bounces at lists.jboss.org
[mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Waruzjan
Shahbazian
Sent: 30 October 2007 09:31
To: Rules Users List
Subject: Re: [rules-users] Initialize Global

Thanks, that sounds logical to me. I guess I just have to "trust" that 
the rule where the array gets initialised activates earlier than the 
rule where I need the array. That shouldn't be a problem since I use 
salience and to avoid nullpoint exceptions I can use the if(objectList 
!= null)  in the "Then" part of the rule where I need the array.

Maybe an idea for the drools developers to create an "update" function 
for the globals so we can eval on them too...in the future.

vdelbart schreef:
> It's normal.
>
> Globally, the rule engine works in two steps :
>  - first : the activation (the "when" statement)
>  - second : the execution (the "then" statement) with salience, ruleflow
...
>
> In your test, you have in the first step objectList = null and in the
second
> step objectList = [1, 2].
>
> If you want to re-activate your rule, you have to do an
update/insert/remove
> action... But you can't do that with globals. So you need to use the WM
> facts.
>
>
>
> Waruzjan Shahbazian-2 wrote:
>   
>> I have the same "problem", but eval(objectList == null) doesn't work. If 
>> I don't execute "drools.getWorkingMemory().setGlobal()" it "works fine", 
>> as if the rule activates and the object is null.
>>
>> global List objectList;
>>
>> rule "Start"
>>     salience 101
>>     when
>>         #conditions
>>     then
>>         objectList = new ArrayList() ;
>>         objectList.add("1");
>>         objectList.add("2");   
>>         System.out.println("Start objectList:"+objectList); 
>>         //drools.getWorkingMemory().setGlobal("objectList", objectList);
>> end
>>
>> rule "end"
>>     salience 97
>>     when
>>         eval (objectList == null)
>>     then
>>         System.out.println("End objectList: "+objectList);
>> end
>>        
>> gives:
>>    
>>     Start objectList:[1, 2]
>>     End objectList: null
>>
>> Next I uncomment the "drools.getWorkingMemory().setGlobal("objectList", 
>> objectList);" and run the rule again an get:
>>
>>     Start objectList:[1, 2]
>>     End objectList: [1, 2]
>>
>> so the objectList isn't null, but the rule still activates...
>>
>> Kris Verlaenen schreef:
>>     
>>>> That initialized my global but the rule still runs every execution.  
>>>> Can I
>>>> disable the rule after the first execution?
>>>>         
>>> What do you mean by "every execution".  A rule should only be executed 
>>> once, unless it gets reactivated (which should not be the case in this 
>>> situation).
>>>
>>>       
>>>> I would like to use (if (objectList==null)).  My list is not 
>>>> immutable. Can
>>>> I make a similar rule for the LHS?
>>>>         
>>> You can test whether the global is null using eval( objectList == null 
>>> )  in the LHS of the rule.
>>>
>>> Kris
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>       
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>     
>
>   

_______________________________________________
rules-users mailing list
rules-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 4159 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20071030/9b306a03/attachment.bin 


More information about the rules-users mailing list