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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Apr 12 17:07:30 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-04-12 17:07:29 -0400 (Thu, 12 Apr 2007)
New Revision: 10945

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/AgendaGroupImpl.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/PropagationContextImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PropagationContext.java
Log:
JBRULES-788 lock-on-activate rule attribute
-found a bug with modify, we need to know when activations are truly new on modify. So now keep a hashmap of retractions during modify.

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-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -987,6 +987,8 @@
                                                                factHandle,
                                                                originalObject,
                                                                object );
+            
+            propagationContext.clearRetractedTuples();
 
             if ( !this.factQueue.isEmpty() ) {
                 propagateQueuedActions();
@@ -1145,6 +1147,13 @@
         }
     }
     
+    public class RuleFlowDeactivateEvent {
+        
+        public void propagate() {
+            
+        }
+    }
+    
     public IProcessInstance startProcess(String processId) {
     	IProcess process = getRuleBase().getProcess(processId);
     	if (process == null) {

Modified: 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-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -77,9 +77,6 @@
     }
 
     public void add(final Activation activation) {
-        if ( this.active && activation.getRule().isLockOnActivate() ) {
-            return;
-        }
         this.queue.enqueue( (Queueable) activation );
     }
 

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-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -195,7 +195,10 @@
     }
 
     public void dequeue() {
-        this.queue.dequeue( this.index );
+        if ( this.queue != null ) {
+            // will only be null if the AgendaGroup is locked when the activation add was attempted
+            this.queue.dequeue( this.index );
+        }
         this.activated = false;
     }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PropagationContextImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PropagationContextImpl.java	2007-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PropagationContextImpl.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -16,9 +16,16 @@
  * limitations under the License.
  */
 
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.reteoo.ReteTuple;
 import org.drools.rule.Rule;
 import org.drools.spi.Activation;
 import org.drools.spi.PropagationContext;
+import org.drools.util.ObjectHashMap;
+import org.drools.util.TupleHashTable;
 
 public class PropagationContextImpl
     implements
@@ -30,11 +37,13 @@
     private final Activation activation;
 
     private final long       propagationNumber;
-    
-    public final int                         activeActivations;
-    
-    public final int                         dormantActivations;
-    
+
+    public final int         activeActivations;
+
+    public final int         dormantActivations;
+
+    public ObjectHashMap     retracted;
+
     public PropagationContextImpl(final long number,
                                   final int type,
                                   final Rule rule,
@@ -44,8 +53,8 @@
         this.activation = activation;
         this.propagationNumber = number;
         this.activeActivations = 0;
-        this.dormantActivations = 0;        
-    }    
+        this.dormantActivations = 0;
+    }
 
     public PropagationContextImpl(final long number,
                                   final int type,
@@ -91,14 +100,38 @@
     public int getType() {
         return this.type;
     }
-    
+
     public int getActiveActivations() {
         return this.activeActivations;
     }
-    
+
     public int getDormantActivations() {
         return this.dormantActivations;
-    }    
+    }
+
+    public void addRetractedTuple(Rule rule, ReteTuple tuple) {
+        if ( this.retracted == null ) {
+            this.retracted = new ObjectHashMap();
+        }
+        
+        TupleHashTable tuples = (TupleHashTable) this.retracted.get( rule );
+        if ( tuples == null ) {
+            tuples = new TupleHashTable();
+            this.retracted.put( rule, tuples );
+        }
+        tuples.add( tuple );        
+    }
     
-
+    public ReteTuple removeRetractedTuple(Rule rule, ReteTuple tuple) {
+        if ( this.retracted == null  ) {
+            return null;
+        }
+        
+        TupleHashTable tuples = (TupleHashTable) this.retracted.get( rule );
+        return tuples.remove( tuple ); 
+    }
+    
+    public void clearRetractedTuples() {
+        this.retracted = null;
+    }
 }

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-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -113,11 +113,7 @@
         return this.list.size();
     }
 
-    public void addActivation(final Activation activation) {
-        if ( this.active && activation.getRule().isLockOnActivate() ) {
-            return;
-        }
-        
+    public void addActivation(final Activation activation) {        
         final RuleFlowGroupNode node = new RuleFlowGroupNode(activation, this);
         activation.setRuleFlowGroupNode(node);
     	list.add( node );

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-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -231,9 +231,13 @@
             if ( this.rule.getRuleFlowGroup() == null ) {
                 // No RuleFlowNode so add  it directly to  the Agenda
 
-                // Makes sure the Lifo is added to the AgendaGroup priority queue
-                // If the AgendaGroup is already in the priority queue it just
-                // returns.
+                // do not add the activation if the rule is "lock-on-active" and the AgendaGroup is active
+                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract                
+                if ( context.getType() == PropagationContext.MODIFICATION &&  this.rule.isLockOnActivate() && agendaGroup.isActivate() && context.removeRetractedTuple( this.rule, tuple ) == null ) {
+                    // This rule is locked and active, do not allow new tuples to activate 
+                    return;
+                }
+                
                 agendaGroup.add( item );
             } else {
                 //There is  a RuleFlowNode so add it there, instead  of the Agenda
@@ -242,7 +246,16 @@
                 if ( memory.getRuleFlowGroup() == null ) {
                     memory.setRuleFlowGroup( workingMemory.getAgenda().getRuleFlowGroup( this.rule.getRuleFlowGroup() ) );
                 }
+
+                // do not add the activation if the rule is "lock-on-active" and the RuleFlowGroup is active
+                // we must check the context to determine if its a new tuple or an exist re-activated tuple as part of the retract 
+                if ( context.getType() == PropagationContext.MODIFICATION &&  this.rule.isLockOnActivate() &&  memory.getRuleFlowGroup().isActive() && context.removeRetractedTuple( this.rule, tuple ) == null ) {
+                    // This rule is locked and active, do not allow new tuples to activate 
+                    return;
+                }                
+                
                 ((InternalRuleFlowGroup) memory.getRuleFlowGroup()).addActivation( item );
+                
             }  
 
             tuple.setActivation( item );
@@ -269,9 +282,14 @@
         	// tuple should only be null if it was asserted and reached a no-loop causing it to exit early
             // before being added to the node memory and an activation created and attached
         	return;
-        }
+        }        
+        
         final Activation activation = tuple.getActivation();
         if ( activation.isActivated() ) {
+            if ( this.rule.isLockOnActivate() && context.getType() == PropagationContext.MODIFICATION ) {
+                context.addRetractedTuple( this.rule, tuple );
+            }
+            
             activation.remove();
 
             if ( activation.getActivationGroupNode() != null ) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PropagationContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PropagationContext.java	2007-04-12 21:06:23 UTC (rev 10944)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PropagationContext.java	2007-04-12 21:07:29 UTC (rev 10945)
@@ -17,8 +17,11 @@
  */
 
 import java.io.Serializable;
+import java.util.HashMap;
 
+import org.drools.reteoo.ReteTuple;
 import org.drools.rule.Rule;
+import org.drools.util.TupleHashTable;
 
 public interface PropagationContext
     extends
@@ -41,5 +44,11 @@
     public int getActiveActivations();
     
     public int getDormantActivations();    
+    
+    public void addRetractedTuple(Rule rule, ReteTuple tuple);
+    
+    public ReteTuple removeRetractedTuple(Rule rule, ReteTuple tuple);
+    
+    public void clearRetractedTuples();    
 
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list