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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Nov 30 11:11:57 EST 2008


Author: tirelli
Date: 2008-11-30 11:11:55 -0500 (Sun, 30 Nov 2008)
New Revision: 24149

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishedByEvaluatorDefinition.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishesEvaluatorDefinition.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
Log:
JBRULES-1873: fixing and documenting operators

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishedByEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishedByEvaluatorDefinition.java	2008-11-30 13:43:01 UTC (rev 24148)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishedByEvaluatorDefinition.java	2008-11-30 16:11:55 UTC (rev 24149)
@@ -38,29 +38,64 @@
 import org.drools.time.Interval;
 
 /**
- * The implementation of the 'finishedby' evaluator definition
+ * <p>The implementation of the <code>finishedby</code> evaluator definition.</p>
+ * 
+ * <p>The <b><code>finishedby</code></b> evaluator correlates two events and matches when the current event 
+ * start timestamp happens before the correlated event start timestamp, but both end timestamps occur at
+ * the same time. This is the symmetrical opposite of <code>finishes</code> evaluator.</p> 
+ * 
+ * <p>Lets look at an example:</p>
+ * 
+ * <pre>$eventA : EventA( this finishedby $eventB )</pre>
  *
+ * <p>The previous pattern will match if and only if the $eventA starts before $eventB starts and finishes
+ * at the same time $eventB finishes. In other words:</p>
+ * 
+ * <pre> 
+ * $eventA.startTimestamp < $eventB.startTimestamp &&
+ * $eventA.endTimestamp == $eventB.endTimestamp 
+ * </pre>
+ * 
+ * <p>The <b><code>finishedby</code></b> evaluator accepts one optional parameter. If it is defined, it determines
+ * the maximum distance between the end timestamp of both events in order for the operator to match. Example:</p>
+ * 
+ * <pre>$eventA : EventA( this finishedby[ 5s ] $eventB )</pre>
+ * 
+ * Will match if and only if:
+ * 
+ * <pre> 
+ * $eventA.startTimestamp < $eventB.startTimestamp &&
+ * abs( $eventA.endTimestamp - $eventB.endTimestamp ) <= 5s
+ * </pre>
+ * 
+ * <p><b>NOTE:</b> it makes no sense to use a negative interval value for the parameter and the 
+ * engine will raise an exception if that happens.</p>
+ * 
+ * @author etirelli
  * @author mgroch
  */
 public class FinishedByEvaluatorDefinition
     implements
     EvaluatorDefinition {
 
-    public static final Operator  FINISHED_BY       = Operator.addOperatorToRegistry( "finishedby",
-                                                                                  false );
-    public static final Operator  NOT_FINISHED_BY   = Operator.addOperatorToRegistry( "finishedby",
-                                                                                  true );
+    public static final Operator             FINISHED_BY     = Operator.addOperatorToRegistry( "finishedby",
+                                                                                               false );
+    public static final Operator             NOT_FINISHED_BY = Operator.addOperatorToRegistry( "finishedby",
+                                                                                               true );
 
-    private static final String[] SUPPORTED_IDS = { FINISHED_BY.getOperatorString() };
+    private static final String[]            SUPPORTED_IDS   = {FINISHED_BY.getOperatorString()};
 
-    private Map<String, FinishedByEvaluator> cache        = Collections.emptyMap();
+    private Map<String, FinishedByEvaluator> cache           = Collections.emptyMap();
+    private volatile TimeIntervalParser      parser          = new TimeIntervalParser();
 
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-        cache  = (Map<String, FinishedByEvaluator>)in.readObject();
+    @SuppressWarnings("unchecked")
+    public void readExternal(ObjectInput in) throws IOException,
+                                            ClassNotFoundException {
+        cache = (Map<String, FinishedByEvaluator>) in.readObject();
     }
 
     public void writeExternal(ObjectOutput out) throws IOException {
-        out.writeObject(cache);
+        out.writeObject( cache );
     }
 
     /**
@@ -99,9 +134,11 @@
         String key = isNegated + ":" + parameterText;
         FinishedByEvaluator eval = this.cache.get( key );
         if ( eval == null ) {
+            Long[] params = parser.parse( parameterText );
             eval = new FinishedByEvaluator( type,
-                                       isNegated,
-                                       parameterText );
+                                            isNegated,
+                                            params,
+                                            parameterText );
             this.cache.put( key,
                             eval );
         }
@@ -142,34 +179,35 @@
      * Implements the 'finishedby' evaluator itself
      */
     public static class FinishedByEvaluator extends BaseEvaluator {
-		private static final long serialVersionUID = -5156972073099070733L;
+        private static final long serialVersionUID = -5156972073099070733L;
 
-		private long                  startMinDev, startMaxDev;
-        private long                  endDev;
+        private long              endDev;
+        private String            paramText;
 
         public FinishedByEvaluator() {
         }
 
         public FinishedByEvaluator(final ValueType type,
-                              final boolean isNegated,
-                              final String parameters) {
+                                   final boolean isNegated,
+                                   final Long[] parameters,
+                                   final String paramText) {
             super( type,
                    isNegated ? NOT_FINISHED_BY : FINISHED_BY );
-            this.parseParameters( parameters );
+            this.paramText = paramText;
+            this.setParameters( parameters );
         }
 
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-            super.readExternal(in);
-            startMinDev = in.readLong();
-            startMaxDev = in.readLong();
-            endDev   = in.readLong();
+        public void readExternal(ObjectInput in) throws IOException,
+                                                ClassNotFoundException {
+            super.readExternal( in );
+            endDev = in.readLong();
+            paramText = (String) in.readObject();
         }
 
         public void writeExternal(ObjectOutput out) throws IOException {
-            super.writeExternal(out);
-            out.writeLong(startMinDev);
-            out.writeLong(startMaxDev);
-            out.writeLong(endDev);
+            super.writeExternal( out );
+            out.writeLong( endDev );
+            out.writeObject( paramText );
         }
 
         @Override
@@ -181,15 +219,17 @@
         public boolean isTemporal() {
             return true;
         }
-        
+
         @Override
         public Interval getInterval() {
-            if( this.getOperator().isNegated() ) {
-                return new Interval( Interval.MIN, Interval.MAX );
+            if ( this.getOperator().isNegated() ) {
+                return new Interval( Interval.MIN,
+                                     Interval.MAX );
             }
-            return new Interval( Interval.MIN, 0 );
+            return new Interval( Interval.MIN,
+                                 0 );
         }
-        
+
         public boolean evaluate(InternalWorkingMemory workingMemory,
                                 final InternalReadAccessor extractor,
                                 final Object object1,
@@ -198,48 +238,45 @@
         }
 
         public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
-                final VariableContextEntry context,
-                final Object left) {
+                                           final VariableContextEntry context,
+                                           final Object left) {
 
-        	if ( context.rightNull ) {
-        		return false;
-				}
-			long distStart = ((EventFactHandle) left ).getStartTimestamp() - ((EventFactHandle)((ObjectVariableContextEntry) context).right).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) left ).getEndTimestamp() - ((EventFactHandle)((ObjectVariableContextEntry) context).right).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+            if ( context.rightNull ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) left).getStartTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) left).getEndTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
-		public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
-			               final VariableContextEntry context,
-			               final Object right) {
-			if ( context.extractor.isNullValue( workingMemory,
-			                     right ) ) {
-			return false;
-			}
-			long distStart = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp() - ((EventFactHandle) right ).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp() - ((EventFactHandle) right ).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+                                          final VariableContextEntry context,
+                                          final Object right) {
+            if ( context.extractor.isNullValue( workingMemory,
+                                                right ) ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp() - ((EventFactHandle) right).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp() - ((EventFactHandle) right).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
-		public boolean evaluate(InternalWorkingMemory workingMemory,
-			     final InternalReadAccessor extractor1,
-			     final Object object1,
-			     final InternalReadAccessor extractor2,
-			     final Object object2) {
-			if ( extractor1.isNullValue( workingMemory,
-			              object1 ) ) {
-			return false;
-			}
-			long distStart = ((EventFactHandle) object2 ).getStartTimestamp() - ((EventFactHandle) object1 ).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) object2 ).getEndTimestamp() - ((EventFactHandle) object1 ).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final InternalReadAccessor extractor1,
+                                final Object object1,
+                                final InternalReadAccessor extractor2,
+                                final Object object2) {
+            if ( extractor1.isNullValue( workingMemory,
+                                         object1 ) ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) object2).getStartTimestamp() - ((EventFactHandle) object1).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) object2).getEndTimestamp() - ((EventFactHandle) object1).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
         public String toString() {
-            return "finishedby[" + startMinDev + ", " + startMaxDev + ", " + endDev + "]";
+            return "finishedby[" + ((paramText != null) ? paramText : "") + "]";
         }
 
         /* (non-Javadoc)
@@ -250,8 +287,6 @@
             final int PRIME = 31;
             int result = super.hashCode();
             result = PRIME * result + (int) (endDev ^ (endDev >>> 32));
-            result = PRIME * result + (int) (startMaxDev ^ (startMaxDev >>> 32));
-            result = PRIME * result + (int) (startMinDev ^ (startMinDev >>> 32));
             return result;
         }
 
@@ -264,50 +299,26 @@
             if ( !super.equals( obj ) ) return false;
             if ( getClass() != obj.getClass() ) return false;
             final FinishedByEvaluator other = (FinishedByEvaluator) obj;
-            return endDev == other.endDev && startMaxDev == other.startMaxDev && startMinDev == other.startMinDev;
+            return endDev == other.endDev;
         }
 
         /**
-         * This methods tries to parse the string of parameters to customize
-         * the evaluator.
+         * This methods sets the parameters appropriately.
          *
          * @param parameters
          */
