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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 19 07:33:01 EDT 2007


Author: mark.proctor at jboss.com
Date: 2007-07-19 07:33:01 -0400 (Thu, 19 Jul 2007)
New Revision: 13630

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.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/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
Log:
-updated polling api for RuleAgent
-can now clear ActivationGroups and RuleFlowGroups from the main workingmemory api.

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java	2007-07-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/Agenda.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -124,11 +124,15 @@
     public void clearActivationGroup(String name);
 
     /**
-     * Clears all Activations from an Xor Group. Any Activations that are also in an Agenda Group are removed
+     * Clears all Activations from an Activation Group. Any Activations that are also in an Agenda Group are removed
      * from the Agenda Group.
      * 
      * @param activationGroup
      */
     public void clearActivationGroup(ActivationGroup activationGroup);
+    
+    public void clearRuleFlowGroup(final String name);
+    
+    public void clearRuleFlowGroup(final RuleFlowGroup ruleFlowGroup);
 
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java	2007-07-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -325,6 +325,18 @@
     public void clearAgendaGroup(String group);
 
     /**
+     * Clears the Activation Group, cancellings all its Activations
+     * @param group
+     */
+    public void clearActivationGroup(String group);
+    
+    /**
+     * Clears the RuleFlow group, cancelling all its Activations
+     * @param group
+     */
+    public void clearRuleFlowGroup(String group);
+    
+    /**
      * Starts a new process instance for the process with the given id. 
      */
     ProcessInstance startProcess(String processId);

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-07-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/RuleAgent.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -123,6 +123,11 @@
      * For logging events (important for stuff that happens in the background).
      */
     AgentEventListener          listener = getDefaultListener();
+    
+    /**
+     * Polling interval value, in seconds, used in the Timer.
+     */
+    private int secondsToRefresh;
 
 
 
@@ -246,24 +251,7 @@
         refreshRuleBase();
 
         if ( secondsToRefresh != -1 ) {
-            int interval = secondsToRefresh * 1000;
-            //now schedule it for polling
-            timer = new Timer( true );
-            timer.schedule( new TimerTask() {
-                                public void run() {
-                                    try {
-                                        
-                                        listener.debug( "Checking for updates." );
-                                        refreshRuleBase();
-                                        
-                                    } catch (Exception e) {
-                                        //don't want to stop execution here.                                        
-                                        listener.exception( e );
-                                    }
-                                }
-                            },
-                            interval,
-                            interval );
+            startPolling( secondsToRefresh );
         }
 
     }
@@ -342,16 +330,63 @@
     /**
      * Stop the polling (if it is happening)
      */
