[jboss-svn-commits] JBL Code SVN: r19253 - in labs/jbossrules/trunk/drools-core/src: test/java/org/drools/reteoo and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Mar 26 14:36:31 EDT 2008


Author: mark.proctor at jboss.com
Date: 2008-03-26 14:36:31 -0400 (Wed, 26 Mar 2008)
New Revision: 19253

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ExistsNodeTest.java
Log:
JBRULES-1520 RightTuple merge for asymmetrical Rete propagations
-Fixed ExistsNodeTest

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java	2008-03-26 17:37:16 UTC (rev 19252)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java	2008-03-26 18:36:31 UTC (rev 19253)
@@ -91,9 +91,9 @@
      */
     public void assertLeftTuple(final LeftTuple leftTuple,
                                 final PropagationContext context,
-                                final InternalWorkingMemory workingMemory) {        
+                                final InternalWorkingMemory workingMemory) {
         final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-        
+
         this.constraints.updateFromTuple( memory.getContext(),
                                           workingMemory,
                                           leftTuple );
@@ -118,15 +118,14 @@
         this.constraints.resetTuple( memory.getContext() );
 
         if ( leftTuple.getBlocker() != null ) {
-            // only add it to node memory if still need Objects to attempt to match
-            if ( this.tupleMemoryEnabled ) {
-                memory.getLeftTupleMemory().add( leftTuple );
-            }
-
+            // tuple is not blocked to propagate
             this.sink.propagateAssertLeftTuple( leftTuple,
                                                 context,
                                                 workingMemory );
-        }        
+        } else if ( this.tupleMemoryEnabled ) {
+            // LeftTuple is not blocked, so add to memory so other RightTuples can match
+            memory.getLeftTupleMemory().add( leftTuple );
+        }
     }
 
     /**
@@ -144,30 +143,30 @@
     public void assertObject(final InternalFactHandle factHandle,
                              final PropagationContext context,
                              final InternalWorkingMemory workingMemory) {
-//        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-//        memory.getRightTupleMemory().add( factHandle );
-//
-//        if ( !this.tupleMemoryEnabled ) {
-//            // do nothing here, as we know there are no left tuples at this stage in sequential mode.
-//            return;
-//        }
-//
-//        final Iterator it = memory.getLeftTupleMemory().iterator( factHandle );
-//        this.constraints.updateFromFactHandle( memory.getContext(),
-//                                               workingMemory,
-//                                               factHandle );
-//        for ( LeftTuple tuple = (LeftTuple) it.next(); tuple != null; tuple = (LeftTuple) it.next() ) {
-//            if ( this.constraints.isAllowedCachedRight( memory.getContext(),
-//                                                        tuple ) && tuple.getMatch() == null ) {
-//                tuple.setMatch( factHandle );
-//                this.sink.propagateAssertLeftTuple( tuple,
-//                                                    context,
-//                                                    workingMemory );
-//            }
-//        }
-//
-//        this.constraints.resetFactHandle( memory.getContext() );
-        
+        //        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
+        //        memory.getRightTupleMemory().add( factHandle );
+        //
+        //        if ( !this.tupleMemoryEnabled ) {
+        //            // do nothing here, as we know there are no left tuples at this stage in sequential mode.
+        //            return;
+        //        }
+        //
+        //        final Iterator it = memory.getLeftTupleMemory().iterator( factHandle );
+        //        this.constraints.updateFromFactHandle( memory.getContext(),
+        //                                               workingMemory,
+        //                                               factHandle );
+        //        for ( LeftTuple tuple = (LeftTuple) it.next(); tuple != null; tuple = (LeftTuple) it.next() ) {
+        //            if ( this.constraints.isAllowedCachedRight( memory.getContext(),
+        //                                                        tuple ) && tuple.getMatch() == null ) {
+        //                tuple.setMatch( factHandle );
+        //                this.sink.propagateAssertLeftTuple( tuple,
+        //                                                    context,
+        //                                                    workingMemory );
+        //            }
+        //        }
+        //
+        //        this.constraints.resetFactHandle( memory.getContext() );
+
         final RightTuple rightTuple = new RightTuple( factHandle,
                                                       this );
 
@@ -198,20 +197,18 @@
                 }
                 rightTuple.setBlocked( leftTuple );
 
-                // this is now blocked so remove from memory
+                // this is now blocked so add it from memory
                 memory.getLeftTupleMemory().remove( leftTuple );
 
-                if ( leftTuple.getBetaChildren() != null ) {
-                    this.sink.propagateAssertLeftTuple( leftTuple,
-                                                         context,
-                                                         workingMemory );
-                }
+                this.sink.propagateAssertLeftTuple( leftTuple,
+                                                    context,
+                                                    workingMemory );
             }
 
             leftTuple = temp;
         }
 
-        this.constraints.resetFactHandle( memory.getContext() );        
+        this.constraints.resetFactHandle( memory.getContext() );
     }
 
     /**
@@ -227,53 +224,53 @@
      *            The working memory session.
      */
     public void retractRightTuple(final RightTuple rightTuple,
-                              final PropagationContext context,
-                              final InternalWorkingMemory workingMemory) {
-//        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-//        if ( !memory.getRightTupleMemory().remove( handle ) ) {
-//            return;
-//        }
-//
-//        final Iterator it = memory.getLeftTupleMemory().iterator( handle );
-//        this.constraints.updateFromFactHandle( memory.getContext(),
-//                                               workingMemory,
-//                                               handle );
-//        for ( LeftTuple tuple = (LeftTuple) it.next(); tuple != null; tuple = (LeftTuple) it.next() ) {
-//            if ( this.constraints.isAllowedCachedRight( memory.getContext(),
-//                                                        tuple ) ) {
-//                if ( tuple.getMatch() == handle ) {
-//                    // reset the match
-//                    tuple.setMatch( null );
-//
-//                    // find next match, remember it and break.
-//                    final Iterator tupleIt = memory.getRightTupleMemory().iterator( tuple );
-//                    this.constraints.updateFromTuple( memory.getContext(),
-//                                                      workingMemory,
-//                                                      tuple );
-//
-//                    for ( FactEntry entry = (FactEntry) tupleIt.next(); entry != null; entry = (FactEntry) tupleIt.next() ) {
-//                        final InternalFactHandle rightHandle = entry.getFactHandle();
-//                        if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
-//                                                                   rightHandle ) ) {
-//                            tuple.setMatch( rightHandle );
-//                            break;
-//                        }
-//                    }
-//
-//                    this.constraints.resetTuple( memory.getContext() );
-//
-//                    // if there is now no new tuple match then propagate assert.
-//                    if ( tuple.getMatch() == null ) {
-//                        this.sink.propagateRetractLeftTuple( tuple,
-//                                                             context,
-//                                                             workingMemory );
-//                    }
-//                }
-//
-//            }
-//        }
-//
-//        this.constraints.resetFactHandle( memory.getContext() );
+                                  final PropagationContext context,
+                                  final InternalWorkingMemory workingMemory) {
+        //        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
+        //        if ( !memory.getRightTupleMemory().remove( handle ) ) {
+        //            return;
+        //        }
+        //
+        //        final Iterator it = memory.getLeftTupleMemory().iterator( handle );
+        //        this.constraints.updateFromFactHandle( memory.getContext(),
+        //                                               workingMemory,
+        //                                               handle );
+        //        for ( LeftTuple tuple = (LeftTuple) it.next(); tuple != null; tuple = (LeftTuple) it.next() ) {
+        //            if ( this.constraints.isAllowedCachedRight( memory.getContext(),
+        //                                                        tuple ) ) {
+        //                if ( tuple.getMatch() == handle ) {
+        //                    // reset the match
+        //                    tuple.setMatch( null );
+        //
+        //                    // find next match, remember it and break.
+        //                    final Iterator tupleIt = memory.getRightTupleMemory().iterator( tuple );
+        //                    this.constraints.updateFromTuple( memory.getContext(),
+        //                                                      workingMemory,
+        //                                                      tuple );
+        //
+        //                    for ( FactEntry entry = (FactEntry) tupleIt.next(); entry != null; entry = (FactEntry) tupleIt.next() ) {
+        //                        final InternalFactHandle rightHandle = entry.getFactHandle();
+        //                        if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
+        //                                                                   rightHandle ) ) {
+        //                            tuple.setMatch( rightHandle );
+        //                            break;
+        //                        }
+        //                    }
+        //
+        //                    this.constraints.resetTuple( memory.getContext() );
+        //
+        //                    // if there is now no new tuple match then propagate assert.
+        //                    if ( tuple.getMatch() == null ) {
+        //                        this.sink.propagateRetractLeftTuple( tuple,
+        //                                                             context,
+        //                                                             workingMemory );
+        //                    }
+        //                }
+        //
+        //            }
+        //        }
+        //
+        //        this.constraints.resetFactHandle( memory.getContext() );
         // assign now, so we can remove from memory before doing any possible propagations
         final RightTuple rootBlocker = (RightTuple) rightTuple.getPrevious();
 
@@ -317,13 +314,13 @@
                 memory.getLeftTupleMemory().add( leftTuple );
 
                 this.sink.propagateRetractLeftTuple( leftTuple,
-                                                    context,
-                                                    workingMemory );
+                                                     context,
+                                                     workingMemory );
             }
 
             leftTuple = temp;
         }
