[jboss-svn-commits] JBL Code SVN: r12979 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/base and 10 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Jul 1 18:42:29 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-07-01 18:42:28 -0400 (Sun, 01 Jul 2007)
New Revision: 12979

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupFactory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroupFactory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PriorityQueueAgendaGroupFactory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SequentialAgendaGroupImpl.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/common/SequentialAgendaTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooBuilderTest.java
Removed:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatefulSession.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatelessSession.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.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/InternalRuleBase.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/QuadroupleBetaConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/PrimitiveLongMap.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Queueable.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/BaseQueueable.java
Log:
JBRULES-947 sequential rete
-Initial sequential implementation

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -23,6 +23,9 @@
 import java.util.Map;
 import java.util.Properties;
 
+import org.drools.common.AgendaGroupFactory;
+import org.drools.common.ArrayAgendaGroupFactory;
+import org.drools.common.PriorityQueueAgendaGroupFactory;
 import org.drools.concurrent.ExecutorService;
 import org.drools.spi.ConflictResolver;
 import org.drools.util.ChainedProperties;
@@ -403,6 +406,14 @@
         }
     }
 
+    public AgendaGroupFactory getAgendaGroupFactory() {
+        if ( isSequential() ) {
+            return ArrayAgendaGroupFactory.getInstance();
+        } else {
+            return PriorityQueueAgendaGroupFactory.getInstance();
+        }
+    }
+    
     private static ConflictResolver determineConflictResolver(String className) {
         Class clazz = null;
         try {
@@ -495,7 +506,6 @@
             List list = (List) object;
             return !list.contains( name );
         }
-
     }
 
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatefulSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatefulSession.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatefulSession.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,6 @@
 package org.drools;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.drools.concurrent.Future;
@@ -11,13 +12,15 @@
  * 
  * Caution should be used when using the async methods (take not of the javadocs for specific methods). 
  */
