[rules-users] Strategy for initializing objects

Greg Barton greg_barton at yahoo.com
Sat Aug 8 08:20:40 EDT 2009


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 at gmail.com> wrote:

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


      
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DroolsBox.tar.gz
Type: application/x-gzip
Size: 4572 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20090808/51da05b9/attachment.gz 


More information about the rules-users mailing list