[jboss-svn-commits] JBL Code SVN: r16916 - in labs/jbossrules/contrib/benchmarks/src: java/benchmarks/dispatch/fact/derived and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Nov 29 15:50:58 EST 2007


Author: shabino
Date: 2007-11-29 15:50:58 -0500 (Thu, 29 Nov 2007)
New Revision: 16916

Modified:
   labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/DispatchBenchmark.java
   labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/fact/derived/TopWorkers.java
   labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/function/Helper.java
   labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/simulation/JobGenerator.java
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Derivations.dslr
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/LogicalTopTier.dslr
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Queries.drl
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/ScoreManagement.dslr
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Scoring.dslr
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/WorkerEligibility.dslr
   labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/dispatch.dsl
Log:
Dispatch Benchmark Unit Tests (Partially Complete)

Modified: labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/DispatchBenchmark.java
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/DispatchBenchmark.java	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/DispatchBenchmark.java	2007-11-29 20:50:58 UTC (rev 16916)
@@ -16,23 +16,9 @@
  * limitations under the License.
  */
 
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.util.Date;
-import java.util.HashSet;
-import java.util.Set;
 
 import org.drools.FactHandle;
-import org.drools.RuleBase;
-import org.drools.RuleBaseConfiguration;
-import org.drools.RuleBaseFactory;
-import org.drools.WorkingMemory;
-import org.drools.compiler.DrlParser;
-import org.drools.compiler.PackageBuilder;
-import org.drools.compiler.PackageBuilderConfiguration;
-import org.drools.lang.descr.PackageDescr;
-import org.drools.spi.Activation;
-import org.drools.spi.AgendaFilter;
 
 import benchmarks.dispatch.fact.independent.DispatchState;
 import benchmarks.dispatch.fact.independent.Worker;
