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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Feb 10 22:15:11 EST 2007


Author: mark.proctor at jboss.com
Date: 2007-02-10 22:15:11 -0500 (Sat, 10 Feb 2007)
New Revision: 9430

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/InternalAgenda.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PropagationContextImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.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
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java
Log:
-Gather activate/dormant activation stats
-fix magic

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-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -76,9 +76,9 @@
     private final Map                  agendaGroups;
 
     private final Map                  activationGroups;
-    
-    private final Map				   ruleFlowGroups;
 
+    private final Map                  ruleFlowGroups;
+
     private final LinkedList           focusStack;
 
     private AgendaGroupImpl            currentModule;
@@ -87,6 +87,9 @@
 
     private DefaultKnowledgeHelper     knowledgeHelper;
 
+    public int                         activeActivations;
+    public int                         dormantActivations;
+
     // ------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------
@@ -260,16 +263,16 @@
         }
         return activationGroup;
     }
-    
+
     public RuleFlowGroup getRuleFlowGroup(final String name) {
-    	RuleFlowGroupImpl ruleFlowGroup = (RuleFlowGroupImpl) this.ruleFlowGroups.get( name );
+        RuleFlowGroupImpl ruleFlowGroup = (RuleFlowGroupImpl) this.ruleFlowGroups.get( name );
         if ( ruleFlowGroup == null ) {
-        	ruleFlowGroup = new RuleFlowGroupImpl( name );
+            ruleFlowGroup = new RuleFlowGroupImpl( name );
             this.ruleFlowGroups.put( name,
-            						 ruleFlowGroup );
+                                     ruleFlowGroup );
         }
         return ruleFlowGroup;
-    }    
+    }
 
     /* (non-Javadoc)
      * @see org.drools.common.AgendaI#focusStackSize()
@@ -332,7 +335,8 @@
         if ( this.scheduledActivations != null && !this.scheduledActivations.isEmpty() ) {
             for ( ScheduledAgendaItem item = (ScheduledAgendaItem) this.scheduledActivations.removeFirst(); item != null; item = (ScheduledAgendaItem) this.scheduledActivations.removeFirst() ) {
                 item.remove();
-                eventsupport.getAgendaEventSupport().fireActivationCancelled( item, this.workingMemory );
+                eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
+                                                                              this.workingMemory );
             }
         }
     }
@@ -367,7 +371,8 @@
                 item.getActivationGroupNode().getActivationGroup().removeActivation( item );
             }
 
-            eventsupport.getAgendaEventSupport().fireActivationCancelled( item, this.workingMemory  );
+            eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
+                                                                          this.workingMemory );
         }
         ((AgendaGroupImpl) agendaGroup).clear();
     }
@@ -387,16 +392,17 @@
      */
     public void clearActivationGroup(final ActivationGroup activationGroup) {
         final EventSupport eventsupport = (EventSupport) this.workingMemory;
-        
+
         for ( final Iterator it = activationGroup.iterator(); it.hasNext(); ) {
-            ActivationGroupNode node = (ActivationGroupNode) it.next() ;             
+            ActivationGroupNode node = (ActivationGroupNode) it.next();
             final Activation activation = node.getActivation();
             activation.setActivationGroupNode( null );
 
             if ( activation.isActivated() ) {
                 activation.setActivated( false );
                 activation.remove();
-                eventsupport.getAgendaEventSupport().fireActivationCancelled( activation, this.workingMemory  );
+                eventsupport.getAgendaEventSupport().fireActivationCancelled( activation,
+                                                                              this.workingMemory );
             }
         }
         activationGroup.clear();
@@ -438,9 +444,14 @@
      *             If an error occurs while attempting to fire the consequence.
      */
     public synchronized void fireActivation(final Activation activation) throws ConsequenceException {
+        // We do this first as if a node modifies a fact that causes a recursion on an empty pattern
+        // we need to make sure it re-activates
+        increaseDormantActivations();
+        
         final EventSupport eventsupport = (EventSupport) this.workingMemory;
 
-        eventsupport.getAgendaEventSupport().fireBeforeActivationFired( activation, this.workingMemory  );
+        eventsupport.getAgendaEventSupport().fireBeforeActivationFired( activation,
+                                                                        this.workingMemory );
 
         if ( activation.getActivationGroupNode() != null ) {
             // We know that this rule will cancel all other activatiosn in the group
@@ -449,7 +460,7 @@
             activationGroup.removeActivation( activation );
             clearActivationGroup( activationGroup );
         }
-        activation.setActivated( false );        
+        activation.setActivated( false );
 
         try {
             this.knowledgeHelper.setActivation( activation );
@@ -460,20 +471,45 @@
             throw new ConsequenceException( e,
                                             activation.getRule() );
         }
-        
+
         if ( activation.getRuleFlowGroupNode() != null ) {
             // Now the rule has fired, remove it and check if the rule flow group is empty
             // if its empty, activate the child groups. We do this after the consequence 
             // fired as we don't want to populate the Agenda prior to the rule actually firing.
             final RuleFlowGroup ruleFlowGroup = activation.getRuleFlowGroupNode().getRuleFlowGroup();
             ruleFlowGroup.removeActivation( activation );
-            
+
             if ( ruleFlowGroup.isEmpty() ) {
                 ruleFlowGroup.activateChildren();
             }
         }
 
-        eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation );
+        eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation );                        
     }
