Thanks to Greg for sending out a simplified example.<div><br></div><div>I&#39;ve one more query. Is there any operators to do calculation on fixed time window?</div><div>Here is the scenario.</div><div>My data will stream into the engine starting from second 0.</div>
<div><br></div><div>0,0,0,0,1,1,1,1,.......60,61.......120,121................180,181.....</div><div><br></div><div>If  I want to find average between 0 to 60 sec, that is the previous minute of 61 to 120, how can I achieve this? I feel sliding window will not help me.</div>
<div><br></div><div>Suggestions are welcomed.<br><br><div class="gmail_quote">2009/7/28 Greg Barton <span dir="ltr">&lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
See the attached project.  You can run it using maven with this command:<br>
<br>
mvn package exec:java -Dexec.mainClass=&quot;com.sample.DroolsTest&quot;<br>
<br>
There are three basic rules:<br>
<br>
MakeCollision: Detect that a collision has happened<br>
<br>
MatchCollision: Match vehicles to collisions<br>
<br>
IdentifyLargeCollision: Find collisions that are &quot;large&quot;<br>
<br>
rule &quot;MakeCollision&quot;<br>
        salience 10<br>
        when<br>
                s : Sighting( collision == null )<br>
                not Collision( direction == s.direction, position == s.position, lane == s.lane )<br>
        then<br>
                Collision c = new Collision( s.direction, s.position, s.lane );<br>
                c.getSightings().add( s );<br>
                s.setCollision( c );<br>
                System.out.println(&quot;Creating &quot; + c);<br>
                update( s );<br>
                insert( c );<br>
end<br>
<br>
rule &quot;MatchCollision&quot;<br>
        salience 10<br>
        when<br>
                s : Sighting( collision == null )<br>
                c : Collision( direction == s.direction, position == s.position, lane == s.lane )<br>
        then<br>
                c.getSightings().add( s );<br>
                s.setCollision( c );<br>
                System.out.println(&quot;Matching &quot; + s);<br>
                update( s );<br>
                update( c );<br>
end<br>
<br>
rule &quot;IdentifyLargeCollision&quot;<br>
        salience 5<br>
        when<br>
                num : Number ( intValue &gt; 1 )<br>
                        from accumulate (<br>
                                Collision( sightings.size &gt; 2 ),<br>
                                init( int count = 0; ),<br>
                                action( count++; ),<br>
                                reverse( count--; ),<br>
                                result( count )<br>
                        );<br>
        then<br>
                System.out.println(&quot;Found &quot; + num);<br>
