[jboss-svn-commits] JBL Code SVN: r12927 - in labs/jbossrules/trunk: drools-compiler/src/test/resources/org/drools/integrationtests and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 28 11:20:58 EDT 2007


Author: tirelli
Date: 2007-06-28 11:20:58 -0400 (Thu, 28 Jun 2007)
New Revision: 12927

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_fireLimit.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
Log:
JBRULES-313: adding fire limit option

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-06-28 14:51:03 UTC (rev 12926)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java	2007-06-28 15:20:58 UTC (rev 12927)
@@ -2945,6 +2945,52 @@
         }
     }
 
+    public void testFireLimit() throws Exception {
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_fireLimit.drl" ) ) );
+        final Package pkg = builder.getPackage();
+
+        final RuleBase ruleBase = getRuleBase();
+        ruleBase.addPackage( pkg );
+        final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        final List results = new ArrayList();
+        workingMemory.setGlobal( "results",
+                                 results );
+
+        workingMemory.insert( new Integer( 0 ) );
+        workingMemory.fireAllRules();
+
+        assertEquals( 20,
+                      results.size() );
+        for( int i = 0; i < 20; i++ ) {
+            assertEquals( new Integer( i ), results.get( i ) );
+        }
+        results.clear();
+        
+        workingMemory.insert( new Integer( 0 ) );
+        workingMemory.fireAllRules( 10 );
+
+        assertEquals( 10,
+                      results.size() );
+        for( int i = 0; i < 10; i++ ) {
+            assertEquals( new Integer( i ), results.get( i ) );
+        }
+        results.clear();
+        
+        workingMemory.insert( new Integer( 0 ) );
+        workingMemory.fireAllRules( -1 );
+
+        assertEquals( 20,
+                      results.size() );
+        for( int i = 0; i < 20; i++ ) {
+            assertEquals( new Integer( i ), results.get( i ) );
+        }
+        results.clear();
+        
+        
+    }
+
     
     
 }
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_fireLimit.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_fireLimit.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_fireLimit.drl	2007-06-28 15:20:58 UTC (rev 12927)
@@ -0,0 +1,21 @@
+package org.drools;
+
+global java.util.List results;
+
+rule "fire"
+when
+    $old : Number( $val : intValue )
+then
+    results.add( $old );
+    insert( new Integer( $val + 1 ) );
+    retract( $old );
+end
+
+rule "stop"
+	salience 10
+when
+	$old : Number( intValue == 20 )
+then
+    retract( $old );
+    drools.halt();
+end
\ 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-06-28 14:51:03 UTC (rev 12926)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java	2007-06-28 15:20:58 UTC (rev 12927)
@@ -162,8 +162,24 @@
      */
     void fireAllRules(AgendaFilter agendaFilter) throws FactException;
       
+    /**
+     * Fire all items on the agenda until empty or at most 'fireLimit' rules have fired
+     * 
+     * @throws FactException
+     *             If an error occurs.
+     */
+    void fireAllRules( int fireLimit ) throws FactException;
 
     /**
+     * Fire all items on the agenda using the given AgendaFiler
+     * until empty or at most 'fireLimit' rules have fired
+     * 
+     * @throws FactException
+     *             If an error occurs.
+     */
+    void fireAllRules(final AgendaFilter agendaFilter, int fireLimit ) throws FactException;
+
+    /**
      * Retrieve the object associated with a <code>FactHandle</code>.
      * 
      * @see #containsObject

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-06-28 14:51:03 UTC (rev 12926)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2007-06-28 15:20:58 UTC (rev 12927)
@@ -404,10 +404,18 @@
      * @see WorkingMemory
      */
     public synchronized void fireAllRules() throws FactException {
-        fireAllRules( null );
+        fireAllRules( null, -1 );
     }
 
-    public synchronized void fireAllRules(final AgendaFilter agendaFilter) throws FactException {
+    public synchronized void fireAllRules( int fireLimit ) throws FactException {
+        fireAllRules( null, fireLimit );
+    }
+
+    public synchronized void fireAllRules( final AgendaFilter agendaFilter ) throws FactException {
+        fireAllRules( agendaFilter, -1 );
+    }
+
+    public synchronized void fireAllRules(final AgendaFilter agendaFilter, int fireLimit ) throws FactException {
         // If we're already firing a rule, then it'll pick up
         // the firing for any other assertObject(..) that get
         // nested inside, avoiding concurrent-modification
@@ -430,7 +438,8 @@
             try {
                 this.firing = true;
 
-                while ( (!halt) && this.agenda.fireNextItem( agendaFilter ) ) {
+                while ( continueFiring( fireLimit ) && this.agenda.fireNextItem( agendaFilter ) ) {
+                    fireLimit = updateFireLimit( fireLimit );
                     noneFired = false;
                     if ( !this.actionQueue.isEmpty() ) {
                         executeQueuedActions();
@@ -439,26 +448,34 @@
             } finally {
                 this.firing = false;
                 if ( noneFired ) {
-                    doOtherwise( agendaFilter );
+                    doOtherwise( agendaFilter, fireLimit );
                 }
 
             }
         }
     }
+    
+    private final boolean continueFiring( final int fireLimit ) {
+        return (!halt) && (fireLimit != 0 );
+    }
+    
+    private final int updateFireLimit( final int fireLimit ) {
+        return fireLimit > 0 ? fireLimit-1 : fireLimit;
+    }
 
     /**
      * This does the "otherwise" phase of processing.
      * If no items are fired, then it will assert a temporary "Otherwise"
      * fact and allow any rules to fire to handle "otherwise" cases.
      */
-    private void doOtherwise(final AgendaFilter agendaFilter) {
+    private void doOtherwise(final AgendaFilter agendaFilter, int fireLimit ) {
         final FactHandle handle = this.insert( new Otherwise() );
         if ( !this.actionQueue.isEmpty() ) {
             executeQueuedActions();
         }
 
-        while ( (!halt) && this.agenda.fireNextItem( agendaFilter ) ) {
-            ;
+        while ( continueFiring( fireLimit ) && this.agenda.fireNextItem( agendaFilter ) ) {
+            fireLimit = updateFireLimit( fireLimit );
         }
 
         this.retract( handle );




More information about the jboss-svn-commits mailing list