<br> Your reasoning on how to solve the problem is correct. I don't know about your implementation, but my suggestion is: <br><br>1. Create a function to calculate the distance between 2 points. I would prefer to create it as a static method in a helper class, but you can define it in the DRL if you prefer:
<br><br>function double distance( long originX, long originY, long actualX, long actualY ) {<br> // return the calculated distance<br>}<br><br>2.a. If you want to do it in a single rule, write something like this:<br><br>
when<br> $a: Agent( $x1:x, $y1:y, $r:range)<br> $t1: Tile( $x2: x, $y2 : y, eval( distance( $x1, $y1, $x2, $y2 ) <= $r ) )<br> not Tile( eval( distance( $x1, $y1, x, y ) < distance( $x1, $y1, $x2, $y2 ) ) )
<br>then<br>end <br><br>The downside of such approach is that it recalculates the distance many times and the performance would be poor.<br> <br>2.b. If you want best performance, try a more relational approach with 2 rules:
<br><br>rule "calculate distance"<br>when<br> $a: Agent( $x1 : x, $y1 : y, $r : range )<br> $t: Tile( eval( distance( $x1, $y1, x, y ) < $r ) )<br>then<br> assertLogical( new TileInRange( $a, $t, distance( $x1, $y1, x, y ) ) );
<br>end<br><br>rule "find the closest"<br>when<br> $a: Agent()<br> $tr TileInRange( agent == $a, $tile : tile, $dist : distance )<br> not TileInRange( agent == $a, distance < $dist )<br> $t : Tile( this == $tile ) // this last pattern may be removed if you don't need to constrain any other attribute
<br>then<br> // do something<br>end<br><br> Hope it helps<br><br> Edson<br><br><div><span class="gmail_quote">2007/12/6, velven <<a href="mailto:velven83@hotmail.com">velven83@hotmail.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>HI, i have a problem<br>using tileworld model, i'm supposed to write a rule which will cause the<br>agents to choose the tile in the agent's range which is closest to the<br>agent.<br><br>so a rough idea is<br>
<br>when<br><br>agent: Agent(x1:x, y1:y, r:range)<br><br>tile1: Tile(<tile's x to be between (x1+r) and (x1-r)> && <tile's y to be<br>between (y1+r and y1-r)>)<br><br>tile2: not Tile( < which is closer than the tile1 to agent> )
<br><br>desperately need help,<br>alternatives tried::<br>i tried to use a query to return a list of tiles which is within the agent's<br>range then compared which one is closer and other similar implementation,<br>but this too slow for my simulation...
<br><br>I need one single good rule or efficient way of implementing the above rule<br><br><br>then i tried to do multiple constraints too, say x (> 30 && < 40) is ok<br>but<br> x (>
<br>($x-$r) && < ($x+$r)) gives errors ,due to extra parenthesis? i dunno.. any<br>workarounds?<br><br>Thanks!<br><br><br>--<br>View this message in context: <a href="http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14166650">
http://www.nabble.com/How-to-write-a-rule-which-only-fires-for-a-closest-value--tf4947931.html#a14166650</a><br>Sent from the drools - user mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<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">https://lists.jboss.org/mailman/listinfo/rules-users
</a><br></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> Office: +55 11 3529-6000<br> Mobile: +55 11 9287-5646<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">
www.jboss.com</a>