-        this.constraints.resetTuple( memory.getContext() );        
+        this.constraints.resetTuple( memory.getContext() );
     }
 
     /**
@@ -340,29 +337,27 @@
     public void retractLeftTuple(final LeftTuple leftTuple,
                                  final PropagationContext context,
                                  final InternalWorkingMemory workingMemory) {
-//        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-//
-//        // Must use the tuple in memory as it has the tuple matches count
-//        final LeftTuple tuple = memory.getLeftTupleMemory().remove( leftTuple );
-//        if ( tuple == null ) {
-//            return;
-//        }
-//
-//        if ( tuple.getMatch() != null ) {
-//            this.sink.propagateRetractLeftTuple( tuple,
-//                                                 context,
-//                                                 workingMemory );
-//        }
-        
+        //        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
+        //
+        //        // Must use the tuple in memory as it has the tuple matches count
+        //        final LeftTuple tuple = memory.getLeftTupleMemory().remove( leftTuple );
+        //        if ( tuple == null ) {
+        //            return;
+        //        }
+        //
+        //        if ( tuple.getMatch() != null ) {
+        //            this.sink.propagateRetractLeftTuple( tuple,
+        //                                                 context,
+        //                                                 workingMemory );
+        //        }
+
         RightTuple blocker = leftTuple.getBlocker();
+        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
         if ( blocker != null ) {
-            final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-            memory.getLeftTupleMemory().remove( leftTuple );
-
             this.sink.propagateRetractLeftTuple( leftTuple,
                                                  context,
                                                  workingMemory );
-            
+
             LeftTuple previous = (LeftTuple) leftTuple.getBlockedPrevious();
             LeftTuple next = (LeftTuple) leftTuple.getBlockedNext();
             if ( previous != null && next != null ) {
@@ -379,7 +374,9 @@
             } else {
                 blocker.setBlocked( null );
             }
-        }        
+        } else {
+            memory.getLeftTupleMemory().remove( leftTuple );            
+        }
     }
 
     /**
@@ -389,16 +386,16 @@
     public void updateSink(final LeftTupleSink sink,
                            final PropagationContext context,
                            final InternalWorkingMemory workingMemory) {
-//        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
-//
-//        final Iterator tupleIter = memory.getLeftTupleMemory().iterator();
-//        for ( LeftTuple tuple = (LeftTuple) tupleIter.next(); tuple != null; tuple = (LeftTuple) tupleIter.next() ) {
-//            if ( tuple.getMatch() != null ) {
-//                sink.assertLeftTuple( new LeftTuple( tuple ),
-//                                      context,
-//                                      workingMemory );
-//            }
-//        }
+        //        final BetaMemory memory = (BetaMemory) workingMemory.getNodeMemory( this );
+        //
+        //        final Iterator tupleIter = memory.getLeftTupleMemory().iterator();
+        //        for ( LeftTuple tuple = (LeftTuple) tupleIter.next(); tuple != null; tuple = (LeftTuple) tupleIter.next() ) {
+        //            if ( tuple.getMatch() != null ) {
+        //                sink.assertLeftTuple( new LeftTuple( tuple ),
+        //                                      context,
+        //                                      workingMemory );
+        //            }
+        //        }
         // @FIXME
     }
 

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ExistsNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ExistsNodeTest.java	2008-03-26 17:37:16 UTC (rev 19252)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ExistsNodeTest.java	2008-03-26 18:36:31 UTC (rev 19253)
@@ -94,11 +94,12 @@
                                            10 );
         final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.insert( cheddar );
 
-        final LeftTuple tuple1 = new LeftTuple( f0 );
+        final LeftTuple tuple1 = new LeftTuple( f0,
+                                                this.node );
 
         this.node.assertLeftTuple( tuple1,
-                               this.context,
-                               this.workingMemory );
+                                   this.context,
+                                   this.workingMemory );
 
         // no matching objects, so should not propagate
         assertLength( 0,
@@ -107,6 +108,10 @@
         assertLength( 0,
                       this.sink.getRetracted() );
 
+        // LeftTuple is not matched so should still be in memory
+        assertEquals( 1,
+                      this.memory.getLeftTupleMemory().size() );
+
         // assert will match, so should propagate
         final Cheese brie = new Cheese( "brie",
                                         10 );
@@ -116,6 +121,10 @@
                                 this.context,
                                 this.workingMemory );
 
+        // LeftTuple is now matched so should not be in memory
+        assertEquals( 0,
+                      this.memory.getLeftTupleMemory().size() );
+
         // check a single assertion
         assertLength( 1,
                       this.sink.getAsserted() );
@@ -123,16 +132,18 @@
         assertLength( 0,
                       this.sink.getRetracted() );
 
-        assertEquals( new LeftTuple( f0 ),
+        assertEquals( new LeftTuple( f0,
+                                     this.node ),
                       ((Object[]) this.sink.getAsserted().get( 0 ))[0] );
 
         // assert tuple, will have matches, so propagate
         final DefaultFactHandle f2 = (DefaultFactHandle) this.workingMemory.insert( new Cheese( "gouda",
                                                                                                 10 ) );
-        final LeftTuple tuple2 = new LeftTuple( f2 );
+        final LeftTuple tuple2 = new LeftTuple( f2,
+                                                this.node );
         this.node.assertLeftTuple( tuple2,
-                               this.context,
-                               this.workingMemory );
+                                   this.context,
+                                   this.workingMemory );
 
         // check propagations 
         assertLength( 2,
@@ -141,17 +152,21 @@
         assertLength( 0,
                       this.sink.getRetracted() );
 
-        // check memory sizes
-        assertEquals( 2,
+        // both LeftTuples should match, so no LeftTupleMemory
+        assertEquals( 0,
                       this.memory.getLeftTupleMemory().size() );
         assertEquals( 1,
                       this.memory.getRightTupleMemory().size() );
 
         // When this is retracter both tuples should be retracted
-        this.node.retractObject( f1,
-                                 this.context,
-                                 this.workingMemory );
+        this.node.retractRightTuple( f1.getRightTuple(),
+                                     this.context,
+                                     this.workingMemory );
 
+        // both LeftTuples are no longer matched, so should have LeftTupleMemory
+        assertEquals( 2,
+                      this.memory.getLeftTupleMemory().size() );
+
         // check retracts 
         assertLength( 2,
                       this.sink.getAsserted() );
@@ -173,11 +188,12 @@
                                            10 );
         final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.insert( cheddar );
 
-        final LeftTuple tuple1 = new LeftTuple( f0 );
+        final LeftTuple tuple1 = new LeftTuple( f0,
+                                                this.node );
 
         this.node.assertLeftTuple( tuple1,
-                               this.context,
-                               this.workingMemory );
+                                   this.context,
+                                   this.workingMemory );
 
         // no matching objects, so don't propagate
         assertLength( 0,
@@ -205,10 +221,11 @@
         // assert tuple, will have matches, so do assert propagation
         final DefaultFactHandle f2 = (DefaultFactHandle) this.workingMemory.insert( new Cheese( "gouda",
                                                                                                 10 ) );
-        final LeftTuple tuple2 = new LeftTuple( f2 );
+        final LeftTuple tuple2 = new LeftTuple( f2,
+                                                this.node );
         this.node.assertLeftTuple( tuple2,
-                               this.context,
-                               this.workingMemory );
+                                   this.context,
+                                   this.workingMemory );
 
         assertLength( 0,
                       this.sink.getAsserted() );
@@ -223,72 +240,89 @@
      * @throws AssertionException
      */
     public void testExistsMemoryManagement() throws FactException {
-        try {
-            // assert tuple
-            final Cheese cheddar = new Cheese( "cheddar",
-                                               10 );
-            final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.insert( cheddar );
-            final LeftTuple tuple1 = new LeftTuple( f0 );
+        // assert tuple
+        final Cheese cheddar = new Cheese( "cheddar",
+                                           10 );
+        final DefaultFactHandle f0 = (DefaultFactHandle) this.workingMemory.insert( cheddar );
+        final LeftTuple tuple1 = new LeftTuple( f0,
+                                                this.node );
 
-            this.node.assertLeftTuple( tuple1,
+        this.node.assertLeftTuple( tuple1,
                                    this.context,
                                    this.workingMemory );
 
-            // assert will match, so should propagate
-            final Cheese brie = new Cheese( "brie",
-                                            10 );
-            final DefaultFactHandle f1 = (DefaultFactHandle) this.workingMemory.insert( brie );
+        // not blocked, so should be in memory
+        assertEquals( 1,
+                      this.memory.getLeftTupleMemory().size() );
 
-            // Initially, no objects in right memory
-            assertEquals( 0,
-                          this.memory.getRightTupleMemory().size() );
-            this.node.assertObject( f1,
-                                    this.context,
-                                    this.workingMemory );
+        // assert will match, so should propagate
+        final Cheese brie = new Cheese( "brie",
+                                        10 );
+        final DefaultFactHandle f1 = (DefaultFactHandle) this.workingMemory.insert( brie );
 
-            // Now, needs to have 1 object in right memory
-            assertEquals( 1,
-                          this.memory.getRightTupleMemory().size() );
+        // Initially, no objects in right memory
+        assertEquals( 0,
+                      this.memory.getRightTupleMemory().size() );
+        this.node.assertObject( f1,
+                                this.context,
+                                this.workingMemory );
 
-            // simulate modify
-            this.node.retractObject( f1,
+        // blocked, so should not be in memory
+        assertEquals( 0,
+                      this.memory.getLeftTupleMemory().size() );
+
+        // Now, needs to have 1 object in right memory
+        assertEquals( 1,
+                      this.memory.getRightTupleMemory().size() );
+
+        // simulate modify
+        this.node.retractRightTuple( f1.getRightTuple(),
                                      this.context,
                                      this.workingMemory );
-            this.node.assertObject( f1,
-                                    this.context,
-                                    this.workingMemory );
-            // Memory should not change
-            assertEquals( 1,
-                          this.memory.getRightTupleMemory().size() );
 
-            // When this is retracter both tuples should assert
-            this.node.retractObject( f1,
+        // not blocked, so should be in memory
+        assertEquals( 1,
+                      this.memory.getLeftTupleMemory().size() );
+
+        this.node.assertObject( f1,
+                                this.context,
+                                this.workingMemory );
+
+        // blocked again, so should not be in memory
+        assertEquals( 0,
+                      this.memory.getLeftTupleMemory().size() );
+
+        // Memory should not change
+        assertEquals( 1,
+                      this.memory.getRightTupleMemory().size() );
+
+        // When this is retracter the tuple should assert
+        this.node.retractRightTuple( f1.getRightTuple(),
                                      this.context,
                                      this.workingMemory );
-            assertEquals( 0,
-                          this.memory.getRightTupleMemory().size() );
+        assertEquals( 0,
+                      this.memory.getRightTupleMemory().size() );
 
-            // check memory sizes
-            assertEquals( 1,
-                          this.memory.getLeftTupleMemory().size() );
+        // not blocked, so should be in memory
+        assertEquals( 1,
+                      this.memory.getLeftTupleMemory().size() );
 
-            // simulate modify
-            this.node.retractLeftTuple( tuple1,
+        // simulate modify
+        this.node.retractLeftTuple( tuple1,
                                     this.context,
                                     this.workingMemory );
-            this.node.assertLeftTuple( tuple1,
+        assertEquals( 0,
+                      this.memory.getLeftTupleMemory().size() );
+        this.node.assertLeftTuple( tuple1,
                                    this.context,
                                    this.workingMemory );
-            assertEquals( 1,
-                          this.memory.getLeftTupleMemory().size() );
-            this.node.retractLeftTuple( tuple1,
+        assertEquals( 1,
+                      this.memory.getLeftTupleMemory().size() );
+        this.node.retractLeftTuple( tuple1,
                                     this.context,
                                     this.workingMemory );
-            assertEquals( 0,
-                          this.memory.getLeftTupleMemory().size() );
-        } catch ( final Exception e ) {
-            Assert.fail( "No exception should be raised in this procedure, but got: " + e.toString() );
-        }
+        assertEquals( 0,
+                      this.memory.getLeftTupleMemory().size() );
     }
 
 }




More information about the jboss-svn-commits mailing list