[jboss-svn-commits] JBL Code SVN: r15783 - in labs/jbossrules/trunk/drools-compiler/src: main/java/org/drools/testframework and 4 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Oct 11 23:49:02 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-10-11 23:49:02 -0400 (Thu, 11 Oct 2007)
New Revision: 15783

Added:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/TestingEventListener.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestScenarioCriteria.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestingEventListenerTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/test_rules.drl
Log:
JBRULES-1271 First dig for the Test Manager feature. From small beginnings come great things.

Added: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/TestingEventListener.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/TestingEventListener.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/TestingEventListener.java	2007-10-12 03:49:02 UTC (rev 15783)
@@ -0,0 +1,139 @@
+package org.drools.testframework;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.drools.RuleBase;
+import org.drools.WorkingMemory;
+import org.drools.event.ActivationCancelledEvent;
+import org.drools.event.ActivationCreatedEvent;
+import org.drools.event.AfterActivationFiredEvent;
+import org.drools.event.AgendaEventListener;
+import org.drools.event.AgendaGroupPoppedEvent;
+import org.drools.event.AgendaGroupPushedEvent;
+import org.drools.event.BeforeActivationFiredEvent;
+import org.drools.rule.Package;
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaFilter;
+import org.drools.spi.Consequence;
+import org.drools.spi.KnowledgeHelper;
+
+/**
+ * This tracks what is happening in the engine with rule activations and firings.
+ * It also allows you to choose what to include/exclude from firing.
+ *
+ * If a rule is not allowed to fire, it will still be counted as an activation.
+ * If it is allowed to fire, then it will only be counted after the activation is fired.
+ *
+ * @author Michael Neale
+ */
+public class TestingEventListener implements AgendaEventListener {
+
+    final Map	firingCounts = new HashMap(100);
+
+    HashSet	ruleNames = new HashSet();
+
+
+    /**
+     * Exclusive means DO NOT fire the rules mentioned.
+     * For those rules, they will still be counted, just not allowed to activate.
+     * Inclusive means only the rules on the given set are allowed to fire.
+     * The other rules will have their activation counted but not be allowed to fire.
+     */
+    public TestingEventListener(HashSet ruleNames, RuleBase ruleBase, boolean inclusive) {
+    	if (inclusive) {
+    		Package[] pkgs = ruleBase.getPackages();
+    		for (int i = 0; i < pkgs.length; i++) {
+				Rule[] rules = pkgs[i].getRules();
+				for (int j = 0; j < rules.length; j++) {
+					Rule rule = rules[j];
+					if (!ruleNames.contains(rule.getName())) {
+						rule.setConsequence(new NilConsequence());
+					}
+				}
+			}
+    	} else {
+    		Package[] pkgs = ruleBase.getPackages();
+    		for (int i = 0; i < pkgs.length; i++) {
+    			Package pkg = pkgs[i];
+    			for (Iterator iter = ruleNames.iterator(); iter.hasNext();) {
+					String name = (String) iter.next();
+					Rule rule = pkg.getRule(name);
+					rule.setConsequence(new NilConsequence());
+				}
+
+    		}
+    	}
+    	this.ruleNames = ruleNames;
+    }
+
+
+
+
+
+    public void activationCancelled(ActivationCancelledEvent event,
+            WorkingMemory workingMemory) {
+    }
+
+    public void activationCreated(ActivationCreatedEvent event,
+            WorkingMemory workingMemory) {
+    }
+
+    public void afterActivationFired(AfterActivationFiredEvent event,
+            WorkingMemory workingMemory) {
+    	recordFiring(event.getActivation().getRule());
+    }
+
+
+
+
+
+	private void recordFiring(Rule rule) {
+		record(rule, this.firingCounts);
+	}
+
+
+
+	public void agendaGroupPopped(AgendaGroupPoppedEvent event,
+            WorkingMemory workingMemory) {
+    }
+
+    public void agendaGroupPushed(AgendaGroupPushedEvent event,
+            WorkingMemory workingMemory) {
+    }
+
+	public void beforeActivationFired(BeforeActivationFiredEvent event, WorkingMemory workingMemory) {
+
+	}
+
+
+
+
+
+	private void record(Rule rule, Map counts) {
+		String name = rule.getName();
+		if (!counts.containsKey(name)) {
+			counts.put(name, new Integer(1));
+		} else {
+			Integer count = (Integer) counts.get(name);
+			counts.put(name, new Integer(count.intValue() + 1));
+		}
+	}
+
+	boolean hasRule(Rule rule) {
+		return this.ruleNames.contains(rule.getName());
+	}
+
+
+
+}
+
+class NilConsequence implements Consequence {
+
+	public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
+	}
+}
+