-    public void stopPolling() {
+    public synchronized void stopPolling() {
         if ( this.timer != null ) timer.cancel();
         timer = null;
     }
+    
+    /**
+     * Will start polling. If polling is already running it does nothing.
+     *
+     */
+    public synchronized void startPolling() {
+        if ( this.timer == null ) {
+            startPolling( this.secondsToRefresh );
+        }
+    }
+    
+    /**
+     * Will start polling. If polling is already happening and of the same interval
+     * it will do nothing, if the interval is different it will stop the current Timer
+     * and create a new Timer for the new interval.
+     * @param secondsToRefresh
+     */
+    public synchronized void startPolling(int secondsToRefresh) {
+        if ( this.timer != null ) {
+            if ( this.secondsToRefresh != secondsToRefresh ) {
+                stopPolling();
+            } else {
+                // do nothing.
+                return;
+            }
+        }
+        
+        this.secondsToRefresh = secondsToRefresh;
+        int interval = this.secondsToRefresh * 1000;
+        //now schedule it for polling
+        timer = new Timer( true );
+        timer.schedule( new TimerTask() {
+                            public void run() {
+                                try {
+                                    
+                                    listener.debug( "Checking for updates." );
+                                    refreshRuleBase();
+                                    
+                                } catch (Exception e) {
+                                    //don't want to stop execution here.                                        
+                                    listener.exception( e );
+                                }
+                            }
+                        },
+                        interval,
+                        interval );        
+    }
 
     boolean isNewInstance() {
         return newInstance;
     }
 
-    boolean isPolling() {
+    public synchronized boolean isPolling() {
         return this.timer != null;
     }
 

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-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -371,6 +371,14 @@
     public void clearAgendaGroup(final String group) {
         this.agenda.clearAgendaGroup( group );
     }
+    
+    public void clearActivationGroup(final String group) {
+        this.agenda.clearActivationGroup( group );
+    }
+    
+    public void clearRuleFlowGroup(final String group) {
+        this.agenda.clearRuleFlowGroup( group );
+    }    
 
     public RuleBase getRuleBase() {
         return this.ruleBase;

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-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -28,6 +28,7 @@
 import org.drools.WorkingMemory;
 import org.drools.base.DefaultKnowledgeHelper;
 import org.drools.base.SequentialKnowledgeHelper;
+import org.drools.common.RuleFlowGroupImpl.DeactivateCallback;
 import org.drools.spi.Activation;
 import org.drools.spi.ActivationGroup;
 import org.drools.spi.AgendaFilter;
@@ -392,7 +393,7 @@
                 continue;
             }
 
-            // this must be set false before removal from the XorGroup. Otherwise the XorGroup will also try to cancel the Actvation
+            // this must be set false before removal from the activationGroup. Otherwise the activationGroup will also try to cancel the Actvation
             item.setActivated( false );
 
             if ( item.getActivationGroupNode() != null ) {
@@ -446,7 +447,45 @@
         }
         activationGroup.clear();
     }
+    
+    public void clearRuleFlowGroup(final String name) {
+        final RuleFlowGroup ruleFlowGrlup = (RuleFlowGroup) this.ruleFlowGroups.get( name );
+        if ( ruleFlowGrlup != null ) {
+            clearRuleFlowGroup( ruleFlowGrlup );
+        }
+    }    
+    
+    public void clearRuleFlowGroup(final RuleFlowGroup ruleFlowGroup) {
+        final EventSupport eventsupport = (EventSupport) this.workingMemory;
 
+        
+        for ( Iterator it = ruleFlowGroup.iterator(); it.hasNext(); ) {
+            RuleFlowGroupNode node = ( RuleFlowGroupNode ) it.next();
+            AgendaItem item = ( AgendaItem ) node.getActivation();            
+            if ( item != null ) {
+                item.setActivated( false );
+                item.remove();
+                
+                if ( item.getActivationGroupNode() != null ) {
+                    item.getActivationGroupNode().getActivationGroup().removeActivation( item );
+                }                
+            }
+            
+        
+            
+            eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
+                                                                          this.workingMemory );                        
+        }
+        
+        ((InternalRuleFlowGroup) ruleFlowGroup).clear();       
+        
+        if ( ruleFlowGroup.isActive() && ruleFlowGroup.isAutoDeactivate() ) {
+                // deactivate callback
+                WorkingMemoryAction action = new DeactivateCallback( (InternalRuleFlowGroup) ruleFlowGroup );
+                this.workingMemory.queueWorkingMemoryAction( action );
+        }        
+    }    
+
     /**
      * Fire the next scheduled <code>Agenda</code> item.
      * 

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	2007-07-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleFlowGroup.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -15,6 +15,8 @@
     void addActivation(Activation activation);
 
     void removeActivation(final Activation activation);
+    
+    void clear();
 
     /**
      * Activates or deactivates this <code>RuleFlowGroup</code>.

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-19 09:54:17 UTC (rev 13629)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java	2007-07-19 11:33:01 UTC (rev 13630)
@@ -41,13 +41,13 @@
     implements
     InternalRuleFlowGroup {
 
-    private static final long serialVersionUID = 400L;
+    private static final long     serialVersionUID = 400L;
 
-    private InternalWorkingMemory 	workingMemory;
-    private final String      		name;
-    private boolean           		active           = false;
-    private final LinkedList  		list;
-    private boolean           		autoDeactivate   = true;
+    private InternalWorkingMemory workingMemory;
+    private final String          name;
+    private boolean               active           = false;
+    private final LinkedList      list;
+    private boolean               autoDeactivate   = true;
 
     /**
      * Construct a <code>RuleFlowGroupImpl</code> with the given name.
@@ -63,13 +63,13 @@
     public String getName() {
         return this.name;
     }
-    
+
     public void setWorkingMemory(InternalWorkingMemory workingMemory) {
-    	this.workingMemory = workingMemory;
+        this.workingMemory = workingMemory;
     }
-    
+
     public InternalWorkingMemory getWorkingMemory() {
-    	return this.workingMemory;
+        return this.workingMemory;
     }
 
     public void setActive(final boolean active) {
@@ -79,17 +79,17 @@
         this.active = active;
         if ( active ) {
             if ( this.list.isEmpty() ) {
-            	if ( this.autoDeactivate ) {
-                	// if the list of activations is empty and
-            		// auto-deactivate is on, deactivate this group
-    				WorkingMemoryAction action = new DeactivateCallback( this );
-    				this.workingMemory.queueWorkingMemoryAction( action );
-            	}
+                if ( this.autoDeactivate ) {
+                    // if the list of activations is empty and
+                    // auto-deactivate is on, deactivate this group
+                    WorkingMemoryAction action = new DeactivateCallback( this );
+                    this.workingMemory.queueWorkingMemoryAction( action );
+                }
             } else {
-            	triggerActivations();
+                triggerActivations();
             }
-            ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
-    			.fireRuleFlowGroupActivated(this, this.workingMemory);
+            ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireRuleFlowGroupActivated( this,
+                                                                                                      this.workingMemory );
         } else {
             final Iterator it = this.list.iterator();
             for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
@@ -99,8 +99,8 @@
                     activation.getActivationGroupNode().getActivationGroup().removeActivation( activation );
                 }
             }
-            ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
-				.fireRuleFlowGroupDeactivated(this, this.workingMemory);
+            ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireRuleFlowGroupDeactivated( this,
+                                                                                                        this.workingMemory );
         }
     }
 
@@ -129,10 +129,7 @@
     }
 
     public void clear() {
-        final Iterator it = this.list.iterator();
-        for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
-            node.getActivation().remove();
-        }
+        this.list.clear();
     }
 
     public int size() {
@@ -154,11 +151,11 @@
         final RuleFlowGroupNode node = activation.getRuleFlowGroupNode();
         this.list.remove( node );
         activation.setActivationGroupNode( null );
-        if ( this.autoDeactivate ) {
+        if ( this.active && this.autoDeactivate ) {
             if ( this.list.isEmpty() ) {
-            	// deactivate callback
-            	WorkingMemoryAction action = new DeactivateCallback( this );
-            	this.workingMemory.queueWorkingMemoryAction( action );
+                // deactivate callback
+                WorkingMemoryAction action = new DeactivateCallback( this );
+                this.workingMemory.queueWorkingMemoryAction( action );
             }
         }
     }
@@ -195,7 +192,9 @@
         setActive( true );
     }
 
-    public static class DeactivateCallback implements WorkingMemoryAction {
+    public static class DeactivateCallback
+        implements
+        WorkingMemoryAction {
         private final InternalRuleFlowGroup ruleFlowGroup;
 
         public DeactivateCallback(InternalRuleFlowGroup ruleFlowGroup) {
@@ -203,9 +202,9 @@
         }
 
         public void execute(InternalWorkingMemory workingMemory) {
-        	// check whether ruleflow group is still empty first
+            // check whether ruleflow group is still empty first
             if ( this.ruleFlowGroup.isEmpty() ) {
-            	// deactivate ruleflow group
+                // deactivate ruleflow group
                 this.ruleFlowGroup.setActive( false );
                 // only trigger next node if this RuleFlowGroup was
                 // triggered from inside a process instance




More information about the jboss-svn-commits mailing list