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 <[email protected]> wrote:

> From: sonia <[email protected]>
> Subject: Re: [rules-users] Strategy for initializing objects
> To: "Rules Users List" <[email protected]>
> 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 <[email protected]>:
> > 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 <[email protected]>
> 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
> >> [email protected]
> >> https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > [email protected]
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
> 
> 
> 
> -- 
> robosonia
> 
> _______________________________________________
> rules-users mailing list
> [email protected]
> https://lists.jboss.org/mailman/listinfo/rules-users
>


      

Attachment: DroolsBox.tar.gz
Description: GNU Zip compressed data

_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to