Thanks to Greg for sending out a simplified example.<div><br></div><div>I'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"><<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>></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="com.sample.DroolsTest"<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 "large"<br>
<br>
rule "MakeCollision"<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("Creating " + c);<br>
update( s );<br>
insert( c );<br>
end<br>
<br>
rule "MatchCollision"<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("Matching " + s);<br>
update( s );<br>
update( c );<br>
end<br>
<br>
rule "IdentifyLargeCollision"<br>
salience 5<br>
when<br>
num : Number ( intValue > 1 )<br>
from accumulate (<br>
Collision( sightings.size > 2 ),<br>
init( int count = 0; ),<br>
action( count++; ),<br>
reverse( count--; ),<br>
result( count )<br>
);<br>
then<br>
System.out.println("Found " + num);<br>
end<br>
<br>
--- On Fri, 7/24/09, <a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a> <<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>> wrote:<br>
<br>
> From: <a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a> <<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>><br>
<div class="im">> Subject: Re: [rules-users] Using eval in LHS<br>
</div>> To: "Rules Users List" <<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>><br>
> Date: Friday, July 24, 2009, 2:16 PM<br>
<div><div></div><div class="h5">> Thanks Greg.....<br>
><br>
> The reason I chose HashMap is that an accident is said to<br>
> occur if and only if more than 1 vehicle contains four<br>
> consecutive position report as same. So in my hashmap key is<br>
> vehicle id and value is the no. Of times same position<br>
> report received... Can you suggest me a way to<br>
> 1. Collect all vehicles stopped at some point and the<br>
> occurance of that event is reported more than thrice<br>
> 2. Report accident if there are more than one vehicle found<br>
> in step 1<br>
> <br>
> ---Priya<br>
> -original message-<br>
> Subject: Re: [rules-users] Using eval in LHS<br>
> From: Greg Barton <<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>><br>
> Date: 24/07/2009 10:36 pm<br>
><br>
><br>
> Try this:<br>
><br>
> rule "Create Collision Location"<br>
> salience 90<br>
> no-loop true<br>
> when<br>
> vehLoc : VehicleLocation()<br>
> not CollisionLocation(xway == vehLoc.xway,<br>
> pos ==<br>
> vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
> then<br>
> CollisionLocation collisionLoc = new<br>
> CollisionLocation(vehLoc.getXway(),<br>
> vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane());<br>
> insert(collisionLoc);<br>
> end<br>
><br>
> rule "Collect Collided Vehicles"<br>
> salience 90<br>
> no-loop true<br>
> when<br>
> vehLoc : VehicleLocation()<br>
> $collisionLoc : CollisionLocation(xway ==<br>
> vehLoc.xway, pos ==<br>
> vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
> then<br>
> <br>
> collisionLoc.getVehicleLocations().add(vehLoc);<br>
> update(collisionLoc);<br>
> end<br>
><br>
> And now, the rule that detects more than three coinciding<br>
> vehicle locations is simple:<br>
><br>
> rule "Detect Too Many"<br>
> when<br>
> CollisionLocation(vehicleLocations.size ><br>
> 2)<br>
> then<br>
> ...foo...<br>
> end<br>
><br>
> And, if you want to make the execution a bit more<br>
> efficient, put a reference in VehicleLocation to it's<br>
> associated CollisionLocation. That way you can have<br>
> "vehLoc : VehicleLocation(collisionLocation == null)" to<br>
> reduce partial matches.<br>
><br>
> --- On Fri, 7/24/09, PriyaSha <<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>><br>
> wrote:<br>
><br>
> > From: PriyaSha <<a href="mailto:nash.8103@gmail.com">nash.8103@gmail.com</a>><br>
> > Subject: [rules-users] Using eval in LHS<br>
> > To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> > Date: Friday, July 24, 2009, 10:45 AM<br>
> ><br>
> > Input:<br>
> > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
> > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
> > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
> > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1<br>
> > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1<br>
> > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1<br>
> > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1<br>
> > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1<br>
> > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1<br>
> ><br>
> > Problem:<br>
> ><br>
> > Should find vehicles with same data (if it occurs more<br>
> than<br>
> > thrice).<br>
> ><br>
> > Though eval in rule 'Detect Accident' results in<br>
> true,<br>
> > consequence is not<br>
> > fired.<br>
> ><br>
> > Output:<br>
> ><br>
> > Added<br>
> > Not Found -- 107---2<br>
> > test---2----107<br>
> > Not Found -- 107---3<br>
> > test---3----107<br>
> > Not Found -- 107---4<br>
> > test---4----107<br>
> > no of veh : 1<br>
> > no of veh : 1<br>
> > no of veh : 1<br>
> > Not Found -- 109---5<br>
> > test---1----109<br>
> > Not Found -- 109---6<br>
> > test---2----109<br>
> > Not Found -- 106---7<br>
> ><br>
> > Please find the DRL below.<br>
> ><br>
> ><br>
> > package <a href="http://com.hp.hpl.CHAOS.LR" target="_blank">com.hp.hpl.CHAOS.LR</a>;<br>
> ><br>
> > # importing classes<br>
> > import java.lang.Integer;<br>
> ><br>
> > import java.util.ArrayList;<br>
> > import java.util.HashMap;<br>
> > import java.util.Iterator;<br>
> ><br>
> > import com.hp.hpl.CHAOS.Rules.VehicleLocation;<br>
> ><br>
> > global java.lang.String output<br>
> ><br>
> > declare VehicleLocation<br>
> > @role ( event )<br>
> > @expires ( 1m )<br>
> > end<br>
> ><br>
> > declare Statistics<br>
> > smashedcars : ArrayList<br>
> > stopped_cars : ArrayList<br>
> > accidents :<br>
> ArrayList<br>
> > collided_at : HashMap<br>
> > end<br>
> ><br>
> > rule "Setup statistics"<br>
> > salience 110<br>
> > no-loop true<br>
> > when<br>
> > not( Statistics( ) )<br>
> > vehLoc : VehicleLocation()<br>
> > then<br>
> > Statistics s = new Statistics();<br>
> > s.setSmashedcars (new ArrayList());<br>
> > s.setStopped_cars (new ArrayList());<br>
> > s.getStopped_cars().add(vehLoc);<br>
> > s.setCollided_at(new HashMap());<br>
> > insert( s );<br>
> > <br>
> > System.out.println("Added");<br>
> > end<br>
> ><br>
> > rule "Add to stopped cars"<br>
> > salience 100<br>
> > no-loop true<br>
> > when<br>
> > vehLoc : VehicleLocation()<br>
> > $stat : Statistics ()<br>
> > ((not( VehicleLocation(vid ==<br>
> > vehLoc.vid) from $stat.getSmashedcars())))<br>
> > then<br>
> > modify($stat) {<br>
> > <br>
> > getStopped_cars().add(vehLoc);<br>
> > }<br>
> > System.out.println("Not Found<br>
> -- " +<br>
> > vehLoc.getVid() + "---" +<br>
> > $stat.getStopped_cars().size());<br>
> > end<br>
> ><br>
> ><br>
> > rule "Identify Collided Vehicles"<br>
> > salience 90<br>
> > no-loop true<br>
> > when<br>
> > vehLoc : VehicleLocation()<br>
> > $stat : Statistics ()<br>
> > $allStoppedcars : ArrayList(<br>
> size > 0<br>
> > )<br>
> > from<br>
> > collect ( VehicleLocation(xway == vehLoc.xway, pos ==<br>
> > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane)<br>
> from<br>
> > $stat.stopped_cars)<br>
> > then<br>
> > System.out.println("test" +<br>
> "---" +<br>
> > $allStoppedcars.size() + "----" +<br>
> > vehLoc.getVid());<br>
> > <br>
> > modify($stat) {<br>
> > <br>
> > setCollided_at(collided_at($allStoppedcars,<br>
> > vehLoc.getXway(),<br>
> > vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()));<br>
> > }<br>
> > retract (vehLoc);<br>
> > end<br>
> ><br>
> > rule "Detect Accident"<br>
> > salience 80<br>
> > no-loop true<br>
> > when<br>
> > vehLoc : VehicleLocation()<br>
> > $stat : Statistics ()<br>
> > <br>
> > eval(collision_occured($stat.getCollided_at()))<br>
> > then<br>
> > System.out.println("Detect<br>
> Accident");<br>
> > end<br>
> ><br>
> > function HashMap collided_at(ArrayList stopped_cars,<br>
> int x,<br>
> > int pos, int<br>
> > dir, int lane) {<br>
> > HashMap collided_at = new<br>
> HashMap();<br>
> > for (Iterator iterator =<br>
> > stopped_cars.iterator(); iterator.hasNext(); )<br>
> > {<br>
> > VehicleLocation<br>
> vehLoc =<br>
> > (VehicleLocation) iterator.next();<br>
> > if<br>
> (vehLoc.getXway() == x<br>
> > <br>
> &&<br>
> > vehLoc.getPos() == pos<br>
> > <br>
> &&<br>
> > vehLoc.getDir() == dir<br>
> > <br>
> &&<br>
> > vehLoc.getLane() == lane) {<br>
> > <br>
> > <br>
> int key =<br>
> > vehLoc.getVid();<br>
> > <br>
> if<br>
> > (!collided_at.containsKey(key)) {<br>
> > <br>
><br>
> > collided_at.put (key, new Integer(1));<br>
> > <br>
><br>
> > continue;<br>
> > }<br>
> > <br>
> collided_at.put<br>
> > (key,<br>
> > ((Integer)collided_at.get(key)).intValue()+1);<br>
> > }<br>
> > }<br>
> > return collided_at;<br>
> > }<br>
> ><br>
> > function boolean collision_occured(HashMap<br>
> > collided_vehicles) {<br>
> > java.util.Set entries =<br>
> > collided_vehicles.entrySet();<br>
> > int noOfCollidedVehicles = 0;<br>
> > java.util.Iterator iterator =<br>
> > entries.iterator();<br>
> ><br>
> > while ( iterator.hasNext() )<br>
> {<br>
> > <br>
> java.util.Map.Entry object =<br>
> > (java.util.Map.Entry) iterator.next();<br>
> > if<br>
> ((Integer)object.getValue()<br>
> > > 3) {<br>
> > <br>
> > noOfCollidedVehicles += 1;<br>
> > }<br>
> > }<br>
> ><br>
> > if (noOfCollidedVehicles ><br>
> 0) {<br>
> > <br>
> System.out.println("no of veh :<br>
> > " + noOfCollidedVehicles);<br>
> > return true;<br>
> > }<br>
> > return false;<br>
> > }<br>
> ><br>
> > Please let me know what should I correct here.<br>
> > --<br>
> > 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>
> > Sent from the drools - user mailing list archive at<br>
> > Nabble.com.<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>
> 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>
</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>