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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Mar 29 22:16:50 EDT 2009


Author: michael.neale at jboss.com
Date: 2009-03-29 22:16:50 -0400 (Sun, 29 Mar 2009)
New Revision: 25878

Added:
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/evaluators/BigDecimalEqualityTest.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/EqualityEvaluatorsDefinition.java
Log:
big decimal uses compareTo instead of equals for equality.

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/EqualityEvaluatorsDefinition.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/EqualityEvaluatorsDefinition.java	2009-03-30 01:50:40 UTC (rev 25877)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/evaluators/EqualityEvaluatorsDefinition.java	2009-03-30 02:16:50 UTC (rev 25878)
@@ -21,6 +21,7 @@
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.util.Date;
+import java.math.BigDecimal;
 
 import org.drools.base.BaseEvaluator;
 import org.drools.base.ValueType;
@@ -303,10 +304,18 @@
         }
     }
 
+
+
     public static class BigDecimalEqualEvaluator extends BaseEvaluator {
         /**
          *
          */
+
+        static boolean isEqual(Object value1, Object value2) {
+            if (!(value1 instanceof BigDecimal ) || !(value2 instanceof BigDecimal)) return false;
+            return  ((BigDecimal)value1).compareTo((BigDecimal) value2) == 0;
+
+        }
         private static final long     serialVersionUID = 400L;
         public final static Evaluator INSTANCE         = new BigDecimalEqualEvaluator();
 
@@ -319,11 +328,12 @@
                                 final InternalReadAccessor extractor,
                                 final Object object1, final FieldValue object2) {
             final Object value1 = extractor.getValue( workingMemory, object1 );
-            final Object value2 = object2.getBigDecimalValue();
+            final BigDecimal value2 = object2.getBigDecimalValue();
             if ( value1 == null ) {
                 return value2 == null;
             }
-            return value1.equals( value2 );
+
+            return isEqual(value1, value2);
         }
 
         public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
@@ -332,7 +342,7 @@
             if ( value == null ) {
                 return ((ObjectVariableContextEntry) context).right == null;
             }
-            return value.equals( ((ObjectVariableContextEntry) context).right );
+            return isEqual(value, ((ObjectVariableContextEntry) context).right);
         }
 
         public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
@@ -341,7 +351,8 @@
             if ( ((ObjectVariableContextEntry) context).left == null ) {
                 return value == null;
             }
-            return ((ObjectVariableContextEntry) context).left.equals( value );
+
+            return isEqual(((ObjectVariableContextEntry) context).left, value);
         }
 
         public boolean evaluate(InternalWorkingMemory workingMemory,
@@ -353,7 +364,7 @@
             if ( value1 == null ) {
                 return value2 == null;
             }
-            return value1.equals( value2 );
+            return isEqual(value1, value2);
         }
 
         public String toString() {
@@ -369,6 +380,9 @@
         private static final long     serialVersionUID = 400L;
         public final static Evaluator INSTANCE         = new BigDecimalNotEqualEvaluator();
 
+
+
+
         public BigDecimalNotEqualEvaluator() {
             super( ValueType.BIG_DECIMAL_TYPE,
                    Operator.NOT_EQUAL );
@@ -382,7 +396,7 @@
             if ( value1 == null ) {
                 return value2 != null;
             }
-            return !value1.equals( value2 );
+            return !BigDecimalEqualEvaluator.isEqual(value1, value2 );
         }
 
         public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
@@ -391,7 +405,7 @@
             if ( value == null ) {
                 return ((ObjectVariableContextEntry) context).right != null;
             }
-            return !value.equals( ((ObjectVariableContextEntry) context).right );
+            return !BigDecimalEqualEvaluator.isEqual(value, ((ObjectVariableContextEntry) context).right);
         }
 
         public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
@@ -400,7 +414,7 @@
             if ( ((ObjectVariableContextEntry) context).left == null ) {
                 return value != null;
             }