Property changes on: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/testframework/TestingEventListener.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestScenarioCriteria.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestScenarioCriteria.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestScenarioCriteria.java	2007-10-12 03:49:02 UTC (rev 15783)
@@ -0,0 +1,38 @@
+package org.drools.testframework;
+
+
+public class TestScenarioCriteria {
+
+
+    /**
+     * This ensures that these rules are NOT fired - all other rules may be fired.
+     */
+    public String[] rulesToExclude;
+
+
+    /**
+     * The list of rules that may be fired (all other activations are ignored).
+     */
+    public String[] rulesToInclude;
+
+
+    /**
+     * No rules will fire.
+     */
+    public boolean preventAllFiring = false;
+
+
+
+    /**
+     * Activation counts. ?? do I need to count firing versus activations ??????
+     */
+
+
+
+    /**
+     * Collect and show statistics.
+     */
+    boolean collectStats = false;
+
+
+}


Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestScenarioCriteria.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestingEventListenerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestingEventListenerTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestingEventListenerTest.java	2007-10-12 03:49:02 UTC (rev 15783)
@@ -0,0 +1,93 @@
+package org.drools.testframework;
+
+import java.io.InputStreamReader;
+import java.util.HashSet;
+
+import org.drools.Cheese;
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.StatefulSession;
+import org.drools.WorkingMemory;
+import org.drools.compiler.PackageBuilder;
+import org.drools.event.ActivationCancelledEvent;
+import org.drools.event.ActivationCreatedEvent;
+import org.drools.event.AfterActivationFiredEvent;
+import org.drools.event.AgendaEventListener;
+import org.drools.event.AgendaGroupPoppedEvent;
+import org.drools.event.AgendaGroupPushedEvent;
+import org.drools.event.BeforeActivationFiredEvent;
+
+import junit.framework.TestCase;
+
+public class TestingEventListenerTest extends TestCase {
+
+	public void testInclusive() throws Exception {
+		HashSet set = new HashSet();
+		set.add("rule1");
+		set.add("rule2");
+
+        PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader(this.getClass().getResourceAsStream( "test_rules.drl" )) );
+
+        assertFalse(builder.getErrors().toString(), builder.hasErrors());
+
+        RuleBase rb = RuleBaseFactory.newRuleBase();
+        rb.addPackage(builder.getPackage());
+
+        TestingEventListener ls = new TestingEventListener(set, rb, true);
+
+        StatefulSession session = rb.newStatefulSession();
+        session.addEventListener(ls);
+
+        session.insert(new Cheese());
+        session.fireAllRules();
+
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule1"));
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule2"));
+
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule3"));
+        assertTrue(ls.firingCounts.containsKey("rule3"));
+        assertFalse(ls.firingCounts.containsKey("rule4"));
+
+        session.insert(new Cheese());
+        session.fireAllRules();
+        assertEquals(new Integer(2), (Integer) ls.firingCounts.get("rule1"));
+        assertEquals(new Integer(2), (Integer) ls.firingCounts.get("rule2"));
+        assertEquals(new Integer(2), (Integer) ls.firingCounts.get("rule3"));
+
+	}
+
+
+	public void testExclusive() throws Exception {
+		HashSet set = new HashSet();
+		set.add("rule3");
+
+
+        PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader(this.getClass().getResourceAsStream( "test_rules.drl" )) );
+
+        assertFalse(builder.getErrors().toString(), builder.hasErrors());
+
+        RuleBase rb = RuleBaseFactory.newRuleBase();
+        rb.addPackage(builder.getPackage());
+
+        TestingEventListener ls = new TestingEventListener(set, rb, false);
+
+        StatefulSession session = rb.newStatefulSession();
+        session.addEventListener(ls);
+
+        session.insert(new Cheese());
+        session.fireAllRules();
+
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule1"));
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule2"));
+
+        assertEquals(new Integer(1), (Integer) ls.firingCounts.get("rule3"));
+        assertTrue(ls.firingCounts.containsKey("rule3"));
+        assertFalse(ls.firingCounts.containsKey("rule4"));
+
+
+	}
+
+
+}


Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/TestingEventListenerTest.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/test_rules.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/test_rules.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/test_rules.drl	2007-10-12 03:49:02 UTC (rev 15783)
@@ -0,0 +1,39 @@
+package foo.bar
+
+import org.drools.Cheese
+
+global java.util.List list
+
+rule "rule1"
+	when
+		c: Cheese()
+	then
+		System.out.println("rule1---");
+		c.setType("rule1");
+end
+
+rule "rule2"
+	when
+		c: Cheese()
+	then
+		c.setType("rule2");
+		System.out.println("rule2---");
+end
+
+rule "rule3"
+	when
+		c: Cheese()
+	then
+		//we are not using a list, but thats cool, we won't fire this rule...
+		list.add("rule3");
+		c.setType("rule3");
+		System.out.println("rule3---");
+end
+
+rule "rule4"
+	when
+		not Cheese()
+	then
+		System.err.println("whee");
+end
+


Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/testframework/test_rules.drl
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list