@@ -41,81 +27,35 @@
 import benchmarks.dispatch.simulation.WorkerGenerator;
 
 public class DispatchBenchmark {
-
-	private static final Set<String> rules = new HashSet<String>();
-
-	static {
-		rules.add("/benchmarks/dispatch/Derivations.dslr");
-		rules.add("/benchmarks/dispatch/EligibilityExclusions.dslr");
-		rules.add("/benchmarks/dispatch/LogicalTopTier.dslr");
-		rules.add("/benchmarks/dispatch/Queries.drl");
-		rules.add("/benchmarks/dispatch/Scoring.dslr");
-		rules.add("/benchmarks/dispatch/ScoreManagement.dslr");
-		rules.add("/benchmarks/dispatch/WorkerEligibility.dslr");
+	
+	private DispatchWrapper dw;
+	
+	public DispatchBenchmark() throws Exception {
+		dw = new DispatchWrapper();
 	}
-
-	private WorkingMemory wm;
-
-	private void createWM() throws Exception {
-
-		RuleBaseConfiguration conf = new RuleBaseConfiguration();
-
-		conf.setAssertBehaviour(RuleBaseConfiguration.AssertBehaviour.EQUALITY);
-		conf.setRemoveIdentities(true);
-		conf.setLogicalOverride(RuleBaseConfiguration.LogicalOverride.DISCARD);
-		conf.setMaintainTms(true);
-		conf.setShadowProxy(true);
-
-		RuleBase ruleBase = RuleBaseFactory.newRuleBase(conf);
-
-		PackageBuilderConfiguration pbc = new PackageBuilderConfiguration();
-
-		pbc
-				.addAccumulateFunction("top_workers",
-						"benchmarks.dispatch.accumulator.TopWorkerAccumulator");
-
-		PackageBuilder builder = new PackageBuilder(pbc);
-
-		for (String ruleFile : rules) {
-			DrlParser parser = new DrlParser();
-			PackageDescr desc = parser.parse(getReader(ruleFile),
-					getReader("/benchmarks/dispatch/dispatch.dsl"));
-			builder.addPackage(desc);
-		}
-
-		ruleBase.addPackage(builder.getPackage());
-
-		wm = ruleBase.newStatefulSession();
-
-	}
-
-	private Reader getReader(String resourceName) {
-		return new InputStreamReader(getClass().getResourceAsStream(
-				resourceName));
-	}
-
+	
 	public void go(int numWorkers, int numJobs) throws Exception {
 
 		DispatchState ds = new DispatchState();
 		ds.setCurrentTime(new Date());
-		FactHandle dsfh = wm.insert(ds);
+		FactHandle dsfh = dw.insert(ds);
 
 		WorkerGenerator wg = new WorkerGenerator(100, ds.getCurrentTime());
 
 		for (int i = 0; i < numWorkers; i++) {
 			Worker w = wg.generateWorker();
-			wm.insert(w);
+			dw.insert(w);
 			WorkerPosition wp = wg.generateWorkerPosition(w);
-			wm.insert(wp);
+			dw.insert(wp);
 		}
 
 		JobGenerator jg = new JobGenerator(100, ds.getCurrentTime());
 
 		for (int i = 0; i < numJobs; i++) {
-			wm.insert(jg.generateJob());
+			dw.insert(jg.generateJob());
 		}
 
-		wm.fireAllRules();
+		dw.fireAllRules();
 	}
 
 	public static void main(String args[]) throws Exception {
@@ -131,8 +71,6 @@
 		
 		DispatchBenchmark db = new DispatchBenchmark();
 		
-		db.createWM();
-		
 		long startTime = System.currentTimeMillis();
 		
 		db.go(numWorkers, numJobs);

Modified: labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/fact/derived/TopWorkers.java
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/fact/derived/TopWorkers.java	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/fact/derived/TopWorkers.java	2007-11-29 20:50:58 UTC (rev 16916)
@@ -48,6 +48,11 @@
     public List<String> getTopWorkers() {
         return workerScores;
     }
+    
+    //Allow indexing by drools
+    public int getSize(){
+    	return workerScores.size();
+    }
 
     public String getJobId() {
         return jobId;

Modified: labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/function/Helper.java
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/function/Helper.java	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/function/Helper.java	2007-11-29 20:50:58 UTC (rev 16916)
@@ -22,31 +22,37 @@
 
 public class Helper {
 
-    public static Double distanceToPickupInMiles(Job job,
-                                                 Double workerLatitude,
-                                                 Double workerLongitude) {
+	public static Double distanceToPickupInMiles(Job job,
+			Double workerLatitude, Double workerLongitude) {
 
-        Double jobLatitude = job.getLatitude();
-        Double jobLongitude = job.getLongitude();
+		Double jobLatitude = job.getLatitude();
+		Double jobLongitude = job.getLongitude();
 
-        jobLatitude = Math.toRadians( jobLatitude );
-        workerLatitude = Math.toRadians( workerLatitude );
-        jobLongitude = Math.toRadians( jobLongitude );
-        workerLongitude = Math.toRadians( workerLongitude );
+		jobLatitude = Math.toRadians(jobLatitude);
+		workerLatitude = Math.toRadians(workerLatitude);
+		jobLongitude = Math.toRadians(jobLongitude);
+		workerLongitude = Math.toRadians(workerLongitude);
 
-        double angle = Math.acos( Math.sin( jobLatitude ) * Math.sin( workerLatitude ) + Math.cos( jobLatitude ) * Math.cos( workerLatitude ) * Math.cos( jobLongitude - workerLongitude ) );
+		double angle = Math.acos(Math.sin(jobLatitude)
+				* Math.sin(workerLatitude) + Math.cos(jobLatitude)
+				* Math.cos(workerLatitude)
+				* Math.cos(jobLongitude - workerLongitude));
 
-        return Math.toDegrees( angle ) * 69.1105; //miles per degree
-    }
+		return Math.toDegrees(angle) * 69.1105; // miles per degree
+	}
 
-    public static Double minutesLateToJob(Date jobStartTime,
-                                          Date workerAvailableTime,
-                                          Double minutesToJob) {
-        return 1D;
-    }
+	public static Double minutesLateToJob(Date jobStartTime,
+			Date workerAvailableTime, Double minutesToJob) {
+		Date earliestArrival = new Date(workerAvailableTime.getTime() + Math.round(minutesToJob * 1000 * 60));
+		if (earliestArrival.before(jobStartTime)){
+			return 0D;
+		} else {
+			long diff = earliestArrival.getTime() - jobStartTime.getTime();
+			return diff / 1000.0 / 60.0;
+		}
+	}
 
-    public static Double minutesBetweenDates(Date start,
-                                             Date end) {
-        return 0D;
-    }
+	public static Double minutesBetweenDates(Date start, Date end) {
+		return 0D;
+	}
 }

Modified: labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/simulation/JobGenerator.java
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/simulation/JobGenerator.java	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/java/benchmarks/dispatch/simulation/JobGenerator.java	2007-11-29 20:50:58 UTC (rev 16916)
@@ -1,11 +1,7 @@
 package benchmarks.dispatch.simulation;
 
 import java.util.Date;
-import java.util.Random;
 
-import org.apache.commons.math.random.RandomDataImpl;
-import org.apache.commons.math.random.RandomGenerator;
-
 import benchmarks.dispatch.fact.independent.Job;
 import benchmarks.dispatch.fact.independent.VehicleSize;
 

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Derivations.dslr
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Derivations.dslr	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Derivations.dslr	2007-11-29 20:50:58 UTC (rev 16916)
@@ -110,7 +110,6 @@
 	then 
 		DecimalInfo ti = new DecimalInfo(j ,w);
 		ti.setType(Info.Type.MINUTES_LATE_TO_JOB);
-		
 		ti.setValue(minutesLateToJob(j.getStartTime(), availDate.getValue(), timeToJob.getValue()));
 		insertLogical(ti);
 end

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/LogicalTopTier.dslr
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/LogicalTopTier.dslr	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/LogicalTopTier.dslr	2007-11-29 20:50:58 UTC (rev 16916)
@@ -18,6 +18,7 @@
 
 import benchmarks.dispatch.fact.derived.*
 import benchmarks.dispatch.fact.independent.*
+import benchmarks.dispatch.DispatchParameters;
 import java.util.Date
 
 expander dispatch.dsl
@@ -38,8 +39,28 @@
 		retract(j);
 end
 
+rule "Establish Beginning Max Radius"
+	salience 300
+	when
+		j: Job()
+		not MaxRadius(jobId == j.jobId)
+	then
+		MaxRadius mr = new MaxRadius(j);
+		mr.setMaxRadius(DispatchParameters.INITIAL_SEARCH_RADIUS);
+		#Do not insert logical to avoid infinite loop
+		insert(mr);
+end
 
+rule "Clobber Dead Max Radius"
+	salience 300
+	when
+		mr: MaxRadius()
+		not Job(jobId == mr.jobId)
+	then
+		retract(mr);
+end
 
+
 rule "Clobber dead positions"
 	salience 200 
 	when

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Queries.drl
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Queries.drl	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Queries.drl	2007-11-29 20:50:58 UTC (rev 16916)
@@ -22,3 +22,11 @@
 query getTopWorkersForJob (String s)
 	e: TopWorkers(jobId == s)
 end
+
+query getWorkerInfo(String id, WorkerInfo.Type t)
+	wi: WorkerInfo(workerId == id, type == t)
+end
+
+query getInfo(String wId, String jId, Info.Type t)
+	i: Info(workerId == wId, jobId == jId, type == t)
+end

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/ScoreManagement.dslr
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/ScoreManagement.dslr	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/ScoreManagement.dslr	2007-11-29 20:50:58 UTC (rev 16916)
@@ -18,6 +18,7 @@
 
 import benchmarks.dispatch.fact.derived.*
 import benchmarks.dispatch.fact.independent.*
+import benchmarks.dispatch.DispatchParameters;
 import java.util.Date
 import java.util.Set
 
@@ -26,10 +27,11 @@
 rule "Default Score Component"
 salience -25
 when
-	#Force score accumulation if no score components
+	#Force score accumulation even if no score components
 	j : Job()
 	job dispatchable
 	w: Worker()
+	within range
     #Don't score workers that aren't eligible
     not Exclusion(workerId == w.workerId, jobId == j.jobId)
 then
@@ -44,6 +46,7 @@
 	j : Job()
 	job dispatchable
 	w: Worker()
+	within range
     #Don't score workers that aren't eligible
     not Exclusion(workerId == w.workerId, jobId == j.jobId)
     totScore : Double() 
@@ -74,5 +77,17 @@
     insertLogical(topWorkers);
 end
 
+rule "Ensure Enough Workers"
+salience -200
+when
+	tw: TopWorkers(size < DispatchParameters.MIN_SCORED_WORKERS)
+	#Ensure no infinite loop?  Doesn't propogation cease on its own?
+	mw: MaxRadius(jobId == tw.jobId, maxRadius < 4000)
+	#TODO: Ensure sufficient minimum score of Top N vehicles here
+then
+	mw.setMaxRadius(mw.getMaxRadius() + DispatchParameters.INCREMENTAL_SEARCH_RADIUS);
+    insert(mw);
+end
 
 
+

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Scoring.dslr
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Scoring.dslr	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/Scoring.dslr	2007-11-29 20:50:58 UTC (rev 16916)
@@ -23,10 +23,12 @@
 expander dispatch.dsl
 
 rule "Distance to Job Score - Extra Large"
+	salience -10
 	when
 		j: Job()
 		job dispatchable
 		w: Worker(vehicleSize == VehicleSize.EXTRA_LARGE)
+		within range
 		i: DecimalInfo(workerId == w.workerId, jobId == j.jobId,
 			type == Info.Type.DISTANCE_TO_JOB_MILES, dist: value >= 100)
 	then 
@@ -38,10 +40,12 @@
 end
 
 rule "Distance to Job Score - Regular"
+	salience -10
 	when
 		j: Job()
 		job dispatchable
 		w: Worker(vehicleSize != VehicleSize.EXTRA_LARGE)
+		within range
 		i: DecimalInfo(workerId == w.workerId, jobId == j.jobId,
 			type == Info.Type.DISTANCE_TO_JOB_MILES, dist: value >= 100)
 	then 
@@ -53,10 +57,12 @@
 end
 
 rule "Late to Job Score"
+	salience -10
 	when
 		j : Job 
 		job dispatchable
 		w : Worker()
+		within range
 		i: DecimalInfo(jobId == j.jobId, workerId == w.workerId,
 			type == Info.Type.MINUTES_LATE_TO_JOB, minutesLate: value >= 0)
 	then 

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/WorkerEligibility.dslr
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/WorkerEligibility.dslr	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/WorkerEligibility.dslr	2007-11-29 20:50:58 UTC (rev 16916)
@@ -27,6 +27,7 @@
 		j : Job(vs: vehicleSizeRequired.sizeNumber)
 		job dispatchable
 		w: Worker(vehicleSize.sizeNumber < vs)
+		within range
 	then
 		ineligible because of VEHICLE_SIZE
 end
@@ -36,6 +37,7 @@
 		j : Job(wrenchRequired == true)
 		job dispatchable
 		w : Worker(hasWrench == false)
+		within range
 	then 
 		ineligible because of WRENCH_REQUIRED
 end
@@ -45,6 +47,7 @@
 		j : Job(req:numberOfSticksRequired)
 		job dispatchable
 		w : Worker(numberOfSticks < req)
+		within range
 	then 
 		ineligible because of NUM_STICKS
 end
@@ -54,6 +57,7 @@
 		j : Job(req:numberOfRocksRequired)
 		job dispatchable
 		w : Worker(numberOfRocks < req)
+		within range
 	then 
 		ineligible because of NUM_ROCKS
 end

Modified: labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/dispatch.dsl
===================================================================
--- labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/dispatch.dsl	2007-11-29 19:45:27 UTC (rev 16915)
+++ labs/jbossrules/contrib/benchmarks/src/rules/benchmarks/dispatch/dispatch.dsl	2007-11-29 20:50:58 UTC (rev 16916)
@@ -2,3 +2,4 @@
 [consequence][]ineligible because of {condition}=Exclusion e = new Exclusion(j, w);  e.setType(Exclusion.Type.{condition});insertLogical(e);
 [condition][]time matters=ds: DispatchState()
 [condition][]job dispatchable=not JobExclusion(jobId == j.jobId)
+[condition][]within range=dist_mr: DecimalInfo(jobId == j.jobId, workerId == w.workerId, type == Info.Type.DISTANCE_TO_JOB_MILES);MaxRadius(jobId == j.jobId, maxRadius >= dist_mr.value)




More information about the jboss-svn-commits mailing list