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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Dec 26 16:19:51 EST 2007


Author: tirelli
Date: 2007-12-26 16:19:51 -0500 (Wed, 26 Dec 2007)
New Revision: 17409

Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/MockWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
Log:
JBRULES-1308: fixing the getFactHandle() method contract

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-12-26 20:47:36 UTC (rev 17408)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-12-26 21:19:51 UTC (rev 17409)
@@ -42,6 +42,7 @@
 import org.drools.Attribute;
 import org.drools.Cell;
 import org.drools.Cheese;
+import org.drools.CheeseEqual;
 import org.drools.Cheesery;
 import org.drools.Child;
 import org.drools.FactA;
@@ -4723,4 +4724,55 @@
         assertEquals( 31, bob.getAge() );
     }
     
+    public void testOrCE() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_OrCE.drl" ) ) );
+        final Package pkg = builder.getPackage();
+
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        final List list = new ArrayList();
+        workingMemory.setGlobal( "results",
+                                 list );
+        
+        workingMemory.insert( new Cheese( "brie", 10 ) );
+        workingMemory.insert( new Person( "bob" ) );
+
+        workingMemory.fireAllRules();
+
+        assertEquals( "should have fired once", 
+                      1,
+                      list.size() );
+    }
+    
+    public void testGetFactHandleEqualityBehavior() throws Exception {
+        final RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setAssertBehaviour( RuleBaseConfiguration.AssertBehaviour.EQUALITY );
+        final RuleBase ruleBase = RuleBaseFactory.newRuleBase( conf );
+
+        final StatefulSession session = ruleBase.newStatefulSession();
+
+        CheeseEqual cheese = new CheeseEqual("stilton", 10);
+        session.insert(cheese);
+        FactHandle fh = session.getFactHandle(new CheeseEqual("stilton", 10));
+        assertNotNull(fh);
+    }    
+
+    public void testGetFactHandleIdentityBehavior() throws Exception {
+        final RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setAssertBehaviour( RuleBaseConfiguration.AssertBehaviour.IDENTITY );
+        final RuleBase ruleBase = RuleBaseFactory.newRuleBase( conf );
+
+        final StatefulSession session = ruleBase.newStatefulSession();
+
+        CheeseEqual cheese = new CheeseEqual("stilton", 10);
+        session.insert(cheese);
+        FactHandle fh1 = session.getFactHandle(new Cheese("stilton", 10));
+        assertNull(fh1);
+        FactHandle fh2 = session.getFactHandle( cheese );
+        assertNotNull(fh2);
+    }    
+
 }

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/MockWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/MockWorkingMemory.java	2007-12-26 20:47:36 UTC (rev 17408)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/testframework/MockWorkingMemory.java	2007-12-26 21:19:51 UTC (rev 17409)
@@ -442,4 +442,9 @@
         return null;
     }
 
+    public FactHandle getFactHandleByIdentity(Object object) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java	2007-12-26 20:47:36 UTC (rev 17408)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java	2007-12-26 21:19:51 UTC (rev 17409)
@@ -88,7 +88,7 @@
     }
 
     public void update(final Object object) throws FactException {
-        FactHandle handle = this.workingMemory.getFactHandle( object );
+        FactHandle handle = this.workingMemory.getFactHandleByIdentity( object );
         if ( handle == null ) {
             throw new FactException( "Update error: handle not found for object: " + object + ". Is it in the working memory?" );
         }
@@ -108,7 +108,7 @@
     }
 
     public void retract(final Object object) throws FactException {
-        FactHandle handle = this.workingMemory.getFactHandle( object );
+        FactHandle handle = this.workingMemory.getFactHandleByIdentity( object );
         if ( handle == null ) {
             throw new FactException( "Retract error: handle not found for object: " + object + ". Is it in the working memory?" );
         }
@@ -120,7 +120,7 @@
     }
 
     public void modifyRetract(final Object object) {
-        FactHandle handle = this.workingMemory.getFactHandle( object );
+        FactHandle handle = this.workingMemory.getFactHandleByIdentity( object );
         this.workingMemory.modifyRetract( handle, rule, activation );
     }
 
@@ -129,7 +129,7 @@
     }
 
     public void modifyInsert(final Object object) {
-        FactHandle handle = this.workingMemory.getFactHandle( object );
+        FactHandle handle = this.workingMemory.getFactHandleByIdentity( object );
         this.workingMemory.modifyInsert( handle, object, rule, activation );
     }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-12-26 20:47:36 UTC (rev 17408)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-12-26 21:19:51 UTC (rev 17409)
@@ -608,6 +608,20 @@
     public FactHandle getFactHandle(final Object object) {
         try {
             this.lock.lock();
+            final FactHandle factHandle = (FactHandle) this.assertMap.get( object );
+
+            return factHandle;
+        } finally {
+            this.lock.unlock();
+        }
+    }
+
+    /**
+     * @see InternalWorkingMemory
+     */
+    public FactHandle getFactHandleByIdentity(final Object object) {
+        try {
+            this.lock.lock();
             final FactHandle factHandle = (FactHandle) this.identityMap.get( object );
 
             return factHandle;

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java	2007-12-26 20:47:36 UTC (rev 17408)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java	2007-12-26 21:19:51 UTC (rev 17409)
@@ -52,6 +52,16 @@
     public void queueWorkingMemoryAction(final WorkingMemoryAction action);
 
     public FactHandleFactory getFactHandleFactory();
+    
+    /**
+     * Looks for the fact handle associated to the given object
+     * by looking up the object IDENTITY (==), even if rule base
+     * is configured to AssertBehavior.EQUALITY.
+     * 
+     * @param object
+     * @return null if fact handle not found
+     */
+    public FactHandle getFactHandleByIdentity(final Object object);
 
     public void removeLogicalDependencies(final Activation activation,
                                           final PropagationContext context,




More information about the jboss-svn-commits mailing list