+    
+    public void increaseActiveActivations() {
+        this.activeActivations++;
+    }
+    
+    public void decreaseActiveActivations() {
+        this.activeActivations--;        
+    }
+    
+    public void increaseDormantActivations() {
+        this.activeActivations--;
+        this.dormantActivations++;
+    }
+    
+    public void decreaseDormantActivations() {
+        this.dormantActivations--;
+    }
+    
+    public int getActiveActivations() {
+        return this.activeActivations;
+    }
+    
+    public int getDormantActivations() {
+        return this.dormantActivations;
+    }
 
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java	2007-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalAgenda.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -19,5 +19,17 @@
     public void scheduleItem(final ScheduledAgendaItem item);
 
     public void addAgendaGroup(final AgendaGroup agendaGroup);
+    
+    public void increaseActiveActivations();
+    
+    public void decreaseActiveActivations();
+    
+    public void increaseDormantActivations();
+    
+    public void decreaseDormantActivations();
+    
+    public int getActiveActivations();
+    
+    public int getDormantActivations();
 
 }

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-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/PropagationContextImpl.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -30,7 +30,11 @@
     private final Activation activation;
 
     private final long       propagationNumber;
-
+    
+    public final int                         activeActivations;
+    
+    public final int                         dormantActivations;
+    
     public PropagationContextImpl(final long number,
                                   final int type,
                                   final Rule rule,
@@ -39,6 +43,22 @@
         this.rule = rule;
         this.activation = activation;
         this.propagationNumber = number;
+        this.activeActivations = 0;
+        this.dormantActivations = 0;        
+    }    
+
+    public PropagationContextImpl(final long number,
+                                  final int type,
+                                  final Rule rule,
+                                  final Activation activation,
+                                  final int activeActivations,
+                                  final int dormantActivations) {
+        this.type = type;
+        this.rule = rule;
+        this.activation = activation;
+        this.propagationNumber = number;
+        this.activeActivations = activeActivations;
+        this.dormantActivations = dormantActivations;
     }
 
     public long getPropagationNumber() {
@@ -71,5 +91,14 @@
     public int getType() {
         return this.type;
     }
+    
+    public int getActiveActivations() {
+        return this.activeActivations;
+    }
+    
+    public int getDormantActivations() {
+        return this.dormantActivations;
+    }    
+    
 
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java	2007-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -149,12 +149,11 @@
             if ( this.constraints.isAllowedCachedRight( tuple ) ) {
                 final int matches = tuple.getMatches();
                 tuple.setMatches( matches + 1 );
-                this.sink.propagateRetractTuple( tuple,
-                                                 context,
-                                                 workingMemory );
-                //                if ( matches == 0 ) {
-                //                    this.sink.propagateRetractTuple( tuple, context, workingMemory );
-                //                }
+                if ( matches == 0 ) {
+                    this.sink.propagateRetractTuple( tuple,
+                                                     context,
+                                                     workingMemory );
+                }
             }
         }
     }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2007-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -22,6 +22,7 @@
 import org.drools.base.ShadowProxy;
 import org.drools.common.BaseNode;
 import org.drools.common.EmptyBetaConstraints;
+import org.drools.common.InternalAgenda;
 import org.drools.common.InternalFactHandle;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.common.NodeMemory;
@@ -151,7 +152,7 @@
             }
         }
         
