[jboss-svn-commits] JBL Code SVN: r6624 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/common main/java/org/drools/reteoo test/java/org/drools/reteoo

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Oct 5 16:36:08 EDT 2006


Author: mark.proctor at jboss.com
Date: 2006-10-05 16:36:01 -0400 (Thu, 05 Oct 2006)
New Revision: 6624

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BetaNodeConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectHashTable.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/InstrumentedReteTuple.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java
Log:
JBRULES-498 Optimised HashMap implementations
-More work on the JoinNode and its unittest

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BetaNodeConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BetaNodeConstraints.java	2006-10-05 20:07:30 UTC (rev 6623)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BetaNodeConstraints.java	2006-10-05 20:36:01 UTC (rev 6624)
@@ -74,7 +74,7 @@
                 
         for ( LinkedListEntry entry = ( LinkedListEntry ) this.constraints.getFirst(); entry != null; entry = ( LinkedListEntry ) entry.getNext() ) {
             FieldConstraint constraint = (FieldConstraint) entry.getObject();
-            if ( constraint.isAllowed( handle.getObject(),
+            if ( !constraint.isAllowed( handle.getObject(),
                                        tuple,
                                        workingMemory ) ) {
                 return false;

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java	2006-10-05 20:07:30 UTC (rev 6623)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java	2006-10-05 20:36:01 UTC (rev 6624)
@@ -47,7 +47,8 @@
  * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
  * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
  */
-abstract class BetaNode extends TupleSource implements
+abstract class BetaNode extends TupleSource
+    implements
     TupleSinkNode,
     ObjectSinkNode,
     NodeMemory {
@@ -56,19 +57,19 @@
     // ------------------------------------------------------------
 
     /** The left input <code>TupleSource</code>. */
-    private final TupleSource    leftInput;
+    private final TupleSource           leftInput;
 
     /** The right input <code>TupleSource</code>. */
-    private final ObjectSource   rightInput;
+    private final ObjectSource          rightInput;
 
     protected final BetaNodeConstraints constraints;
-    
-    private TupleSinkNode previousTupleSinkNode;
-    private TupleSinkNode nextTupleSinkNode;
-    
-    private ObjectSinkNode previousObjectSinkNode;
-    private ObjectSinkNode nextObjectSinkNode;   
 
+    private TupleSinkNode               previousTupleSinkNode;
+    private TupleSinkNode               nextTupleSinkNode;
+
+    private ObjectSinkNode              previousObjectSinkNode;
+    private ObjectSinkNode              nextObjectSinkNode;
+
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
@@ -105,18 +106,18 @@
         super( id );
         this.leftInput = leftInput;
         this.rightInput = rightInput;
-        this.constraints = constraints;  
-    }        
+        this.constraints = constraints;
+    }
 
     public FieldConstraint[] getConstraints() {
         LinkedList constraints = this.constraints.getConstraints();
-        
-        FieldConstraint[] array = new FieldConstraint[ constraints.size() ];
+
+        FieldConstraint[] array = new FieldConstraint[constraints.size()];
         int i = 0;
-        for ( LinkedListEntry entry = ( LinkedListEntry ) constraints.getFirst(); entry != null; entry = ( LinkedListEntry ) entry.getNext() ) {
-            array[i++] = ( FieldConstraint ) entry.getObject();
+        for ( LinkedListEntry entry = (LinkedListEntry) constraints.getFirst(); entry != null; entry = (LinkedListEntry) entry.getNext() ) {
+            array[i++] = (FieldConstraint) entry.getObject();
         }
-        return array;        
+        return array;
     }
 
     /* (non-Javadoc)
@@ -137,8 +138,8 @@
                                                                                       null,
                                                                                       null );
             this.leftInput.updateSink( this,
-                                      propagationContext,
-                                      workingMemory );
+                                       propagationContext,
+                                       workingMemory );
             this.rightInput.updateSink( this,
                                         propagationContext,
                                         workingMemory );
@@ -148,23 +149,23 @@
 
     public void remove(final BaseNode node,
                        final InternalWorkingMemory[] workingMemories) {
-//        if( !node.isInUse()) {
-//            getTupleSinks().remove( node );
-//        }
-//        removeShare();
-//
-//        if ( ! this.isInUse() ) {
-//            for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
-//                workingMemories[i].clearNodeMemory( this );
-//            }
-//        }
-//        this.rightInput.remove( this,
-//                                workingMemories );
-//        this.leftInput.remove( this,
-//                               workingMemories );
+        //        if( !node.isInUse()) {
+        //            getTupleSinks().remove( node );
+        //        }
+        //        removeShare();
+        //
+        //        if ( ! this.isInUse() ) {
+        //            for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+        //                workingMemories[i].clearNodeMemory( this );
+        //            }
+        //        }
+        //        this.rightInput.remove( this,
+        //                                workingMemories );
+        //        this.leftInput.remove( this,
+        //                               workingMemories );
 
-    }     
-    
+    }
+
     //public abstract TupleSink getTupleSink();
 
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -207,29 +208,36 @@
         // BetaMemory for it. If we don't find one, we create a normal beta memory. We don't need the constraint as we can assume that 
         // anything  returned by the memory already passes that test.
         LinkedList constraints = this.constraints.getConstraints();
-        for ( LinkedListEntry entry = ( LinkedListEntry ) constraints.getFirst(); entry != null; entry = ( LinkedListEntry ) entry.getNext() ) {
-            FieldConstraint constraint = ( FieldConstraint ) entry.getObject();            
-            if ( constraint.getClass() == VariableConstraint.class ) {
-                VariableConstraint variableConstraint = ( VariableConstraint ) constraint;
-                FieldExtractor extractor = variableConstraint.getFieldExtractor();
-                Evaluator evaluator = variableConstraint.getEvaluator();
-                if ( evaluator.getOperator() == Operator.EQUAL ) {
-                    // remove this entry                    
-                    constraints.remove( entry );                    
-                    BetaMemory memory = new BetaMemory( new TupleHashTable(),
-                                                        new FieldIndexHashTable(extractor, variableConstraint.getRequiredDeclarations()[0]) );
-                    return memory;
-                    
+        BetaMemory memory = null;
+        
+        if ( constraints != null ) {
+            for ( LinkedListEntry entry = (LinkedListEntry) constraints.getFirst(); entry != null; entry = (LinkedListEntry) entry.getNext() ) {
+                FieldConstraint constraint = (FieldConstraint) entry.getObject();
+                if ( constraint.getClass() == VariableConstraint.class ) {
+                    VariableConstraint variableConstraint = (VariableConstraint) constraint;
+                    FieldExtractor extractor = variableConstraint.getFieldExtractor();
+                    Evaluator evaluator = variableConstraint.getEvaluator();
+                    if ( evaluator.getOperator() == Operator.EQUAL ) {
+                        // remove this entry                    
+                        constraints.remove( entry );
+                        memory = new BetaMemory( new TupleHashTable(),
+                                                 new FieldIndexHashTable( extractor,
+                                                                          variableConstraint.getRequiredDeclarations()[0] ) );
+                        break;
+
+                    }
                 }
             }
         }
         
-        BetaMemory memory = new BetaMemory( new TupleHashTable(),
-                                            new FactHashTable() );                
+        if ( memory == null )  {
+            memory = new BetaMemory( new TupleHashTable(),
+                                     new FactHashTable() );            
+        }
+        
         return memory;
     }
-         
-    
+
     /**
      * Returns the next node
      * @return
@@ -254,7 +262,7 @@
      *      The previous TupleSinkNode
      */
     public TupleSinkNode getPreviousTupleSinkNode() {
-       return this.previousTupleSinkNode;
+        return this.previousTupleSinkNode;
     }
 
     /**
@@ -290,7 +298,7 @@
      *      The previous ObjectSinkNode
      */
     public ObjectSinkNode getPreviousObjectSinkNode() {
-       return this.previousObjectSinkNode;
+        return this.previousObjectSinkNode;
     }
 
     /**
@@ -301,5 +309,5 @@
     public void setPreviousObjectSinkNode(ObjectSinkNode previous) {
         this.previousObjectSinkNode = previous;
     }
-    
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectHashTable.java	2006-10-05 20:07:30 UTC (rev 6623)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectHashTable.java	2006-10-05 20:36:01 UTC (rev 6624)
@@ -14,4 +14,6 @@
     public boolean remove(InternalFactHandle handle);
     public boolean contains(InternalFactHandle handle);
     
+    public int size();
+    
 }

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/InstrumentedReteTuple.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/InstrumentedReteTuple.java	2006-10-05 20:07:30 UTC (rev 6623)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/InstrumentedReteTuple.java	2006-10-05 20:36:01 UTC (rev 6624)
@@ -18,6 +18,7 @@
 
 import org.drools.FactHandle;
 import org.drools.common.DefaultFactHandle;
+import org.drools.common.InternalFactHandle;
 import org.drools.spi.Tuple;
 
 public class InstrumentedReteTuple extends ReteTuple {
@@ -26,10 +27,10 @@
      */
     private static final long serialVersionUID = -5225370016555785111L;
 
-    public InstrumentedReteTuple(final Tuple left,
+    public InstrumentedReteTuple(final ReteTuple left,
                                  final FactHandle handle) {
         super( left,
-               (DefaultFactHandle) handle );
+               (InternalFactHandle) handle );
     }
 
     public InstrumentedReteTuple(final FactHandle handle) {

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java	2006-10-05 20:07:30 UTC (rev 6623)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java	2006-10-05 20:36:01 UTC (rev 6624)
@@ -16,20 +16,23 @@
  * limitations under the License.
  */
 
-import java.util.Iterator;
-import java.util.Map;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.drools.DroolsTestCase;
 import org.drools.RuleBaseFactory;
 import org.drools.common.BetaNodeConstraints;
 import org.drools.common.DefaultFactHandle;
+import org.drools.common.InternalFactHandle;
 import org.drools.common.PropagationContextImpl;
-import org.drools.reteoo.beta.BetaRightMemory;
 import org.drools.rule.Rule;
 import org.drools.spi.FieldConstraint;
 import org.drools.spi.MockConstraint;
 import org.drools.spi.PropagationContext;
 import org.drools.spi.Tuple;
+import org.drools.util.Iterator;
+import org.drools.util.AbstractHashTable.FactEntry;
 
 public class JoinNodeTest extends DroolsTestCase {
     Rule                rule;
@@ -69,35 +72,43 @@
 
         // check memories are empty
         assertEquals( 0,
-                      this.memory.getLeftTupleMemory().size() );
+                      this.memory.getTupleMemory().size() );
         assertEquals( 0,
-                      this.memory.getRightObjectMemory().size() );
+                      this.memory.getObjectMemory().size() );
 
     }
 
     public void testAttach() throws Exception {
+        Field objectFfield =  ObjectSource.class.getDeclaredField( "sink" );
+        objectFfield.setAccessible( true );
+        ObjectSinkPropagator objectSink = ( ObjectSinkPropagator ) objectFfield.get( this.objectSource );
+
+        Field tupleField =  TupleSource.class.getDeclaredField( "sink" );
+        tupleField.setAccessible( true );
+        TupleSinkPropagator tupleSink = ( TupleSinkPropagator ) tupleField.get( this.tupleSource );
+        
         assertEquals( 15,
                       this.node.getId() );
 
-        assertLength( 0,
-                      this.objectSource.getObjectSinksAsList() );
+        assertNull( objectSink );
+        assertNull( tupleSink );
 
-        assertLength( 0,
-                      this.tupleSource.getTupleSinks() );
-
         this.node.attach();
 
-        assertLength( 1,
-                      this.objectSource.getObjectSinksAsList() );
+        objectSink = ( ObjectSinkPropagator ) objectFfield.get( this.objectSource );
+        tupleSink = ( TupleSinkPropagator ) tupleField.get( this.tupleSource );
+        
+        assertEquals( 1,
+                      objectSink.getSinks().length );
 
-        assertLength( 1,
-                      this.tupleSource.getTupleSinks() );
+        assertEquals( 1,
+                      tupleSink.getSinks().length );
 
         assertSame( this.node,
-                    this.objectSource.getObjectSinks().getLastObjectSink() );
+                    objectSink.getSinks()[0]);
 
         assertSame( this.node,
-                    this.tupleSource.getTupleSinks().get( 0 ) );
+                    tupleSink.getSinks()[0] );
     }
 
     public void testMemory() {
@@ -132,9 +143,9 @@
                                this.workingMemory );
         // check memories are empty
         assertEquals( 1,
-                      this.memory.getLeftTupleMemory().size() );
+                      this.memory.getTupleMemory().size() );
         assertEquals( 0,
-                      this.memory.getRightObjectMemory().size() );
+                      this.memory.getObjectMemory().size() );
 
         // assert tuple, should add left memory should be 2
         final DefaultFactHandle f1 = new DefaultFactHandle( 1,
@@ -144,14 +155,13 @@
                                this.context,
                                this.workingMemory );
         assertEquals( 2,
-                      this.memory.getLeftTupleMemory().size() );
-
-        final ReteTuple tuple = (ReteTuple) this.memory.getLeftTupleMemory().iterator( this.workingMemory,
-                                                                                       f0 ).next();
+                      this.memory.getTupleMemory().size() );        
+        
+        Iterator it = this.memory.getTupleMemory().iterator();
         assertEquals( tuple0,
-                      tuple );
+                      it.next() );
         assertEquals( tuple1,
-                      tuple.getNext() );
+                      it.next() );
     }
 
     /**
@@ -167,9 +177,9 @@
                                 this.context,
                                 this.workingMemory );
         assertEquals( 0,
-                      this.memory.getLeftTupleMemory().size() );
+                      this.memory.getTupleMemory().size() );
         assertEquals( 1,
-                      this.memory.getRightObjectMemory().size() );
+                      this.memory.getObjectMemory().size() );
 
         // check new objects/handles still assert
         final DefaultFactHandle f1 = (DefaultFactHandle) this.workingMemory.assertObject( "test1" );
@@ -177,14 +187,12 @@
                                 this.context,
                                 this.workingMemory );
         assertEquals( 2,
-                      this.memory.getRightObjectMemory().size() );
+                      this.memory.getObjectMemory().size() );
 
-        final BetaRightMemory rightMemory = this.memory.getRightObjectMemory();
-        final Iterator it = rightMemory.iterator( this.workingMemory,
-                                                  new ReteTuple( f0 ) );
+        final Iterator it = this.memory.getObjectMemory().iterator( new ReteTuple( f0 ) );
 
-        final DefaultFactHandle rf0 = ((ObjectMatches) it.next()).getFactHandle();
-        final DefaultFactHandle rf1 = ((ObjectMatches) it.next()).getFactHandle();
+        final InternalFactHandle rf0 = ((FactEntry) it.next()).getFactHandle();
+        final InternalFactHandle rf1 = ((FactEntry) it.next()).getFactHandle();
 
         assertEquals( f0,
                       rf0 );
@@ -211,42 +219,36 @@
         this.node.assertTuple( tuple1,
                                this.context,
                                this.workingMemory );
+        
+        assertEquals( 1, this.sink.getAsserted().size() );
 
-        // check bi directional match to resulting TupleMatch
-        final Map map = tuple1.getTupleMatches();
-        final TupleMatch match = (TupleMatch) map.get( f0 );
-
-        ObjectMatches matches = (ObjectMatches) this.memory.rightObjectIterator( this.workingMemory,
-                                                                                 tuple1 ).next();
-        assertSame( match,
-                    matches.getFirstTupleMatch() );
-
-        // check reference form TupleMatch to propgated ReteTuple
-        assertSame( match.getJoinedTuples().get( 0 ),
-                    ((Object[]) this.sink.getAsserted().get( 0 ))[0] );
-
-        // check objectmatches correct references second asserted tuple
+        assertEquals(  new ReteTuple( tuple1, f0), ((Object[]) this.sink.getAsserted().get( 0 ))[0]);
+        
+        
         final DefaultFactHandle f2 = new DefaultFactHandle( 2,
                                                             "cheese" );
         final ReteTuple tuple2 = new ReteTuple( f2 );
         this.node.assertTuple( tuple2,
                                this.context,
                                this.workingMemory );
+        
+        assertEquals( 2, this.sink.getAsserted().size() );  
+        assertEquals(  new ReteTuple( tuple2, f0), ((Object[]) this.sink.getAsserted().get( 1 ))[0]);
 
-        assertSame( (tuple2.getTupleMatches()).get( f0 ),
-                    matches.getFirstTupleMatch().getNext() );
-
+        
         final DefaultFactHandle f3 = (DefaultFactHandle) this.workingMemory.assertObject( "test2" );
         this.node.assertObject( f3,
                                 this.context,
                                 this.workingMemory );
-
-        matches = getMatchesFor( tuple1,
-                                 f3 );
-        assertSame( (tuple1.getTupleMatches()).get( f3 ),
-                    matches.getFirstTupleMatch() );
-        assertSame( (tuple2.getTupleMatches()).get( f3 ),
-                    matches.getFirstTupleMatch().getNext() );
+        
+        assertEquals( 4, this.sink.getAsserted().size() );                 
+        
+        List tuples = new  ArrayList();
+        tuples.add( ((Object[]) this.sink.getAsserted().get( 2 ))[0] );
+        tuples.add( ((Object[]) this.sink.getAsserted().get( 3 ))[0] );
+        
+        assertTrue( tuples.contains(  new ReteTuple( tuple1, f3) ) );
+        assertTrue( tuples.contains(  new ReteTuple( tuple2, f3) ) );     
     }
 
     /**
@@ -287,36 +289,41 @@
         assertLength( 6,
                       this.sink.getAsserted() );
 
-        // Retract an object and make sure its removed from the previous
-        // matching ReteTuples
+        
+        // Double check the item is in memory
+        BetaMemory memory = ( BetaMemory ) this.workingMemory.getNodeMemory( this.node );
+        assertTrue( memory.getObjectMemory().contains( f0 ) );
+        
+        // Retract an object, check propagations  and memory
         this.node.retractObject( f0,
                                  this.context,
                                  this.workingMemory );
         assertLength( 2,
                       this.sink.getRetracted() );
+        
+        List tuples = new  ArrayList();
+        tuples.add( ((Object[]) this.sink.getRetracted().get( 0 ))[0] );
+        tuples.add( ((Object[]) this.sink.getRetracted().get( 1 ))[0] );
+        
+        assertTrue( tuples.contains(  new ReteTuple( tuple1, f0) ) );
+        assertTrue( tuples.contains(  new ReteTuple( tuple1, f0) ) );
+          
+        // Now check the item  is no longer in memory
+        assertFalse( memory.getObjectMemory().contains( f0 ) );
 
-        //assertNull( this.memory.getRightFactHandleMemory().get( f0 ) );
-        assertNull( tuple1.getTupleMatches().get( f0 ) );
-        assertNull( tuple2.getTupleMatches().get( f0 ) );
 
         this.node.retractTuple( tuple2,
                                 this.context,
                                 this.workingMemory );
-        ObjectMatches matches = this.getMatchesFor( tuple2,
-                                                    f3 );
-        for ( CompositeTupleMatch match = matches.getFirstTupleMatch(); match != null; match = (CompositeTupleMatch) match.getNext() ) {
-            assertNotSame( tuple2,
-                           match.getTuple() );
-        }
-
-        matches = this.getMatchesFor( tuple2,
-                                      f4 );
-        for ( CompositeTupleMatch match = matches.getFirstTupleMatch(); match != null; match = (CompositeTupleMatch) match.getNext() ) {
-            assertNotSame( tuple2,
-                           match.getTuple() );
-        }
-        assertLength( 4,
-                      this.sink.getRetracted() );
+        assertEquals( 4,
+                      this.sink.getRetracted().size() );     
+      
+        tuples = new  ArrayList();
+        tuples.add( ((Object[]) this.sink.getRetracted().get( 2 ))[0] );
+        tuples.add( ((Object[]) this.sink.getRetracted().get( 3 ))[0] );
+      
+        assertTrue( tuples.contains(  new ReteTuple( tuple2, f3) ) );
+        assertTrue( tuples.contains(  new ReteTuple( tuple2, f4) ) );             
     }
 
     public void testConstraintPropagations() throws Exception {




More information about the jboss-svn-commits mailing list