Here's the drools fibonacci example, which is similar to what you want. (Initializing
like objects based on ones "earlier" in a sequence.)
From: Greg Barton <greg_barton(a)yahoo.com>
Subject: Re: [rules-users] Strategy for initializing objects
To: "Rules Users List" <rules-users(a)lists.jboss.org>
Date: Saturday, August 8, 2009, 12:27 PM
Yes, unless you want the rules to react to the object in
which you're accumulating the value. Globals should
not be used in rule conditions if they're mutable. (The rete
network cannot be informed when they change.)
--- On Sat, 8/8/09, sonia <robotnic+drools(a)gmail.com>
wrote:
> From: sonia <robotnic+drools(a)gmail.com>
> Subject: Re: [rules-users] Strategy for initializing
objects
> To: "Rules Users List" <rules-users(a)lists.jboss.org>
> Date: Saturday, August 8, 2009, 12:16 PM
> Thank you.
>
> Accumulating increasingly on every step seems better
then
> doing
> 'accumulate' on all objects, all the time.
> Logically, it is the same as what I wanted - to
accumulate
> the total
> size somewhere. Implementation-wise, the difference is
the
> additional
> fact object for accumulation.
>
> Should I avoid using static or any non-fact external
data
> as part of
> my calculations?
> Isnt it just like using globals?
>
> 2009/8/8 Wolfgang Laun <wolfgang.laun(a)gmail.com>:
> > As an alternative, consider asserting a single
Fact
> Shelf with field x
> > initially 0. Then you could do
> >
> > rule "Another box on the shelf"
> > when
> > b: Box( x == -1, $size : size )
> > s: Shelf( $x : x )
> > then
> > b.setX( $x ); update( b );
> > s.setX( $x + $size ); update( s );
> > end
> >
> > -W
> >
> >
> > 2009/8/8 Greg Barton <greg_barton(a)yahoo.com>
> >>
> >> You answer your own question. Use
accumulate:
> >>
> >> rule "Hello World"
> >> when
> >> b : Box( x == -1 )
> >> newX : Integer() from
> accumulate( box: Box( x != -1 ),
> >>
> init( int i = 0; ),
> >>
> action( i += box.size; ),
> >>
> reverse( i -= box.size; ),
> >>
> result( i ) )
> >> then
> >>
System.out.println("Sum: "
> + newX);
> >> b.setX(newX);
> >>
System.out.println(b);
> >> update(b);
> >> end
> >>
> >> See attached project.
> >>
> >> --- On Sat, 8/8/09, sonia <robotnic+drools(a)gmail.com>
> wrote:
> >>
> >> > From: sonia <robotnic+drools(a)gmail.com>
> >> > Subject: Re: [rules-users] Strategy for
> initializing objects
> >> > To: "Rules Users List" <rules-users(a)lists.jboss.org>
> >> > Date: Saturday, August 8, 2009, 2:36 AM
> >> > In general: I want to set a property
> >> > in the Box objects, depending on
> >> > all previously set objects..
> >> >
> >> > My goal is to place the boxes one after
> another, for this I
> >> > keep a
> >> > variable in Box object.
> >> > It seems like the easiest way to
accumulate
> the sizes of
> >> > boxes that
> >> > are already placed.
> >> >
> >> > In the 'then' clause, I set the box
location,
> and update
> >> > the X by
> >> > adding the size of the currently placed
box.
> >> >
> >> > 2009/8/8 Wolfgang Laun <wolfgang.laun(a)gmail.com>:
> >> > > Your goal and the presented code
aren't
> clear to me.
> >> > The consequence (after
> >> > > then) contains calls to
static/class
> methods
> >> > (Box.setX()) which isn't going
> >> > > to change anything in your fact
object.
> The call
> >> > b.setX() might change your
> >> > > object, but this depends on what
the
> class method
> >> > Box.getX() returns.
> >> > >
> >> > > Assuming Drools 5, a typical
consequence
> would look
> >> > like
> >> > > b: Box(...)
> >> > > then
> >> > > modify( b ){
> >> > > setX( ... )
> >> > > }
> >> > > end
> >> > >
> >> > > -W
> >> > >
> >> > > On Sat, Aug 8, 2009 at 1:48 AM,
sonia
> <robotnic+drools(a)gmail.com>
> >> > wrote:
> >> > >>
> >> > >> Hello
> >> > >>
> >> > >> I want to achieve this:
> >> > >> box1.setX(0)
> >> > >> box2.setX(box1.size());
> >> > >> box2.setX(box1.size +
box2.size)
> >> > >> ...
> >> > >>
> >> > >> I want to do it with rules,
and
> would like to know
> >> > what's the best
> >> > >> method of initializing (or any
other
> operation
> >> > with varying data) a
> >> > >> group of objects
> >> > >>
> >> > >> rule "init box"
> >> > >> when
> >> > >> b : Box( x == -1)
> >> > >> then
> >> > >> b.setX(Box.getX());
> >> > >> Box.setX( Box.getX() +
b.size);
> >> > >> end
> >> > >>
> >> > >> This does not work.
> >> > >> A single box object is matched
> several times
> >> > instead of different box
> >> > >> objects.
> >> > >> What is happening? how come a
single
> box object is
> >> > matched several
> >> > >> times? Why doesnt drools
choose
> other instances of
> >> > box?
> >> > >>
> >> > >> What do I need to do to
initialize
> all boxes, once
> >> > for every box?
> >> > >>
> >> > >> Thank you.
> >> > >>
> >> > >> --
> >> > >> robosonia
> >> > >>
> _______________________________________________
> >> > >> rules-users mailing list
> >> > >> rules-users(a)lists.jboss.org
> >> > >>
https://lists.jboss.org/mailman/listinfo/rules-users
> >> > >
> >> > >
> >> > >
> _______________________________________________
> >> > > rules-users mailing list
> >> > > rules-users(a)lists.jboss.org
> >> > >
https://lists.jboss.org/mailman/listinfo/rules-users
> >> > >
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > robosonia
> >> >
> >> >
> _______________________________________________
> >> > rules-users mailing list
> >> > rules-users(a)lists.jboss.org
> >> >
https://lists.jboss.org/mailman/listinfo/rules-users
> >> >
> >>
> >>
> >>
> >>
_______________________________________________
> >> rules-users mailing list
> >> rules-users(a)lists.jboss.org
> >>
https://lists.jboss.org/mailman/listinfo/rules-users
> >>
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users(a)lists.jboss.org
> >
https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
>
>
> --
> robosonia
>
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users
>
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users