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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue May 4 15:42:42 EDT 2010


Author: tirelli
Date: 2010-05-04 15:42:42 -0400 (Tue, 04 May 2010)
New Revision: 32725

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/InternalRuleFlowGroup.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
Log:
JBRULES-2339: JBRULES-2440: fixing ruleflow group management

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	2010-05-04 17:57:16 UTC (rev 32724)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2010-05-04 19:42:42 UTC (rev 32725)
@@ -904,8 +904,9 @@
             }
             activation.setActivated( false );
 
+            InternalRuleFlowGroup ruleFlowGroup = null;
             if ( activation.getActivationNode() != null ) {
-                final InternalRuleFlowGroup ruleFlowGroup = (InternalRuleFlowGroup) activation.getActivationNode().getParentContainer();
+                ruleFlowGroup = (InternalRuleFlowGroup) activation.getActivationNode().getParentContainer();
                 // it is possible that the ruleflow group is no longer active if it was
                 // cleared during execution of this activation
                 ruleFlowGroup.removeActivation( activation );
@@ -930,6 +931,10 @@
                     throw new RuntimeException( e );
                 }
             }
+            
+            if( ruleFlowGroup != null ) {
+                ruleFlowGroup.deactivateIfEmpty();
+            }
 
             // if the tuple contains expired events 
             for ( LeftTuple tuple = (LeftTuple) activation.getTuple(); tuple != null; tuple = tuple.getParent() ) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleFlowGroup.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleFlowGroup.java	2010-05-04 17:57:16 UTC (rev 32724)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleFlowGroup.java	2010-05-04 19:42:42 UTC (rev 32725)
@@ -18,6 +18,12 @@
     void clear();
 
     /**
+     * Checks if this ruleflow group is active and should automatically deactivate.
+     * If the queue is empty, it deactivates the group.
+     */
+    public void deactivateIfEmpty();
+
+    /**
      * Activates or deactivates this <code>RuleFlowGroup</code>.
      * When activating, all activations of this <code>RuleFlowGroup</code> are added
      * to the agenda.

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	2010-05-04 17:57:16 UTC (rev 32724)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java	2010-05-04 19:42:42 UTC (rev 32725)
@@ -53,7 +53,7 @@
     private InternalWorkingMemory       workingMemory;
     private String                      name;
     private boolean                     active           = false;
-    private boolean                     autoDeactivate   = true;    
+    private boolean                     autoDeactivate   = true;
     private LinkedList                  list;
     private List<RuleFlowGroupListener> listeners;
     private Map<Long, String>           nodeInstances    = new HashMap<Long, String>();
@@ -72,13 +72,15 @@
         this.name = name;
         this.list = new LinkedList();
     }
-    
-    public RuleFlowGroupImpl(final String name, final boolean active, final boolean autoDeactivate) {
+
+    public RuleFlowGroupImpl(final String name,
+                             final boolean active,
+                             final boolean autoDeactivate) {
         this.name = name;
         this.active = active;
         this.autoDeactivate = autoDeactivate;
         this.list = new LinkedList();
-    }    
+    }
 
     public void readExternal(ObjectInput in) throws IOException,
                                             ClassNotFoundException {
@@ -187,7 +189,7 @@
     public void addActivation(final Activation activation) {
         assert activation.getActivationNode() == null;
         final ActivationNode node = new ActivationNode( activation,
-                                                              this );
+                                                        this );
         activation.setActivationNode( node );
         this.list.add( node );
 
@@ -200,12 +202,17 @@
         final ActivationNode node = activation.getActivationNode();
         this.list.remove( node );
         activation.setActivationNode( null );
-        if ( this.active && this.autoDeactivate ) {
-            if ( this.list.isEmpty() ) {
-                // deactivate callback
-                WorkingMemoryAction action = new DeactivateCallback( this );
-                this.workingMemory.queueWorkingMemoryAction( action );
-            }
+    }
+
+    /**
+     * Checks if this ruleflow group is active and should automatically deactivate.
+     * If the queue is empty, it deactivates the group.
+     */
+    public void deactivateIfEmpty() {
+        if ( this.active && this.autoDeactivate && this.list.isEmpty() ) {
+            // deactivate callback
+            WorkingMemoryAction action = new DeactivateCallback( this );
+            this.workingMemory.queueWorkingMemoryAction( action );
         }
     }
 
@@ -217,8 +224,8 @@
     }
 
     public void removeRuleFlowGroupListener(RuleFlowGroupListener listener) {
-        if (listeners != null) {
-            listeners.remove(listener);
+        if ( listeners != null ) {
+            listeners.remove( listener );
         }
     }
 
@@ -258,10 +265,12 @@
         return this.name.hashCode();
     }
 
