As an alternative, consider asserting a single Fact Shelf with field x initially 0. Then you could do<br><br>rule "Another box on the shelf"<br>when<br> b: Box( x == -1, $size : size )<br> s: Shelf( $x : x )<br>
then<br> b.setX( $x ); update( b );<br> s.setX( $x + $size ); update( s );<br>end<br><br>-W<br><br><br><div class="gmail_quote">2009/8/8 Greg Barton <span dir="ltr"><<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You answer your own question. Use accumulate:<br>
<br>
rule "Hello World"<br>
<div class="im"> when<br>
b : Box( x == -1 )<br>
</div> newX : Integer() from accumulate( box: Box( x != -1 ),<br>
init( int i = 0; ),<br>
action( i += box.size; ),<br>
reverse( i -= box.size; ),<br>
result( i ) )<br>
then<br>
System.out.println("Sum: " + newX);<br>
b.setX(newX);<br>
System.out.println(b);<br>
update(b);<br>
end<br>
<br>
See attached project.<br>
<br>
--- On Sat, 8/8/09, sonia <<a href="mailto:robotnic%2Bdrools@gmail.com">robotnic+drools@gmail.com</a>> wrote:<br>
<br>
> From: sonia <<a href="mailto:robotnic%2Bdrools@gmail.com">robotnic+drools@gmail.com</a>><br>
> Subject: Re: [rules-users] Strategy for initializing objects<br>
> To: "Rules Users List" <<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>><br>
> Date: Saturday, August 8, 2009, 2:36 AM<br>
<div><div></div><div class="h5">> In general: I want to set a property<br>
> in the Box objects, depending on<br>
> all previously set objects..<br>
><br>
> My goal is to place the boxes one after another, for this I<br>
> keep a<br>
> variable in Box object.<br>
> It seems like the easiest way to accumulate the sizes of<br>
> boxes that<br>
> are already placed.<br>
><br>
> In the 'then' clause, I set the box location, and update<br>
> the X by<br>
> adding the size of the currently placed box.<br>
><br>
> 2009/8/8 Wolfgang Laun <<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>>:<br>
> > Your goal and the presented code aren't clear to me.<br>
> The consequence (after<br>
> > then) contains calls to static/class methods<br>
> (Box.setX()) which isn't going<br>
> > to change anything in your fact object. The call<br>
> b.setX() might change your<br>
> > object, but this depends on what the class method<br>
> Box.getX() returns.<br>
> ><br>
> > Assuming Drools 5, a typical consequence would look<br>
> like<br>
> > b: Box(...)<br>
> > then<br>
> > modify( b ){<br>
> > setX( ... )<br>
> > }<br>
> > end<br>
> ><br>
> > -W<br>
> ><br>
> > On Sat, Aug 8, 2009 at 1:48 AM, sonia <<a href="mailto:robotnic%2Bdrools@gmail.com">robotnic+drools@gmail.com</a>><br>
> wrote:<br>
> >><br>
> >> Hello<br>
> >><br>
> >> I want to achieve this:<br>
> >> box1.setX(0)<br>
> >> box2.setX(box1.size());<br>
> >> box2.setX(box1.size + box2.size)<br>
> >> ...<br>
> >><br>
> >> I want to do it with rules, and would like to know<br>
> what's the best<br>
> >> method of initializing (or any other operation<br>
> with varying data) a<br>
> >> group of objects<br>
> >><br>
> >> rule "init box"<br>
> >> when<br>
> >> b : Box( x == -1)<br>
> >> then<br>
> >> b.setX(Box.getX());<br>
> >> Box.setX( Box.getX() + b.size);<br>
> >> end<br>
> >><br>
> >> This does not work.<br>
> >> A single box object is matched several times<br>
> instead of different box<br>
> >> objects.<br>
> >> What is happening? how come a single box object is<br>
> matched several<br>
> >> times? Why doesnt drools choose other instances of<br>
> box?<br>
> >><br>
> >> What do I need to do to initialize all boxes, once<br>
> for every box?<br>
> >><br>
> >> Thank you.<br>
> >><br>
> >> --<br>
> >> robosonia<br>
> >> _______________________________________________<br>
> >> rules-users mailing list<br>
> >> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> >> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > rules-users mailing list<br>
> > <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> > <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
> ><br>
> ><br>
><br>
><br>
><br>
> --<br>
> robosonia<br>
><br>
> _______________________________________________<br>
> rules-users mailing list<br>
> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
><br>
<br>
<br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>