end<br>
<br>
--- On Fri, 7/24/09, <a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a> &lt;<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>&gt; wrote:<br>
<br>
&gt; From: <a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a> &lt;<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>&gt;<br>
<div class="im">&gt; Subject: Re: [rules-users] Using eval in LHS<br>
</div>&gt; To: &quot;Rules Users List&quot; &lt;<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>&gt;<br>
&gt; Date: Friday, July 24, 2009, 2:16 PM<br>
<div><div></div><div class="h5">&gt; Thanks Greg.....<br>
&gt;<br>
&gt; The reason I chose HashMap is that an accident is said to<br>
&gt; occur if and only if more than 1 vehicle contains  four<br>
&gt; consecutive position report as same. So in my hashmap key is<br>
&gt; vehicle id and value is the no. Of times same position<br>
&gt; report received... Can you suggest me a way to<br>
&gt; 1. Collect all vehicles stopped at some point and the<br>
&gt; occurance of that event  is reported more than thrice<br>
&gt; 2. Report accident if there are more than one vehicle found<br>
&gt; in step 1<br>
&gt;  <br>
&gt; ---Priya<br>
&gt; -original message-<br>
&gt; Subject: Re: [rules-users] Using eval in LHS<br>
&gt; From: Greg Barton &lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt;<br>
&gt; Date: 24/07/2009 10:36 pm<br>
&gt;<br>
&gt;<br>
&gt; Try this:<br>
&gt;<br>
&gt; rule &quot;Create Collision Location&quot;<br>
&gt;     salience 90<br>
&gt;     no-loop true<br>
&gt; when<br>
&gt;     vehLoc : VehicleLocation()<br>
&gt;     not CollisionLocation(xway == vehLoc.xway,<br>
&gt; pos ==<br>
&gt; vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
&gt; then<br>
&gt;     CollisionLocation collisionLoc = new<br>
&gt; CollisionLocation(vehLoc.getXway(),<br>
&gt; vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane());<br>
&gt;     insert(collisionLoc);<br>
&gt; end<br>
&gt;<br>
&gt; rule &quot;Collect Collided Vehicles&quot;<br>
&gt;     salience 90<br>
&gt;     no-loop true<br>
&gt; when<br>
&gt;     vehLoc : VehicleLocation()<br>
&gt;     $collisionLoc : CollisionLocation(xway ==<br>
&gt; vehLoc.xway, pos ==<br>
&gt; vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
&gt; then<br>
&gt;    <br>
&gt; collisionLoc.getVehicleLocations().add(vehLoc);<br>
&gt;     update(collisionLoc);<br>
&gt; end<br>
&gt;<br>
&gt; And now, the rule that detects more than three coinciding<br>
&gt; vehicle locations is simple:<br>
&gt;<br>
&gt; rule &quot;Detect Too Many&quot;<br>
&gt; when<br>
&gt;     CollisionLocation(vehicleLocations.size &gt;<br>
&gt; 2)<br>
&gt; then<br>
&gt;     ...foo...<br>
&gt; end<br>
&gt;<br>
&gt; And, if you want to make the execution a bit more<br>
&gt; efficient, put a reference in VehicleLocation to it&#39;s<br>
&gt; associated CollisionLocation.  That way you can have<br>
&gt; &quot;vehLoc : VehicleLocation(collisionLocation == null)&quot; to<br>
&gt; reduce partial matches.<br>
&gt;<br>
&gt; --- On Fri, 7/24/09, PriyaSha &lt;<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>&gt;<br>
&gt; wrote:<br>
&gt;<br>
&gt; &gt; From: PriyaSha &lt;<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>&gt;<br>
&gt; &gt; Subject: [rules-users]  Using eval in LHS<br>
&gt; &gt; To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; Date: Friday, July 24, 2009, 10:45 AM<br>
&gt; &gt;<br>
&gt; &gt; Input:<br>
&gt; &gt; 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1<br>
&gt; &gt; 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1<br>
&gt; &gt;<br>
&gt; &gt; Problem:<br>
&gt; &gt;<br>
&gt; &gt; Should find vehicles with same data (if it occurs more<br>
&gt; than<br>
&gt; &gt; thrice).<br>
&gt; &gt;<br>
&gt; &gt; Though eval in rule &#39;Detect Accident&#39; results in<br>
&gt; true,<br>
&gt; &gt; consequence is not<br>
&gt; &gt; fired.<br>
&gt; &gt;<br>
&gt; &gt; Output:<br>
&gt; &gt;<br>
&gt; &gt; Added<br>
&gt; &gt; Not Found -- 107---2<br>
&gt; &gt; test---2----107<br>
&gt; &gt; Not Found -- 107---3<br>
&gt; &gt; test---3----107<br>
&gt; &gt; Not Found -- 107---4<br>
&gt; &gt; test---4----107<br>
&gt; &gt; no of veh : 1<br>
&gt; &gt; no of veh : 1<br>
&gt; &gt; no of veh : 1<br>
&gt; &gt; Not Found -- 109---5<br>
&gt; &gt; test---1----109<br>
&gt; &gt; Not Found -- 109---6<br>
&gt; &gt; test---2----109<br>
&gt; &gt; Not Found -- 106---7<br>
&gt; &gt;<br>
&gt; &gt; Please find the DRL below.<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; package <a href="http://com.hp.hpl.CHAOS.LR" target="_blank">com.hp.hpl.CHAOS.LR</a>;<br>
&gt; &gt;<br>
&gt; &gt; # importing classes<br>
&gt; &gt; import java.lang.Integer;<br>
&gt; &gt;<br>
&gt; &gt; import java.util.ArrayList;<br>
&gt; &gt; import java.util.HashMap;<br>
&gt; &gt; import java.util.Iterator;<br>
&gt; &gt;<br>
&gt; &gt; import com.hp.hpl.CHAOS.Rules.VehicleLocation;<br>
&gt; &gt;<br>
&gt; &gt; global java.lang.String output<br>
&gt; &gt;<br>
&gt; &gt; declare VehicleLocation<br>
&gt; &gt;     @role    ( event )<br>
&gt; &gt;     @expires ( 1m )<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; declare Statistics<br>
&gt; &gt;     smashedcars  : ArrayList<br>
&gt; &gt;     stopped_cars : ArrayList<br>
&gt; &gt;     accidents    :<br>
&gt; ArrayList<br>
&gt; &gt;     collided_at  : HashMap<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; rule &quot;Setup statistics&quot;<br>
&gt; &gt;     salience 110<br>
&gt; &gt;     no-loop true<br>
&gt; &gt; when<br>
&gt; &gt;    not( Statistics( ) )<br>
&gt; &gt;    vehLoc : VehicleLocation()<br>
&gt; &gt; then<br>
&gt; &gt;    Statistics s = new Statistics();<br>
&gt; &gt;    s.setSmashedcars (new ArrayList());<br>
&gt; &gt;    s.setStopped_cars (new ArrayList());<br>
&gt; &gt;    s.getStopped_cars().add(vehLoc);<br>
&gt; &gt;    s.setCollided_at(new HashMap());<br>
&gt; &gt;    insert( s );<br>
&gt; &gt;       <br>
&gt; &gt; System.out.println(&quot;Added&quot;);<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; rule &quot;Add to stopped cars&quot;<br>
&gt; &gt;     salience 100<br>
&gt; &gt;     no-loop true<br>
&gt; &gt; when<br>
&gt; &gt;     vehLoc : VehicleLocation()<br>
&gt; &gt;     $stat  : Statistics ()<br>
&gt; &gt;     ((not( VehicleLocation(vid ==<br>
&gt; &gt; vehLoc.vid) from $stat.getSmashedcars())))<br>
&gt; &gt; then<br>
&gt; &gt;     modify($stat) {<br>
&gt; &gt;       <br>
&gt; &gt; getStopped_cars().add(vehLoc);<br>
&gt; &gt;     }<br>
&gt; &gt;     System.out.println(&quot;Not Found<br>
&gt; -- &quot; +<br>
&gt; &gt; vehLoc.getVid() + &quot;---&quot; +<br>
&gt; &gt; $stat.getStopped_cars().size());<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; rule &quot;Identify Collided Vehicles&quot;<br>
&gt; &gt;     salience 90<br>
&gt; &gt;     no-loop true<br>
&gt; &gt; when<br>
&gt; &gt;     vehLoc : VehicleLocation()<br>
&gt; &gt;     $stat  : Statistics ()<br>
&gt; &gt;     $allStoppedcars : ArrayList(<br>
&gt; size &gt; 0<br>
&gt; &gt; )<br>
&gt; &gt;              from<br>
&gt; &gt; collect ( VehicleLocation(xway == vehLoc.xway, pos ==<br>
&gt; &gt; vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
&gt; from<br>
&gt; &gt; $stat.stopped_cars)<br>
&gt; &gt; then<br>
&gt; &gt;     System.out.println(&quot;test&quot; +<br>
&gt; &quot;---&quot; +<br>
&gt; &gt; $allStoppedcars.size() + &quot;----&quot; +<br>
&gt; &gt; vehLoc.getVid());<br>
&gt; &gt;     <br>
&gt; &gt;     modify($stat) {<br>
&gt; &gt;       <br>
&gt; &gt; setCollided_at(collided_at($allStoppedcars,<br>
&gt; &gt; vehLoc.getXway(),<br>
&gt; &gt; vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()));<br>
&gt; &gt;     }<br>
&gt; &gt;     retract (vehLoc);<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; rule &quot;Detect Accident&quot;<br>
&gt; &gt;     salience 80<br>
&gt; &gt;     no-loop true<br>
&gt; &gt; when<br>
&gt; &gt;     vehLoc : VehicleLocation()<br>
&gt; &gt;     $stat  : Statistics ()<br>
&gt; &gt;   <br>
&gt; &gt; eval(collision_occured($stat.getCollided_at()))<br>
&gt; &gt; then<br>
&gt; &gt;     System.out.println(&quot;Detect<br>
&gt; Accident&quot;);<br>
&gt; &gt; end<br>
&gt; &gt;<br>
&gt; &gt; function HashMap collided_at(ArrayList stopped_cars,<br>
&gt; int x,<br>
&gt; &gt; int pos, int<br>
&gt; &gt; dir, int lane) {<br>
&gt; &gt;     HashMap collided_at = new<br>
&gt; HashMap();<br>
&gt; &gt;     for (Iterator iterator =<br>
&gt; &gt; stopped_cars.iterator(); iterator.hasNext(); )<br>
&gt; &gt; {<br>
&gt; &gt;         VehicleLocation<br>
&gt; vehLoc =<br>
&gt; &gt; (VehicleLocation) iterator.next();<br>
&gt; &gt;         if<br>
&gt; (vehLoc.getXway() == x<br>
&gt; &gt;         <br>
&gt;    &amp;&amp;<br>
&gt; &gt; vehLoc.getPos() == pos<br>
&gt; &gt;         <br>
&gt;    &amp;&amp;<br>
&gt; &gt; vehLoc.getDir() == dir<br>
&gt; &gt;         <br>
&gt;    &amp;&amp;<br>
&gt; &gt; vehLoc.getLane() == lane) {<br>
&gt; &gt;             <br>
&gt; &gt;         <br>
&gt;    int key =<br>
&gt; &gt; vehLoc.getVid();<br>
&gt; &gt;         <br>
&gt;    if<br>
&gt; &gt; (!collided_at.containsKey(key)) {<br>
&gt; &gt;               <br>
&gt;<br>
&gt; &gt; collided_at.put (key, new Integer(1));<br>
&gt; &gt;               <br>
&gt;<br>
&gt; &gt; continue;<br>
&gt; &gt;             }<br>
&gt; &gt;         <br>
&gt;    collided_at.put<br>
&gt; &gt; (key,<br>
&gt; &gt; ((Integer)collided_at.get(key)).intValue()+1);<br>
&gt; &gt;         }<br>
&gt; &gt;     }<br>
&gt; &gt;     return collided_at;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; function boolean collision_occured(HashMap<br>
&gt; &gt; collided_vehicles) {<br>
&gt; &gt;     java.util.Set entries =<br>
&gt; &gt; collided_vehicles.entrySet();<br>
&gt; &gt;     int noOfCollidedVehicles = 0;<br>
&gt; &gt;     java.util.Iterator iterator =<br>
&gt; &gt; entries.iterator();<br>
&gt; &gt;<br>
&gt; &gt;     while ( iterator.hasNext() )<br>
&gt; {<br>
&gt; &gt;     <br>
&gt;    java.util.Map.Entry object =<br>
&gt; &gt; (java.util.Map.Entry) iterator.next();<br>
&gt; &gt;         if<br>
&gt; ((Integer)object.getValue()<br>
&gt; &gt; &gt; 3) {<br>
&gt; &gt;           <br>
&gt; &gt; noOfCollidedVehicles += 1;<br>
&gt; &gt;         }<br>
&gt; &gt;     }<br>
&gt; &gt;<br>
&gt; &gt;     if (noOfCollidedVehicles &gt;<br>
&gt; 0) {<br>
&gt; &gt;     <br>
&gt;    System.out.println(&quot;no of veh :<br>
&gt; &gt; &quot;  + noOfCollidedVehicles);<br>
&gt; &gt;         return true;<br>
&gt; &gt;     }<br>
&gt; &gt;     return false;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt; Please let me know what should I correct here.<br>
&gt; &gt; --<br>
&gt; &gt; View this message in context: <a href="http://www.nabble.com/Using-eval-in-LHS-tp24646946p24646946.html" target="_blank">http://www.nabble.com/Using-eval-in-LHS-tp24646946p24646946.html</a><br>
&gt; &gt; Sent from the drools - user mailing list archive at<br>
&gt; &gt; Nabble.com.<br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; rules-users mailing list<br>
&gt; &gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt; &gt;<br>
&gt;<br>
&gt;<br>
&gt;      <br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<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><br clear="all"><br>-- <br>Regards,<br>PriyaKathan<br>
</div>