[jboss-svn-commits] JBL Code SVN: r26383 - in labs/jbossrules/trunk: drools-compiler/src/test/resources/org/drools/integrationtests and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue May 5 16:31:04 EDT 2009


Author: tirelli
Date: 2009-05-05 16:31:04 -0400 (Tue, 05 May 2009)
New Revision: 26383

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ModifyBlockWithFrom.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
Log:
JBRULES-2085: allowing the engine to re-resolve fact handles against working memory when the handle was created by a from CE

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	2009-05-05 18:25:28 UTC (rev 26382)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2009-05-05 20:31:04 UTC (rev 26383)
@@ -6017,6 +6017,37 @@
                       bob.getStatus() );
     }
 
+    public void testModifyBlockWithFrom() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ModifyBlockWithFrom.drl" ) ) );
+        final Package pkg = builder.getPackage();
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        final List results = new ArrayList();
+        workingMemory.setGlobal( "results",
+                                 results );
+
+        Person bob = new Person( "Bob" );
+        Address addr = new Address("abc");
+        bob.addAddress( addr );
+
+        workingMemory.insert( bob );
+        workingMemory.insert( addr );
+
+        workingMemory.fireAllRules();
+
+        // modify worked
+        assertEquals( "12345",
+                      addr.getZipCode() );
+        // chaining worked
+        assertEquals( 1, 
+                      results.size() );
+        assertEquals( addr, 
+                      results.get( 0 ) );
+    }
+
     // this test requires mvel 1.2.19. Leaving it commented until mvel is released.
     public void testJavaModifyBlock() throws Exception {
         final PackageBuilder builder = new PackageBuilder();

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ModifyBlockWithFrom.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ModifyBlockWithFrom.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_ModifyBlockWithFrom.drl	2009-05-05 20:31:04 UTC (rev 26383)
@@ -0,0 +1,20 @@
+package org.drools;
+
+global java.util.List results;
+
+rule "test modify with from"
+when
+    $p: Person( $addrs : addresses )
+    $a: Address( street == "abc" ) from $addrs
+then
+    modify( $a ) { 
+        setZipCode("12345") 
+    }
+end
+
+rule "is it chaining correctly?"
+when
+    $a : Address( zipCode == "12345" )
+then
+    results.add( $a );
+end 
\ No newline at end of file

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	2009-05-05 18:25:28 UTC (rev 26382)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java	2009-05-05 20:31:04 UTC (rev 26383)
@@ -147,17 +147,11 @@
     }
 
     public void update(final Object object) throws FactException {
-        FactHandle handle = identityMap.get(object);
-        if ( handle == null ) {
+        FactHandle handle = getFactHandle( object );
+        if( handle == null ) {
             throw new FactException( "Update error: handle not found for object: " + object + ". Is it in the working memory?" );
         }
-        // only update if this fact exists in the wm
-       ((InternalWorkingMemoryEntryPoint)((InternalFactHandle)handle).getEntryPoint())
-                            .update( handle,
-                                   object,
-                                   this.rule,
-                                   this.activation );
-       this.getIdentityMap().put(object, handle);
+        update( handle, object );
     }
 
     public void retract(final FactHandle handle) throws FactException {
@@ -171,26 +165,19 @@
     }
 
     public void retract(final Object object) throws FactException {
-        FactHandle handle =  getIdentityMap().get( object );
+        FactHandle handle = getFactHandle( object );
         if ( handle == null ) {
             throw new FactException( "Retract error: handle not found for object: " + object + ". Is it in the working memory?" );
         }
-        ((InternalWorkingMemoryEntryPoint)((InternalFactHandle)handle).getEntryPoint())
-                           .retract( handle,
-                                    true,
-                                    true,
-                                    this.rule,
-                                    this.activation );
-        this.getIdentityMap().remove(object);
-
+        retract( handle );
     }
 
     public void modifyRetract(final Object object) {
-        FactHandle handle =  getIdentityMap().get( object );
-        ((InternalWorkingMemoryEntryPoint)((InternalFactHandle)handle).getEntryPoint())
-                            .modifyRetract( handle,
-                                          rule,
-                                          activation );
+        FactHandle handle = getFactHandle( object );
+        if ( handle == null ) {
+            throw new FactException( "Modify error: handle not found for object: " + object + ". Is it in the working memory?" );
+        }
+        modifyRetract( handle );
     }
 
     public void modifyRetract(final FactHandle factHandle) {
@@ -201,14 +188,12 @@
     }
 
     public void modifyInsert(final Object object) {
-        FactHandle handle =  getIdentityMap().get( object );
-
-       ((InternalWorkingMemoryEntryPoint)((InternalFactHandle)handle).getEntryPoint())
-                            .modifyInsert( handle,
-                                         object,
-                                         rule,
-                                         activation );
-        this.getIdentityMap().put(object, handle);
+        FactHandle handle = getFactHandle( object );
+        if ( handle == null ) {
+            throw new FactException( "Modify error: handle not found for object: " + object + ". Is it in the working memory?" );
+        }
+        modifyInsert( handle,
+                      object );
     }
 
     public void modifyInsert(final FactHandle factHandle,
@@ -225,22 +210,6 @@
         return this.rule;
     }
 
-    //    public List getObjects() {
-    //        return null; //this.workingMemory.getObjects();
-    //    }
-    //
-    //    public List getObjects(final Class objectClass) {
-    //        return null; //this.workingMemory.getObjects( objectClass );
-    //    }
-    //
-    //    public void clearAgenda() {
-    //        this.workingMemory.clearAgenda();
-    //    }
-    //
-    //    public void clearAgendaGroup(final String group) {
-    //        this.workingMemory.clearAgendaGroup( group );
-    //    }
-
     public Tuple getTuple() {
         return this.tuple;
     }
@@ -257,23 +226,10 @@
         return this.activation;
     }
 
-    //    public QueryResults getQueryResults(final String query) {
-    //        return this.workingMemory.getQueryResults( query );
-    //    }
-    //
-    //    public AgendaGroup getFocus() {
-    //        return this.workingMemory.getFocus();
-    //    }
-    //
     public void setFocus(final String focus) {
         this.workingMemory.setFocus( focus );
     }
 
-    //
-    //    public void setFocus(final AgendaGroup focus) {
-    //        this.workingMemory.setFocus( focus );
-    //    }
-
     public Object get(final Declaration declaration) {
          InternalWorkingMemoryEntryPoint wmTmp = ((InternalWorkingMemoryEntryPoint)(this.tuple.get(declaration)).getEntryPoint());
          
@@ -285,8 +241,6 @@
                 return object;
         }
         return null;
-      //  return declaration.getValue( workingMemory,
-      //                               this.tuple.get( declaration ).getObject() );
     }
 
     public Declaration getDeclaration(final String identifier) {
@@ -326,4 +280,21 @@
     public void setIdentityMap(IdentityHashMap<Object, FactHandle> identityMap) {
         this.identityMap = identityMap;
     }
+
+    private FactHandle getFactHandle(final Object object) {
+        FactHandle handle = identityMap.get(object);
+        // entry point null means it is a generated fact, not a regular inserted fact
+        // NOTE: it would probably be a good idea to create a specific attribute for that
+        if ( handle == null || ((InternalFactHandle)handle).getEntryPoint() == null ) {
+            for( WorkingMemoryEntryPoint ep : workingMemory.getEntryPoints().values() ) {
+                handle = (FactHandle) ep.getFactHandle( object );
+                if( handle != null ) {
+                    identityMap.put( object, handle );
+                    break;
+                }
+            }
+        }
+        return handle;
+    }
+
 }




More information about the jboss-svn-commits mailing list