-            return !((ObjectVariableContextEntry) context).left.equals( value );
+            return !BigDecimalEqualEvaluator.isEqual(((ObjectVariableContextEntry) context).left,  value );
         }
 
         public boolean evaluate(InternalWorkingMemory workingMemory,
@@ -412,7 +426,7 @@
             if ( value1 == null ) {
                 return value2 != null;
             }
-            return !value1.equals( value2 );
+            return !BigDecimalEqualEvaluator.isEqual(value1, value2 );
         }
 
         public String toString() {

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/evaluators/BigDecimalEqualityTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/evaluators/BigDecimalEqualityTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/evaluators/BigDecimalEqualityTest.java	2009-03-30 02:16:50 UTC (rev 25878)
@@ -0,0 +1,266 @@
+package org.drools.base.evaluators;
+
+import junit.framework.TestCase;
+import org.drools.spi.InternalReadAccessor;
+import org.drools.spi.FieldValue;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.InternalFactHandle;
+import org.drools.base.ValueType;
+import org.drools.rule.VariableRestriction;
+import org.drools.rule.Declaration;
+import org.drools.reteoo.LeftTuple;
+
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+/**
+ * @author Michael Neale
+ */
+public class BigDecimalEqualityTest extends TestCase {
+
+    public void testEquality() {
+        EqualityEvaluatorsDefinition.BigDecimalEqualEvaluator d = new EqualityEvaluatorsDefinition.BigDecimalEqualEvaluator();
+
+
+        assertTrue(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("42")) ));
+        assertFalse(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("43")) ));
+
+        assertTrue(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42.0")), null, new MockFieldValue(new BigDecimal("42")) ));
+        assertFalse(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("43.0")) ));
+
+
+
+        assertTrue(d.isEqual(new BigDecimal("42"), new BigDecimal("42")));
+        assertFalse(d.isEqual(new BigDecimal("42"), new BigDecimal("43")));
+        assertFalse(d.isEqual(new BigDecimal("42"), 43));
+        assertFalse(d.isEqual(new BigDecimal("42"), "43"));
+        assertFalse(d.isEqual(new BigDecimal("42"), 42.0));
+
+        assertFalse(d.isEqual(43, 43));
+        assertFalse(d.isEqual("43", new BigDecimal("43")));
+
+
+    }
+
+    public void testNotEquals() {
+       EqualityEvaluatorsDefinition.BigDecimalNotEqualEvaluator d = new EqualityEvaluatorsDefinition.BigDecimalNotEqualEvaluator();
+        assertFalse(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("42")) ));
+        assertTrue(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("43")) ));
+
+        assertFalse(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42.0")), null, new MockFieldValue(new BigDecimal("42")) ));
+        assertTrue(d.evaluate(null, new MockInternalReadAcessor(new BigDecimal("42")), null, new MockFieldValue(new BigDecimal("43.0")) ));
+
+
+
+    }
+
+
+
+
+
+    class MockFieldValue implements FieldValue {
+        private BigDecimal val;
+
+        MockFieldValue(BigDecimal bd) {
+            this.val = bd;
+        }
+
+        public Object getValue() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public char getCharValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public BigDecimal getBigDecimalValue() {
+            return val; 
+        }
+
+        public BigInteger getBigIntegerValue() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getIntValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public byte getByteValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public short getShortValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public long getLongValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public float getFloatValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public double getDoubleValue() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean getBooleanValue() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isNull() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isBooleanField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isIntegerNumberField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isFloatNumberField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isObjectField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isCollectionField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isStringField() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+    }
+
+    class MockInternalReadAcessor implements InternalReadAccessor {
+        private Object val;
+
+        MockInternalReadAcessor(Object val) {
+            this.val = val;
+        }
+
+        public Object getValue(InternalWorkingMemory workingMemory, Object object) {
+            return val;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public char getCharValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getIntValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public byte getByteValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public short getShortValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public long getLongValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public float getFloatValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public double getDoubleValue(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean getBooleanValue(InternalWorkingMemory workingMemory, Object object) {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isNullValue(InternalWorkingMemory workingMemory, Object object) {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getHashCode(InternalWorkingMemory workingMemory, Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isGlobal() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isSelfReference() {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public Object getValue(Object object) {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public char getCharValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getIntValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public byte getByteValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public short getShortValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public long getLongValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public float getFloatValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public double getDoubleValue(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean getBooleanValue(Object object) {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public boolean isNullValue(Object object) {
+            return false;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public ValueType getValueType() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public Class<?> getExtractToClass() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public String getExtractToClassName() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public Method getNativeReadMethod() {
+            return null;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getHashCode(Object object) {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+
+        public int getIndex() {
+            return 0;  //To change body of implemented methods use File | Settings | File Templates.
+        }
+    }
+    
+}




More information about the jboss-svn-commits mailing list