[jboss-svn-commits] JBL Code SVN: r28401 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 24 18:37:35 EDT 2009


Author: tirelli
Date: 2009-07-24 18:37:35 -0400 (Fri, 24 Jul 2009)
New Revision: 28401

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Behavior.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/BehaviorManager.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingLengthWindow.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingTimeWindow.java
Log:
Improving the Behavior interface

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Behavior.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Behavior.java	2009-07-24 22:12:48 UTC (rev 28400)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Behavior.java	2009-07-24 22:37:35 UTC (rev 28401)
@@ -68,8 +68,12 @@
      * @param context The behavior context object
      * @param tuple The new fact entering behavior's scope
      * @param workingMemory The working memory session reference
+     * 
+     * @return true if the propagation should continue, false otherwise. I.e., 
+     *         the behaviour has veto power over the fact propagation, and prevents
+     *         the propagation to continue if returns false on this method. 
      */
-    public void assertRightTuple(Object context,
+    public boolean assertRightTuple(Object context,
                                  RightTuple tuple,
                                  InternalWorkingMemory workingMemory);
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/BehaviorManager.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/BehaviorManager.java	2009-07-24 22:12:48 UTC (rev 28400)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/BehaviorManager.java	2009-07-24 22:37:35 UTC (rev 28401)
@@ -42,7 +42,7 @@
     public BehaviorManager() {
         this( NO_BEHAVIORS );
     }
-    
+
     /**
      * @param behaviors
      */
@@ -83,12 +83,13 @@
     public boolean assertRightTuple(final Object behaviorContext,
                                     final RightTuple rightTuple,
                                     final InternalWorkingMemory workingMemory) {
+        boolean result = true;
         for ( int i = 0; i < behaviors.length; i++ ) {
-            behaviors[i].assertRightTuple( ((Object[]) behaviorContext)[i],
-                                           rightTuple,
-                                           workingMemory );
+            result = result && behaviors[i].assertRightTuple( ((Object[]) behaviorContext)[i],
+                                                              rightTuple,
+                                                              workingMemory );
         }
-        return true;
+        return result;
     }
 
     /**

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingLengthWindow.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingLengthWindow.java	2009-07-24 22:12:48 UTC (rev 28400)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingLengthWindow.java	2009-07-24 22:37:35 UTC (rev 28401)
@@ -39,7 +39,7 @@
     Behavior {
 
     private int size;
-    
+
     public SlidingLengthWindow() {
         this( 0 );
     }
@@ -69,7 +69,7 @@
      */
     public void writeExternal(final ObjectOutput out) throws IOException {
         out.writeInt( this.size );
-        
+
     }
 
     public BehaviorType getType() {
@@ -99,12 +99,12 @@
      *
      * @see org.drools.rule.Behavior#assertRightTuple(java.lang.Object, org.drools.reteoo.RightTuple, org.drools.common.InternalWorkingMemory)
      */
-    public void assertRightTuple(final Object context,
-                                 final RightTuple rightTuple,
-                                 final InternalWorkingMemory workingMemory) {
+    public boolean assertRightTuple(final Object context,
+                                    final RightTuple rightTuple,
+                                    final InternalWorkingMemory workingMemory) {
         SlidingLengthWindowContext window = (SlidingLengthWindowContext) context;
         window.pos = (window.pos + 1) % window.rightTuples.length;
-        if( window.rightTuples[window.pos] != null ) {
+        if ( window.rightTuples[window.pos] != null ) {
             final RightTuple tuple = window.rightTuples[window.pos];
             // retract previous
             final PropagationContext propagationContext = new PropagationContextImpl( workingMemory.getNextPropagationIdCounter(),
@@ -116,9 +116,10 @@
                                                          propagationContext,
                                                          workingMemory );
             tuple.unlinkFromRightParent();
-            
+
         }
         window.rightTuples[window.pos] = rightTuple;
+        return true;
     }
 
     /**
@@ -130,12 +131,12 @@
                                   final RightTuple rightTuple,
                                   final InternalWorkingMemory workingMemory) {
         SlidingLengthWindowContext window = (SlidingLengthWindowContext) context;
-        final int last = ( window.pos == 0 ) ? window.rightTuples.length-1 : window.pos-1;
+        final int last = (window.pos == 0) ? window.rightTuples.length - 1 : window.pos - 1;
         // we start the loop on current pos because the most common scenario is to retract the
         // right tuple referenced by the current "pos" position, causing this loop to only execute
         // the first iteration
-        for( int i = window.pos; i != last; i = (i+1)%window.rightTuples.length ) {
-            if( window.rightTuples[i] == rightTuple ) {
+        for ( int i = window.pos; i != last; i = (i + 1) % window.rightTuples.length ) {
+            if ( window.rightTuples[i] == rightTuple ) {
                 window.rightTuples[i] = null;
                 break;
             }
@@ -146,7 +147,6 @@
                              InternalWorkingMemory workingMemory) {
         // do nothing
     }
-    
 
     /**
      * Length windows don't change expiration offset, so
@@ -157,7 +157,7 @@
     }
 
     public String toString() {
-        return "SlidingLengthWindow( size="+size+" )";
+        return "SlidingLengthWindow( size=" + size + " )";
     }
 
     /**
@@ -168,11 +168,11 @@
     private static class SlidingLengthWindowContext
         implements
         Externalizable {
-        
+
         public RightTuple[] rightTuples;
-        public int pos = 0;
-        
-        public SlidingLengthWindowContext( final int size ) {
+        public int          pos = 0;
+
+        public SlidingLengthWindowContext(final int size) {
             this.rightTuples = new RightTuple[size];
         }
 
@@ -180,7 +180,7 @@
                                                 ClassNotFoundException {
             this.pos = in.readInt();
             this.rightTuples = (RightTuple[]) in.readObject();
-            
+
         }
 
         public void writeExternal(ObjectOutput out) throws IOException {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingTimeWindow.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingTimeWindow.java	2009-07-24 22:12:48 UTC (rev 28400)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/SlidingTimeWindow.java	2009-07-24 22:37:35 UTC (rev 28401)
@@ -48,7 +48,7 @@
     Externalizable,
     Behavior {
 
-    private long size;
+    private long              size;
     // stateless job
     private final BehaviorJob job = new BehaviorJob();
 
@@ -110,9 +110,9 @@
      *
      * @see org.drools.rule.Behavior#assertRightTuple(java.lang.Object, org.drools.reteoo.RightTuple, org.drools.common.InternalWorkingMemory)
      */
-    public void assertRightTuple(final Object context,
-                                 final RightTuple rightTuple,
-                                 final InternalWorkingMemory workingMemory) {
+    public boolean assertRightTuple(final Object context,
+                                    final RightTuple rightTuple,
+                                    final InternalWorkingMemory workingMemory) {
         SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
         queue.queue.add( rightTuple );
         if ( queue.queue.peek() == rightTuple ) {
@@ -121,6 +121,7 @@
                                   workingMemory,
                                   queue );
         }
+        return true;
     }
 
     /**
@@ -253,7 +254,7 @@
 
     private static class BehaviorJobContext
         implements
-        JobContext, 
+        JobContext,
         Externalizable {
         public InternalWorkingMemory workingMemory;
         public Behavior              behavior;
@@ -289,7 +290,7 @@
 
         public void writeExternal(ObjectOutput out) throws IOException {
             // TODO Auto-generated method stub
-            
+
         }
 
     }



More information about the jboss-svn-commits mailing list