Re: [rules-users] Using eval in LHS
Thanks to Greg for sending out a simplified example. I've one more query. Is there any operators to do calculation on fixed time window? Here is the scenario. My data will stream into the engine starting from second 0. 0,0,0,0,1,1,1,1,...60,61...120,121180,181. 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. Suggestions are welcomed. 2009/7/28 Greg Barton > See the attached project. You can run it using maven with this command: > > mvn package exec:java -Dexec.mainClass="com.sample.DroolsTest" > > There are three basic rules: > > MakeCollision: Detect that a collision has happened > > MatchCollision: Match vehicles to collisions > > IdentifyLargeCollision: Find collisions that are "large" > > rule "MakeCollision" >salience 10 >when >s : Sighting( collision == null ) >not Collision( direction == s.direction, position == > s.position, lane == s.lane ) >then >Collision c = new Collision( s.direction, s.position, s.lane > ); >c.getSightings().add( s ); >s.setCollision( c ); >System.out.println("Creating " + c); >update( s ); >insert( c ); > end > > rule "MatchCollision" >salience 10 >when >s : Sighting( collision == null ) >c : Collision( direction == s.direction, position == > s.position, lane == s.lane ) >then >c.getSightings().add( s ); >s.setCollision( c ); >System.out.println("Matching " + s); >update( s ); >update( c ); > end > > rule "IdentifyLargeCollision" >salience 5 >when >num : Number ( intValue > 1 ) >from accumulate ( >Collision( sightings.size > 2 ), >init( int count = 0; ), >action( count++; ), >reverse( count--; ), > result( count ) >); >then >System.out.println("Found " + num); > end > > --- On Fri, 7/24/09, nash.8...@gmail.com wrote: > > > From: nash.8...@gmail.com > > Subject: Re: [rules-users] Using eval in LHS > > To: "Rules Users List" > > Date: Friday, July 24, 2009, 2:16 PM > > Thanks Greg. > > > > The reason I chose HashMap is that an accident is said to > > occur if and only if more than 1 vehicle contains four > > consecutive position report as same. So in my hashmap key is > > vehicle id and value is the no. Of times same position > > report received... Can you suggest me a way to > > 1. Collect all vehicles stopped at some point and the > > occurance of that event is reported more than thrice > > 2. Report accident if there are more than one vehicle found > > in step 1 > > > > ---Priya > > -original message- > > Subject: Re: [rules-users] Using eval in LHS > > From: Greg Barton > > Date: 24/07/2009 10:36 pm > > > > > > Try this: > > > > rule "Create Collision Location" > > salience 90 > > no-loop true > > when > > vehLoc : VehicleLocation() > > not CollisionLocation(xway == vehLoc.xway, > > pos == > > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) > > then > > CollisionLocation collisionLoc = new > > CollisionLocation(vehLoc.getXway(), > > vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()); > > insert(collisionLoc); > > end > > > > rule "Collect Collided Vehicles" > > salience 90 > > no-loop true > > when > > vehLoc : VehicleLocation() > > $collisionLoc : CollisionLocation(xway == > > vehLoc.xway, pos == > > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) > > then > > > > collisionLoc.getVehicleLocations().add(vehLoc); > > update(collisionLoc); > > end > > > > And now, the rule that detects more than three coinciding > > vehicle locations is simple: > > > > rule "Detect Too Many" > > when > > CollisionLocation(vehicleLocations.size > > > 2) > > then > > ...foo... > > end > > > > And, if you want to make the execution a bit more > >
Re: [rules-users] Using eval in LHS
See the attached project. You can run it using maven with this command: mvn package exec:java -Dexec.mainClass="com.sample.DroolsTest" There are three basic rules: MakeCollision: Detect that a collision has happened MatchCollision: Match vehicles to collisions IdentifyLargeCollision: Find collisions that are "large" rule "MakeCollision" salience 10 when s : Sighting( collision == null ) not Collision( direction == s.direction, position == s.position, lane == s.lane ) then Collision c = new Collision( s.direction, s.position, s.lane ); c.getSightings().add( s ); s.setCollision( c ); System.out.println("Creating " + c); update( s ); insert( c ); end rule "MatchCollision" salience 10 when s : Sighting( collision == null ) c : Collision( direction == s.direction, position == s.position, lane == s.lane ) then c.getSightings().add( s ); s.setCollision( c ); System.out.println("Matching " + s); update( s ); update( c ); end rule "IdentifyLargeCollision" salience 5 when num : Number ( intValue > 1 ) from accumulate ( Collision( sightings.size > 2 ), init( int count = 0; ), action( count++; ), reverse( count--; ), result( count ) ); then System.out.println("Found " + num); end --- On Fri, 7/24/09, nash.8...@gmail.com wrote: > From: nash.8...@gmail.com > Subject: Re: [rules-users] Using eval in LHS > To: "Rules Users List" > Date: Friday, July 24, 2009, 2:16 PM > Thanks Greg. > > The reason I chose HashMap is that an accident is said to > occur if and only if more than 1 vehicle contains four > consecutive position report as same. So in my hashmap key is > vehicle id and value is the no. Of times same position > report received... Can you suggest me a way to > 1. Collect all vehicles stopped at some point and the > occurance of that event is reported more than thrice > 2. Report accident if there are more than one vehicle found > in step 1 > > ---Priya > -original message- > Subject: Re: [rules-users] Using eval in LHS > From: Greg Barton > Date: 24/07/2009 10:36 pm > > > Try this: > > rule "Create Collision Location" > salience 90 > no-loop true > when > vehLoc : VehicleLocation() > not CollisionLocation(xway == vehLoc.xway, > pos == > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) > then > CollisionLocation collisionLoc = new > CollisionLocation(vehLoc.getXway(), > vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()); > insert(collisionLoc); > end > > rule "Collect Collided Vehicles" > salience 90 > no-loop true > when > vehLoc : VehicleLocation() > $collisionLoc : CollisionLocation(xway == > vehLoc.xway, pos == > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) > then > > collisionLoc.getVehicleLocations().add(vehLoc); > update(collisionLoc); > end > > And now, the rule that detects more than three coinciding > vehicle locations is simple: > > rule "Detect Too Many" > when > CollisionLocation(vehicleLocations.size > > 2) > then > ...foo... > end > > And, if you want to make the execution a bit more > efficient, put a reference in VehicleLocation to it's > associated CollisionLocation. That way you can have > "vehLoc : VehicleLocation(collisionLocation == null)" to > reduce partial matches. > > --- On Fri, 7/24/09, PriyaSha > wrote: > > > From: PriyaSha > > Subject: [rules-users] Using eval in LHS > > To: rules-users@lists.jboss.org > > Date: Friday, July 24, 2009, 10:45 AM > > > > Input: > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 > > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 > > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 > > > > Problem: > > > > Should find vehicles with same data (if it occ
Re: [rules-users] Using eval in LHS
Thanks Greg. The reason I chose HashMap is that an accident is said to occur if and only if more than 1 vehicle contains four consecutive position report as same. So in my hashmap key is vehicle id and value is the no. Of times same position report received... Can you suggest me a way to 1. Collect all vehicles stopped at some point and the occurance of that event is reported more than thrice 2. Report accident if there are more than one vehicle found in step 1 ---Priya -original message- Subject: Re: [rules-users] Using eval in LHS From: Greg Barton Date: 24/07/2009 10:36 pm Try this: rule "Create Collision Location" salience 90 no-loop true when vehLoc : VehicleLocation() not CollisionLocation(xway == vehLoc.xway, pos == vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) then CollisionLocation collisionLoc = new CollisionLocation(vehLoc.getXway(), vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()); insert(collisionLoc); end rule "Collect Collided Vehicles" salience 90 no-loop true when vehLoc : VehicleLocation() $collisionLoc : CollisionLocation(xway == vehLoc.xway, pos == vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) then collisionLoc.getVehicleLocations().add(vehLoc); update(collisionLoc); end And now, the rule that detects more than three coinciding vehicle locations is simple: rule "Detect Too Many" when CollisionLocation(vehicleLocations.size > 2) then ...foo... end And, if you want to make the execution a bit more efficient, put a reference in VehicleLocation to it's associated CollisionLocation. That way you can have "vehLoc : VehicleLocation(collisionLocation == null)" to reduce partial matches. --- On Fri, 7/24/09, PriyaSha wrote: > From: PriyaSha > Subject: [rules-users] Using eval in LHS > To: rules-users@lists.jboss.org > Date: Friday, July 24, 2009, 10:45 AM > > Input: > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 > > Problem: > > Should find vehicles with same data (if it occurs more than > thrice). > > Though eval in rule 'Detect Accident' results in true, > consequence is not > fired. > > Output: > > Added > Not Found -- 107---2 > test---2107 > Not Found -- 107---3 > test---3107 > Not Found -- 107---4 > test---4107 > no of veh : 1 > no of veh : 1 > no of veh : 1 > Not Found -- 109---5 > test---1109 > Not Found -- 109---6 > test---2109 > Not Found -- 106---7 > > Please find the DRL below. > > > package com.hp.hpl.CHAOS.LR; > > # importing classes > import java.lang.Integer; > > import java.util.ArrayList; > import java.util.HashMap; > import java.util.Iterator; > > import com.hp.hpl.CHAOS.Rules.VehicleLocation; > > global java.lang.String output > > declare VehicleLocation > @role( event ) > @expires ( 1m ) > end > > declare Statistics > smashedcars : ArrayList > stopped_cars : ArrayList > accidents: ArrayList > collided_at : HashMap > end > > rule "Setup statistics" > salience 110 > no-loop true > when >not( Statistics( ) ) >vehLoc : VehicleLocation() > then >Statistics s = new Statistics(); >s.setSmashedcars (new ArrayList()); >s.setStopped_cars (new ArrayList()); >s.getStopped_cars().add(vehLoc); >s.setCollided_at(new HashMap()); >insert( s ); > > System.out.println("Added"); > end > > rule "Add to stopped cars" > salience 100 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > ((not( VehicleLocation(vid == > vehLoc.vid) from $stat.getSmashedcars( > then > modify($stat) { > > getStopped_cars().add(vehLoc); > } > System.out.println("Not Found -- " + > vehLoc.getVid() + "---" + > $stat.getStopped_cars().size()); > end > > > rule "Identify Collided Vehicles" > salience 90 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > $allStoppedcars : ArrayList( size > 0 > ) > from > collect ( VehicleLocation(xway == vehLoc.xway, pos == > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) from > $stat.stop
Re: [rules-users] Using eval in LHS
Try this: rule "Create Collision Location" salience 90 no-loop true when vehLoc : VehicleLocation() not CollisionLocation(xway == vehLoc.xway, pos == vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) then CollisionLocation collisionLoc = new CollisionLocation(vehLoc.getXway(), vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane()); insert(collisionLoc); end rule "Collect Collided Vehicles" salience 90 no-loop true when vehLoc : VehicleLocation() $collisionLoc : CollisionLocation(xway == vehLoc.xway, pos == vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) then collisionLoc.getVehicleLocations().add(vehLoc); update(collisionLoc); end And now, the rule that detects more than three coinciding vehicle locations is simple: rule "Detect Too Many" when CollisionLocation(vehicleLocations.size > 2) then ...foo... end And, if you want to make the execution a bit more efficient, put a reference in VehicleLocation to it's associated CollisionLocation. That way you can have "vehLoc : VehicleLocation(collisionLocation == null)" to reduce partial matches. --- On Fri, 7/24/09, PriyaSha wrote: > From: PriyaSha > Subject: [rules-users] Using eval in LHS > To: rules-users@lists.jboss.org > Date: Friday, July 24, 2009, 10:45 AM > > Input: > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 > > Problem: > > Should find vehicles with same data (if it occurs more than > thrice). > > Though eval in rule 'Detect Accident' results in true, > consequence is not > fired. > > Output: > > Added > Not Found -- 107---2 > test---2107 > Not Found -- 107---3 > test---3107 > Not Found -- 107---4 > test---4107 > no of veh : 1 > no of veh : 1 > no of veh : 1 > Not Found -- 109---5 > test---1109 > Not Found -- 109---6 > test---2109 > Not Found -- 106---7 > > Please find the DRL below. > > > package com.hp.hpl.CHAOS.LR; > > # importing classes > import java.lang.Integer; > > import java.util.ArrayList; > import java.util.HashMap; > import java.util.Iterator; > > import com.hp.hpl.CHAOS.Rules.VehicleLocation; > > global java.lang.String output > > declare VehicleLocation > @role ( event ) > @expires ( 1m ) > end > > declare Statistics > smashedcars : ArrayList > stopped_cars : ArrayList > accidents : ArrayList > collided_at : HashMap > end > > rule "Setup statistics" > salience 110 > no-loop true > when > not( Statistics( ) ) > vehLoc : VehicleLocation() > then > Statistics s = new Statistics(); > s.setSmashedcars (new ArrayList()); > s.setStopped_cars (new ArrayList()); > s.getStopped_cars().add(vehLoc); > s.setCollided_at(new HashMap()); > insert( s ); > > System.out.println("Added"); > end > > rule "Add to stopped cars" > salience 100 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > ((not( VehicleLocation(vid == > vehLoc.vid) from $stat.getSmashedcars( > then > modify($stat) { > > getStopped_cars().add(vehLoc); > } > System.out.println("Not Found -- " + > vehLoc.getVid() + "---" + > $stat.getStopped_cars().size()); > end > > > rule "Identify Collided Vehicles" > salience 90 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > $allStoppedcars : ArrayList( size > 0 > ) > from > collect ( VehicleLocation(xway == vehLoc.xway, pos == > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) from > $stat.stopped_cars) > then > System.out.println("test" + "---" + > $allStoppedcars.size() + "" + > vehLoc.getVid()); > > modify($stat) { > > setCollided_at(collided_at($allStoppedcars, > vehLoc.getXway(), > vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane())); > } > retract (vehLoc); > end > > rule "Detect Accident" > salience 80 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > > eval(collision_occured($stat.getCollided_at(
Re: [rules-users] Using eval in LHS
heh. I shouldn't reply to posts while doing three other things. :) I didn't see your use of collect and insertion of VehicleLocation objects below. What you need is an intermediate object, something like CollisionLocation, with all of the attributes you use to create the counters in the HashMap constructed in the rule functions. The CollisionLocation should also include a list of the vehicles participating in the collision. When that list gets longer than three vehicles, trigger the action. --- On Fri, 7/24/09, Greg Barton wrote: > From: Greg Barton > Subject: Re: [rules-users] Using eval in LHS > To: "Rules Users List" > Date: Friday, July 24, 2009, 11:08 AM > > 1) You are completely circumventing the need for rules by > putting business logic in the functions. If you want > to do this, there's no reason to use rules at all. > 2) To do it properly insert the VehicleLocation objects > into working memory and use the "accumulate" keyword to > determine if there are three of them that fit your > criteria. See section "4.8.2.10. Conditional Element > accumulate" here: > > https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/docs/drools-expert/html_single/index.html > > --- On Fri, 7/24/09, PriyaSha > wrote: > > > From: PriyaSha > > Subject: [rules-users] Using eval in LHS > > To: rules-users@lists.jboss.org > > Date: Friday, July 24, 2009, 10:45 AM > > > > Input: > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 > > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 > > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 > > > > Problem: > > > > Should find vehicles with same data (if it occurs more > than > > thrice). > > > > Though eval in rule 'Detect Accident' results in > true, > > consequence is not > > fired. > > > > Output: > > > > Added > > Not Found -- 107---2 > > test---2107 > > Not Found -- 107---3 > > test---3107 > > Not Found -- 107---4 > > test---4107 > > no of veh : 1 > > no of veh : 1 > > no of veh : 1 > > Not Found -- 109---5 > > test---1109 > > Not Found -- 109---6 > > test---2109 > > Not Found -- 106---7 > > > > Please find the DRL below. > > > > > > package com.hp.hpl.CHAOS.LR; > > > > # importing classes > > import java.lang.Integer; > > > > import java.util.ArrayList; > > import java.util.HashMap; > > import java.util.Iterator; > > > > import com.hp.hpl.CHAOS.Rules.VehicleLocation; > > > > global java.lang.String output > > > > declare VehicleLocation > > @role ( event ) > > @expires ( 1m ) > > end > > > > declare Statistics > > smashedcars : ArrayList > > stopped_cars : ArrayList > > accidents : ArrayList > > collided_at : HashMap > > end > > > > rule "Setup statistics" > > salience 110 > > no-loop true > > when > > not( Statistics( ) ) > > vehLoc : VehicleLocation() > > then > > Statistics s = new Statistics(); > > s.setSmashedcars (new ArrayList()); > > s.setStopped_cars (new ArrayList()); > > s.getStopped_cars().add(vehLoc); > > s.setCollided_at(new HashMap()); > > insert( s ); > > > > System.out.println("Added"); > > end > > > > rule "Add to stopped cars" > > salience 100 > > no-loop true > > when > > vehLoc : VehicleLocation() > > $stat : Statistics () > > ((not( VehicleLocation(vid == > > vehLoc.vid) from $stat.getSmashedcars( > > then > > modify($stat) { > > > > getStopped_cars().add(vehLoc); > > } > > System.out.println("Not Found -- " + > > vehLoc.getVid() + "---" + > > $stat.getStopped_cars().size()); > > end > > > > > > rule "Identify Collided Vehicles" > > salience 90 > > no-loop true > > when > > vehLoc : VehicleLocation() > > $stat : Statistics () > > $allStoppedcars : ArrayList( size > 0 > > ) >
Re: [rules-users] Using eval in LHS
1) You are completely circumventing the need for rules by putting business logic in the functions. If you want to do this, there's no reason to use rules at all. 2) To do it properly insert the VehicleLocation objects into working memory and use the "accumulate" keyword to determine if there are three of them that fit your criteria. See section "4.8.2.10. Conditional Element accumulate" here: https://hudson.jboss.org/hudson/job/drools/lastSuccessfulBuild/artifact/trunk/target/docs/drools-expert/html_single/index.html --- On Fri, 7/24/09, PriyaSha wrote: > From: PriyaSha > Subject: [rules-users] Using eval in LHS > To: rules-users@lists.jboss.org > Date: Friday, July 24, 2009, 10:45 AM > > Input: > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 > 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 > 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 > 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 > > Problem: > > Should find vehicles with same data (if it occurs more than > thrice). > > Though eval in rule 'Detect Accident' results in true, > consequence is not > fired. > > Output: > > Added > Not Found -- 107---2 > test---2107 > Not Found -- 107---3 > test---3107 > Not Found -- 107---4 > test---4107 > no of veh : 1 > no of veh : 1 > no of veh : 1 > Not Found -- 109---5 > test---1109 > Not Found -- 109---6 > test---2109 > Not Found -- 106---7 > > Please find the DRL below. > > > package com.hp.hpl.CHAOS.LR; > > # importing classes > import java.lang.Integer; > > import java.util.ArrayList; > import java.util.HashMap; > import java.util.Iterator; > > import com.hp.hpl.CHAOS.Rules.VehicleLocation; > > global java.lang.String output > > declare VehicleLocation > @role ( event ) > @expires ( 1m ) > end > > declare Statistics > smashedcars : ArrayList > stopped_cars : ArrayList > accidents : ArrayList > collided_at : HashMap > end > > rule "Setup statistics" > salience 110 > no-loop true > when > not( Statistics( ) ) > vehLoc : VehicleLocation() > then > Statistics s = new Statistics(); > s.setSmashedcars (new ArrayList()); > s.setStopped_cars (new ArrayList()); > s.getStopped_cars().add(vehLoc); > s.setCollided_at(new HashMap()); > insert( s ); > > System.out.println("Added"); > end > > rule "Add to stopped cars" > salience 100 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > ((not( VehicleLocation(vid == > vehLoc.vid) from $stat.getSmashedcars( > then > modify($stat) { > > getStopped_cars().add(vehLoc); > } > System.out.println("Not Found -- " + > vehLoc.getVid() + "---" + > $stat.getStopped_cars().size()); > end > > > rule "Identify Collided Vehicles" > salience 90 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > $allStoppedcars : ArrayList( size > 0 > ) > from > collect ( VehicleLocation(xway == vehLoc.xway, pos == > vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) from > $stat.stopped_cars) > then > System.out.println("test" + "---" + > $allStoppedcars.size() + "" + > vehLoc.getVid()); > > modify($stat) { > > setCollided_at(collided_at($allStoppedcars, > vehLoc.getXway(), > vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane())); > } > retract (vehLoc); > end > > rule "Detect Accident" > salience 80 > no-loop true > when > vehLoc : VehicleLocation() > $stat : Statistics () > > eval(collision_occured($stat.getCollided_at())) > then > System.out.println("Detect Accident"); > end > > function HashMap collided_at(ArrayList stopped_cars, int x, > int pos, int > dir, int lane) { > HashMap collided_at = new HashMap(); > for (Iterator iterator = > stopped_cars.iterator(); iterator.hasNext(); ) > { > VehicleLocation vehLoc = > (VehicleLocation) iterator.next(); > if (vehLoc.getXway() == x > && > vehLoc.getPos() == pos > && > vehLoc.getDir() == dir > && > vehLoc.getLane() == lane) { &
[rules-users] Using eval in LHS
Input: 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 0,0,107,32,0,0,0,10,53320,-1,-1,-1,-1,-1,-1 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 0,0,109,20,0,0,0,19,100644,-1,-1,-1,-1,-1,-1 0,0,106,28,0,0,0,26,137745,-1,-1,-1,-1,-1,-1 0,0,108,32,0,0,0,67,354281,-1,-1,-1,-1,-1,-1 0,0,105,30,0,0,1,94,501089,-1,-1,-1,-1,-1,-1 Problem: Should find vehicles with same data (if it occurs more than thrice). Though eval in rule 'Detect Accident' results in true, consequence is not fired. Output: Added Not Found -- 107---2 test---2107 Not Found -- 107---3 test---3107 Not Found -- 107---4 test---4107 no of veh : 1 no of veh : 1 no of veh : 1 Not Found -- 109---5 test---1109 Not Found -- 109---6 test---2109 Not Found -- 106---7 Please find the DRL below. package com.hp.hpl.CHAOS.LR; # importing classes import java.lang.Integer; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.hp.hpl.CHAOS.Rules.VehicleLocation; global java.lang.String output declare VehicleLocation @role( event ) @expires ( 1m ) end declare Statistics smashedcars : ArrayList stopped_cars : ArrayList accidents: ArrayList collided_at : HashMap end rule "Setup statistics" salience 110 no-loop true when not( Statistics( ) ) vehLoc : VehicleLocation() then Statistics s = new Statistics(); s.setSmashedcars (new ArrayList()); s.setStopped_cars (new ArrayList()); s.getStopped_cars().add(vehLoc); s.setCollided_at(new HashMap()); insert( s ); System.out.println("Added"); end rule "Add to stopped cars" salience 100 no-loop true when vehLoc : VehicleLocation() $stat : Statistics () ((not( VehicleLocation(vid == vehLoc.vid) from $stat.getSmashedcars( then modify($stat) { getStopped_cars().add(vehLoc); } System.out.println("Not Found -- " + vehLoc.getVid() + "---" + $stat.getStopped_cars().size()); end rule "Identify Collided Vehicles" salience 90 no-loop true when vehLoc : VehicleLocation() $stat : Statistics () $allStoppedcars : ArrayList( size > 0 ) from collect ( VehicleLocation(xway == vehLoc.xway, pos == vehLoc.pos, dir == vehLoc.dir, lane == vehLoc.lane) from $stat.stopped_cars) then System.out.println("test" + "---" + $allStoppedcars.size() + "" + vehLoc.getVid()); modify($stat) { setCollided_at(collided_at($allStoppedcars, vehLoc.getXway(), vehLoc.getPos(), vehLoc.getDir(), vehLoc.getLane())); } retract (vehLoc); end rule "Detect Accident" salience 80 no-loop true when vehLoc : VehicleLocation() $stat : Statistics () eval(collision_occured($stat.getCollided_at())) then System.out.println("Detect Accident"); end function HashMap collided_at(ArrayList stopped_cars, int x, int pos, int dir, int lane) { HashMap collided_at = new HashMap(); for (Iterator iterator = stopped_cars.iterator(); iterator.hasNext(); ) { VehicleLocation vehLoc = (VehicleLocation) iterator.next(); if (vehLoc.getXway() == x && vehLoc.getPos() == pos && vehLoc.getDir() == dir && vehLoc.getLane() == lane) { int key = vehLoc.getVid(); if (!collided_at.containsKey(key)) { collided_at.put (key, new Integer(1)); continue; } collided_at.put (key, ((Integer)collided_at.get(key)).intValue()+1); } } return collided_at; } function boolean collision_occured(HashMap collided_vehicles) { java.util.Set entries = collided_vehicles.entrySet(); int noOfCollidedVehicles = 0; java.util.Iterator iterator = entries.iterator(); while ( iterator.hasNext() ) { java.util.Map.Entry object = (java.util.Map.Entry) iterator.next(); if ((Integer)object.getValue() > 3) { noOfCollidedVehicles += 1; } } if (noOfCollidedVehicles > 0) { System.out.println("no of veh : " + noOfCollidedVehicles); return true; } return false; } Please let me know what should I correct here. -- View this message in context: http://www.nabble.com/Using-eval-in-LHS-tp24646946p24646946.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users