-    public static class DeactivateCallback implements WorkingMemoryAction {
-    	
-        private static final long serialVersionUID = 400L;
-        
+    public static class DeactivateCallback
+        implements
+        WorkingMemoryAction {
+
+        private static final long     serialVersionUID = 400L;
+
         private InternalRuleFlowGroup ruleFlowGroup;
 
         public DeactivateCallback(InternalRuleFlowGroup ruleFlowGroup) {
@@ -269,12 +278,12 @@
         }
 
         public DeactivateCallback(MarshallerReaderContext context) throws IOException {
-        	this.ruleFlowGroup = (InternalRuleFlowGroup) context.wm.getAgenda().getRuleFlowGroup(context.readUTF());
+            this.ruleFlowGroup = (InternalRuleFlowGroup) context.wm.getAgenda().getRuleFlowGroup( context.readUTF() );
         }
 
         public void write(MarshallerWriteContext context) throws IOException {
-        	context.writeInt( WorkingMemoryAction.DeactivateCallback );
-        	context.writeUTF(ruleFlowGroup.getName());
+            context.writeInt( WorkingMemoryAction.DeactivateCallback );
+            context.writeUTF( ruleFlowGroup.getName() );
         }
 
         public void readExternal(ObjectInput in) throws IOException,
@@ -283,28 +292,32 @@
         }
 
         public void writeExternal(ObjectOutput out) throws IOException {
-            out.writeObject(ruleFlowGroup);
+            out.writeObject( ruleFlowGroup );
         }
 
         public void execute(InternalWorkingMemory workingMemory) {
             // check whether ruleflow group is still empty first
-            if (this.ruleFlowGroup.isEmpty()) {
+            if ( this.ruleFlowGroup.isEmpty() ) {
                 // deactivate ruleflow group
-                this.ruleFlowGroup.setActive(false);
+                this.ruleFlowGroup.setActive( false );
             }
         }
     }
-    
-    public void addNodeInstance(Long processInstanceId, String nodeInstanceId) {
-    	nodeInstances.put(processInstanceId, nodeInstanceId);
+
+    public void addNodeInstance(Long processInstanceId,
+                                String nodeInstanceId) {
+        nodeInstances.put( processInstanceId,
+                           nodeInstanceId );
     }
 
-    public void removeNodeInstance(Long processInstanceId, String nodeInstanceId) {
-    	nodeInstances.put(processInstanceId, nodeInstanceId);
+    public void removeNodeInstance(Long processInstanceId,
+                                   String nodeInstanceId) {
+        nodeInstances.put( processInstanceId,
+                           nodeInstanceId );
     }
-    
+
     public Map<Long, String> getNodeInstances() {
-    	return nodeInstances;
+        return nodeInstances;
     }
-    
+
 }

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	2010-05-04 17:57:16 UTC (rev 32724)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java	2010-05-04 19:42:42 UTC (rev 32725)
@@ -298,7 +298,7 @@
         }
 
         AgendaItem item = (AgendaItem) leftTuple.getActivation();
-        if ( item.isActivated() ) {
+        if ( item != null && item.isActivated() ) {
             // already activated, do nothing
             return;
         }
@@ -314,12 +314,18 @@
         final Timer timer = this.rule.getTimer();
 
         if ( timer != null ) {
+            if ( item == null ) {
+                item = agenda.createScheduledAgendaItem( leftTuple,
+                                                         context,
+                                                         this.rule,
+                                                         this.subrule );
+            }
             agenda.scheduleItem( (ScheduledAgendaItem) item,
                                  workingMemory );
             item.setActivated( true );
-//            workingMemory.removeLogicalDependencies( item,
-//                                                     context,
-//                                                     this.rule );
+            //            workingMemory.removeLogicalDependencies( item,
+            //                                                     context,
+            //                                                     this.rule );
 
             ((EventSupport) workingMemory).getAgendaEventSupport().fireActivationCreated( item,
                                                                                           workingMemory );
@@ -334,18 +340,32 @@
                 }
             }
 
-            item.setSalience( rule.getSalience().getValue( leftTuple,
-                                                           workingMemory ) ); // need to re-evaluate salience, as used fields may have changed
-            item.setPropagationContext( context ); // update the Propagation Context
+            if ( item == null ) {
+                // -----------------
+                // Lazy instantiation and addition to the Agenda of AgendGroup
+                // implementations
+                // ----------------
+                item = agenda.createAgendaItem( leftTuple,
+                                                rule.getSalience().getValue( leftTuple,
+                                                                             workingMemory ),
+                                                context,
+                                                this.rule,
+                                                this.subrule );
+                item.setSequenence( this.sequence );
+            } else {
+                item.setSalience( rule.getSalience().getValue( leftTuple,
+                                                               workingMemory ) ); // need to re-evaluate salience, as used fields may have changed
+                item.setPropagationContext( context ); // update the Propagation Context
+            }
 
             boolean added = agenda.addActivation( item );
 
             item.setActivated( added );
 
             if ( added ) {
-//                workingMemory.removeLogicalDependencies( item,
-//                                                         context,
-//                                                         this.rule );
+                //                workingMemory.removeLogicalDependencies( item,
+                //                                                         context,
+                //                                                         this.rule );
                 ((EventSupport) workingMemory).getAgendaEventSupport().fireActivationCreated( item,
                                                                                               workingMemory );
             }
@@ -395,7 +415,7 @@
                                  builder,
                                  this,
                                  workingMemories );
-        for( InternalWorkingMemory workingMemory : workingMemories ) {
+        for ( InternalWorkingMemory workingMemory : workingMemories ) {
             workingMemory.executeQueuedActions();
         }
         context.setCleanupAdapter( adapter );
@@ -470,19 +490,21 @@
         return NodeTypeEnums.RuleTerminalNode;
     }
 
-    public static class RTNCleanupAdapter implements CleanupAdapter {
+    public static class RTNCleanupAdapter
+        implements
+        CleanupAdapter {
         private RuleTerminalNode node;
-        
+
         public RTNCleanupAdapter(RuleTerminalNode node) {
             this.node = node;
         }
 
         public void cleanUp(final LeftTuple leftTuple,
                             final InternalWorkingMemory workingMemory) {
-            if( leftTuple.getLeftTupleSink() != node ) {
+            if ( leftTuple.getLeftTupleSink() != node ) {
                 return;
             }
-            
+
             final Activation activation = leftTuple.getActivation();
 
             if ( activation.isActivated() ) {



More information about the jboss-svn-commits mailing list