-        if (context.getType() == PropagationContext.MODIFICATION && this.skipOnModify ) {
+        if (context.getType() == PropagationContext.MODIFICATION && this.skipOnModify && context.getDormantActivations() == 0 ) {
             // we do this after the shadowproxy update, just so that its up to date for the future
             return;
         }                
@@ -178,7 +179,7 @@
     public void retractObject(final InternalFactHandle handle,
                               final PropagationContext context,
                               final InternalWorkingMemory workingMemory) {
-        if (context.getType() == PropagationContext.MODIFICATION && this.skipOnModify ) {
+        if (context.getType() == PropagationContext.MODIFICATION && this.skipOnModify && context.getDormantActivations() == 0 ) {
             return;
         }         
         

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-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -22,6 +22,7 @@
 import org.drools.common.AgendaGroupImpl;
 import org.drools.common.AgendaItem;
 import org.drools.common.BaseNode;
+import org.drools.common.DefaultAgenda;
 import org.drools.common.InternalAgenda;
 import org.drools.common.InternalWorkingMemory;
 import org.drools.common.NodeMemory;
@@ -59,18 +60,18 @@
     /**
      * 
      */
-    private static final long serialVersionUID = 320;
+    private static final long  serialVersionUID = 320;
     /** The rule to invoke upon match. */
-    private final Rule        rule;
+    private final Rule         rule;
     /** 
      * the subrule reference is needed to resolve declarations
      * because declarations may have different offsets in each subrule
      */
     private final GroupElement subrule;
-    private final TupleSource tupleSource;
+    private final TupleSource  tupleSource;
 
-    private TupleSinkNode     previousTupleSinkNode;
-    private TupleSinkNode     nextTupleSinkNode;
+    private TupleSinkNode      previousTupleSinkNode;
+    private TupleSinkNode      nextTupleSinkNode;
 
     // ------------------------------------------------------------
     // Constructors
@@ -135,9 +136,9 @@
                             final PropagationContext context,
                             final InternalWorkingMemory workingMemory,
                             final boolean fireActivationCreated) {
-        
+
         //check if the rule is effective
-        if (!this.rule.isEffective()) {
+        if ( !this.rule.isEffective() ) {
             return;
         }
 
@@ -145,8 +146,8 @@
         // return
         if ( this.rule.getNoLoop() && this.rule.equals( context.getRuleOrigin() ) ) {
             return;
-        }        
-        
+        }
+
         //we only have to clone the head fact to make sure the graph is not affected during consequence reads after a modify
         final ReteTuple cloned = new ReteTuple( tuple );
 
@@ -223,24 +224,24 @@
                 }
                 memory.getActivationGroup().addActivation( item );
             }
-            
+
             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.
-                agendaGroup.add( item );            	
+                agendaGroup.add( item );
             } else {
-            	//There is  a RuleFlowNode so add it there, instead  of the Agenda
-            	
+                //There is  a RuleFlowNode so add it there, instead  of the Agenda
+
                 // Lazy cache ruleFlowGroup
                 if ( memory.getRuleFlowGroup() == null ) {
                     memory.setRuleFlowGroup( workingMemory.getAgenda().getRuleFlowGroup( this.rule.getRuleFlowGroup() ) );
                 }
                 memory.getRuleFlowGroup().addActivation( item );
-            }  
-            
+            }
+
             item.setAgendaGroup( agendaGroup );
 
             tuple.setActivation( item );
@@ -254,6 +255,8 @@
                                                                              workingMemory );
             }
         }
