[rules-users] Initialize Global

Waruzjan Shahbazian wshahbazian at xiam.nl
Tue Oct 30 06:15:02 EDT 2007


Oke I get it. I am using the globals just for temporary immutable 
values, first to be set by a rule and then to be used by the other rules.

I just have another question, how can I declare an array as a global?

When I try

    global int[] type;
    global String[] test;

and then in the RHS
    $type = new int[4];

Eclipse complains:

    Multiple markers at this line
        - Syntax error on token "}", delete this token
        - Syntax error on token "[", delete this token
        - Syntax error on token "}", delete this token

and i get errrors during the compilation too.

Ofcourse I can make an object with an String and/or int array in it and 
use that, but is this the expected behavior?

Thanks,

Waruzjan

Anstis, Michael (M.) schreef:
> 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
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>   




More information about the rules-users mailing list