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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Oct 21 13:23:01 EDT 2006


Author: mark.proctor at jboss.com
Date: 2006-10-21 13:22:58 -0400 (Sat, 21 Oct 2006)
New Revision: 7002

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ActivationGroupImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultBetaConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DoubleBetaConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SingleBetaConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java
Log:
JBRULES-526 Implement a CompositeIndex for Facts
-'from' cannot index, so need to be able to turn indexing off

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ActivationGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ActivationGroupImpl.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ActivationGroupImpl.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -34,8 +34,8 @@
         activation.setActivationGroupNode( null );
     }
 
-    public Iterator iterator() {
-        return this.list.iterator();
+    public java.util.Iterator iterator() {
+        return this.list.javaUtilIterator();
     }
 
     public boolean isEmpty() {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -373,7 +373,7 @@
      */
     public void clearActivationGroup(final ActivationGroup activationGroup) {
         final EventSupport eventsupport = (EventSupport) this.workingMemory;
-        final Iterator it = activationGroup.iterator();
+        final java.util.Iterator it = activationGroup.iterator();
         for ( ActivationGroupNode node = (ActivationGroupNode) it.next() ; node != null; node = (ActivationGroupNode) it.next()) {
             final Activation activation = node.getActivation();
             activation.setActivationGroupNode( null );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultBetaConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultBetaConstraints.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultBetaConstraints.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -17,6 +17,9 @@
  */
 
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 import org.drools.base.evaluators.Operator;
 import org.drools.reteoo.BetaMemory;
@@ -46,28 +49,40 @@
 
     private ContextEntry      contexts;
 
-    private boolean           indexed;
+    private int               indexed;
 
-    public DefaultBetaConstraints(final BetaNodeFieldConstraint constraint) {
-        this( new BetaNodeFieldConstraint[]{constraint} );
+    public DefaultBetaConstraints(final BetaNodeFieldConstraint[] constraints) {
+        this( constraints, true );
     }
 
-    public DefaultBetaConstraints(final BetaNodeFieldConstraint[] constraints) {
+    public DefaultBetaConstraints(final BetaNodeFieldConstraint[] constraints, boolean index ) {
         this.constraints = new LinkedList();
         ContextEntry current = null;
+
+        // First create a LinkedList of constraints, with the indexed constraints first.
         for ( int i = 0, length = constraints.length; i < length; i++ ) {
             // Determine  if this constraint is indexable
-            // it is only indexable if there is already no indexed constraints
-            // An indexed constraint is always the first constraint
-            if ( isIndexable( constraints[i] ) && !this.indexed ) {
-                this.constraints.insertAfter( null,
-                                              new LinkedListEntry( constraints[i] ) );
-                this.indexed = true;
+            if ( index && isIndexable( constraints[i] ) ) {
+                if ( indexed == 0 ) {
+                    // first index, so just add to the front
+                    this.constraints.insertAfter( null,
+                                                  new LinkedListEntry( constraints[i] ) );
+                    indexed++;
+                } else {
+                    // insert this index after  the previous index
+                    this.constraints.insertAfter( findNode( indexed++ ),
+                                                  new LinkedListEntry( constraints[i] ) );
+                }
             } else {
+                // not indexed, so just add to the  end
                 this.constraints.add( new LinkedListEntry( constraints[i] ) );
             }
-            //Setup  the  contextEntry cache to be iterated  in the same order
-            final ContextEntry context = constraints[i].getContextEntry();
+        }
+
+        // Now create the ContextEntries  in the same order the constraints
+        for ( LinkedListEntry entry = (LinkedListEntry) this.constraints.getFirst(); entry != null; entry = (LinkedListEntry) entry.getNext() ) {
+            BetaNodeFieldConstraint constraint = (BetaNodeFieldConstraint) entry.getObject();
+            final ContextEntry context = constraint.getContextEntry();
             if ( current == null ) {
                 current = context;
                 this.contexts = context;
@@ -78,6 +93,14 @@
         }
     }
 
+    private LinkedListEntry findNode(int pos) {
+        LinkedListEntry current = (LinkedListEntry) this.constraints.getFirst();
+        for ( int i = 0; i < pos; i++ ) {
+            current = (LinkedListEntry) current.getNext();
+        }
+        return current;
+    }
+
     private boolean isIndexable(final BetaNodeFieldConstraint constraint) {
         if ( constraint.getClass() == VariableConstraint.class ) {
             final VariableConstraint variableConstraint = (VariableConstraint) constraint;
@@ -90,18 +113,22 @@
     /* (non-Javadoc)
      * @see org.drools.common.BetaNodeConstraints#updateFromTuple(org.drools.reteoo.ReteTuple)
      */
-    public void updateFromTuple(final InternalWorkingMemory workingMemory, final ReteTuple tuple) {
+    public void updateFromTuple(final InternalWorkingMemory workingMemory,
+                                final ReteTuple tuple) {
         for ( ContextEntry context = this.contexts; context != null; context = context.getNext() ) {
-            context.updateFromTuple( workingMemory, tuple );
+            context.updateFromTuple( workingMemory,
+                                     tuple );
         }
     }
 
     /* (non-Javadoc)
      * @see org.drools.common.BetaNodeConstraints#updateFromFactHandle(org.drools.common.InternalFactHandle)
      */
-    public void updateFromFactHandle(final InternalWorkingMemory workingMemory, final InternalFactHandle handle) {
+    public void updateFromFactHandle(final InternalWorkingMemory workingMemory,
+                                     final InternalFactHandle handle) {
         for ( ContextEntry context = this.contexts; context != null; context = context.getNext() ) {
-            context.updateFromFactHandle( workingMemory, handle );
+            context.updateFromFactHandle( workingMemory,
+                                          handle );
         }
     }
 
@@ -140,7 +167,7 @@
     }
 
     public boolean isIndexed() {
-        return this.indexed;
+        return this.indexed > 0;
     }
 
     public boolean isEmpty() {
@@ -149,13 +176,21 @@
 
     public BetaMemory createBetaMemory() {
         BetaMemory memory;
-        if ( this.indexed ) {
-            final Constraint constraint = (Constraint) this.constraints.getFirst();
-            final VariableConstraint variableConstraint = (VariableConstraint) constraint;
-            final FieldIndex index = new FieldIndex( variableConstraint.getFieldExtractor(),
-                                               variableConstraint.getRequiredDeclarations()[0] );
+        if ( this.indexed > 0 ) {
+            LinkedListEntry entry = (LinkedListEntry) this.constraints.getFirst();
+            List list = new ArrayList();
+            
+            for ( int pos = 0; pos < this.indexed; pos++ ) {
+                final Constraint constraint = (Constraint) entry.getNext();
+                final VariableConstraint variableConstraint = (VariableConstraint) constraint;
+                final FieldIndex index = new FieldIndex( variableConstraint.getFieldExtractor(),
+                                                         variableConstraint.getRequiredDeclarations()[0] );
+                list.add( index );
+            }
+            
+            FieldIndex[] indexes = ( FieldIndex[] ) list.toArray( new FieldIndex[ list.size() ] );
             memory = new BetaMemory( new TupleHashTable(),
-                                     new FieldIndexHashTable( new FieldIndex[]{index} ) );
+                                     new FieldIndexHashTable( indexes ) );
         } else {
             memory = new BetaMemory( new TupleHashTable(),
                                      new FactHashTable() );
@@ -164,17 +199,6 @@
         return memory;
     }
 
-    //    public Set getRequiredDeclarations() {
-    //        final Set declarations = new HashSet();
-    //        for ( int i = 0; i < this.constraints.length; i++ ) {
-    //            final Declaration[] array = this.constraints[i].getRequiredDeclarations();
-    //            for ( int j = 0; j < array.length; j++ ) {
-    //                declarations.add( array[j] );
-    //            }
-    //        }
-    //        return declarations;
-    //    }
-
     public int hashCode() {
         return this.constraints.hashCode();
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DoubleBetaConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DoubleBetaConstraints.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DoubleBetaConstraints.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -53,8 +53,12 @@
     private boolean                       indexed1;
 
     public DoubleBetaConstraints(final BetaNodeFieldConstraint[] constraints) {
-        final boolean i0 = isIndexable( constraints[0] );
-        final boolean i1 = isIndexable( constraints[1] );
+        this( constraints, true );
+    }
+    
+    public DoubleBetaConstraints(final BetaNodeFieldConstraint[] constraints, boolean index) {
+        final boolean i0 = index && isIndexable( constraints[0] );
+        final boolean i1 = index && isIndexable( constraints[1] );
 
         if ( i0 ) {
             this.indexed0 = true;
@@ -157,17 +161,6 @@
         return memory;
     }
 
-    //    public Set getRequiredDeclarations() {
-    //        final Set declarations = new HashSet();
-    //        for ( int i = 0; i < this.constraints.length; i++ ) {
-    //            final Declaration[] array = this.constraints[i].getRequiredDeclarations();
-    //            for ( int j = 0; j < array.length; j++ ) {
-    //                declarations.add( array[j] );
-    //            }
-    //        }
-    //        return declarations;
-    //    }
-
     public int hashCode() {
         return this.constraint0.hashCode() ^ this.constraint0.hashCode();
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SingleBetaConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SingleBetaConstraints.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SingleBetaConstraints.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -48,9 +48,13 @@
     private boolean                       indexed;
 
     public SingleBetaConstraints(final BetaNodeFieldConstraint constraint) {
+        this( constraint, true );
+    }
+    
+    public SingleBetaConstraints(final BetaNodeFieldConstraint constraint, boolean index ) {
         this.constraint = constraint;
         // Determine  if this constraint is indexable
-        if ( isIndexable( constraint ) ) {
+        if ( index  && isIndexable( constraint ) ) {
             this.indexed = true;
         }
 
@@ -120,17 +124,6 @@
         return memory;
     }
 
-    //    public Set getRequiredDeclarations() {
-    //        final Set declarations = new HashSet();
-    //        for ( int i = 0; i < this.constraints.length; i++ ) {
-    //            final Declaration[] array = this.constraints[i].getRequiredDeclarations();
-    //            for ( int j = 0; j < array.length; j++ ) {
-    //                declarations.add( array[j] );
-    //            }
-    //        }
-    //        return declarations;
-    //    }
-
     public int hashCode() {
         return this.constraint.hashCode();
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java	2006-10-21 15:28:13 UTC (rev 7001)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java	2006-10-21 17:22:58 UTC (rev 7002)
@@ -56,33 +56,13 @@
     private boolean                       indexed2;
 
     public TripleBetaConstraints(final BetaNodeFieldConstraint[] constraints) {
-        //        // find the first instrumental
-        //        for ( int i = 0, length = constraints.length; i < length; i++ ) {
-        //            if ( isIndexable( constraints[i] ) ) {
-        //                if ( i > 0) {
-        //                    // swap the constraint to the first position
-        //                    BetaNodeFieldConstraint temp = constraints[0];
-        //                    constraints[0] = constraints[i];
-        //                    constraints[i] = temp;
-        //                }
-        //                this.indexed = true;
-        //                break;
-        //            }
-        //        }
-        //
-        //        this.constraint0 = constraints[0];
-        //        this.context0 = this.constraint0.getContextEntry();
-        //
-        //        this.constraint1 = constraints[1];
-        //        this.context1 = this.constraint1.getContextEntry();
-        //        
-        //        this.constraint2 = constraints[2];
-        //        this.context2 = this.constraint2.getContextEntry();     
+        this( constraints, true );
+    }
+    public TripleBetaConstraints(final BetaNodeFieldConstraint[] constraints, boolean index) {
+        final boolean i0 = index && isIndexable( constraints[0] );
+        final boolean i1 = index && isIndexable( constraints[1] );
+        final boolean i2 = index && isIndexable( constraints[2] );
 
-        final boolean i0 = isIndexable( constraints[0] );
-        final boolean i1 = isIndexable( constraints[1] );
-        final boolean i2 = isIndexable( constraints[2] );
-
         if ( i0 ) {
             this.indexed0 = true;
             if ( i1 ) {
@@ -180,18 +160,6 @@
     }
 
     public BetaMemory createBetaMemory() {
-        //        BetaMemory memory;
-        //        if ( this.indexed ) {
-        //            VariableConstraint variableConstraint = (VariableConstraint) this.constraint0;
-        //            Index  index  = new  Index(variableConstraint.getFieldExtractor(), variableConstraint.getRequiredDeclarations()[0]);
-        //            memory = new BetaMemory( new TupleHashTable(),
-        //                                     new CompositeFieldIndexHashTable( new Index[] { index }  ) );
-        //        } else {
-        //            memory = new BetaMemory( new TupleHashTable(),
-        //                                     new FactHashTable() );
-        //        }
-        //
-        //        return memory;
 
         BetaMemory memory;
 
@@ -230,17 +198,6 @@
         return memory;
     }
 
-    //    public Set getRequiredDeclarations() {
-    //        final Set declarations = new HashSet();
-    //        for ( int i = 0; i < this.constraints.length; i++ ) {
-    //            final Declaration[] array = this.constraints[i].getRequiredDeclarations();
-    //            for ( int j = 0; j < array.length; j++ ) {
-    //                declarations.add( array[j] );
-    //            }
-    //        }
-    //        return declarations;
-    //    }
-
     public int hashCode() {
         return this.constraint0.hashCode() ^ this.constraint0.hashCode() ^ this.constraint0.hashCode();
     }




More information about the jboss-svn-commits mailing list