+        
+        agenda.increaseActiveActivations();        
     }
 
     public void retractTuple(final ReteTuple leftTuple,
@@ -261,24 +264,24 @@
                              final InternalWorkingMemory workingMemory) {
         final TerminalNodeMemory memory = (TerminalNodeMemory) workingMemory.getNodeMemory( this );
         final ReteTuple tuple = (ReteTuple) memory.getTupleMemory().remove( leftTuple );
-        //an activation is null if the tuple was never propagated as an assert
-        if ( tuple != null && tuple.getActivation() != null ) {
-            final Activation activation = tuple.getActivation();
-            if ( activation.isActivated() ) {
-                activation.remove();
+        final Activation activation = tuple.getActivation();
+        if ( activation.isActivated() ) {
+            activation.remove();
 
-                if ( activation.getActivationGroupNode() != null ) {
-                	activation.getActivationGroupNode().getActivationGroup().removeActivation( activation );
-                }
-
-                workingMemory.getAgendaEventSupport().fireActivationCancelled( activation,
-                                                                               workingMemory );
+            if ( activation.getActivationGroupNode() != null ) {
+                activation.getActivationGroupNode().getActivationGroup().removeActivation( activation );
             }
 
-            workingMemory.getTruthMaintenanceSystem().removeLogicalDependencies( activation,
-                                                                                 context,
-                                                                                 this.rule );
+            workingMemory.getAgendaEventSupport().fireActivationCancelled( activation,
+                                                                           workingMemory );
+            ((InternalAgenda)workingMemory.getAgenda()).decreaseActiveActivations();
+        } else {
+            ((InternalAgenda)workingMemory.getAgenda()).decreaseDormantActivations();
         }
+
+        workingMemory.getTruthMaintenanceSystem().removeLogicalDependencies( activation,
+                                                                             context,
+                                                                             this.rule );
     }
 
     public String toString() {
@@ -390,7 +393,7 @@
             return true;
         }
 
-        if ( object == null || !(object instanceof RuleTerminalNode ) ) {
+        if ( object == null || !(object instanceof RuleTerminalNode) ) {
             return false;
         }
 
@@ -406,7 +409,7 @@
         private AgendaGroupImpl   agendaGroup;
 
         private ActivationGroup   activationGroup;
-        
+
         private RuleFlowGroup     ruleFlowGroup;
 
         private TupleHashTable    tupleMemory;
@@ -435,12 +438,12 @@
             return this.tupleMemory;
         }
 
-		public RuleFlowGroup getRuleFlowGroup() {
-			return ruleFlowGroup;
-		}
+        public RuleFlowGroup getRuleFlowGroup() {
+            return ruleFlowGroup;
+        }
 
-		public void setRuleFlowGroup(RuleFlowGroup ruleFlowGroup) {
-			this.ruleFlowGroup = ruleFlowGroup;
-		}                        
+        public void setRuleFlowGroup(RuleFlowGroup ruleFlowGroup) {
+            this.ruleFlowGroup = ruleFlowGroup;
+        }
     }
 }

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-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/PropagationContext.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -37,5 +37,9 @@
     public Activation getActivationOrigin();
 
     public int getType();
+    
+    public int getActiveActivations();
+    
+    public int getDormantActivations();    
 
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java	2007-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/NotNodeTest.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -28,6 +28,7 @@
 import org.drools.common.BetaConstraints;
 import org.drools.common.DefaultBetaConstraints;
 import org.drools.common.DefaultFactHandle;
+import org.drools.common.EmptyBetaConstraints;
 import org.drools.common.PropagationContextImpl;
 import org.drools.rule.Rule;
 import org.drools.spi.BetaNodeFieldConstraint;
@@ -299,13 +300,12 @@
     
 
     public void testGetConstraints_ReturnsNullEvenWithEmptyBinder() {
-        BetaConstraints nullConstraints = null;
+        BetaConstraints nullConstraints = EmptyBetaConstraints.getInstance();
         NotNode notNode = new NotNode( 1,
                                                 this.tupleSource,
                                                 this.objectSource, nullConstraints);        
-        AlphaNodeFieldConstraint[] constraints = notNode.getConstraints();
-        assertNull(constraints);
-        
+        BetaNodeFieldConstraint[] constraints = notNode.getConstraints();
+        assertEquals( 0, constraints.length );        
     }
 
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java	2007-02-10 22:12:51 UTC (rev 9429)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java	2007-02-11 03:15:11 UTC (rev 9430)
@@ -27,6 +27,8 @@
 import org.drools.RuleBaseFactory;
 import org.drools.base.ClassObjectType;
 import org.drools.common.DefaultFactHandle;
+import org.drools.common.PropagationContextImpl;
+import org.drools.spi.PropagationContext;
 import org.drools.util.ObjectHashMap;
 
 /**
@@ -109,13 +111,13 @@
         final DefaultFactHandle h1 = new DefaultFactHandle( 1,
                                                             new ArrayList() );
         rete.assertObject( h1,
-                           null,
+                           new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null),
                            workingMemory );
 
         // LinkedList matches two ObjectTypeNodes        
         h1.setObject( new LinkedList() );
         rete.assertObject( h1,
-                           null,
+                           new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null),
                            workingMemory );
 
         final ObjectHashMap map = (ObjectHashMap) workingMemory.getNodeMemory( rete );
@@ -166,7 +168,7 @@
                                                             list );
 
         rete.assertObject( h2,
-                           null,
+                           new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null),
                            workingMemory );
 
         final List asserted = sink1.getAsserted();
@@ -215,11 +217,11 @@
 
         // need  to assert first, to force it to build  up the cache
         rete.assertObject( h2,
-                           null,
+                           new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null),
                            workingMemory );
 
         rete.retractObject( h2,
-                            null,
+                            new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null),
                             workingMemory );
 
         final List retracted = sink1.getRetracted();




More information about the jboss-svn-commits mailing list