[rules-users] Simple Global Error !!

Esteban Aliverti esteban.aliverti at gmail.com
Mon Jul 16 08:27:17 EDT 2012


No matter the type of a variable or whether it is inmutable or not, if you
reassign a variable inside a method you are not going to see that change
outside the method. Each method in java receives a copy of the variable
(pointing to the same object of course) we are passing. Changing the object
reference of the variable inside a method is not going to affect the
original variable.

@Abhinay, the truth is that Drools transforms the RHS of a rule is into a
java class. And, just like Wolfgang says, globals are stored in some kind
of Map, so you must use setGlobal() if you want to change the value of a
global in drools.

Best Regards,

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Esteban Aliverti
- Blog @ http://ilesteban.wordpress.com


On Mon, Jul 16, 2012 at 2:08 PM, Wolfgang Laun <wolfgang.laun at gmail.com>wrote:

> It may not be quite so simple as that - using int to illustrate a
> problem occuring with Integer is misleading.
>
> An Integer is an immutable object.
>    Integer a = Integer.valueOf( 42 );
>    a += 1
> You'll certainly still have an Integer object available via 'a', but
> it's *not* the one containing 42 any more - that's already in the
> garbage.
>
> The situation is aggravated by the fact that a 'global' is stored in a
> sort of container and this container is not directly accessible for
> effecting a change, only via setGlobal().
>
> -W
>
> On 16/07/2012, Esteban Aliverti <esteban.aliverti at gmail.com> wrote:
> > Let's say you have this method in a java class:
> >
> > public void addOne(int number){
> >    number = number +1;
> > }
> >
> >
> > And let's say you want to invoke that method from another method in the
> > same (or a different. It doesn't matter) object:
> >
> > int number = 0;
> > this.addOne(number);
> > System.out.println("Number= "+number);
> >
> > What is the output of the code above? Figure out that and you will figure
> > out what is happening with your global.
> >
> > Best Regards,
> >
> > XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
> >
> > Esteban Aliverti
> > - Blog @ http://ilesteban.wordpress.com
> >
> >
> > On Mon, Jul 16, 2012 at 11:53 AM, abhinay_agarwal <
> > abhinay_agarwal at infosys.com> wrote:
> >
> >> global java.lang.Integer count
> >>
> >>
> >> rule "rule1"
> >> agenda-group "1"
> >> when
> >>        //smething
> >> then
> >>       count=0;
> >>       System.out.println(count); // output is 0
> >>       count = count +1;
> >>       System.out.println(count); // output is 1
> >>       drools.setFocus("2");
> >> end
> >>
> >>
> >> rule "rule2"
> >> agenda-group "2"
> >> when
> >>        //smething
> >> then
> >>       System.out.println(count); // output is 0
> >> end
> >>
> >> in the rule2, the value of the global variable "count" must be 1, why m
> i
> >> getting it as zero ?
> >>
> >> --
> >> View this message in context:
> >> http://drools.46999.n3.nabble.com/Simple-Global-Error-tp4018697.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
> >>
> >
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120716/6c54eebd/attachment-0001.html 


More information about the rules-users mailing list