-        private void parseParameters(String parameters) {
-        	if ( parameters == null || parameters.trim().length() == 0 ) {
-                // exact matching at the end of the intervals, open bounded range for the starts
-                this.startMinDev = 1;
-                this.startMaxDev = Long.MAX_VALUE;
+        private void setParameters(Long[] parameters) {
+            if ( parameters == null || parameters.length == 0 ) {
                 this.endDev = 0;
-                return;
-            }
-
-            try {
-                String[] ranges = parameters.split( "," );
-                if ( ranges.length == 1 ) {
-                    // exact matching at the end of the intervals
-                	// deterministic point in time for deviations of the starts of the intervals
-                	this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = this.startMinDev;
-                    this.endDev = 0;
-                } else if ( ranges.length == 2 ) {
-                    // exact matching at the end of the intervals
-                	// range for deviations of the starts of the intervals
-                    this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = Long.parseLong( ranges[1] );
-                    this.endDev = 0;
-                } else if ( ranges.length == 3 ) {
-                	// max. deviation at the ends of the intervals
-                	// range for deviations of the starts of the intervals
-                    this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = Long.parseLong( ranges[1] );
-                    this.endDev = Long.parseLong( ranges[2] );
+            } else if ( parameters.length == 1 ) {
+                if( parameters[0].longValue() >= 0 ) {
+                    // defined deviation for end timestamp
+                    this.endDev = parameters[0].longValue();
                 } else {
-                    throw new RuntimeDroolsException( "[FinishedBy Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+                    throw new RuntimeDroolsException("[FinishedBy Evaluator]: Not possible to use negative parameter: '" + paramText + "'");
                 }
-            } catch ( NumberFormatException e ) {
-                throw new RuntimeDroolsException( "[FinishedBy Evaluator]: Not possible to parse parameters: '" + parameters + "'",
-                                                  e );
+            } else {
+                throw new RuntimeDroolsException( "[FinishedBy Evaluator]: Not possible to use " + parameters.length + " parameters: '" + paramText + "'" );
             }
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishesEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishesEvaluatorDefinition.java	2008-11-30 13:43:01 UTC (rev 24148)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/FinishesEvaluatorDefinition.java	2008-11-30 16:11:55 UTC (rev 24149)
@@ -38,60 +38,39 @@
 import org.drools.time.Interval;
 
 /**
- * <p>The implementation of the <code>during</code> evaluator definition.</p>
+ * <p>The implementation of the <code>finishes</code> evaluator definition.</p>
  * 
- * <p>The <b><code>during</code></b> evaluator correlates two events and matches when the current event 
- * happens during the occurrence of the event being correlated.</p> 
+ * <p>The <b><code>finishes</code></b> evaluator correlates two events and matches when the current event 
+ * start timestamp happens after the correlated event start timestamp, but both end timestamps occur at
+ * the same time.</p> 
  * 
  * <p>Lets look at an example:</p>
  * 
- * <pre>$eventA : EventA( this during $eventB )</pre>
+ * <pre>$eventA : EventA( this finishes $eventB )</pre>
  *
  * <p>The previous pattern will match if and only if the $eventA starts after $eventB starts and finishes
- * before $eventB finishes. In other words:</p>
+ * at the same time $eventB finishes. In other words:</p>
  * 
- * <pre> $eventB.startTimestamp < $eventA.startTimestamp <= $eventA.endTimestamp < $eventB.endTimestamp </pre>
- * 
- * <p>The <b><code>during</code></b> operator accepts 1, 2 or 4 optional parameters as follow:</p>
- * 
- * <ul><li>If one value is defined, this will be the maximum distance between the start timestamp of both
- * event and the maximum distance between the end timestamp of both events in order to operator match. Example:</li></lu>
- * 
- * <pre>$eventA : EventA( this during[ 5s ] $eventB )</pre>
- * 
- * Will match if and only if:
- * 
  * <pre> 
- * 0 < $eventA.startTimestamp - $eventB.startTimestamp <= 5s &&
- * 0 < $eventB.endTimestamp - $eventA.endTimestamp <= 5s
+ * $eventB.startTimestamp < $eventA.startTimestamp &&
+ * $eventA.endTimestamp == $eventB.endTimestamp 
  * </pre>
  * 
- * <ul><li>If two values are defined, the first value will be the minimum distance between the timestamps
- * of both events, while the second value will be the maximum distance between the timestamps of both events. 
- * Example:</li></lu>
+ * <p>The <b><code>finishes</code></b> evaluator accepts one optional parameter. If it is defined, it determines
+ * the maximum distance between the end timestamp of both events in order for the operator to match. Example:</p>
  * 
- * <pre>$eventA : EventA( this during[ 5s, 10s ] $eventB )</pre>
+ * <pre>$eventA : EventA( this finishes[ 5s ] $eventB )</pre>
  * 
  * Will match if and only if:
  * 
  * <pre> 
- * 5s <= $eventA.startTimestamp - $eventB.startTimestamp <= 10s &&
- * 5s <= $eventB.endTimestamp - $eventA.endTimestamp <= 10s
+ * $eventB.startTimestamp < $eventA.startTimestamp &&
+ * abs( $eventA.endTimestamp - $eventB.endTimestamp ) <= 5s
  * </pre>
  * 
- * <ul><li>If four values are defined, the first two values will be the minimum and maximum distances between the 
- * start timestamp of both events, while the last two values will be the minimum and maximum distances between the 
- * end timestamp of both events. Example:</li></lu>
+ * <p><b>NOTE:</b> it makes no sense to use a negative interval value for the parameter and the 
+ * engine will raise an exception if that happens.</p>
  * 
- * <pre>$eventA : EventA( this during[ 2s, 6s, 4s, 10s ] $eventB )</pre>
- * 
- * Will match if and only if:
- * 
- * <pre> 
- * 2s <= $eventA.startTimestamp - $eventB.startTimestamp <= 6s &&
- * 4s <= $eventB.endTimestamp - $eventA.endTimestamp <= 10s
- * </pre>
- * 
  * @author etirelli
  * @author mgroch
  */
@@ -99,22 +78,24 @@
     implements
     EvaluatorDefinition {
 
-    public static final Operator  FINISHES       = Operator.addOperatorToRegistry( "finishes",
-                                                                                  false );
-    public static final Operator  FINISHES_NOT   = Operator.addOperatorToRegistry( "finishes",
-                                                                                  true );
+    public static final Operator           FINISHES      = Operator.addOperatorToRegistry( "finishes",
+                                                                                           false );
+    public static final Operator           FINISHES_NOT  = Operator.addOperatorToRegistry( "finishes",
+                                                                                           true );
 
-    private static final String[] SUPPORTED_IDS = { FINISHES.getOperatorString() };
+    private static final String[]          SUPPORTED_IDS = {FINISHES.getOperatorString()};
 
-    private Map<String, FinishesEvaluator> cache        = Collections.emptyMap();
+    private Map<String, FinishesEvaluator> cache         = Collections.emptyMap();
+    private volatile TimeIntervalParser    parser        = new TimeIntervalParser();
 
     @SuppressWarnings("unchecked")
-    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-        cache  = (Map<String, FinishesEvaluator>)in.readObject();
+    public void readExternal(ObjectInput in) throws IOException,
+                                            ClassNotFoundException {
+        cache = (Map<String, FinishesEvaluator>) in.readObject();
     }
 
     public void writeExternal(ObjectOutput out) throws IOException {
-        out.writeObject(cache);
+        out.writeObject( cache );
     }
 
     /**
@@ -153,9 +134,11 @@
         String key = isNegated + ":" + parameterText;
         FinishesEvaluator eval = this.cache.get( key );
         if ( eval == null ) {
+            Long[] params = parser.parse( parameterText );
             eval = new FinishesEvaluator( type,
-                                       isNegated,
-                                       parameterText );
+                                          isNegated,
+                                          params,
+                                          parameterText );
             this.cache.put( key,
                             eval );
         }
@@ -196,38 +179,37 @@
      * Implements the 'finishes' evaluator itself
      */
     public static class FinishesEvaluator extends BaseEvaluator {
-		private static final long serialVersionUID = 6232789044144077522L;
+        private static final long serialVersionUID = 6232789044144077522L;
 
-		private long                  startMinDev, startMaxDev;
-        private long                  endDev;
+        private long              endDev;
+        private String            paramText;
 
         public FinishesEvaluator() {
         }
 
         public FinishesEvaluator(final ValueType type,
-                              final boolean isNegated,
-                              final String parameters) {
+                                 final boolean isNegated,
+                                 final Long[] parameters,
+                                 final String paramText) {
             super( type,
                    isNegated ? FINISHES_NOT : FINISHES );
-            this.parseParameters( parameters );
+            this.paramText = paramText;
+            this.setParameters( parameters );
         }
 
-        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-            super.readExternal(in);
-            startMinDev = in.readLong();
-            startMaxDev   = in.readLong();
-            endDev   = in.readLong();
+        public void readExternal(ObjectInput in) throws IOException,
+                                                ClassNotFoundException {
+            super.readExternal( in );
+            endDev = in.readLong();
+            paramText = (String) in.readObject();
         }
 
         public void writeExternal(ObjectOutput out) throws IOException {
-            super.writeExternal(out);
-            out.writeLong(startMinDev);
-            out.writeLong(startMaxDev);
-            out.writeLong(endDev);
+            super.writeExternal( out );
+            out.writeLong( endDev );
+            out.writeObject( paramText );
         }
 
-
-
         @Override
         public Object prepareObject(InternalFactHandle handle) {
             return handle;
@@ -237,15 +219,17 @@
         public boolean isTemporal() {
             return true;
         }
-        
+
         @Override
         public Interval getInterval() {
-            if( this.getOperator().isNegated() ) {
-                return new Interval( Interval.MIN, Interval.MAX );
+            if ( this.getOperator().isNegated() ) {
+                return new Interval( Interval.MIN,
+                                     Interval.MAX );
             }
-            return new Interval( 0, Interval.MAX );
+            return new Interval( 0,
+                                 Interval.MAX );
         }
-        
+
         public boolean evaluate(InternalWorkingMemory workingMemory,
                                 final InternalReadAccessor extractor,
                                 final Object object1,
@@ -254,48 +238,45 @@
         }
 
         public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
-                final VariableContextEntry context,
-                final Object left) {
+                                           final VariableContextEntry context,
+                                           final Object left) {
 
-        	if ( context.rightNull ) {
-        		return false;
-				}
-			long distStart = ((EventFactHandle)((ObjectVariableContextEntry) context).right).getStartTimestamp() - ((EventFactHandle) left ).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) left ).getEndTimestamp() - ((EventFactHandle)((ObjectVariableContextEntry) context).right).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+            if ( context.rightNull ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getStartTimestamp() - ((EventFactHandle) left).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) left).getEndTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).right).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
-		public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
-			               final VariableContextEntry context,
-			               final Object right) {
-			if ( context.extractor.isNullValue( workingMemory,
-			                     right ) ) {
-			return false;
-			}
-			long distStart = ((EventFactHandle) right ).getStartTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp() - ((EventFactHandle) right ).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+                                          final VariableContextEntry context,
+                                          final Object right) {
+            if ( context.extractor.isNullValue( workingMemory,
+                                                right ) ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) right).getStartTimestamp() - ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp() - ((EventFactHandle) right).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
-		public boolean evaluate(InternalWorkingMemory workingMemory,
-			     final InternalReadAccessor extractor1,
-			     final Object object1,
-			     final InternalReadAccessor extractor2,
-			     final Object object2) {
-			if ( extractor1.isNullValue( workingMemory,
-			              object1 ) ) {
-			return false;
-			}
-			long distStart = ((EventFactHandle) object1 ).getStartTimestamp() - ((EventFactHandle) object2 ).getStartTimestamp();
-			long distEnd = Math.abs(((EventFactHandle) object2 ).getEndTimestamp() - ((EventFactHandle) object1 ).getEndTimestamp());
-			return this.getOperator().isNegated() ^ ( distStart >= this.startMinDev && distStart <= this.startMaxDev
-					&& distEnd <= this.endDev );
-		}
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final InternalReadAccessor extractor1,
+                                final Object object1,
+                                final InternalReadAccessor extractor2,
+                                final Object object2) {
+            if ( extractor1.isNullValue( workingMemory,
+                                         object1 ) ) {
+                return false;
+            }
+            long distStart = ((EventFactHandle) object1).getStartTimestamp() - ((EventFactHandle) object2).getStartTimestamp();
+            long distEnd = Math.abs( ((EventFactHandle) object2).getEndTimestamp() - ((EventFactHandle) object1).getEndTimestamp() );
+            return this.getOperator().isNegated() ^ (distStart > 0 && distEnd <= this.endDev);
+        }
 
         public String toString() {
-            return "finishes[" + startMinDev + ", " + startMaxDev + ", " + endDev + "]";
+            return "finishes[" + ((paramText != null) ? paramText : "") + "]";
         }
 
         /* (non-Javadoc)
@@ -306,8 +287,6 @@
             final int PRIME = 31;
             int result = super.hashCode();
             result = PRIME * result + (int) (endDev ^ (endDev >>> 32));
-            result = PRIME * result + (int) (startMaxDev ^ (startMaxDev >>> 32));
-            result = PRIME * result + (int) (startMinDev ^ (startMinDev >>> 32));
             return result;
         }
 
@@ -320,50 +299,26 @@
             if ( !super.equals( obj ) ) return false;
             if ( getClass() != obj.getClass() ) return false;
             final FinishesEvaluator other = (FinishesEvaluator) obj;
-            return endDev == other.endDev && startMaxDev == other.startMaxDev && startMinDev == other.startMinDev;
+            return endDev == other.endDev;
         }
 
         /**
-         * This methods tries to parse the string of parameters to customize
-         * the evaluator.
+         * This methods sets the parameters appropriately.
          *
          * @param parameters
          */
-        private void parseParameters(String parameters) {
-        	if ( parameters == null || parameters.trim().length() == 0 ) {
-                // exact matching at the end of the intervals, open bounded range for the starts
-                this.startMinDev = 1;
-                this.startMaxDev = Long.MAX_VALUE;
+        private void setParameters(Long[] parameters) {
+            if ( parameters == null || parameters.length == 0 ) {
                 this.endDev = 0;
-                return;
-            }
-
-            try {
-                String[] ranges = parameters.split( "," );
-                if ( ranges.length == 1 ) {
-                    // exact matching at the end of the intervals
-                	// deterministic point in time for deviations of the starts of the intervals
-                	this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = this.startMinDev;
-                    this.endDev = 0;
-                } else if ( ranges.length == 2 ) {
-                    // exact matching at the end of the intervals
-                	// range for deviations of the starts of the intervals
-                    this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = Long.parseLong( ranges[1] );
-                    this.endDev = 0;
-                } else if ( ranges.length == 3 ) {
-                	// max. deviation at the ends of the intervals
-                	// range for deviations of the starts of the intervals
-                    this.startMinDev = Long.parseLong( ranges[0] );
-                    this.startMaxDev = Long.parseLong( ranges[1] );
-                    this.endDev = Long.parseLong( ranges[2] );
+            } else if ( parameters.length == 1 ) {
+                if( parameters[0].longValue() >= 0 ) {
+                    // defined deviation for end timestamp
+                    this.endDev = parameters[0].longValue();
                 } else {
-                    throw new RuntimeDroolsException( "[Finishes Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+                    throw new RuntimeDroolsException("[Finishes Evaluator]: Not possible to use negative parameter: '" + paramText + "'");
                 }
-            } catch ( NumberFormatException e ) {
-                throw new RuntimeDroolsException( "[Finishes Evaluator]: Not possible to parse parameters: '" + parameters + "'",
-                                                  e );
+            } else {
+                throw new RuntimeDroolsException( "[Finishes Evaluator]: Not possible to use " + parameters.length + " parameters: '" + paramText + "'" );
             }
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java	2008-11-30 13:43:01 UTC (rev 24148)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TemporalEvaluatorFactoryTest.java	2008-11-30 16:11:55 UTC (rev 24149)
@@ -414,6 +414,110 @@
                           ValueType.OBJECT_TYPE );
     }
 
+    public void testFinishes() {
+        registry.addEvaluatorDefinition( DuringEvaluatorDefinition.class.getName() );
+
+        EventFactHandle foo = new EventFactHandle( 1,
+                                                   "foo",
+                                                   1,
+                                                   2,
+                                                   10 );
+        EventFactHandle bar = new EventFactHandle( 2,
+                                                   "bar",
+                                                   1,
+                                                   5,
+                                                   7 );
+        EventFactHandle drool = new EventFactHandle( 1,
+                                                     "drool",
+                                                     1,
+                                                     2,
+                                                     10 );
+        EventFactHandle mole = new EventFactHandle( 1,
+                                                    "mole",
+                                                    1,
+                                                    7,
+                                                    6 );
+
+        final Object[][] data = {
+                 {bar,   "finishes", foo, Boolean.TRUE}, 
+                 {drool, "finishes", foo, Boolean.FALSE}, 
+                 {mole,  "finishes", foo, Boolean.FALSE}, 
+                 {foo,   "finishes", bar, Boolean.FALSE},
+                 
+                 {bar,   "not finishes", foo, Boolean.FALSE}, 
+                 {drool, "not finishes", foo, Boolean.TRUE}, 
+                 {mole,  "not finishes", foo, Boolean.TRUE}, 
+                 {foo,   "not finishes", bar, Boolean.TRUE},
+                 
+                 {bar,   "finishes[1]", foo, Boolean.TRUE}, 
+                 {drool, "finishes[1]", foo, Boolean.FALSE}, 
+                 {mole,  "finishes[1]", foo, Boolean.TRUE}, 
+                 {foo,   "finishes[1]", bar, Boolean.FALSE},
+                 
+                 {bar,   "not finishes[1]", foo, Boolean.FALSE}, 
+                 {drool, "not finishes[1]", foo, Boolean.TRUE}, 
+                 {mole,  "not finishes[1]", foo, Boolean.FALSE}, 
+                 {foo,   "not finishes[1]", bar, Boolean.TRUE},
+                 
+                 {mole,  "finishes[3]", foo, Boolean.TRUE}, 
+                };
+
+        runEvaluatorTest( data,
+                          ValueType.OBJECT_TYPE );
+    }
+
+    public void testFinishedBy() {
+        registry.addEvaluatorDefinition( DuringEvaluatorDefinition.class.getName() );
+
+        EventFactHandle foo = new EventFactHandle( 1,
+                                                   "foo",
+                                                   1,
+                                                   2,
+                                                   10 );
+        EventFactHandle bar = new EventFactHandle( 2,
+                                                   "bar",
+                                                   1,
+                                                   5,
+                                                   7 );
+        EventFactHandle drool = new EventFactHandle( 1,
+                                                     "drool",
+                                                     1,
+                                                     2,
+                                                     10 );
+        EventFactHandle mole = new EventFactHandle( 1,
+                                                    "mole",
+                                                    1,
+                                                    7,
+                                                    6 );
+
+        final Object[][] data = {
+                 {foo, "finishedby", bar, Boolean.TRUE}, 
+                 {foo, "finishedby", drool, Boolean.FALSE}, 
+                 {foo, "finishedby", mole, Boolean.FALSE}, 
+                 {bar, "finishedby", foo, Boolean.FALSE},
+                 
+                 {foo, "not finishedby", bar, Boolean.FALSE}, 
+                 {foo, "not finishedby", drool, Boolean.TRUE}, 
+                 {foo, "not finishedby", mole, Boolean.TRUE}, 
+                 {bar, "not finishedby", foo, Boolean.TRUE},
+                 
+                 {foo, "finishedby[1]", bar, Boolean.TRUE}, 
+                 {foo, "finishedby[1]", drool, Boolean.FALSE}, 
+                 {foo, "finishedby[1]", mole, Boolean.TRUE}, 
+                 {bar, "finishedby[1]", foo, Boolean.FALSE},
+                 
+                 {foo, "not finishedby[1]", bar, Boolean.FALSE}, 
+                 {foo, "not finishedby[1]", drool, Boolean.TRUE}, 
+                 {foo, "not finishedby[1]", mole, Boolean.FALSE}, 
+                 {bar, "not finishedby[1]", foo, Boolean.TRUE},
+                 
+                 {foo, "finishedby[3]", mole, Boolean.TRUE}, 
+                };
+
+        runEvaluatorTest( data,
+                          ValueType.OBJECT_TYPE );
+    }
+
     private void runEvaluatorTest(final Object[][] data,
                                   final ValueType valueType) {
         final InternalReadAccessor extractor = new MockExtractor();




More information about the jboss-svn-commits mailing list