[rules-users] How to use global variables

Wolfgang Laun wolfgang.laun at gmail.com
Tue Mar 19 02:22:30 EDT 2013


You realize that each Integer is a unique, immutable object. Java
softens the consequence by auto(un)boxing so that you can use Integer
objects just like int variables.

But Java isn't aware of some Integer being stored as a DRL global.
Therefore, you'll have to call the KnowledgeRuntime method setGlobal(
String, Object ) to really change a global object.

Or wrap the counter into an object and count using get and set.

-W

On 19/03/2013, goforcrazy <goforcrazy at gmail.com> wrote:
> Hi All,
>
> My rule file is as shown below:
>
> global Integer count;
>
> rule "Rule1"
>
> when   eval(1==1)
> then
>        for(int a = 0; a<10; a++)
>  		{
>  			System.out.println("1 COUNT: " + count);
>  			count++; 			
> 		}
>
> rule "Rule 2"
> when   eval(1==1)
> then
>        for(int a = 0; a<10; a++)
>  		{
>  			System.out.println("2 COUNT: " + count);
>  			count++; 			
> 		}
>
>
> As shown above I have created a global variable count in my drools file. In
> my Java file I have included ksession.setGlobal("count", 1); to start the
> count from 1 for first rule but after Rule 1 gets executed and before the
> beginning of rule 2 I find that count value is again taken as 1. It does
> not
> remember the last count value at the end of the loop.
>
> I understand I need to use the workingMemory function to update the result
> of count but I am not sure on how and where to update the same.
>
> Any help regarding this will be greatly appreciated.
>
> Thanks and Regards,
> Malathi
>
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/How-to-use-global-variables-tp4022860.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> 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