[jboss-svn-commits] JBL Code SVN: r17166 - labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Dec 10 15:05:28 EST 2007


Author: mgroch
Date: 2007-12-10 15:05:28 -0500 (Mon, 10 Dec 2007)
New Revision: 17166

Added:
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MeetsEvaluatorDefinition.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MetByEvaluatorDefinition.java
Log:
support for before, meets, met-by

Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java	                        (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/BeforeEvaluatorDefinition.java	2007-12-10 20:05:28 UTC (rev 17166)
@@ -0,0 +1,259 @@
+/*
+ * Copyright 2007 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Dec 6, 2007
+ */
+package org.drools.base.evaluators;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.base.BaseEvaluator;
+import org.drools.base.ValueType;
+import org.drools.common.EventFactHandle;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
+import org.drools.rule.VariableRestriction.VariableContextEntry;
+import org.drools.spi.Evaluator;
+import org.drools.spi.Extractor;
+import org.drools.spi.FieldValue;
+
+/**
+ * The implementation of the 'before' evaluator definition
+ * 
+ * @author mgroch
+ */
+public class BeforeEvaluatorDefinition
+    implements
+    EvaluatorDefinition {
+
+    public static final Operator  BEFORE       = Operator.addOperatorToRegistry( "before",
+                                                                                  false );
+    public static final Operator  NOT_BEFORE   = Operator.addOperatorToRegistry( "before",
+                                                                                  true );
+    
+    private static final String[] SUPPORTED_IDS = { BEFORE.getOperatorString() };
+    
+    private Map<String, BeforeEvaluator> cache        = Collections.emptyMap();
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  null );
+    }
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator,
+                                  String parameterText) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  parameterText );
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Evaluator getEvaluator(final ValueType type,
+                                  final String operatorId,
+                                  final boolean isNegated,
+                                  final String parameterText) {
+        if ( this.cache == Collections.EMPTY_MAP ) {
+            this.cache = new HashMap<String, BeforeEvaluator>();
+        }
+        String key = isNegated + ":" + parameterText;
+        BeforeEvaluator eval = this.cache.get( key );
+        if ( eval == null ) {
+            eval = new BeforeEvaluator( type,
+                                       isNegated,
+                                       parameterText );
+            this.cache.put( key,
+                            eval );
+        }
+        return eval;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public String[] getEvaluatorIds() {
+        return SUPPORTED_IDS;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean isNegatable() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean operatesOnFactHandles() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean supportsType(ValueType type) {
+        // supports all types, since it operates over fact handles
+        // Note: should we change this interface to allow checking of event classes only?
+        return true;
+    }
+
+    /**
+     * Implements the 'before' evaluator itself
+     */
+    public static class BeforeEvaluator extends BaseEvaluator {
+
+    	private static final long serialVersionUID = -4778826341073034320L;
+		
+    	private long                  initRange;
+        private long                  finalRange;
+
+        public BeforeEvaluator(final ValueType type,
+                              final boolean isNegated,
+                              final String parameters) {
+            super( type,
+                   isNegated ? NOT_BEFORE : BEFORE );
+            this.parseParameters( parameters );
+        }
+        
+        @Override
+        public Object prepareObject(InternalFactHandle handle) {
+            return handle;
+        }
+        
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final Extractor extractor,
+                                final Object object1,
+                                final FieldValue object2) {
+            throw new RuntimeDroolsException( "The 'before' operator can only be used to compare one event to another, and never to compare to literal constraints." );
+        }
+
+        public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
+                                           final VariableContextEntry context,
+                                           final Object left) {
+            if ( context.rightNull ) {
+                return false;
+            }
+            long dist = ((EventFactHandle) left ).getStartTimestamp() - 
+            			((EventFactHandle)((ObjectVariableContextEntry) context).right).getEndTimestamp();
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+                                          final VariableContextEntry context,
+                                          final Object right) {
+            if ( context.extractor.isNullValue( workingMemory,
+                                                right ) ) {
+                return false;
+            }
+            long dist = ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp() - 
+            			((EventFactHandle) right ).getEndTimestamp();
+
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final Extractor extractor1,
+                                final Object object1,
+                                final Extractor extractor2,
+                                final Object object2) {
+            if ( extractor1.isNullValue( workingMemory,
+                                         object1 ) ) {
+                return false;
+            }
+            long dist = ((EventFactHandle) object2 ).getStartTimestamp() - ((EventFactHandle) object1 ).getEndTimestamp();
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public String toString() {
+            return "before[" + initRange + ", " + finalRange + "]";
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#hashCode()
+         */
+        @Override
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = super.hashCode();
+            result = PRIME * result + (int) (finalRange ^ (finalRange >>> 32));
+            result = PRIME * result + (int) (initRange ^ (initRange >>> 32));
+            return result;
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object obj) {
+            if ( this == obj ) return true;
+            if ( !super.equals( obj ) ) return false;
+            if ( getClass() != obj.getClass() ) return false;
+            final BeforeEvaluator other = (BeforeEvaluator) obj;
+            return finalRange == other.finalRange && initRange == other.initRange;
+        }
+
+        /**
+         * This methods tries to parse the string of parameters to customize 
+         * the evaluator.
+         * 
+         * @param parameters
+         */
+        private void parseParameters(String parameters) {
+            if ( parameters == null || parameters.trim().length() == 0 ) {
+                // open bounded range
+                this.initRange = 1;
+                this.finalRange = Long.MAX_VALUE;
+                return;
+            }
+
+            try {
+                String[] ranges = parameters.split( "," );
+                if ( ranges.length == 1 ) {
+                    // deterministic point in time
+                    this.initRange = Long.parseLong( ranges[0] );
+                    this.finalRange = this.initRange;
+                } else if ( ranges.length == 2 ) {
+                    // regular range
+                    this.initRange = Long.parseLong( ranges[0] );
+                    this.finalRange = Long.parseLong( ranges[1] );
+                } else {
+                    throw new RuntimeDroolsException( "[Before Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+                }
+            } catch ( NumberFormatException e ) {
+                throw new RuntimeDroolsException( "[Before Evaluator]: Not possible to parse parameters: '" + parameters + "'",
+                                                  e );
+            }
+        }
+
+    }
+
+}

Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MeetsEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MeetsEvaluatorDefinition.java	                        (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MeetsEvaluatorDefinition.java	2007-12-10 20:05:28 UTC (rev 17166)
@@ -0,0 +1,257 @@
+/*
+ * Copyright 2007 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Dec 6, 2007
+ */
+package org.drools.base.evaluators;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.base.BaseEvaluator;
+import org.drools.base.ValueType;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringMatchesEvaluator;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringNotMatchesEvaluator;
+import org.drools.common.EventFactHandle;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.LongVariableContextEntry;
+import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
+import org.drools.rule.VariableRestriction.VariableContextEntry;
+import org.drools.spi.Evaluator;
+import org.drools.spi.Extractor;
+import org.drools.spi.FieldValue;
+
+/**
+ * The implementation of the 'meets' evaluator definition
+ * 
+ * @author mgroch
+ */
+public class MeetsEvaluatorDefinition
+    implements
+    EvaluatorDefinition {
+
+    public static final Operator  MEETS       = Operator.addOperatorToRegistry( "meets",
+                                                                                  false );
+    public static final Operator  MEETS_NOT   = Operator.addOperatorToRegistry( "meets",
+                                                                                  true );
+    
+    private static final String[] SUPPORTED_IDS = { MEETS.getOperatorString() };
+    
+    private Map<String, MeetsEvaluator> cache        = Collections.emptyMap();
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  null );
+    }
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator,
+                                  String parameterText) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  parameterText );
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Evaluator getEvaluator(final ValueType type,
+                                  final String operatorId,
+                                  final boolean isNegated,
+                                  final String parameterText) {
+        if ( this.cache == Collections.EMPTY_MAP ) {
+            this.cache = new HashMap<String, MeetsEvaluator>();
+        }
+        String key = isNegated + ":" + parameterText;
+        MeetsEvaluator eval = this.cache.get( key );
+        if ( eval == null ) {
+            eval = new MeetsEvaluator( type,
+                                       isNegated,
+                                       parameterText );
+            this.cache.put( key,
+                            eval );
+        }
+        return eval;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public String[] getEvaluatorIds() {
+        return SUPPORTED_IDS;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean isNegatable() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean operatesOnFactHandles() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean supportsType(ValueType type) {
+        // supports all types, since it operates over fact handles
+        // Note: should we change this interface to allow checking of event classes only?
+        return true;
+    }
+
+    /**
+     * Implements the 'meets' evaluator itself
+     */
+    public static class MeetsEvaluator extends BaseEvaluator {
+        private static final long serialVersionUID = 1018648684053005286L;
+		
+		private long                  initRange;
+        private long                  finalRange;
+
+        public MeetsEvaluator(final ValueType type,
+                              final boolean isNegated,
+                              final String parameters) {
+            super( type,
+                   isNegated ? MEETS_NOT : MEETS );
+            this.parseParameters( parameters );
+        }
+        
+        @Override
+        public Object prepareObject(InternalFactHandle handle) {
+            return handle;
+        }
+        
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final Extractor extractor,
+                                final Object object1,
+                                final FieldValue object2) {
+            throw new RuntimeDroolsException( "The 'meets' operator can only be used to compare one event to another, and never to compare to literal constraints." );
+        }
+
+        public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
+                final VariableContextEntry context,
+                final Object left) {
+			if ( context.rightNull ) {
+			return false;
+			}
+			long dist = Math.abs(((EventFactHandle) left ).getStartTimestamp() - 
+						((EventFactHandle)((ObjectVariableContextEntry) context).right).getEndTimestamp());
+			return dist >= this.initRange && dist <= this.finalRange;
+		}
+			
+		public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+			               final VariableContextEntry context,
+			               final Object right) {
+			if ( context.extractor.isNullValue( workingMemory,
+			                     right ) ) {
+			return false;
+			}
+			long dist = Math.abs(((EventFactHandle) ((ObjectVariableContextEntry) context).left).getStartTimestamp() - 
+						((EventFactHandle) right ).getEndTimestamp());
+			
+			return dist >= this.initRange && dist <= this.finalRange;
+		}
+			
+		public boolean evaluate(InternalWorkingMemory workingMemory,
+			     final Extractor extractor1,
+			     final Object object1,
+			     final Extractor extractor2,
+			     final Object object2) {
+			if ( extractor1.isNullValue( workingMemory,
+			              object1 ) ) {
+			return false;
+			}
+			long dist = Math.abs(((EventFactHandle) object2 ).getStartTimestamp() - ((EventFactHandle) object1 ).getEndTimestamp());
+			return dist >= this.initRange && dist <= this.finalRange;
+		}
+
+        public String toString() {
+            return "meets[" + initRange + ", " + finalRange + "]";
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#hashCode()
+         */
+        @Override
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = super.hashCode();
+            result = PRIME * result + (int) (finalRange ^ (finalRange >>> 32));
+            result = PRIME * result + (int) (initRange ^ (initRange >>> 32));
+            return result;
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object obj) {
+            if ( this == obj ) return true;
+            if ( !super.equals( obj ) ) return false;
+            if ( getClass() != obj.getClass() ) return false;
+            final MeetsEvaluator other = (MeetsEvaluator) obj;
+            return finalRange == other.finalRange && initRange == other.initRange;
+        }
+
+        /**
+         * This methods tries to parse the string of parameters to customize 
+         * the evaluator.
+         * 
+         * @param parameters
+         */
+        private void parseParameters(String parameters) {
+            if ( parameters == null || parameters.trim().length() == 0 ) {
+                // exact meet
+                this.initRange = 0;
+                this.finalRange = 0;
+                return;
+            }
+
+            try {
+                String[] ranges = parameters.split( "," );
+                if ( ranges.length == 1 ) {
+                	// limit of tolerance
+                    this.initRange = 0;
+                    this.finalRange = Long.parseLong( ranges[0] );
+                } else {
+                    throw new RuntimeDroolsException( "[Meets Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+                }
+            } catch ( NumberFormatException e ) {
+                throw new RuntimeDroolsException( "[Meets Evaluator]: Not possible to parse parameters: '" + parameters + "'",
+                                                  e );
+            }
+        }
+
+    }
+
+}

Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MetByEvaluatorDefinition.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MetByEvaluatorDefinition.java	                        (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/base/evaluators/MetByEvaluatorDefinition.java	2007-12-10 20:05:28 UTC (rev 17166)
@@ -0,0 +1,257 @@
+/*
+ * Copyright 2007 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Dec 6, 2007
+ */
+package org.drools.base.evaluators;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.base.BaseEvaluator;
+import org.drools.base.ValueType;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringMatchesEvaluator;
+import org.drools.base.evaluators.MatchesEvaluatorsDefinition.StringNotMatchesEvaluator;
+import org.drools.common.EventFactHandle;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.rule.VariableRestriction.LongVariableContextEntry;
+import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
+import org.drools.rule.VariableRestriction.VariableContextEntry;
+import org.drools.spi.Evaluator;
+import org.drools.spi.Extractor;
+import org.drools.spi.FieldValue;
+
+/**
+ * The implementation of the 'met-by' evaluator definition
+ * 
+ * @author mgroch
+ */
+public class MetByEvaluatorDefinition
+    implements
+    EvaluatorDefinition {
+
+    public static final Operator  MET_BY       = Operator.addOperatorToRegistry( "metby",
+                                                                                  false );
+    public static final Operator  NOT_MET_BY   = Operator.addOperatorToRegistry( "metby",
+                                                                                  true );
+    
+    private static final String[] SUPPORTED_IDS = { MET_BY.getOperatorString() };
+    
+    private Map<String, MetByEvaluator> cache        = Collections.emptyMap();
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  null );
+    }
+
+    /**
+     * @inheridDoc
+     */
+    public Evaluator getEvaluator(ValueType type,
+                                  Operator operator,
+                                  String parameterText) {
+        return this.getEvaluator( type,
+                                  operator.getOperatorString(),
+                                  operator.isNegated(),
+                                  parameterText );
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public Evaluator getEvaluator(final ValueType type,
+                                  final String operatorId,
+                                  final boolean isNegated,
+                                  final String parameterText) {
+        if ( this.cache == Collections.EMPTY_MAP ) {
+            this.cache = new HashMap<String, MetByEvaluator>();
+        }
+        String key = isNegated + ":" + parameterText;
+        MetByEvaluator eval = this.cache.get( key );
+        if ( eval == null ) {
+            eval = new MetByEvaluator( type,
+                                       isNegated,
+                                       parameterText );
+            this.cache.put( key,
+                            eval );
+        }
+        return eval;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public String[] getEvaluatorIds() {
+        return SUPPORTED_IDS;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean isNegatable() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean operatesOnFactHandles() {
+        return true;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public boolean supportsType(ValueType type) {
+        // supports all types, since it operates over fact handles
+        // Note: should we change this interface to allow checking of event classes only?
+        return true;
+    }
+
+    /**
+     * Implements the 'met-by' evaluator itself
+     */
+    public static class MetByEvaluator extends BaseEvaluator {
+        private static final long serialVersionUID = 4628787624924047853L;
+		
+        private long                  initRange;
+        private long                  finalRange;
+
+        public MetByEvaluator(final ValueType type,
+                              final boolean isNegated,
+                              final String parameters) {
+            super( type,
+                   isNegated ? NOT_MET_BY : MET_BY );
+            this.parseParameters( parameters );
+        }
+        
+        @Override
+        public Object prepareObject(InternalFactHandle handle) {
+            return handle;
+        }
+        
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final Extractor extractor,
+                                final Object object1,
+                                final FieldValue object2) {
+            throw new RuntimeDroolsException( "The 'metby' operator can only be used to compare one event to another, and never to compare to literal constraints." );
+        }
+
+        public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
+                                           final VariableContextEntry context,
+                                           final Object left) {
+            if ( context.rightNull ) {
+                return false;
+            }
+            long dist = Math.abs(((EventFactHandle)((ObjectVariableContextEntry) context).right).getStartTimestamp() - 
+                        ((EventFactHandle) left ).getEndTimestamp());
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
+                                          final VariableContextEntry context,
+                                          final Object right) {
+            if ( context.extractor.isNullValue( workingMemory,
+                                                right ) ) {
+                return false;
+            }
+            long dist = Math.abs(((EventFactHandle) right ).getStartTimestamp() - 
+                        ((EventFactHandle) ((ObjectVariableContextEntry) context).left).getEndTimestamp());
+
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public boolean evaluate(InternalWorkingMemory workingMemory,
+                                final Extractor extractor1,
+                                final Object object1,
+                                final Extractor extractor2,
+                                final Object object2) {
+            if ( extractor1.isNullValue( workingMemory,
+                                         object1 ) ) {
+                return false;
+            }
+            long dist = Math.abs(((EventFactHandle) object1 ).getStartTimestamp() - ((EventFactHandle) object2 ).getEndTimestamp());
+            return dist >= this.initRange && dist <= this.finalRange;
+        }
+
+        public String toString() {
+            return "metby[" + initRange + ", " + finalRange + "]";
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#hashCode()
+         */
+        @Override
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = super.hashCode();
+            result = PRIME * result + (int) (finalRange ^ (finalRange >>> 32));
+            result = PRIME * result + (int) (initRange ^ (initRange >>> 32));
+            return result;
+        }
+
+        /* (non-Javadoc)
+         * @see java.lang.Object#equals(java.lang.Object)
+         */
+        @Override
+        public boolean equals(Object obj) {
+            if ( this == obj ) return true;
+            if ( !super.equals( obj ) ) return false;
+            if ( getClass() != obj.getClass() ) return false;
+            final MetByEvaluator other = (MetByEvaluator) obj;
+            return finalRange == other.finalRange && initRange == other.initRange;
+        }
+
+        /**
+         * This methods tries to parse the string of parameters to customize 
+         * the evaluator.
+         * 
+         * @param parameters
+         */
+        private void parseParameters(String parameters) {
+            if ( parameters == null || parameters.trim().length() == 0 ) {
+                // exact met-by
+                this.initRange = 0;
+                this.finalRange = 0;
+                return;
+            }
+
+            try {
+                String[] ranges = parameters.split( "," );
+                if ( ranges.length == 1 ) {
+                    // limit of tolerance
+                    this.initRange = 0;
+                    this.finalRange = Long.parseLong( ranges[0] );
+                } else {
+                    throw new RuntimeDroolsException( "[Metby Evaluator]: Not possible to parse parameters: '" + parameters + "'" );
+                }
+            } catch ( NumberFormatException e ) {
+                throw new RuntimeDroolsException( "[Metby Evaluator]: Not possible to parse parameters: '" + parameters + "'",
+                                                  e );
+            }
+        }
+
+    }
+
+}




More information about the jboss-svn-commits mailing list