[jboss-svn-commits] JBL Code SVN: r16842 - labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 26 14:06:21 EST 2007


Author: mark.proctor at jboss.com
Date: 2007-11-26 14:06:21 -0500 (Mon, 26 Nov 2007)
New Revision: 16842

Modified:
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/NotNode.java
   labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
Log:
JBRULES-1349 NotNode and Exists Improvements

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java	2007-11-26 19:00:06 UTC (rev 16841)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java	2007-11-26 19:06:21 UTC (rev 16842)
@@ -99,17 +99,15 @@
         final Iterator it = memory.getFactHandleMemory().iterator( leftTuple );
         this.constraints.updateFromTuple( workingMemory,
                                           leftTuple );
-        int matches = 0;
-        for ( FactEntry entry = (FactEntry) it.next(); entry != null; entry = (FactEntry) it.next() ) {
+        for ( FactEntry entry = (FactEntry) it.next(); entry != null; entry = (FactEntry) it.next() ) {            
             final InternalFactHandle handle = entry.getFactHandle();
             if ( this.constraints.isAllowedCachedLeft( handle.getObject() ) ) {
-                matches++;
-            }
+                leftTuple.setMatch( handle );
+                break;
+            }            
         }
 
-        leftTuple.setMatches( matches );
-
-        if ( matches > 0 ) {
+        if ( leftTuple.getMatch() != null ) {
             this.sink.propagateAssertTuple( leftTuple,
                                             context,
                                             workingMemory );
@@ -143,16 +141,11 @@
         this.constraints.updateFromFactHandle( workingMemory,
                                                handle );
         for ( ReteTuple tuple = (ReteTuple) it.next(); tuple != null; tuple = (ReteTuple) it.next() ) {
-            if ( this.constraints.isAllowedCachedRight( tuple ) ) {
-                final int matches = tuple.getMatches();
-                tuple.setMatches( matches + 1 );
-
-                // if this is the first match, propagate tuple
-                if ( tuple.getMatches() == 1 ) {
+            if ( this.constraints.isAllowedCachedRight( tuple ) && tuple.getMatch() == null) {
+                    tuple.setMatch( handle );
                     this.sink.propagateAssertTuple( tuple,
-                                                    context,
-                                                    workingMemory );
-                }
+                                                     context,
+                                                     workingMemory );                                 
             }
         }
     }
@@ -182,12 +175,30 @@
                                                handle );
         for ( ReteTuple tuple = (ReteTuple) it.next(); tuple != null; tuple = (ReteTuple) it.next() ) {
             if ( this.constraints.isAllowedCachedRight( tuple ) ) {
-                tuple.setMatches( tuple.getMatches() - 1 );
-                if ( tuple.getMatches() == 0 ) {
-                    this.sink.propagateRetractTuple( tuple,
-                                                     context,
-                                                     workingMemory );
+                if ( tuple.getMatch() == handle ) {
+                    // reset the match                    
+                    tuple.setMatch( null );
+                    
+                    // find next match, remember it and break.
+                    final Iterator tupleIt = memory.getFactHandleMemory().iterator( tuple );
+                    this.constraints.updateFromTuple( workingMemory, tuple );
+                    
+                    for ( FactEntry entry = (FactEntry) tupleIt.next(); entry != null; entry = (FactEntry) tupleIt.next() ) {
+                        final InternalFactHandle rightHandle = entry.getFactHandle();
+                        if ( this.constraints.isAllowedCachedLeft( rightHandle.getObject() ) ) {
+                            tuple.setMatch( rightHandle );
+                            break;
+                        }
+                    }
+                    
+                    // if there is now no new tuple match then propagate assert.
+                    if ( tuple.getMatch() == null ) {
+                        this.sink.propagateRetractTuple( tuple,
+                                                        context,
+                                                        workingMemory );
+                    }                    
                 }
+                
             }
         }
     }
@@ -214,7 +225,7 @@
             return;
         }
 