-public interface StatefulSession extends WorkingMemory {
-    
+public interface StatefulSession
+    extends
+    WorkingMemory {
+
     /**
      * Forces the workingMemory to be derefenced from
      * 
      */
-    void dispose();    
+    void dispose();
 
     /**
      * Insert/Assert an object asynchronously.
@@ -27,10 +30,13 @@
      * insertion task to take effect (a new thread may be created). 
      * If you are not sure, then you probably don't need to use it !
      */
-    Future asyncInsert(Object object);    
-    Future asyncRetract(FactHandle factHandle);   
-    Future asyncUpdate(FactHandle factHandle, Object object);
+    Future asyncInsert(Object object);
 
+    Future asyncRetract(FactHandle factHandle);
+
+    Future asyncUpdate(FactHandle factHandle,
+                       Object object);
+
     /**
      * Insert/Assert an array of objects..
      * (return immediately, even while the insertion is taking effect).
@@ -38,31 +44,31 @@
      * You should only use the async methods if you are sure you require a background 
      * insertion task to take effect (a new thread may be created). 
      * If you are not sure, then you probably don't need to use it !
-     */    
-    Future asyncInsert(Object[] list);
-    
+     */
+    Future asyncInsert(Object[] array);
+
     /**
-     * Insert/Assert a list of objects..
+     * Insert/Assert a collect of objects..
      * (return immediately, even while the insertion is taking effect).
      * The returned Future object can be queried to check on the status of the task.
      * You should only use the async methods if you are sure you require a background 
      * insertion task to take effect (a new thread may be created). 
      * If you are not sure, then you probably don't need to use it !
-     */        
-    Future asyncInsert(List list);
-    
+     */
+    Future asyncInsert(Collection collect);
+
     /**
      * This will initiate the firing phase (in the background). 
      * And return immediately. The returned Future object can be queried
      * to check on the status of the task.
      */
     Future asyncFireAllRules();
-    
+
     /**
      * This will initiate the firing phase (in the background). 
      * And return immediately. The returned Future object can be queried
      * to check on the status of the task.
-     */    
-    Future asyncFireAllRules(AgendaFilter agendaFilter); 
+     */
+    Future asyncFireAllRules(AgendaFilter agendaFilter);
 
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatelessSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatelessSession.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/StatelessSession.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,6 @@
 package org.drools;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.drools.event.AgendaEventListener;
@@ -61,14 +62,14 @@
      * This will assert the list of facts as SEPARATE facts to the engine
      * (NOT an array).
      */    
-    void execute(Object[] list);
+    void execute(Object[] array);
 
     /**
      * Insert a List of facts, an fire the rules, returning when finished.
      * This will assert the list of facts as SEPARATE facts to the engine
      * (NOT as a List).
      */        
-    void execute(List list);    
+    void execute(Collection collection);    
     
     /**
      * This will assert the object in the background. This is
@@ -80,13 +81,13 @@
      * This will assert the object array (as SEPARATE facts) in the background. This is
      * "send and forget" execution.
      */
-    void asyncExecute(Object[] list);
+    void asyncExecute(Object[] array);
 
     /**
      * This will assert the object List (as SEPARATE facts) in the background. This is
      * "send and forget" execution.
      */    
-    void asyncExecute(List list);      
+    void asyncExecute(Collection collection);      
     
     
     /**
@@ -99,11 +100,11 @@
      * Similar to the normal execute method, but this will return
      * "results". 
      */    
-    StatelessSessionResult executeWithResults(Object[] list);
+    StatelessSessionResult executeWithResults(Object[] array);
 
     /**
      * Similar to the normal execute method, but this will return
      * "results". 
      */   
-    StatelessSessionResult executeWithResults(List list);
+    StatelessSessionResult executeWithResults(Collection collection);
 }

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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -162,10 +162,6 @@
     //        this.workingMemory.clearAgendaGroup( group );
     //    }
 
-    public Object get(final Declaration declaration) {
-        return declaration.getValue( workingMemory, this.tuple.get( declaration ).getObject() );
-    }
-
     public Tuple getTuple() {
         return this.tuple;
     }
@@ -194,6 +190,10 @@
     //    public void setFocus(final AgendaGroup focus) {
     //        this.workingMemory.setFocus( focus );
     //    }
+    
+    public Object get(final Declaration declaration) {
+        return declaration.getValue( workingMemory, this.tuple.get( declaration ).getObject() );
+    }    
 
     public Declaration getDeclaration(final String identifier) {
         return (Declaration) this.subrule.getOuterDeclarations().get( identifier );

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,158 @@
+package org.drools.base;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.List;
+
+import org.drools.FactException;
+import org.drools.FactHandle;
+import org.drools.QueryResults;
+import org.drools.WorkingMemory;
+import org.drools.common.InternalWorkingMemoryActions;
+import org.drools.rule.Declaration;
+import org.drools.rule.GroupElement;
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.KnowledgeHelper;
+import org.drools.spi.Tuple;
+
+public class SequentialKnowledgeHelper
+    implements
+    KnowledgeHelper {
+
+    private static final long                  serialVersionUID = 3692991484157215686L;
+
+    private Rule                               rule;
+    private GroupElement                       subrule;
+    private Activation                         activation;
+    private Tuple                              tuple;
+    private final InternalWorkingMemoryActions workingMemory;
+
+    public SequentialKnowledgeHelper(final WorkingMemory workingMemory) {
+        this.workingMemory = (InternalWorkingMemoryActions) workingMemory;
+    }
+
+    public void setActivation(final Activation agendaItem) {
+        this.rule = agendaItem.getRule();
+        this.subrule = agendaItem.getSubRule();
+        this.activation = agendaItem;
+        this.tuple = agendaItem.getTuple();
+    }
+
+    public void insert(final Object object) throws FactException {        
+    }
+
+    public void insert(final Object object,
+                       final boolean dynamic) throws FactException {
+    }
+
+    public void insertLogical(final Object object) throws FactException {
+    }
+
+    public void insertLogical(final Object object,
+                              final boolean dynamic) throws FactException {
+    }
+
+    public void update(final FactHandle handle,
+                       final Object newObject) throws FactException {
+    }
+
+    public void update(final Object object) throws FactException {
+    }
+
+    public void retract(final FactHandle handle) throws FactException {
+    }
+
+    public void retract(final Object object) throws FactException {
+    }
+
+    public void modifyRetract(final Object object) {
+    }
+
+    public void modifyRetract(final FactHandle factHandle) {
+    }
+
+    public void modifyInsert(final Object object) {
+    }
+
+    public void modifyInsert(final FactHandle factHandle,
+                             final Object object) {      
+    }
+
+    public Rule getRule() {
+        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;
+    }
+
+    public WorkingMemory getWorkingMemory() {
+        return this.workingMemory;
+    }
+
+    public Activation getActivation() {
+        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) {
+        return declaration.getValue( workingMemory, this.tuple.get( declaration ).getObject() );
+    }    
+
+    public Declaration getDeclaration(final String identifier) {
+        return (Declaration) this.subrule.getOuterDeclarations().get( identifier );
+    }
+    
+    public void halt() {
+        this.workingMemory.halt();
+    }    
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -678,6 +678,7 @@
         
         if ( isSequential() ) {
             handle = this.handleFactory.newFactHandle( object );
+            this.assertMap.put( handle, handle, false );
             insert(handle, object, rule, activation);
             return handle;
         }

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupFactory.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupFactory.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,7 @@
+package org.drools.common;
+
+import org.drools.spi.AgendaGroup;
+
+public interface AgendaGroupFactory {    
+    AgendaGroup createAgendaGroup(String name, InternalRuleBase ruleBase);
+}
\ No newline at end of file

Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,136 +0,0 @@
-package org.drools.common;
-
-/*
- * Copyright 2005 JBoss Inc
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import org.drools.conflict.DepthConflictResolver;
-import org.drools.spi.Activation;
-import org.drools.spi.AgendaGroup;
-import org.drools.spi.ConflictResolver;
-import org.drools.util.BinaryHeapQueue;
-import org.drools.util.Queueable;
-
-/**
- * <code>AgendaGroup</code> implementation that uses a <code>PriorityQueue</code> to prioritise the evaluation of added
- * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a <code>Map</code> of <code>ActivationQueues</code> 
- * for requested salience values.
- * 
- * @see PriorityQueue
- * @see ActivationQueue
- * 
- * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
- * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
- *
- */
-public class AgendaGroupImpl
-    implements
-    AgendaGroup {
-
-    private static final long     serialVersionUID = 320L;
-
-    private final String          name;
-
-    /** Items in the agenda. */
-    private final BinaryHeapQueue queue;
-
-    private boolean               active;
-
-    /**
-     * Construct an <code>AgendaGroup</code> with the given name.
-     * 
-     * @param name
-     *      The <AgendaGroup> name.
-     */
-    
-    
-    public AgendaGroupImpl(final String name, final ConflictResolver conflictResolver) {
-        this.name = name;
-        this.queue = new BinaryHeapQueue( conflictResolver );
-    }    
-
-    /* (non-Javadoc)
-     * @see org.drools.spi.AgendaGroup#getName()
-     */
-    public String getName() {
-        return this.name;
-    }
-
-    public void clear() {
-        this.queue.clear();
-    }
-
-    /* (non-Javadoc)
-     * @see org.drools.spi.AgendaGroup#size()
-     */
-    public int size() {
-        return this.queue.size();
-    }
-
-    public void add(final Activation activation) {
-        this.queue.enqueue( (Queueable) activation );
-    }
-
-    public Activation getNext() {
-        return (Activation) this.queue.dequeue();
-    }
-
-    public boolean isActive() {
-        return this.active;
-    }
-
-    public void setActive(final boolean activate) {
-        this.active = activate;
-    }
-
-    /**
-     * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
-     * otherwise it returns false;
-     * 
-     * @param priorityQueue
-     * @return
-     */
-    public boolean isEmpty() {
-        return this.queue.isEmpty();
-    }
-
-    public Activation[] getActivations() {
-        return (Activation[]) this.queue.toArray( new AgendaItem[this.queue.size()] );
-    }
-
-    public Queueable[] getQueueable() {
-        return this.queue.getQueueable();
-    }
-
-    public String toString() {
-        return "AgendaGroup '" + this.name + "'";
-    }
-
-    public boolean equal(final Object object) {
-        if ( (object == null) || !(object instanceof AgendaGroupImpl) ) {
-            return false;
-        }
-
-        if ( ((AgendaGroupImpl) object).name.equals( this.name ) ) {
-            return true;
-        }
-
-        return false;
-    }
-
-    public int hashCode() {
-        return this.name.hashCode();
-    }
-}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -53,10 +53,13 @@
 
     /** The rule. */
     private final Rule               rule;
-    
+
     /** The salience */
     private final int                salience;
 
+    /** Used for sequential mode */
+    private int                      sequenence;
+
     /** The subrule */
     private final GroupElement       subrule;
 
@@ -75,7 +78,7 @@
 
     private boolean                  activated;
 
-    private AgendaGroupImpl          agendaGroup;
+    private InternalAgendaGroup      agendaGroup;
 
     private ActivationGroupNode      activationGroupNode;
 
@@ -104,7 +107,7 @@
         this.rule = rule;
         this.salience = salience;
         this.subrule = subrule;
-        this.activationNumber = activationNumber;        
+        this.activationNumber = activationNumber;
     }
 
     // ------------------------------------------------------------
@@ -131,11 +134,19 @@
     public Tuple getTuple() {
         return this.tuple;
     }
-    
+
     public int getSalience() {
         return this.salience;
     }
 
+    public int getSequenence() {
+        return sequenence;
+    }
+
+    public void setSequenence(int sequenence) {
+        this.sequenence = sequenence;
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -156,7 +167,7 @@
     public LinkedList getLogicalDependencies() {
         return this.justified;
     }
-    
+
     public void setLogicalDependencies(LinkedList justified) {
         this.justified = justified;
     }
@@ -231,7 +242,7 @@
         return this.agendaGroup;
     }
 
-    public void setAgendaGroup(final AgendaGroupImpl agendaGroup) {
+    public void setAgendaGroup(final InternalAgendaGroup agendaGroup) {
         this.agendaGroup = agendaGroup;
     }
 

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,187 @@
+package org.drools.common;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.drools.conflict.DepthConflictResolver;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.ConflictResolver;
+import org.drools.util.BinaryHeapQueue;
+import org.drools.util.LinkedList;
+import org.drools.util.LinkedListEntry;
+import org.drools.util.PrimitiveLongMap;
+import org.drools.util.Queueable;
+import org.drools.util.LinkedList.LinkedListIterator;
+
+/**
+ * <code>AgendaGroup</code> implementation that uses a <code>PriorityQueue</code> to prioritise the evaluation of added
+ * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a <code>Map</code> of <code>ActivationQueues</code> 
+ * for requested salience values.
+ * 
+ * @see PriorityQueue
+ * @see ActivationQueue
+ * 
+ * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
+ * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
+ *
+ */
+public class ArrayAgendaGroup
+    implements
+    InternalAgendaGroup {
+
+    private static final long serialVersionUID = 320L;
+
+    private final String      name;
+
+    /** Items in the agenda. */
+    private LinkedList[]      array;
+
+    private boolean           active;
+
+    private int               size;
+
+    private int               index;
+
+    /**
+     * Construct an <code>AgendaGroup</code> with the given name.
+     * 
+     * @param name
+     *      The <AgendaGroup> name.
+     */
+
+    public ArrayAgendaGroup(final String name,
+                            final InternalRuleBase ruleBase) {
+        this.name = name;
+        Integer integer = (Integer) ruleBase.getAgendaGroupRuleTotals().get( name );
+        if ( integer == null ) {
+            this.array = new LinkedList[0];
+        } else {
+            this.array = new LinkedList[integer.intValue()];    
+        }
+        
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#getName()
+     */
+    public String getName() {
+        return this.name;
+    }
+
+    public void clear() {
+        this.array = new LinkedList[this.array.length];
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#size()
+     */
+    public int size() {
+        return this.size;
+    }
+
+    public void add(final Activation activation) {
+        AgendaItem item = (AgendaItem) activation;
+        this.size++;
+
+        LinkedList list = this.array[item.getSequenence()];
+        if ( list == null ) {
+            list = new LinkedList();
+            this.array[item.getSequenence()] = list;
+        }
+        
+        list.add( new LinkedListEntry( activation ) );
+    }
+
+    public Activation getNext() {
+        Activation activation = null;
+        int length = this.array.length;
+        while ( this.index < length ) {
+            LinkedList list = this.array[this.index];            
+            if ( list != null ) {
+                activation = (Activation) ((LinkedListEntry)list.removeFirst()).getObject();
+                if ( list.isEmpty()) {
+                    this.array[this.index++] = null;
+                }
+                this.size--;
+                break;
+            }
+            this.index++;
+        }
+        return (Activation) activation;
+    }
+
+    public boolean isActive() {
+        return this.active;
+    }
+
+    public void setActive(final boolean activate) {
+        this.active = activate;
+    }
+
+    /**
+     * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
+     * otherwise it returns false;
+     * 
+     * @param priorityQueue
+     * @return
+     */
+    public boolean isEmpty() {
+        return this.size == 0;
+    }
+
+    public Activation[] getActivations() {
+        Activation[] activations = new Activation[this.size];
+        int j = 0;
+        for ( int i = 0; i < this.array.length; i++ ) {;
+            LinkedList list = this.array[i];
+            if ( list != null ) {
+                LinkedListIterator it = list.iterator();
+                Activation activation = ( Activation ) ((LinkedListEntry)it.next()).getObject();
+                while ( activation != null) {
+                    activations[j++] = activation;
+                    activation = ( Activation ) it.next();
+                }
+            }
+            
+        }
+        return activations;
+    }
+
+    public Activation[] getQueue() {
+        return getActivations();
+    }
+
+    public String toString() {
+        return "AgendaGroup '" + this.name + "'";
+    }
+
+    public boolean equal(final Object object) {
+        if ( (object == null) || !(object instanceof ArrayAgendaGroup) ) {
+            return false;
+        }
+
+        if ( ((ArrayAgendaGroup) object).name.equals( this.name ) ) {
+            return true;
+        }
+
+        return false;
+    }
+
+    public int hashCode() {
+        return this.name.hashCode();
+    }
+}

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroupFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroupFactory.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ArrayAgendaGroupFactory.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,16 @@
+package org.drools.common;
+
+import org.drools.spi.AgendaGroup;
+
+public class ArrayAgendaGroupFactory implements AgendaGroupFactory {
+    private static final AgendaGroupFactory INSTANCE = new ArrayAgendaGroupFactory();
+    
+    public static AgendaGroupFactory getInstance() {
+        return INSTANCE;
+    }    
+    
+    public AgendaGroup createAgendaGroup(String name, InternalRuleBase ruleBase) {
+        return new ArrayAgendaGroup( name,
+                                     ruleBase );        
+    }
+}

Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java (from rev 12876, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,136 @@
+package org.drools.common;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.drools.conflict.DepthConflictResolver;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.ConflictResolver;
+import org.drools.util.BinaryHeapQueue;
+import org.drools.util.Queueable;
+
+/**
+ * <code>AgendaGroup</code> implementation that uses a <code>PriorityQueue</code> to prioritise the evaluation of added
+ * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a <code>Map</code> of <code>ActivationQueues</code> 
+ * for requested salience values.
+ * 
+ * @see PriorityQueue
+ * @see ActivationQueue
+ * 
+ * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
+ * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
+ *
+ */
+public class BinaryHeapQueueAgendaGroup
+    implements
+    InternalAgendaGroup {
+
+    private static final long     serialVersionUID = 320L;
+
+    private final String          name;
+
+    /** Items in the agenda. */
+    private final BinaryHeapQueue queue;
+
+    private boolean               active;
+
+    /**
+     * Construct an <code>AgendaGroup</code> with the given name.
+     * 
+     * @param name
+     *      The <AgendaGroup> name.
+     */
+    
+    
+    public BinaryHeapQueueAgendaGroup(final String name, final InternalRuleBase ruleBase) {
+        this.name = name;
+        this.queue = new BinaryHeapQueue( ruleBase.getConfiguration().getConflictResolver() );
+    }    
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#getName()
+     */
+    public String getName() {
+        return this.name;
+    }
+
+    public void clear() {
+        this.queue.clear();
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#size()
+     */
+    public int size() {
+        return this.queue.size();
+    }
+
+    public void add(final Activation activation) {
+        this.queue.enqueue( (Queueable) activation );
+    }
+
+    public Activation getNext() {
+        return (Activation) this.queue.dequeue();
+    }
+
+    public boolean isActive() {
+        return this.active;
+    }
+
+    public void setActive(final boolean activate) {
+        this.active = activate;
+    }
+
+    /**
+     * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
+     * otherwise it returns false;
+     * 
+     * @param priorityQueue
+     * @return
+     */
+    public boolean isEmpty() {
+        return this.queue.isEmpty();
+    }
+
+    public Activation[] getActivations() {
+        return (Activation[]) this.queue.toArray( new AgendaItem[this.queue.size()] );
+    }
+
+    public Activation[] getQueue() {
+        return this.queue.getQueueable();
+    }
+
+    public String toString() {
+        return "AgendaGroup '" + this.name + "'";
+    }
+
+    public boolean equal(final Object object) {
+        if ( (object == null) || !(object instanceof BinaryHeapQueueAgendaGroup) ) {
+            return false;
+        }
+
+        if ( ((BinaryHeapQueueAgendaGroup) object).name.equals( this.name ) ) {
+            return true;
+        }
+
+        return false;
+    }
+
+    public int hashCode() {
+        return this.name.hashCode();
+    }
+}

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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -27,12 +27,14 @@
 
 import org.drools.WorkingMemory;
 import org.drools.base.DefaultKnowledgeHelper;
+import org.drools.base.SequentialKnowledgeHelper;
 import org.drools.spi.Activation;
 import org.drools.spi.ActivationGroup;
 import org.drools.spi.AgendaFilter;
 import org.drools.spi.AgendaGroup;
 import org.drools.spi.ConflictResolver;
 import org.drools.spi.ConsequenceException;
+import org.drools.spi.KnowledgeHelper;
 import org.drools.spi.RuleFlowGroup;
 import org.drools.util.LinkedListNode;
 import org.drools.util.Queueable;
@@ -82,11 +84,11 @@
 
     private final LinkedList            focusStack;
 
-    private AgendaGroupImpl             currentModule;
+    private AgendaGroup                 currentModule;
 
     private final AgendaGroup           main;
 
-    private DefaultKnowledgeHelper      knowledgeHelper;
+    private KnowledgeHelper      knowledgeHelper;
 
     public int                          activeActivations;
 
@@ -106,15 +108,19 @@
      */
     public DefaultAgenda(final InternalWorkingMemory workingMemory) {
         this.workingMemory = workingMemory;
-        this.knowledgeHelper = new DefaultKnowledgeHelper( this.workingMemory );
+        if ( ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().isSequential() ) {
+            this.knowledgeHelper = new SequentialKnowledgeHelper( this.workingMemory );
+        } else {
+            this.knowledgeHelper = new DefaultKnowledgeHelper( this.workingMemory ); 
+        }
         this.agendaGroups = new HashMap();
         this.activationGroups = new HashMap();
         this.ruleFlowGroups = new HashMap();
         this.focusStack = new LinkedList();
 
         // MAIN should always be the first AgendaGroup and can never be removed
-        this.main = new AgendaGroupImpl( AgendaGroup.MAIN,
-                                         ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getConflictResolver() );
+        AgendaGroupFactory factory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
+        this.main = factory.createAgendaGroup( AgendaGroup.MAIN, ((InternalRuleBase) this.workingMemory.getRuleBase()) );        
 
         this.agendaGroups.put( AgendaGroup.MAIN,
                                this.main );
@@ -169,9 +175,9 @@
     public boolean setFocus(final AgendaGroup agendaGroup) {
         // Set the focus to the agendaGroup if it doesn't already have the focus
         if ( this.focusStack.getLast() != agendaGroup ) {
-            ((AgendaGroupImpl) this.focusStack.getLast()).setActive( false );
+            ((InternalAgendaGroup) this.focusStack.getLast()).setActive( false );
             this.focusStack.add( agendaGroup );
-            ((AgendaGroupImpl) agendaGroup).setActive( true );
+            ((InternalAgendaGroup) agendaGroup).setActive( true );
             final EventSupport eventsupport = (EventSupport) this.workingMemory;
             eventsupport.getAgendaEventSupport().fireAgendaGroupPushed( agendaGroup );
             return true;
@@ -199,10 +205,10 @@
      * @see org.drools.common.AgendaI#getNextFocus()
      */
     public AgendaGroup getNextFocus() {
-        AgendaGroupImpl agendaGroup = null;
+        InternalAgendaGroup agendaGroup = null;
         // Iterate untill we find a populate AgendaModule or we reach the MAIN, default, AgendaGroup
         while ( true ) {
-            agendaGroup = (AgendaGroupImpl) this.focusStack.getLast();
+            agendaGroup = (InternalAgendaGroup) this.focusStack.getLast();
 
             final boolean empty = agendaGroup.isEmpty();
 
@@ -228,7 +234,7 @@
      * @see org.drools.common.AgendaI#setCurrentAgendaGroup(org.drools.spi.AgendaGroup)
      */
     public void setCurrentAgendaGroup(final AgendaGroup agendaGroup) {
-        this.currentModule = (AgendaGroupImpl) agendaGroup;
+        this.currentModule = agendaGroup;
     }
 
     /* (non-Javadoc)
@@ -246,8 +252,8 @@
         if ( agendaGroup == null ) {
             // The AgendaGroup is defined but not yet added to the
             // Agenda, so create the AgendaGroup and add to the Agenda.
-            agendaGroup = new AgendaGroupImpl( name,
-                                               ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getConflictResolver() );
+            AgendaGroupFactory factory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
+            agendaGroup = factory.createAgendaGroup( name, ((InternalRuleBase) this.workingMemory.getRuleBase()) );
             addAgendaGroup( agendaGroup );
         }
         return agendaGroup;
@@ -305,7 +311,7 @@
     public int focusStackSize() {
         int size = 0;
         for ( final java.util.Iterator iterator = this.focusStack.iterator(); iterator.hasNext(); ) {
-            final AgendaGroup group = (AgendaGroupImpl) iterator.next();
+            final AgendaGroup group = (AgendaGroup) iterator.next();
             size += group.size();
         }
         return size;
@@ -317,7 +323,7 @@
     public int agendaSize() {
         int size = 0;
         for ( final java.util.Iterator iterator = this.agendaGroups.values().iterator(); iterator.hasNext(); ) {
-            final AgendaGroup group = (AgendaGroupImpl) iterator.next();
+            final AgendaGroup group = (AgendaGroup) iterator.next();
             size += group.size();
         }
         return size;
@@ -352,7 +358,7 @@
     public void clearAgenda() {
         // Cancel all items and fire a Cancelled event for each Activation
         for ( final java.util.Iterator agendaGroupIterator = this.agendaGroups.values().iterator(); agendaGroupIterator.hasNext(); ) {
-            final AgendaGroupImpl group = (AgendaGroupImpl) agendaGroupIterator.next();
+            final AgendaGroup group = (AgendaGroup) agendaGroupIterator.next();
             clearAgendaGroup( group );
         }
 
@@ -370,7 +376,7 @@
      * @see org.drools.common.AgendaI#clearAgendaGroup(java.lang.String)
      */
     public void clearAgendaGroup(final String name) {
-        final AgendaGroupImpl agendaGroup = (AgendaGroupImpl) this.agendaGroups.get( name );
+        final AgendaGroup agendaGroup = (AgendaGroup) this.agendaGroups.get( name );
         if ( agendaGroup != null ) {
             clearAgendaGroup( agendaGroup );
         }
@@ -382,7 +388,7 @@
     public void clearAgendaGroup(final AgendaGroup agendaGroup) {
         final EventSupport eventsupport = (EventSupport) this.workingMemory;
 
-        final Queueable[] queueable = ((AgendaGroupImpl) agendaGroup).getQueueable();
+        final Activation[] queueable = ((InternalAgendaGroup) agendaGroup).getQueue();
         for ( int i = 0, length = queueable.length; i < length; i++ ) {
             final AgendaItem item = (AgendaItem) queueable[i];
             if ( item == null ) {
@@ -404,7 +410,7 @@
             eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
                                                                           this.workingMemory );
         }
-        ((AgendaGroupImpl) agendaGroup).clear();
+        ((InternalAgendaGroup) agendaGroup).clear();
     }
 
     /* (non-Javadoc)
@@ -451,7 +457,7 @@
      *             If an error occurs while firing an agenda item.
      */
     public boolean fireNextItem(final AgendaFilter filter) throws ConsequenceException {
-        final AgendaGroupImpl group = (AgendaGroupImpl) getNextFocus();
+        final InternalAgendaGroup group = (InternalAgendaGroup) getNextFocus();
 
         // return if there are no Activations to fire
         if ( group == 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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultBetaConstraints.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -54,8 +54,6 @@
 
     private int                   indexed;
 
-    private RuleBaseConfiguration conf;
-
     public DefaultBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                   final RuleBaseConfiguration conf) {
         this( constraints, conf, false );
@@ -65,7 +63,6 @@
     public DefaultBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                   final RuleBaseConfiguration conf,
                                   final boolean disableIndexing ) {
-        this.conf = conf;
         this.indexed = -1;
         this.constraints = new LinkedList();
         ContextEntry current = null;
@@ -198,7 +195,7 @@
         return false;
     }
 
-    public BetaMemory createBetaMemory(final RuleBaseConfiguration config) {
+    public BetaMemory createBetaMemory(RuleBaseConfiguration conf) {
         BetaMemory memory;
         if ( this.indexed > 0 ) {
             LinkedListEntry entry = (LinkedListEntry) this.constraints.getFirst();
@@ -216,22 +213,22 @@
 
             final FieldIndex[] indexes = (FieldIndex[]) list.toArray( new FieldIndex[list.size()] );
             TupleMemory tupleMemory;
-            if ( this.conf.isIndexLeftBetaMemory() ) {
+            if ( conf.isIndexLeftBetaMemory() ) {
                 tupleMemory = new TupleIndexHashTable( indexes );
             } else {
                 tupleMemory = new TupleHashTable();
             }
 
             FactHandleMemory factHandleMemory;
-            if ( this.conf.isIndexRightBetaMemory() ) {
+            if ( conf.isIndexRightBetaMemory() ) {
                 factHandleMemory = new FactHandleIndexHashTable( indexes );
             } else {
                 factHandleMemory = new FactHashTable();
             }
-            memory = new BetaMemory( config.isSequential() ? null : tupleMemory,
+            memory = new BetaMemory( conf.isSequential() ? null : tupleMemory,
                                      factHandleMemory );
         } else {
-            memory = new BetaMemory( config.isSequential() ? null : new TupleHashTable(),
+            memory = new BetaMemory( conf.isSequential() ? null : new TupleHashTable(),
                                      new FactHashTable() );
         }
 

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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DoubleBetaConstraints.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -56,8 +56,6 @@
     private boolean                       indexed0;
     private boolean                       indexed1;
 
-    private RuleBaseConfiguration         conf;
-
     public DoubleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                  final RuleBaseConfiguration conf) {
         this( constraints,
@@ -68,7 +66,6 @@
     public DoubleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                  final RuleBaseConfiguration conf,
                                  final boolean disableIndexing) {
-        this.conf = conf;
         if ( disableIndexing || ( !conf.isIndexLeftBetaMemory() && !conf.isIndexRightBetaMemory() ) ) {
             this.indexed0 = false;
             this.indexed1 = false;
@@ -192,14 +189,14 @@
             final FieldIndex[] indexes = (FieldIndex[]) list.toArray( new FieldIndex[list.size()] );
 
             TupleMemory tupleMemory;
-            if ( this.conf.isIndexLeftBetaMemory() ) {
+            if ( config.isIndexLeftBetaMemory() ) {
                 tupleMemory = new TupleIndexHashTable( indexes );
             } else {
                 tupleMemory = new TupleHashTable();
             }
 
             FactHandleMemory factHandleMemory;
-            if ( this.conf.isIndexRightBetaMemory() ) {
+            if ( config.isIndexRightBetaMemory() ) {
                 factHandleMemory = new FactHandleIndexHashTable( indexes );
             } else {
                 factHandleMemory = new FactHashTable();

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,16 @@
+package org.drools.common;
+
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+
+public interface InternalAgendaGroup extends AgendaGroup {
+    public Activation getNext();
+    
+    public void add(Activation activation);
+    
+    public void setActive(boolean activate);
+    
+    public Activation[] getQueue();
+    
+    public void clear();        
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -44,7 +44,9 @@
     public FactHandleFactory newFactHandleFactory();
 
     public Map getGlobals();
-
+    
+    public Map getAgendaGroupRuleTotals();
+    
     public RuleBaseConfiguration getConfiguration();
     
     public Package getPackage(String name);

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PriorityQueueAgendaGroupFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PriorityQueueAgendaGroupFactory.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PriorityQueueAgendaGroupFactory.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,16 @@
+package org.drools.common;
+
+import org.drools.spi.AgendaGroup;
+
+public class PriorityQueueAgendaGroupFactory implements AgendaGroupFactory {
+    private static final AgendaGroupFactory INSTANCE = new PriorityQueueAgendaGroupFactory();
+    
+    public static AgendaGroupFactory getInstance() {
+        return INSTANCE;
+    }
+    
+    public AgendaGroup createAgendaGroup(String name, InternalRuleBase ruleBase) {
+        return new BinaryHeapQueueAgendaGroup( name,
+                                    ruleBase );        
+    }
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/QuadroupleBetaConstraints.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/QuadroupleBetaConstraints.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/QuadroupleBetaConstraints.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -62,8 +62,6 @@
     private boolean                       indexed2;
     private boolean                       indexed3;
 
-    private RuleBaseConfiguration         conf;
-
     public QuadroupleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                      final RuleBaseConfiguration conf) {
         this( constraints,
@@ -74,7 +72,6 @@
     public QuadroupleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                      final RuleBaseConfiguration conf,
                                      final boolean disableIndexing) {
-        this.conf = conf;
         if ( disableIndexing || ( !conf.isIndexLeftBetaMemory() && !conf.isIndexRightBetaMemory() ) ) {
             this.indexed0 = false;
             this.indexed1 = false;
@@ -239,7 +236,7 @@
         return false;
     }
 
-    public BetaMemory createBetaMemory(final RuleBaseConfiguration config) {
+    public BetaMemory createBetaMemory(RuleBaseConfiguration conf) {
 
         BetaMemory memory;
 
@@ -280,22 +277,22 @@
         if ( !list.isEmpty() ) {
             final FieldIndex[] indexes = (FieldIndex[]) list.toArray( new FieldIndex[list.size()] );
             TupleMemory tupleMemory;
-            if ( this.conf.isIndexLeftBetaMemory() ) {
+            if ( conf.isIndexLeftBetaMemory() ) {
                 tupleMemory = new TupleIndexHashTable( indexes );
             } else {
                 tupleMemory = new TupleHashTable();
             }
 
             FactHandleMemory factHandleMemory;
-            if ( this.conf.isIndexRightBetaMemory() ) {
+            if ( conf.isIndexRightBetaMemory() ) {
                 factHandleMemory = new FactHandleIndexHashTable( indexes );
             } else {
                 factHandleMemory = new FactHashTable();
             }
-            memory = new BetaMemory( config.isSequential() ? null : tupleMemory,
+            memory = new BetaMemory( conf.isSequential() ? null : tupleMemory,
                                      factHandleMemory );
         } else {
-            memory = new BetaMemory( config.isSequential() ? null : new TupleHashTable(),
+            memory = new BetaMemory( conf.isSequential() ? null : new TupleHashTable(),
                                      new FactHashTable() );
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -123,7 +123,7 @@
         final LinkedListIterator it = this.list.iterator();
         for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
             final Activation activation = node.getActivation();
-            ((AgendaGroupImpl) activation.getAgendaGroup()).add( activation );
+            ((BinaryHeapQueueAgendaGroup) activation.getAgendaGroup()).add( activation );
         }
     }
 
@@ -145,7 +145,7 @@
         this.list.add( node );
 
         if ( this.active ) {
-            ((AgendaGroupImpl) activation.getAgendaGroup()).add( activation );
+            ((InternalAgendaGroup) activation.getAgendaGroup()).add( activation );
         }
     }
 

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SequentialAgendaGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SequentialAgendaGroupImpl.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SequentialAgendaGroupImpl.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,154 @@
+package org.drools.common;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.drools.conflict.DepthConflictResolver;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.ConflictResolver;
+import org.drools.util.BinaryHeapQueue;
+import org.drools.util.PrimitiveLongMap;
+import org.drools.util.Queueable;
+
+/**
+ * <code>AgendaGroup</code> implementation that uses a <code>PriorityQueue</code> to prioritise the evaluation of added
+ * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a <code>Map</code> of <code>ActivationQueues</code> 
+ * for requested salience values.
+ * 
+ * @see PriorityQueue
+ * @see ActivationQueue
+ * 
+ * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
+ * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
+ *
+ */
+public class SequentialAgendaGroupImpl
+    implements
+    AgendaGroup {
+
+    private static final long     serialVersionUID = 320L;
+
+    private final String          name;
+
+    /** Items in the agenda. */
+    //private final BinaryHeapQueue queue;
+    private PrimitiveLongMap     queue;
+
+    private boolean               active;
+    
+    private long                  index;
+
+    /**
+     * Construct an <code>AgendaGroup</code> with the given name.
+     * 
+     * @param name
+     *      The <AgendaGroup> name.
+     */
+    
+    
+    public SequentialAgendaGroupImpl(final String name, final ConflictResolver conflictResolver) {
+        this.name = name;
+        this.queue = new PrimitiveLongMap();//new BinaryHeapQueue( conflictResolver );
+    }    
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#getName()
+     */
+    public String getName() {
+        return this.name;
+    }
+
+    public void clear() {
+        this.queue.clear();
+    }
+
+    /* (non-Javadoc)
+     * @see org.drools.spi.AgendaGroup#size()
+     */
+    public int size() {
+        return this.queue.size();
+    }
+
+    public void add(final Activation activation) {
+        this.queue.put( activation.getRule().getLoadOrder(), activation );
+        //this.queue.enqueue( (Queueable) activation );
+    }
+
+    public Activation getNext() {
+        index = this.queue.getNext( index );
+        if ( index == -1 ) {
+            return null;
+        }
+        return ( Activation ) this.queue.get( index );
+//        
+//        if ( index > this.queue.size() ) {
+//            return null;
+//        } else {
+//            return ( Activation ) this.queue.get( index );
+//        }
+    }
+
+    public boolean isActive() {
+        return this.active;
+    }
+
+    public void setActive(final boolean activate) {
+        this.active = activate;
+    }
+
+    /**
+     * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
+     * otherwise it returns false;
+     * 
+     * @param priorityQueue
+     * @return
+     */
+    public boolean isEmpty() {
+        return this.queue.isEmpty();
+    }
+
+    public Activation[] getActivations() {
+        return null;
+        //this.queue.
+        //return (Activation[]) this.queue.toArray( new AgendaItem[this.queue.size()] );
+    }
+
+    public Queueable[] getQueueable() {
+        return null;
+        //return this.queue.getQueueable();
+    }
+
+    public String toString() {
+        return "AgendaGroup '" + this.name + "'";
+    }
+
+    public boolean equal(final Object object) {
+        if ( (object == null) || !(object instanceof SequentialAgendaGroupImpl) ) {
+            return false;
+        }
+
+        if ( ((SequentialAgendaGroupImpl) object).name.equals( this.name ) ) {
+            return true;
+        }
+
+        return false;
+    }
+
+    public int hashCode() {
+        return this.name.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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TripleBetaConstraints.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -59,8 +59,6 @@
     private boolean                       indexed1;
     private boolean                       indexed2;
 
-    private RuleBaseConfiguration         conf;
-
     public TripleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                  final RuleBaseConfiguration conf) {
         this( constraints,
@@ -71,7 +69,6 @@
     public TripleBetaConstraints(final BetaNodeFieldConstraint[] constraints,
                                  final RuleBaseConfiguration conf,
                                  final boolean disableIndexing) {
-        this.conf = conf;
         if ( disableIndexing || (!conf.isIndexLeftBetaMemory() && !conf.isIndexRightBetaMemory()) ) {
             this.indexed0 = false;
             this.indexed1 = false;
@@ -201,7 +198,7 @@
         return false;
     }
 
-    public BetaMemory createBetaMemory(final RuleBaseConfiguration config) {
+    public BetaMemory createBetaMemory(final RuleBaseConfiguration conf) {
 
         BetaMemory memory;
 
@@ -234,22 +231,22 @@
         if ( !list.isEmpty() ) {
             final FieldIndex[] indexes = (FieldIndex[]) list.toArray( new FieldIndex[list.size()] );
             TupleMemory tupleMemory;
-            if ( this.conf.isIndexLeftBetaMemory() ) {
+            if ( conf.isIndexLeftBetaMemory() ) {
                 tupleMemory = new TupleIndexHashTable( indexes );
             } else {
                 tupleMemory = new TupleHashTable();
             }
 
             FactHandleMemory factHandleMemory;
-            if ( this.conf.isIndexRightBetaMemory() ) {
+            if ( conf.isIndexRightBetaMemory() ) {
                 factHandleMemory = new FactHandleIndexHashTable( indexes );
             } else {
                 factHandleMemory = new FactHashTable();
             }
-            memory = new BetaMemory( config.isSequential() ? null : tupleMemory,
+            memory = new BetaMemory( conf.isSequential() ? null : tupleMemory,
                                      factHandleMemory );
         } else {
-            memory = new BetaMemory( config.isSequential() ? null : new TupleHashTable(),
+            memory = new BetaMemory( conf.isSequential() ? null : new TupleHashTable(),
                                      new FactHashTable() );
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AlphaNode.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -120,11 +120,12 @@
                              final InternalWorkingMemory workingMemory) throws FactException {
         if ( this.constraint.isAllowed( handle.getObject(),
                                         workingMemory ) ) {
-            if ( hasMemory() ) {
+            if ( hasMemory()  && !workingMemory.isSequential() ) {
                 final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( this );
                 memory.add( handle,
                             false );
             }
+            
             this.sink.propagateAssertObject( handle,
                                              context,
                                              workingMemory );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -323,7 +323,7 @@
             if ( cls == null || !ruleBase.getConfiguration().isShadowed( cls.getName() ) ) {
                 return;
             }
-
+            
             String pkgName = cls.getPackage().getName();
             if ( "org.drools.reteoo".equals( pkgName ) || "org.drools.base".equals( pkgName ) ) {
                 // We don't shadow internal classes

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -19,11 +19,17 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.drools.RuleIntegrationException;
+import org.drools.base.SalienceInteger;
 import org.drools.common.BaseNode;
 import org.drools.common.DroolsObjectInputStream;
 import org.drools.common.InternalRuleBase;
@@ -31,6 +37,8 @@
 import org.drools.reteoo.builder.ReteooRuleBuilder;
 import org.drools.rule.InvalidPatternException;
 import org.drools.rule.Rule;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.Salience;
 
 /**
  * Builds the Rete-OO network for a <code>Package</code>.
@@ -118,7 +126,85 @@
         this.rules.put( rule,
                         terminals.toArray( new BaseNode[terminals.size()] ) );
     }
+    
+    public void order() {
+        Map map = new HashMap();
+        
+        for ( Iterator it = this.rules.values().iterator(); it.hasNext(); ) {
+            BaseNode[] nodes = (BaseNode[]) it.next();
+            for ( int i = 0 ; i < nodes.length; i++ ) {
+                if ( nodes[i] instanceof RuleTerminalNode ) {
+                    RuleTerminalNode node = ( RuleTerminalNode ) nodes[i];
+                    String agendaGroup = node.getRule().getAgendaGroup();
+                    if ( agendaGroup == null || agendaGroup.equals( "" ) ) {
+                        agendaGroup = "MAIN";
+                    }
+                    List rules = ( List ) map.get( agendaGroup );
+                    if ( rules == null ) {
+                        rules = new ArrayList();
+                        map.put( agendaGroup, rules );
+                    }
+                    rules.add( node );
+                }
+            }
+        }
+        
+        for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
+            Entry entry = ( Entry ) it.next();
+            String agendaGroup = (String) entry.getKey();
+            List rules = ( List ) entry.getValue();
+            Collections.sort( rules, RuleSequenceComparator.INSTANCE );
+            
+            int i = 0;
+            for ( Iterator listIter = rules.iterator(); listIter.hasNext(); ) {
+                RuleTerminalNode node = ( RuleTerminalNode ) listIter.next();
+                node.setSequence( i++ );
+            }
+            
+            ruleBase.getAgendaGroupRuleTotals().put( agendaGroup, new Integer( i ) );
+        }
+    }
 
+    public static class RuleSequenceComparator implements Comparator {
+        public final static RuleSequenceComparator INSTANCE = new RuleSequenceComparator();
+
+        public int compare(Object o1,
+                           Object o2) {
+            RuleTerminalNode r1 = (RuleTerminalNode) o1;
+            RuleTerminalNode r2 = (RuleTerminalNode) o2;
+            
+            Salience so1 = r1.getRule().getSalience();
+            if (so1 != null && !(so1 instanceof SalienceInteger) ) {
+                throw new RuntimeException(r1.getRule().getName() + "must not have a dynamic salience" );
+            }
+            Salience so2 = r2.getRule().getSalience();
+            if (so2 != null && !(so2 instanceof SalienceInteger) ) {
+                throw new RuntimeException(r2.getRule().getName() + "must not have a dynamic salience" );
+            }
+            
+            int s1 = so1.getValue( null, null );
+            int s2 = so2.getValue( null, null );
+            
+            if ( s1 >  s2) {                        
+                return -1;
+            } else if ( s1 < s2 ) {
+                return 1;
+            } 
+            
+            int id1 =r1.getId();
+            int id2 =r2.getId();
+            
+            if ( id1 <  id2) {                        
+                return -1;
+            } else if ( id1 > id2 ) {
+                return 1;
+            } else {
+                return 0;
+            }
+        }
+        
+    }
+    
     public BaseNode[] getTerminalNodes(final Rule rule) {
         return (BaseNode[]) this.rules.remove( rule );
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -42,6 +42,8 @@
 import org.drools.spi.PropagationContext;
 import org.drools.util.ObjectHashSet;
 
+import sun.security.x509.IssuerAlternativeNameExtension;
+
 /**
  * Implementation of <code>RuleBase</code>.
  * 
@@ -240,6 +242,12 @@
     
     public StatelessSession newStatelessSession() {
         ExecutorService executor = this.config.getExecutorService();
+
+        //orders the rules
+        if ( this.config.isSequential() ) {
+            this.reteooBuilder.order();
+        }
+        
         ReteooWorkingMemory wm = new ReteooWorkingMemory( this.workingMemoryCounter++,
                                                           this  );
         

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,6 @@
 package org.drools.reteoo;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.drools.FactHandle;
@@ -43,14 +44,14 @@
                                                        object ) );
     }
 
-    public Future asyncInsert(final Object[] list) {
-        final AssertObjects assertObjects = new AssertObjects( list );
+    public Future asyncInsert(final Object[] array) {
+        final AssertObjects assertObjects = new AssertObjects( array );
         this.executor.submit( assertObjects );
         return assertObjects;
     }
 
-    public Future asyncInsert(final List list) {
-        final AssertObjects assertObjects = new AssertObjects( list );
+    public Future asyncInsert(final Collection collection) {
+        final AssertObjects assertObjects = new AssertObjects( collection );
         this.executor.submit( assertObjects );
         return assertObjects;
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,6 @@
 package org.drools.reteoo;
 
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
@@ -58,34 +59,34 @@
         this.workingMemory.fireAllRules( this.agendaFilter );
     }
 
-    public void execute(Object[] list) {
-        for ( int i = 0, length = list.length; i < length; i++ ) {
-            this.workingMemory.insert( list[i] );
+    public void execute(Object[] array) {
+        for ( int i = 0, length = array.length; i < length; i++ ) {
+            this.workingMemory.insert( array[i] );
         }
         this.workingMemory.fireAllRules( this.agendaFilter );
     }
 
-    public void execute(List list) {
-        for( Iterator it = list.iterator(); it.hasNext(); ) {
+    public void execute(Collection collection) {
+        for( Iterator it = collection.iterator(); it.hasNext(); ) {
             this.workingMemory.insert( it.next() );
         }
         this.workingMemory.fireAllRules( this.agendaFilter );
     }
     
     public void asyncExecute(final Object object) {
-        final AssertObject assertObjects = new AssertObject( object );
-        this.executor.submit( assertObjects );
+        final AssertObject assertObject = new AssertObject( object );
+        this.executor.submit( assertObject );
         this.executor.submit( new FireAllRules( this.agendaFilter ) );
     }       
     
-    public void asyncExecute(final Object[] list) {
-        final AssertObjects assertObjects = new AssertObjects( list );
+    public void asyncExecute(final Object[] array) {
+        final AssertObjects assertObjects = new AssertObjects( array );
         this.executor.submit( assertObjects );
         this.executor.submit( new FireAllRules( this.agendaFilter ) );
     }
     
-    public void asyncExecute(final List list) {
-        final AssertObjects assertObjects = new AssertObjects( list );
+    public void asyncExecute(final Collection collection) {
+        final AssertObjects assertObjects = new AssertObjects( collection );
         this.executor.submit( assertObjects );
         this.executor.submit( new FireAllRules( this.agendaFilter ) );
     }         
@@ -96,16 +97,16 @@
         return new ReteStatelessSessionResult( this.workingMemory );
     }
 
-    public StatelessSessionResult executeWithResults(Object[] list) {
-        for ( int i = 0, length = list.length; i < length; i++ ) {
-            this.workingMemory.insert( list[i] );
+    public StatelessSessionResult executeWithResults(Object[] array) {
+        for ( int i = 0, length = array.length; i < length; i++ ) {
+            this.workingMemory.insert( array[i] );
         }
         this.workingMemory.fireAllRules( this.agendaFilter );
         return new ReteStatelessSessionResult( this.workingMemory );
     }
 
-    public StatelessSessionResult executeWithResults(List list) {
-        for( Iterator it = list.iterator(); it.hasNext(); ) {
+    public StatelessSessionResult executeWithResults(Collection collection) {
+        for( Iterator it = collection.iterator(); it.hasNext(); ) {
             this.workingMemory.insert( it.next() );
         }
         this.workingMemory.fireAllRules( this.agendaFilter );

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -19,11 +19,12 @@
 import java.io.Serializable;
 
 import org.drools.RuleBaseConfiguration;
-import org.drools.common.AgendaGroupImpl;
+import org.drools.common.BinaryHeapQueueAgendaGroup;
 import org.drools.common.AgendaItem;
 import org.drools.common.BaseNode;
 import org.drools.common.EventSupport;
 import org.drools.common.InternalAgenda;
+import org.drools.common.InternalAgendaGroup;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalRuleFlowGroup;
 import org.drools.common.InternalWorkingMemory;
@@ -60,6 +61,8 @@
     // Instance members
     // ------------------------------------------------------------
 
+    private int sequence;
+    
     /**
      * 
      */
@@ -110,7 +113,15 @@
     public Rule getRule() {
         return this.rule;
     }
+    
+    public void setSequence(int seq) {
+        this.sequence = seq;
+    }
 
+    public int getSequence() {
+        return this.sequence;
+    }
+    
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     // org.drools.impl.TupleSink
     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -193,16 +204,17 @@
             // implementations
             // ----------------
             final TerminalNodeMemory memory = (TerminalNodeMemory) workingMemory.getNodeMemory( this );
-            AgendaGroupImpl agendaGroup = memory.getAgendaGroup();
+            InternalAgendaGroup agendaGroup = memory.getAgendaGroup();
             if ( agendaGroup == null ) {
+                // @todo: this logic really should be encapsulated inside the Agenda
                 if ( this.rule.getAgendaGroup() == null || this.rule.getAgendaGroup().equals( "" ) || this.rule.getAgendaGroup().equals( AgendaGroup.MAIN ) ) {
                     // Is the Rule AgendaGroup undefined? If it is use MAIN,
                     // which is added to the Agenda by default
-                    agendaGroup = (AgendaGroupImpl) agenda.getAgendaGroup( AgendaGroup.MAIN );
+                    agendaGroup = (InternalAgendaGroup) agenda.getAgendaGroup( AgendaGroup.MAIN );
                 } else {
                     // AgendaGroup is defined, so try and get the AgendaGroup
                     // from the Agenda
-                    agendaGroup = (AgendaGroupImpl) agenda.getAgendaGroup( this.rule.getAgendaGroup() );
+                    agendaGroup = (InternalAgendaGroup) agenda.getAgendaGroup( this.rule.getAgendaGroup() );
                 }
 
                 memory.setAgendaGroup( agendaGroup );
@@ -219,6 +231,10 @@
                                                     context,
                                                     this.rule,
                                                     this.subrule );
+            
+            if ( workingMemory.isSequential() ) {
+                item.setSequenence( this.sequence );
+            }
 
             if ( this.rule.getActivationGroup() != null ) {
                 // Lazy cache activationGroup
@@ -505,7 +521,7 @@
         Serializable {
         private static final long serialVersionUID = 320L;
 
-        private AgendaGroupImpl   agendaGroup;
+        private InternalAgendaGroup   agendaGroup;
 
         private ActivationGroup   activationGroup;
 
@@ -517,11 +533,11 @@
             this.tupleMemory = new TupleHashTable();
         }
 
-        public AgendaGroupImpl getAgendaGroup() {
+        public InternalAgendaGroup getAgendaGroup() {
             return this.agendaGroup;
         }
 
-        public void setAgendaGroup(final AgendaGroupImpl agendaGroup) {
+        public void setAgendaGroup(final InternalAgendaGroup agendaGroup) {
             this.agendaGroup = agendaGroup;
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/builder/BuildContext.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -16,9 +16,11 @@
 
 package org.drools.reteoo.builder;
 
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 
 import org.drools.common.BetaConstraints;
 import org.drools.common.InternalRuleBase;
@@ -230,6 +232,19 @@
      */
     public void setBetaconstraints(final List betaconstraints) {
         this.betaconstraints = betaconstraints;
+    }   
+    
+    public int getNextSequence(String groupName) {
+        //List list = new ArrayList();
+        
+        Integer seq = ( Integer ) this.rulebase.getAgendaGroupRuleTotals().get( groupName );
+        if ( seq == null ) {
+            seq = new Integer( 0 );            
+        }
+        Integer newSeq = new Integer( seq.intValue() + 1 );
+        this.rulebase.getAgendaGroupRuleTotals().put( groupName, newSeq );
+        
+        return newSeq.intValue();
     }
 
     /**

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Rule.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -473,7 +473,7 @@
         return this.loadOrder;
     }
 
-    void setLoadOrder(final long loadOrder) {
+    public void setLoadOrder(final long loadOrder) {
         this.loadOrder = loadOrder;
     }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/AgendaGroup.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -56,6 +56,8 @@
      *      int value for the total number of activations
      */
     public int size();
+    
+    public boolean isEmpty();
 
     public boolean isActive();
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -40,6 +40,9 @@
 public interface KnowledgeHelper
     extends
     Serializable {
+    
+    public void setActivation(final Activation agendaItem); 
+    
     /**
      * Asserts an object, notice that it does not return the FactHandle
      * 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/PrimitiveLongMap.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/PrimitiveLongMap.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/PrimitiveLongMap.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -46,7 +46,7 @@
     private final int           tableSize;
     private final int           shifts;
     private final int           doubleShifts;
-    private final Page          firstPage;
+    private Page                firstPage;
     private Page                lastPage;
     private int                 lastPageId;
     private long                maxKey;
@@ -91,6 +91,10 @@
 
         this.lastPageId = 0;
 
+        init();
+    }
+
+    private void init() {
         // instantiate the first page
         // previous sibling of first page is null
         // next sibling of last page is null
@@ -105,6 +109,14 @@
         this.lastPage = this.firstPage;
     }
 
+    public void clear() {
+        init();
+    }
+
+    public boolean isEmpty() {
+        return this.totalSize == 0;
+    }
+
     public Object put(final long key,
                       Object value) {
         if ( key < 0 ) {
@@ -163,6 +175,36 @@
         }
         return value;
     }
+    
+    /**
+     * gets the next populated key, after the given key position.
+     * @param key
+     * @return
+     */
+    public long getNext(long key) {
+        final int currentPageId = (int) key >> this.doubleShifts;
+        final int nextPageId = (int) (key+1) >> this.doubleShifts;
+        
+        if ( currentPageId != nextPageId ) {
+            Page page = findPage( key + 1);
+            while ( page.isEmpty() ) {
+                page = page.getNextSibling();
+            }
+            key = this.doubleShifts << page.getPageId();
+        } else {
+            key += 1;
+        }
+        
+        while ( !containsKey( key ) && key <= this.maxKey ) {
+            key++;
+        }
+        
+        if ( key > this.maxKey ) {
+            key -= 1;
+        }
+                                
+        return key;
+    }
 
     public int size() {
         return this.totalSize;
@@ -339,15 +381,15 @@
             // normalise key
             key -= this.pageSize * this.pageId;
 
-            // determine page
-            final int page = (int) key >> this.shifts;
+            // determine table
+            final int table = (int) key >> this.shifts;
 
             // determine offset
-            final int offset = page << this.shifts;
+            final int offset = table << this.shifts;
 
-            // tables[page][slot]
-            return this.tables[page][(int) key - offset];
-        }
+            // tables[table][slot]
+            return this.tables[table][(int) key - offset];
+        }              
 
         public Object put(long key,
                           final Object newValue) {
@@ -359,7 +401,7 @@
             // normalise key
             key -= this.pageSize * this.pageId;
 
-            // determine page
+            // determine table
             final int table = (int) key >> this.shifts;
 
             // determine offset

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Queueable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Queueable.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Queueable.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,7 @@
 package org.drools.util;
 
+import org.drools.spi.Activation;
+
 /*
  * Copyright 2005 JBoss Inc
  * 
@@ -16,7 +18,7 @@
  * limitations under the License.
  */
 
-public interface Queueable {
+public interface Queueable extends Activation {
     public void enqueued(Queue queue,
                          int index);
 

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/common/SequentialAgendaTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/common/SequentialAgendaTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/common/SequentialAgendaTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,42 @@
+package org.drools.common;
+
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+
+import junit.framework.TestCase;
+
+public class SequentialAgendaTest extends TestCase {
+    public void testgetNext() {
+        SequentialAgendaGroupImpl agenda = new SequentialAgendaGroupImpl( "test", null );
+        
+        agenda.add( createActivation( 5 ) );
+        
+        agenda.add( createActivation( 49 ) );
+        
+        agenda.add( createActivation( 108 ) );
+        
+        agenda.add( createActivation( 320 ) );
+        
+        agenda.add( createActivation( 1053 ) );
+        
+        assertEquals( 5, agenda.getNext().getRule().getLoadOrder() );
+        assertEquals( 49, agenda.getNext().getRule().getLoadOrder() );
+        assertEquals( 108, agenda.getNext().getRule().getLoadOrder() );
+        assertEquals( 320, agenda.getNext().getRule().getLoadOrder() );
+        assertEquals( 1053, agenda.getNext().getRule().getLoadOrder() );
+        assertNull( agenda.getNext() );
+        
+    }
+    
+    public Activation createActivation(int index) {
+        Rule rule = new Rule( "test rule");
+        rule.setLoadOrder( index );
+        final AgendaItem item = new AgendaItem( 0,
+                                                null,
+                                                0,
+                                                null,
+                                                rule,
+                                                null );   
+        return item;
+    }
+}

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AgendaTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -24,12 +24,16 @@
 import org.drools.Agenda;
 import org.drools.DroolsTestCase;
 import org.drools.RuleBase;
+import org.drools.RuleBaseConfiguration;
 import org.drools.RuleBaseFactory;
 import org.drools.WorkingMemory;
 import org.drools.base.SalienceInteger;
-import org.drools.common.AgendaGroupImpl;
+import org.drools.common.ArrayAgendaGroup;
+import org.drools.common.BinaryHeapQueueAgendaGroup;
 import org.drools.common.DefaultFactHandle;
 import org.drools.common.InternalAgenda;
+import org.drools.common.InternalAgendaGroup;
+import org.drools.common.InternalRuleBase;
 import org.drools.common.PropagationContextImpl;
 import org.drools.common.RuleFlowGroupImpl;
 import org.drools.conflict.DepthConflictResolver;
@@ -196,30 +200,12 @@
     }
 
     public void testFocusStack() throws ConsequenceException {
-        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        final InternalRuleBase ruleBase = ( InternalRuleBase ) RuleBaseFactory.newRuleBase();
 
         final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
 
-        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
-
-        //       // create the AgendaGroups
-        final AgendaGroupImpl agendaGroup1 = new AgendaGroupImpl( "agendaGroup1", DepthConflictResolver.getInstance() );
-        agenda.addAgendaGroup( agendaGroup1 );
-        //        ActivationQueue queue1 = agendaGroup1.getActivationQueue( 0 );
-
-        final AgendaGroupImpl agendaGroup2 = new AgendaGroupImpl( "agendaGroup2", DepthConflictResolver.getInstance() );
-        agenda.addAgendaGroup( agendaGroup2 );
-        //        ActivationQueue queue2 = agendaGroup2.getActivationQueue( 0 );
-
-        final AgendaGroupImpl agendaGroup3 = new AgendaGroupImpl( "agendaGroup3", DepthConflictResolver.getInstance() );
-        agenda.addAgendaGroup( agendaGroup3 );
-        //        ActivationQueue queue3 = agendaGroup3.getActivationQueue( 0 );
-
         // create the consequence
         final Consequence consequence = new Consequence() {
-            /**
-             * 
-             */
             private static final long serialVersionUID = -5828597682602481200L;
 
             public void evaluate(KnowledgeHelper knowledgeHelper,
@@ -278,7 +264,19 @@
                                                                         PropagationContext.ASSERTION,
                                                                         rule3,
                                                                         null );
+        
+        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
 
+        // create the AgendaGroups
+        final AgendaGroup agendaGroup1 = new BinaryHeapQueueAgendaGroup( "agendaGroup1", ruleBase );
+        agenda.addAgendaGroup( agendaGroup1 );
+
+        final AgendaGroup agendaGroup2 = new BinaryHeapQueueAgendaGroup( "agendaGroup2", ruleBase );
+        agenda.addAgendaGroup( agendaGroup2 );
+
+        final AgendaGroup agendaGroup3 = new BinaryHeapQueueAgendaGroup( "agendaGroup3", ruleBase );
+        agenda.addAgendaGroup( agendaGroup3 );        
+
         // focus at this point is MAIN
         assertEquals( 0,
                       agenda.focusStackSize() );
@@ -288,7 +286,7 @@
                            workingMemory );
 
         // check focus is main
-        final AgendaGroupImpl main = (AgendaGroupImpl) agenda.getAgendaGroup( AgendaGroup.MAIN );
+        final AgendaGroup main = agenda.getAgendaGroup( AgendaGroup.MAIN );
         assertEquals( agenda.getFocus(),
                       main );
         // check main got the tuple
@@ -421,13 +419,13 @@
 
     //
     public void testAutoFocus() throws ConsequenceException {
-        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        final InternalRuleBase ruleBase = ( InternalRuleBase ) RuleBaseFactory.newRuleBase();
 
         final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
         final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
 
         // create the agendaGroup
-        final AgendaGroupImpl agendaGroup = new AgendaGroupImpl( "agendaGroup", DepthConflictResolver.getInstance() );
+        final AgendaGroup agendaGroup = new BinaryHeapQueueAgendaGroup( "agendaGroup", ruleBase );
         agenda.addAgendaGroup( agendaGroup );
 
         // create the consequence
@@ -498,13 +496,13 @@
     }
 
     public void testAgendaGroupLockOnActive() {
-        final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+        final InternalRuleBase ruleBase = ( InternalRuleBase ) RuleBaseFactory.newRuleBase();
 
         final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
         final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
 
         // create the agendaGroup
-        final AgendaGroupImpl agendaGroup = new AgendaGroupImpl( "agendaGroup", DepthConflictResolver.getInstance() );
+        final InternalAgendaGroup agendaGroup = new BinaryHeapQueueAgendaGroup( "agendaGroup", ruleBase );
         agenda.addAgendaGroup( agendaGroup );
 
         final ReteTuple tuple = new ReteTuple( new DefaultFactHandle( 1,
@@ -1379,5 +1377,229 @@
         assertEquals( 2,
                       ruleFlowGroup.size() );
     }
+    
+    public void testSequentialAgenda() {
+        RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+        InternalRuleBase ruleBase = ( InternalRuleBase ) RuleBaseFactory.newRuleBase( conf );
+        
 
+        // create the consequence
+        final Consequence consequence = new Consequence() {
+            /**
+             * 
+             */
+            private static final long serialVersionUID = -5828597682602481200L;
+
+            public void evaluate(KnowledgeHelper knowledgeHelper,
+                                 WorkingMemory workingMemory) {
+                // do nothing
+            }
+        };
+
+        final ReteTuple tuple = new ReteTuple( new DefaultFactHandle( 1,
+                                                                      "cheese" ) );
+
+        // create a rule for each agendaGroup
+        final Rule rule0 = new Rule( "test-rule0" );
+        final RuleTerminalNode node0 = new RuleTerminalNode( 3,
+                                                             new MockTupleSource( 2 ),
+                                                             rule0,
+                                                             rule0.getLhs() );
+        node0.setSequence( 72 );
+        rule0.setConsequence( consequence );
+        final PropagationContext context0 = new PropagationContextImpl( 0,
+                                                                        PropagationContext.ASSERTION,
+                                                                        rule0,
+                                                                        null );
+
+        final Rule rule1 = new Rule( "test-rule1",
+                                     "agendaGroup1" );
+        final RuleTerminalNode node1 = new RuleTerminalNode( 5,
+                                                             new MockTupleSource( 4 ),
+                                                             rule1,
+                                                             rule1.getLhs() );
+        node1.setSequence( 10 );
+        rule1.setConsequence( consequence );
+        final PropagationContext context1 = new PropagationContextImpl( 0,
+                                                                        PropagationContext.ASSERTION,
+                                                                        rule1,
+                                                                        null );
+
+        final Rule rule2 = new Rule( "test-rule2",
+                                     "agendaGroup1" );
+        final RuleTerminalNode node2 = new RuleTerminalNode( 7,
+                                                             new MockTupleSource( 6 ),
+                                                             rule2,
+                                                             rule2.getLhs() );
+        node2.setSequence( 7 );
+        rule2.setConsequence( consequence );
+        final PropagationContext context2 = new PropagationContextImpl( 0,
+                                                                        PropagationContext.ASSERTION,
+                                                                        rule2,
+                                                                        null );
+
+        final Rule rule3 = new Rule( "test-rule3",
+                                     "agendaGroup2" );
+        final RuleTerminalNode node3 = new RuleTerminalNode( 9,
+                                                             new MockTupleSource( 8 ),
+                                                             rule3,
+                                                             rule3.getLhs() );
+        node3.setSequence( 0 );
+        rule3.setConsequence( consequence );
+        final PropagationContext context3 = new PropagationContextImpl( 0,
+                                                                        PropagationContext.ASSERTION,
+                                                                        rule3,
+                                                                        null );
+        
+        ruleBase.getAgendaGroupRuleTotals().put( "MAIN", new Integer( 100 ) );
+        ruleBase.getAgendaGroupRuleTotals().put( "agendaGroup1", new Integer( 10 ) );
+        ruleBase.getAgendaGroupRuleTotals().put( "agendaGroup2", new Integer( 1 ) );
+
+        final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
+
+        final InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
+
+        final AgendaGroup agendaGroup1 = new ArrayAgendaGroup( "agendaGroup1", ruleBase );
+        agenda.addAgendaGroup( agendaGroup1 );
+
+        final AgendaGroup agendaGroup2 = new ArrayAgendaGroup( "agendaGroup2", ruleBase );
+        agenda.addAgendaGroup( agendaGroup2 );  
+        
+        // focus at this point is MAIN
+        assertEquals( 0,
+                      agenda.focusStackSize() );
+
+        node0.assertTuple( tuple,
+                           context0,
+                           workingMemory );
+
+        // check focus is main
+        final AgendaGroup main = agenda.getAgendaGroup( AgendaGroup.MAIN );
+        assertEquals( agenda.getFocus(),
+                      main );
+        // check main got the tuple
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+        node2.assertTuple( tuple,
+                           context2,
+                           workingMemory );
+
+        // main is still focus and this tuple went to agendaGroup1
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+
+        // check agendaGroup1 still got the tuple
+        assertEquals( 1,
+                      agendaGroup1.size() );
+
+        // make sure total agenda size reflects this
+        assertEquals( 2,
+                      agenda.agendaSize() );
+
+        // put another one on agendaGroup 1
+        node2.assertTuple( tuple,
+                           context2,
+                           workingMemory );
+
+        // main is still focus so shouldn't have increased
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+
+        // check agendaGroup2 still got the tuple
+        assertEquals( 2,
+                      agendaGroup1.size() );
+
+        // make sure total agenda size reflects this
+        assertEquals( 3,
+                      agenda.agendaSize() );
+
+        // set the focus to agendaGroup1, note agendaGroup1 has no activations
+        agenda.setFocus( "agendaGroup1" );
+        // add agendaGroup2 onto the focus stack
+        agenda.setFocus( "agendaGroup2" );
+
+        // agendaGroup2, the current focus, has no activations
+        assertEquals( 0,
+                      agenda.getFocus().size() );
+
+        // add to agendaGroup2
+        node3.assertTuple( tuple,
+                           context3,
+                           workingMemory );
+
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+
+        node3.assertTuple( tuple,
+                           context3,
+                           workingMemory );
+
+        // agendaGroup2 now has 2 activations
+        assertEquals( 2,
+                      agenda.getFocus().size() );
+        
+        // check totalAgendaSize still works
+        assertEquals( 5,
+                      agenda.agendaSize() );
+
+        // ok now lets check that stacks work with fireNextItem
+        agenda.fireNextItem( null );
+
+        // agendaGroup2 should still be the current agendaGroup
+        assertEquals( agendaGroup2,
+                      agenda.getFocus() );
+        // agendaGroup2 has gone from 2 to one activations
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+        // check totalAgendaSize has reduced too
+        assertEquals( 4,
+                      agenda.agendaSize() );
+
+        // now repeat the process
+        agenda.fireNextItem( null );
+
+        // focus is still agendaGroup2, but now its empty
+        assertEquals( agendaGroup2, agenda.getFocus() );
+        assertEquals( 0,
+                      agenda.getFocus().size() );
+        assertEquals( 3,
+                      agenda.agendaSize() );
+
+        // repeat fire again
+        agenda.fireNextItem( null );
+
+        // agendaGroup2 is empty so it should be popped from the stack making agendaGroup1 the current agendaGroup
+        assertEquals( agendaGroup1,
+                      agenda.getFocus() );
+        // agendaGroup1 had 2 activations, now it only has 1
+        assertEquals( 1,
+                      agenda.getFocus().size() );
+        assertEquals( 2,
+                      agenda.agendaSize() );
+
+        // repeat fire again
+        agenda.fireNextItem( null );
+
+        assertEquals( agendaGroup1,
+                      agenda.getFocus() );
+        assertEquals( 0,
+                      agenda.getFocus().size() );
+        assertEquals( 1,
+                      agenda.agendaSize() );
+
+        // this last fire is more interesting as it demonstrates that
+        // agendaGroup1 on
+        // the stack before agendaGroup2 gets skipped as it has no activations
+        agenda.fireNextItem( null );
+
+        assertEquals( agenda.getFocus(),
+                      main );
+        assertEquals( 0,
+                      agenda.getFocus().size() );
+        assertEquals( 0,
+                      agenda.agendaSize() );        
+        
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AlphaNodeTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -21,6 +21,7 @@
 import org.drools.Cheese;
 import org.drools.DroolsTestCase;
 import org.drools.FactException;
+import org.drools.RuleBaseConfiguration;
 import org.drools.RuleBaseFactory;
 import org.drools.base.ClassFieldExtractor;
 import org.drools.base.ClassFieldExtractorCache;
@@ -217,7 +218,89 @@
         assertFalse( "Should not contain 'cheddar handle'",
                      memory.contains( f0 ) );
     }
+    
+    public void testLiteralConstraintAssertSequentialMode() throws Exception {
+        RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+        final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
+                                                                           (ReteooRuleBase) RuleBaseFactory.newRuleBase( conf ) );
+        final Rule rule = new Rule( "test-rule" );
+        final PropagationContext context = new PropagationContextImpl( 0,
+                                                                       PropagationContext.ASSERTION,
+                                                                       null,
+                                                                       null );
 
+        final MockObjectSource source = new MockObjectSource( 15 );
+
+        final ClassFieldExtractor extractor = ClassFieldExtractorCache.getExtractor( Cheese.class,
+                                                                                     "type",
+                                                                                     getClass().getClassLoader() );
+
+        final FieldValue field = FieldFactory.getFieldValue( "cheddar" );
+
+        final Evaluator evaluator = ValueType.OBJECT_TYPE.getEvaluator( Operator.EQUAL );
+        final LiteralConstraint constraint = new LiteralConstraint( extractor,
+                                                                    evaluator,
+                                                                    field );
+
+        // With Memory
+        final AlphaNode alphaNode = new AlphaNode( 2,
+                                                   constraint,
+                                                   source,
+                                                   true,
+                                                   3 ); // has memory
+
+        final MockObjectSink sink = new MockObjectSink();
+        alphaNode.addObjectSink( sink );
+
+        final Cheese cheddar = new Cheese( "cheddar",
+                                           5 );
+        final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert( cheddar );
+
+        // check sink is empty
+        assertLength( 0,
+                      sink.getAsserted() );
+
+        // check alpha memory is empty 
+        final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( alphaNode );
+
+        assertEquals( 0,
+                      memory.size() );
+
+        // object should assert as it passes text
+        alphaNode.assertObject( f0,
+                                context,
+                                workingMemory );
+
+        assertEquals( 1,
+                      sink.getAsserted().size() );
+        assertEquals( 0,
+                      memory.size() );
+        Object[] list = (Object[]) sink.getAsserted().get( 0 );
+        assertSame( cheddar,
+                    workingMemory.getObject( (DefaultFactHandle) list[0] ) );
+
+        final Cheese stilton = new Cheese( "stilton",
+                                           6 );
+        final DefaultFactHandle f1 = new DefaultFactHandle( 1,
+                                                            stilton );
+
+        // object should NOT assert as it does not pass test
+        alphaNode.assertObject( f1,
+                                context,
+                                workingMemory );
+
+        assertLength( 1,
+                      sink.getAsserted() );
+        assertEquals( 0,
+                      memory.size() );
+        list = (Object[]) sink.getAsserted().get( 0 );
+        assertSame( cheddar,
+                    workingMemory.getObject( (DefaultFactHandle) list[0] ) );
+    }    
+    
+    
+
     /*
      * dont need to test with and without memory on this, as it was already done
      * on the previous two tests. This just test AlphaNode With a different

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	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/JoinNodeTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -144,7 +144,7 @@
         this.node.assertTuple( tuple0,
                                this.context,
                                this.workingMemory );
-        // check memories are empty
+        // check memories, left memory is populated, right memory is emptys
         assertEquals( 1,
                       this.memory.getTupleMemory().size() );
         assertEquals( 0,
@@ -168,6 +168,58 @@
     }
 
     /**
+     * Test just tuple assertions
+     * 
+     * @throws AssertionException
+     */
+    public void testAssertTupleSequentialMode() throws Exception {
+        RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+
+        this.workingMemory = new ReteooWorkingMemory( 1,
+                                                      (ReteooRuleBase) RuleBaseFactory.newRuleBase( conf ) );
+
+        // override setup, so its working in sequential mode
+        this.node = new JoinNode( 15,
+                                  this.tupleSource,
+                                  this.objectSource,
+                                  new DefaultBetaConstraints( new BetaNodeFieldConstraint[]{this.constraint},
+                                                              conf ) );
+
+        this.node.addTupleSink( this.sink );
+
+        this.memory = (BetaMemory) this.workingMemory.getNodeMemory( this.node );
+
+        this.workingMemory = new ReteooWorkingMemory( 1,
+                                                      (ReteooRuleBase) RuleBaseFactory.newRuleBase( conf ) );
+
+        final DefaultFactHandle f0 = new DefaultFactHandle( 0,
+                                                            "cheese" );
+        final ReteTuple tuple0 = new ReteTuple( f0 );
+
+        this.node.assertObject( f0,
+                                this.context,
+                                this.workingMemory );
+
+        // assert tuple
+        this.node.assertTuple( tuple0,
+                               this.context,
+                               this.workingMemory );
+
+        assertEquals( 1,
+                      this.sink.getAsserted().size() );
+
+        assertNull( this.memory.getTupleMemory() );
+
+        assertEquals( 0,
+                      this.memory.getFactHandleMemory().size() );
+
+        assertEquals( new ReteTuple( tuple0,
+                                     f0 ),
+                      ((Object[]) this.sink.getAsserted().get( 0 ))[0] );
+    }
+
+    /**
      * Test just object assertions
      * 
      * @throws Exception

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -24,6 +24,7 @@
 import org.drools.FactException;
 import org.drools.Person;
 import org.drools.RuleBase;
+import org.drools.RuleBaseConfiguration;
 import org.drools.RuleBaseFactory;
 import org.drools.base.ClassObjectType;
 import org.drools.base.ShadowProxy;
@@ -111,7 +112,51 @@
                       memory.size() );
         assertTrue( memory.contains( handle1 ) );
     }
+    
+    public void testAssertObjectSequentialMode() {
+        final PropagationContext context = new PropagationContextImpl( 0,
+                                                                       PropagationContext.ASSERTION,
+                                                                       null,
+                                                                       null );
 
+        RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+        final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase( conf );
+        final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
+                                                                           ruleBase );
+
+        final Rete source = ruleBase.getRete();
+
+        final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
+                                                                  new ClassObjectType( String.class ),
+                                                                  source,
+                                                                  3 );
+
+        final MockObjectSink sink = new MockObjectSink();
+        objectTypeNode.addObjectSink( sink );
+
+        final Object string1 = "cheese";
+
+        final InternalFactHandle handle1 = (InternalFactHandle) workingMemory.insert( string1 );
+
+        // should assert as ObjectType matches
+        objectTypeNode.assertObject( handle1,
+                                     context,
+                                     workingMemory );
+
+        // make sure just string1 was asserted 
+        final List asserted = sink.getAsserted();
+        assertLength( 1,
+                      asserted );
+        assertSame( string1,
+                    workingMemory.getObject( (DefaultFactHandle) ((Object[]) asserted.get( 0 ))[0] ) );
+
+        // it's sequential, so check the asserted object was not added to the node memory
+        final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( objectTypeNode );
+        assertEquals( 0,
+                      memory.size() );
+    }
+
     public void testMemory() {
         final RuleBase ruleBase = RuleBaseFactory.newRuleBase();  
         

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooBuilderTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteooBuilderTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -0,0 +1,89 @@
+package org.drools.reteoo;
+
+import org.drools.RuleBaseConfiguration;
+import org.drools.WorkingMemory;
+import org.drools.base.SalienceInteger;
+import org.drools.rule.Rule;
+import org.drools.spi.Consequence;
+import org.drools.spi.KnowledgeHelper;
+
+import junit.framework.TestCase;
+
+public class ReteooBuilderTest extends TestCase {
+    
+    public void testOrder() {
+        //ReteooBuilder
+        RuleBaseConfiguration conf = new RuleBaseConfiguration();
+        conf.setSequential( true );
+        ReteooRuleBase ruleBase = new ReteooRuleBase( conf );
+        ReteooBuilder builder = new ReteooBuilder( ruleBase );
+        
+        
+        final Consequence consequence = new Consequence() {
+            public void evaluate(KnowledgeHelper knowledgeHelper,
+                                 WorkingMemory workingMemory) throws Exception {
+                System.out.println( "Consequence!" );
+            }
+        };        
+        
+        Rule rule0 = new Rule( "rule0" );
+        rule0.setAgendaGroup( "group 0" );
+        rule0.setConsequence( consequence );
+        builder.addRule( rule0 );
+        
+        Rule rule1 = new Rule( "rule1" );
+        rule1.setAgendaGroup( "group 0" );
+        rule1.setConsequence( consequence );
+        builder.addRule( rule1 );
+        
+        Rule rule2 = new Rule( "rule2" );
+        rule2.setAgendaGroup( "group 1" );
+        rule2.setConsequence( consequence );
+        builder.addRule( rule2 );
+        
+        Rule rule3 = new Rule( "rule3" );
+        rule3.setAgendaGroup( "group 0" );
+        rule3.setConsequence( consequence );
+        builder.addRule( rule3 );
+
+        Rule rule4 = new Rule( "rule4" );
+        rule4.setAgendaGroup( "group 2" );  
+        rule4.setConsequence( consequence );
+        builder.addRule( rule4 );
+        
+        Rule rule5 = new Rule( "rule5" );
+        rule5.setAgendaGroup( "group 1" );        
+        rule5.setConsequence( consequence );
+        builder.addRule( rule5 );
+        
+        Rule rule6 = new Rule( "rule6" );
+        rule6.setSalience( new SalienceInteger( 5 ) );
+        rule6.setAgendaGroup( "group 2" );  
+        rule6.setConsequence( consequence );
+        builder.addRule( rule6 );        
+
+        Rule rule7 = new Rule( "rule7" );
+        rule7.setAgendaGroup( "group 0" );
+        rule7.setConsequence( consequence );
+        builder.addRule( rule7 );        
+
+        Rule rule8 = new Rule( "rule8" );
+        rule8.setSalience( new SalienceInteger( 10 ) );
+        rule8.setAgendaGroup( "group 2" );  
+        rule8.setConsequence( consequence );
+        builder.addRule( rule8 );        
+        
+        builder.order();
+        
+        assertEquals( 0, ((RuleTerminalNode) builder.getTerminalNodes( rule0 )[0]).getSequence() );
+        assertEquals( 1, ((RuleTerminalNode) builder.getTerminalNodes( rule1 )[0]).getSequence() );
+        assertEquals( 0, ((RuleTerminalNode) builder.getTerminalNodes( rule2 )[0]).getSequence() ); 
+        assertEquals( 2, ((RuleTerminalNode) builder.getTerminalNodes( rule3 )[0]).getSequence() );
+        assertEquals( 2, ((RuleTerminalNode) builder.getTerminalNodes( rule4 )[0]).getSequence() );
+        assertEquals( 1, ((RuleTerminalNode) builder.getTerminalNodes( rule5 )[0]).getSequence() );        
+        assertEquals( 1, ((RuleTerminalNode) builder.getTerminalNodes( rule6 )[0]).getSequence() );    
+        assertEquals( 3, ((RuleTerminalNode) builder.getTerminalNodes( rule7 )[0]).getSequence() );    
+        assertEquals( 0, ((RuleTerminalNode) builder.getTerminalNodes( rule8 )[0]).getSequence() );            
+                                
+    }
+}

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/builder/ReteooRuleBuilderTest.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -26,6 +26,8 @@
 import org.drools.reteoo.ReteooBuilder;
 import org.drools.reteoo.ReteooRuleBase;
 import org.drools.reteoo.RuleTerminalNode;
+import org.drools.reteoo.ReteooBuilder.IdGenerator;
+import org.drools.rule.Package;
 import org.drools.rule.Pattern;
 import org.drools.rule.GroupElement;
 import org.drools.rule.GroupElementFactory;
@@ -99,4 +101,5 @@
 
     }
 
+
 }

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/BaseQueueable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/BaseQueueable.java	2007-07-01 22:01:54 UTC (rev 12978)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/BaseQueueable.java	2007-07-01 22:42:28 UTC (rev 12979)
@@ -1,5 +1,14 @@
 package org.drools.util;
 
+import org.drools.common.ActivationGroupNode;
+import org.drools.common.LogicalDependency;
+import org.drools.common.RuleFlowGroupNode;
+import org.drools.rule.GroupElement;
+import org.drools.rule.Rule;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.Tuple;
+
 /*
  * Copyright 2005 JBoss Inc
  * 
@@ -31,4 +40,89 @@
     public void dequeue() {
         this.queue.dequeue( this.index );
     }
+
+    public void addLogicalDependency(LogicalDependency node) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public ActivationGroupNode getActivationGroupNode() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public long getActivationNumber() {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public AgendaGroup getAgendaGroup() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public LinkedList getLogicalDependencies() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public PropagationContext getPropagationContext() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Rule getRule() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public RuleFlowGroupNode getRuleFlowGroupNode() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public int getSalience() {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public GroupElement getSubRule() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Tuple getTuple() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public boolean isActivated() {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void remove() {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void setActivated(boolean activated) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void setActivationGroupNode(ActivationGroupNode activationGroupNode) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void setLogicalDependencies(LinkedList justified) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    public void setRuleFlowGroupNode(RuleFlowGroupNode ruleFlowGroupNode) {
+        // TODO Auto-generated method stub
+        
+    }
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list