<br>&nbsp;&nbsp;&nbsp; Your reasoning on how to solve the problem is correct. I don&#39;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>&nbsp;&nbsp;&nbsp; // 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>&nbsp;&nbsp;&nbsp; $a: Agent( $x1:x, $y1:y, $r:range)<br>&nbsp;&nbsp;&nbsp; $t1: Tile( $x2: x, $y2 : y, eval( distance( $x1, $y1, $x2, $y2 ) &lt;= $r ) )<br>&nbsp;&nbsp;&nbsp; not Tile( eval( distance( $x1, $y1, x, y ) &lt; 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>&nbsp;&nbsp;  <br>2.b. If you want best performance, try a more relational approach with 2 rules:
<br><br>rule &quot;calculate distance&quot;<br>when<br>&nbsp;&nbsp;&nbsp; $a: Agent( $x1 : x, $y1 : y, $r : range )<br>&nbsp;&nbsp;&nbsp; $t: Tile( eval( distance( $x1, $y1, x, y ) &lt; $r ) )<br>then<br>&nbsp;&nbsp;&nbsp; assertLogical( new TileInRange( $a, $t, distance( $x1, $y1, x, y ) ) );
<br>end<br><br>rule &quot;find the closest&quot;<br>when<br>&nbsp;&nbsp;&nbsp; $a: Agent()<br>&nbsp;&nbsp;&nbsp; $tr TileInRange( agent == $a, $tile : tile, $dist : distance )<br>&nbsp;&nbsp;&nbsp; not TileInRange( agent == $a, distance &lt; $dist )<br>&nbsp;&nbsp;&nbsp; $t : Tile( this == $tile ) // this last pattern may be removed if you don&#39;t need to constrain any other attribute
<br>then<br>&nbsp;&nbsp;&nbsp; // do something<br>end<br><br>&nbsp;&nbsp; Hope it helps<br><br>&nbsp;&nbsp;&nbsp; Edson<br><br><div><span class="gmail_quote">2007/12/6, velven &lt;<a href="mailto:velven83@hotmail.com">velven83@hotmail.com</a>&gt;:</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&#39;m supposed to write a rule which will cause the<br>agents to choose the tile in the agent&#39;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(&lt;tile&#39;s x to be between (x1+r) and (x1-r)&gt; &amp;&amp; &lt;tile&#39;s y to be<br>between (y1+r and y1-r)&gt;)<br><br>tile2: not Tile( &lt; which is closer than the tile1 to agent&gt; )
<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&#39;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&nbsp;&nbsp;&nbsp;&nbsp; x (&gt; 30 &amp;&amp; &lt; 40) is ok<br>but<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x (&gt;
<br>($x-$r) &amp;&amp; &lt; ($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>&nbsp;&nbsp;Edson Tirelli<br>&nbsp;&nbsp;JBoss Drools Core Development<br>&nbsp;&nbsp;Office: +55 11 3529-6000<br>&nbsp;&nbsp;Mobile: +55 11 9287-5646<br>&nbsp;&nbsp;JBoss, a division of Red Hat @ <a href="http://www.jboss.com">
www.jboss.com</a>