-        if ( tuple.getMatches() > 0 ) {
+        if ( tuple.getMatch() !=  null) {
             this.sink.propagateRetractTuple( tuple,
                                              context,
                                              workingMemory );
@@ -232,7 +243,7 @@
 
         final Iterator tupleIter = memory.getTupleMemory().iterator();
         for ( ReteTuple tuple = (ReteTuple) tupleIter.next(); tuple != null; tuple = (ReteTuple) tupleIter.next() ) {
-            if ( tuple.getMatches() > 0 ) {
+            if ( tuple.getMatch() != null ) {
                 sink.assertTuple( new ReteTuple( tuple ),
                                   context,
                                   workingMemory );

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/NotNode.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/NotNode.java	2007-11-26 19:00:06 UTC (rev 16841)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/NotNode.java	2007-11-26 19:06:21 UTC (rev 16842)
@@ -96,17 +96,16 @@
         final Iterator it = memory.getFactHandleMemory().iterator( leftTuple );
         this.constraints.updateFromTuple( workingMemory,
                                           leftTuple );
-        int matches = 0;
+        
         for ( FactEntry entry = (FactEntry) it.next(); entry != null; entry = (FactEntry) it.next() ) {
             final InternalFactHandle handle = entry.getFactHandle();
             if ( this.constraints.isAllowedCachedLeft( handle.getObject() ) ) {
-                matches++;
+                leftTuple.setMatch( handle );
+                break;
             }
         }
 
-        leftTuple.setMatches( matches );
-
-        if ( matches == 0 ) {
+        if ( leftTuple.getMatch() == null ) {
             this.sink.propagateAssertTuple( leftTuple,
                                             context,
                                             workingMemory );
@@ -140,14 +139,11 @@
         this.constraints.updateFromFactHandle( workingMemory,
                                                handle );
         for ( ReteTuple tuple = (ReteTuple) it.next(); tuple != null; tuple = (ReteTuple) it.next() ) {
-            if ( this.constraints.isAllowedCachedRight( tuple ) ) {
-                final int matches = tuple.getMatches();
-                tuple.setMatches( matches + 1 );
-                if ( matches == 0 ) {
+            if ( this.constraints.isAllowedCachedRight( tuple ) &&  tuple.getMatch() == null) {
+                    tuple.setMatch( handle );
                     this.sink.propagateRetractTuple( tuple,
                                                      context,
-                                                     workingMemory );
-                }
+                                                     workingMemory );                    
             }
         }
     }
@@ -178,11 +174,29 @@
                                                handle );
         for ( ReteTuple tuple = (ReteTuple) it.next(); tuple != null; tuple = (ReteTuple) it.next() ) {
             if ( this.constraints.isAllowedCachedRight( tuple ) ) {
-                tuple.setMatches( tuple.getMatches() - 1 );
-                if ( tuple.getMatches() == 0 ) {
-                    this.sink.propagateAssertTuple( tuple,
-                                                    context,
-                                                    workingMemory );
+                
+                if ( tuple.getMatch() == handle ) {
+                    // reset the match                    
+                    tuple.setMatch( null );
+                    
+                    // find next match, remember it and break.
+                    final Iterator tupleIt = memory.getFactHandleMemory().iterator( tuple );
+                    this.constraints.updateFromTuple( workingMemory, tuple );
+                    
+                    for ( FactEntry entry = (FactEntry) tupleIt.next(); entry != null; entry = (FactEntry) tupleIt.next() ) {
+                        final InternalFactHandle rightHandle = entry.getFactHandle();
+                        if ( this.constraints.isAllowedCachedLeft( rightHandle.getObject() ) ) {
+                            tuple.setMatch( rightHandle );
+                            break;
+                        }
+                    }
+                    
+                    // if there is now no new tuple match then propagate assert.
+                    if ( tuple.getMatch() == null ) {
+                        this.sink.propagateAssertTuple( tuple,
+                                                        context,
+                                                        workingMemory );
+                    }                    
                 }
             }
         }
@@ -210,7 +224,7 @@
             return;
         }
 
-        if ( tuple.getMatches() == 0 ) {
+        if ( tuple.getMatch() ==  null) {
             this.sink.propagateRetractTuple( tuple,
                                              context,
                                              workingMemory );
@@ -227,7 +241,7 @@
 
         final Iterator tupleIter = memory.getTupleMemory().iterator();
         for ( ReteTuple tuple = (ReteTuple) tupleIter.next(); tuple != null; tuple = (ReteTuple) tupleIter.next() ) {
-            if ( tuple.getMatches() == 0 ) {
+            if ( tuple.getMatch() == null ) {
                 sink.assertTuple( new ReteTuple( tuple ),
                                   context,
                                   workingMemory );

Modified: labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
===================================================================
--- labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java	2007-11-26 19:00:06 UTC (rev 16841)
+++ labs/jbossrules/branches/4.0.x/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java	2007-11-26 19:06:21 UTC (rev 16842)
@@ -29,17 +29,15 @@
     private int                      hashCode;
 
     private boolean                  fieldIndexed;
+    
+    private InternalFactHandle       match;
 
-    private int                      matches;
-
     private Entry                    next;
 
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
     public ReteTuple(final InternalFactHandle handle) {
-        this.index = 0;
-        this.parent = null;
         this.recency = handle.getRecency();
         this.handle = handle;
         int h = handle.hashCode();
@@ -91,14 +89,6 @@
         this.fieldIndexed = fieldIndexed;
     }
 
-    public int getMatches() {
-        return this.matches;
-    }
-
-    public void setMatches(final int matches) {
-        this.matches = matches;
-    }
-
     public InternalFactHandle getLastHandle() {
         return this.handle;
     }
@@ -128,7 +118,16 @@
     public long getRecency() {
         return this.recency;
     }
+        
 
+    public InternalFactHandle getMatch() {
+        return match;
+    }
+
+    public void setMatch(InternalFactHandle match) {
+        this.match = match;
+    }
+
     public void setActivation(final Activation activation) {
         this.activation = activation;
     }




More information about the jboss-svn-commits mailing list