[jboss-svn-commits] JBL Code SVN: r5990 - in labs/jbossrules/trunk/drools-core/src/main/java/org/drools: base/dataproviders base/resolvers common util

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Aug 28 11:40:01 EDT 2006


Author: mark.proctor at jboss.com
Date: 2006-08-28 11:39:54 -0400 (Mon, 28 Aug 2006)
New Revision: 5990

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodInvoker.java
Removed:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/ArgumentValueDescr.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodDataProvider.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/DeclarationVariable.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/GlobalVariable.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ListValue.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/LiteralValue.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/MapValue.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ValueHandler.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ReflectiveVisitor.java
Log:
JBRULES-448 Refactor ValueHandler resolvers to be standalone
-Seperated MethodInvoker from MethodDataProvider

Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/ArgumentValueDescr.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/ArgumentValueDescr.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/ArgumentValueDescr.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -1,55 +0,0 @@
-package org.drools.base.dataproviders;
-
-import java.io.Serializable;
-
-/**
- * This holds the value of an argument that has been parsed. 
- * The argument would then be passed to a method, or function etc. 
- * 
- * @author Michael Neale
- *
- */
-public class ArgumentValueDescr implements Serializable {
-
-	private static final long serialVersionUID = -8921442520702424678L;
-	
-	/** Obviously if it was in quotes, its a string literal (which could be anything) */
-	public static final int STRING = 1;
-	
-	/** Means true integer, not Javas interpretation of it */
-	public static final int INTEGRAL = 2;
-	
-	/** Means a decimal number, which may or may not be floating */
-	public static final int DECIMAL = 4;
-	
-	/** If its none of the above, then its a variable */
-	public static final int VARIABLE = 8;
-	
-	public static final int BOOLEAN = 16;
-	
-	public static final int NULL = 32;
-	
-	private final int type;
-	private final String value;
-	
-	/**
-	 * @param type One of the constant types.
-	 * @param value
-	 */
-	public ArgumentValueDescr(int type, String value) {
-		this.type = type;
-		this.value = value;
-	}
-
-	public int getType() {
-		return type;
-	}
-
-	public String getValue() {
-		return value;
-	}
-
-	
-	
-	
-}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodDataProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodDataProvider.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodDataProvider.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -1,19 +1,11 @@
 package org.drools.base.dataproviders;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.math.BigDecimal;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
-import org.drools.RuntimeDroolsException;
 import org.drools.WorkingMemory;
-import org.drools.base.evaluators.DateFactory;
 import org.drools.rule.Declaration;
 import org.drools.spi.DataProvider;
 import org.drools.spi.PropagationContext;
@@ -22,243 +14,30 @@
 public class MethodDataProvider
     implements
     DataProvider {
-
-    private final Declaration[]  requiredDeclarations;
-    private final boolean        variableIsDeclaration;
-    private final Declaration    variableDeclaration;
-    private final String         variableName;
-
-    private final ValueHandler[] valueHandlers;
-
-    private final Method         method;
-    private final Class          variableClass;
-
-    public MethodDataProvider(String variableName,
-                              String methodName,
-                              List arguments,
-                              Map declarations,
-                              Map globals) {
-
-        List requiredDecs = new ArrayList();
-
-        //work out where variable comes from, is it a dec or a global
-        this.variableName = variableName;
-        if ( declarations.containsKey( variableName ) ) {
-            variableDeclaration = (Declaration) declarations.get( variableName );
-            requiredDecs.add( variableDeclaration );
-            variableIsDeclaration = true;
-            this.variableClass = variableDeclaration.getExtractor().getExtractToClass();
-        } else if ( globals.containsKey( variableName ) ) {
-            variableIsDeclaration = false;
-            this.variableClass = (Class) globals.get( variableName );
-            variableDeclaration = null;
-        } else {
-            variableDeclaration = null;
-            throw new IllegalArgumentException( "The variable name [" + variableName + "] was not a global or declaration." );
-        }
-
-        //now handle arguments
-        List argumentData = new ArrayList();
-
-        for ( Iterator iter = arguments.iterator(); iter.hasNext(); ) {
-            ArgumentValueDescr desc = (ArgumentValueDescr) iter.next();
-            if ( desc.getType() == ArgumentValueDescr.VARIABLE ) {
-                if ( declarations.containsKey( desc.getValue() ) ) {
-                    Declaration dec = (Declaration) declarations.get( desc.getValue() );
-                    requiredDecs.add( dec );
-                    argumentData.add( new DeclaredVariable( dec ) );
-                } else if ( globals.containsKey( desc.getValue() ) ) {
-                    argumentData.add( new GlobalVariable( desc.getValue() ) );
-                } else {
-                    throw new IllegalArgumentException( "Uknown variable: " + desc.getValue() );
-                }
-            } else {
-                // handling a literal
-                argumentData.add( new LiteralValue( desc.getValue() ) );
-            }
-        }
-
-        //now find the method
-        this.method = configureMethod( methodName,
-                                       variableClass,
-                                       arguments.size() );
-
-        valueHandlers = (ValueHandler[]) argumentData.toArray( new ValueHandler[argumentData.size()] );
-        requiredDeclarations = new Declaration[requiredDecs.size()];
-        requiredDecs.toArray( requiredDeclarations );
+    private final MethodInvoker method;
+    
+    public MethodDataProvider(MethodInvoker method) {
+        this.method = method;
     }
+     
 
-    /**
-     * work out what method we will be calling at runtime, based on the name and number of parameters.
-     */
-    private Method configureMethod(String methodName,
-                                   Class variableClass,
-                                   int numOfArgs) {
-        Method[] methods = this.variableClass.getMethods();
-        for ( int i = 0; i < methods.length; i++ ) {
-            if ( methods[i].getName().equals( methodName ) ) {
-                if ( methods[i].getParameterTypes().length == numOfArgs ) {
-                    return methods[i];
-                }
-            }
-        }
-        throw new IllegalArgumentException("Unable to resolve the method: [" 
-                                           + methodName + "] on class: [" + variableClass.getName() 
-                                           + "] with " + numOfArgs + " parameters." );
-    }
-
     public Declaration[] getRequiredDeclarations() {
-        return requiredDeclarations;
+        return this.method.getRequiredDeclarations();
     }
 
     public Iterator getResults(Tuple tuple,
                                WorkingMemory wm,
                                PropagationContext ctx) {
-        
-        //get the variable value that we are operating on
-        Object variable = null;
-        if ( variableIsDeclaration ) {
-            variable = tuple.get( this.variableDeclaration ).getObject();
+        Object result = this.method;
+        if ( result instanceof Collection ) {
+            return ((Collection) result).iterator();
+        } else if ( result instanceof Iterator ) {
+            return (Iterator) result;
         } else {
-            variable = wm.getGlobal( this.variableName );
-        }
-
-        if (variable == null) {
-            throw new IllegalArgumentException("Unable to resolve the variable: [" + this.variableName + "]");
-        }
-                
-        //the types we have to convert the arguments to
-        Class[] parameterTypes = this.method.getParameterTypes();
-
-        //the args values that we will pass
-        Object[] args = new Object[this.valueHandlers.length];
-
-        //now we need to set all the values, convert if literal
-        for ( int i = 0; i < this.valueHandlers.length; i++ ) {
-            ValueHandler handler = valueHandlers[i];
-            if ( handler instanceof LiteralValue ) {
-                String text = (String) handler.getValue( tuple,
-                                                         wm );
-                Class type = parameterTypes[i];
-                if ( type == String.class ) {
-                    args[i] = text;
-                } else {
-                    args[i] = convert( text,
-                                       type );
-                }
-            } else {
-                args[i] = handler.getValue( tuple,
-                                            wm );
-            }
-        }
-
-        //now the actual invoking of the method
-        try {
-            Object result = this.method.invoke( variable,
-                                                args );
-            if ( result instanceof Collection ) {
-                return ((Collection) result).iterator();
-            } else if ( result instanceof Iterator ) {
-                return (Iterator) result;
-            } else {
-                List resultAsList = new ArrayList( 1 );
-                resultAsList.add( result );
-                return resultAsList.iterator();
-            }
-        } catch ( IllegalArgumentException e ) {
-            throw new RuntimeDroolsException( e );
-        } catch ( IllegalAccessException e ) {
-            throw new RuntimeDroolsException( e );
-        } catch ( InvocationTargetException e ) {
-            throw new RuntimeDroolsException( e );
-        }
-
+            List resultAsList = new ArrayList( 1 );
+            resultAsList.add( result );
+            return resultAsList.iterator();
+        }        
     }
 
-    /** Attempt to convert text to the target class type */
-    private Object convert(String text,
-                           Class type) {
-        if ( type == Integer.class || type == int.class ) {
-            return new Integer( text );
-        } else if ( text == "null" ) {
-            return null;
-        } else if ( type == Character.class || type == char.class ) {
-            return (new Character( text.charAt( 0 ) ));
-        } else if ( type == Short.class || type == short.class ) {
-            return new Short( text );
-        } else if ( type == Long.class || type == long.class ) {
-            return new Long( text );
-        } else if ( type == Float.class || type == float.class ) {
-            return new Float( text );
-        } else if ( type == Double.class || type == double.class ) {
-            return new Double( text );
-        } else if ( type == Boolean.class || type == boolean.class ) {
-            return new Boolean( text );
-        } else if ( type == Date.class ) {
-            return DateFactory.parseDate( text );
-        } else if ( type == BigDecimal.class ) {
-            return new BigDecimal( text );
-        } else if ( type == BigInteger.class ) {
-            return new BigInteger( text );
-        } else {
-            throw new IllegalArgumentException( "Unable to convert [" + text + "] to type: [" + type.getName() + "]" );
-        }
-    }
-
-    static interface ValueHandler {
-
-        Object getValue(Tuple tuple,
-                        WorkingMemory wm);
-    }
-
-    static class GlobalVariable
-        implements
-        ValueHandler {
-        public String globalName;
-
-        public GlobalVariable(String name) {
-            this.globalName = name;
-        }
-
-        public Object getValue(Tuple tuple,
-                               WorkingMemory wm) {
-            return wm.getGlobal( globalName );
-
-        }
-    }
-
-    static class DeclaredVariable
-        implements
-        ValueHandler {
-
-        private Declaration declaration;
-
-        public DeclaredVariable(Declaration dec) {
-            this.declaration = dec;
-        }
-
-        public Object getValue(Tuple tuple,
-                               WorkingMemory wm) {
-            return tuple.get( this.declaration ).getObject();
-        }
-
-    }
-
-    static class LiteralValue
-        implements
-        ValueHandler {
-
-        private String value;
-
-        public LiteralValue(String value) {
-            this.value = value;
-        }
-
-        public Object getValue(Tuple tuple,
-                               WorkingMemory wm) {
-            return value;
-        }
-
-    }
-
 }

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodInvoker.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodInvoker.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/dataproviders/MethodInvoker.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -0,0 +1,158 @@
+package org.drools.base.dataproviders;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.WorkingMemory;
+import org.drools.base.evaluators.DateFactory;
+import org.drools.base.resolvers.LiteralValue;
+import org.drools.base.resolvers.ValueHandler;
+import org.drools.rule.Declaration;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.Tuple;
+
+public class MethodInvoker {
+    private final ValueHandler   instanceValueHandler;
+    private final Method         method;
+    private final ValueHandler[] valueHandlers;
+    private final Declaration[]  requiredDeclarations;
+
+    /**
+     * Method invoker for static method
+     */
+    public MethodInvoker(String methodName,
+                         Class clazz,
+                         ValueHandler[] valueHandlers,
+                         Declaration[] requiredDeclarations) {
+        this( methodName, clazz, null, valueHandlers, requiredDeclarations );
+        // @todo : defensive errors for referencing non static method
+    }
+    
+    /**
+     * Method invoker for an instance
+     */
+    public MethodInvoker(String methodName,
+                         Class clazz,
+                         ValueHandler instanceValueHandler,
+                         ValueHandler[] valueHandlers,
+                         Declaration[] requiredDeclarations) {
+        this.instanceValueHandler = instanceValueHandler;
+        this.valueHandlers = valueHandlers;
+        this.requiredDeclarations = requiredDeclarations;
+
+        this.method = configureMethod( clazz,
+                                       methodName,
+                                       valueHandlers.length );
+    }
+
+    /**
+     * work out what method we will be calling at runtime, based on the name and number of parameters.
+     */
+    private Method configureMethod(Class clazz,
+                                   String methodName,
+                                   int numOfArgs) {
+        Method[] methods = clazz.getMethods();
+        for ( int i = 0; i < methods.length; i++ ) {
+            if ( methods[i].getName().equals( methodName ) ) {
+                if ( methods[i].getParameterTypes().length == numOfArgs ) {
+                    return methods[i];
+                }
+            }
+        }
+        throw new IllegalArgumentException( "Unable to resolve the method: [" + methodName + "] on class: [" + clazz.getName() + "] with " + numOfArgs + " parameters." );
+    }
+
+    public Declaration[] getRequiredDeclarations() {
+        return requiredDeclarations;
+    }
+
+    public Object invoke(Tuple tuple,
+                         WorkingMemory wm,
+                         PropagationContext ctx) {
+
+        //get the instance that we are operating on
+        Object instance = this.instanceValueHandler.getValue( tuple,
+                                                              wm );
+
+        if ( instance == null ) {
+            throw new IllegalArgumentException( "Unable to resolve the variable: [" + this.instanceValueHandler + "]" );
+        }
+
+        //the types we have to convert the arguments to
+        Class[] parameterTypes = this.method.getParameterTypes();
+
+        //the args values that we will pass
+        Object[] args = new Object[this.valueHandlers.length];
+
+        //now we need to set all the values, convert if literal
+        for ( int i = 0; i < this.valueHandlers.length; i++ ) {
+            ValueHandler handler = valueHandlers[i];
+            if ( handler instanceof LiteralValue ) {
+                String text = (String) handler.getValue( tuple,
+                                                         wm );
+                Class type = parameterTypes[i];
+                if ( type == String.class ) {
+                    args[i] = text;
+                } else {
+                    args[i] = convert( text,
+                                       type );
+                }
+            } else {
+                args[i] = handler.getValue( tuple,
+                                            wm );
+            }
+        }
+
+        Object result = null;
+        //now the actual invoking of the method
+        try {
+            result = this.method.invoke( instance,
+                                         args );
+            // @todo : should wrap and return an exception that the user understands
+        } catch ( IllegalArgumentException e ) {
+            throw new RuntimeDroolsException( e );
+        } catch ( IllegalAccessException e ) {
+            throw new RuntimeDroolsException( e );
+        } catch ( InvocationTargetException e ) {
+            throw new RuntimeDroolsException( e );
+        }
+
+        return result;
+    }
+
+    /** 
+     * Attempt to convert text to the target class type 
+     */
+    private Object convert(String text,
+                           Class type) {
+        if ( type == Integer.class || type == int.class ) {
+            return new Integer( text );
+        } else if ( text == "null" ) {
+            return null;
+        } else if ( type == Character.class || type == char.class ) {
+            return (new Character( text.charAt( 0 ) ));
+        } else if ( type == Short.class || type == short.class ) {
+            return new Short( text );
+        } else if ( type == Long.class || type == long.class ) {
+            return new Long( text );
+        } else if ( type == Float.class || type == float.class ) {
+            return new Float( text );
+        } else if ( type == Double.class || type == double.class ) {
+            return new Double( text );
+        } else if ( type == Boolean.class || type == boolean.class ) {
+            return new Boolean( text );
+        } else if ( type == Date.class ) {
+            return DateFactory.parseDate( text );
+        } else if ( type == BigDecimal.class ) {
+            return new BigDecimal( text );
+        } else if ( type == BigInteger.class ) {
+            return new BigInteger( text );
+        } else {
+            throw new IllegalArgumentException( "Unable to convert [" + text + "] to type: [" + type.getName() + "]" );
+        }
+    }
+}

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/DeclarationVariable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/DeclarationVariable.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/DeclarationVariable.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -11,7 +11,11 @@
     implements
     ValueHandler {
 
+    private static final long serialVersionUID = 320L;
+    
     private Declaration declaration;
+    
+    private Object cachedValue = ValueHandler.EMPTY;
 
     public DeclarationVariable(final Declaration dec) {
         this.declaration = dec;
@@ -19,7 +23,43 @@
 
     public Object getValue(final Tuple tuple,
                            final WorkingMemory wm) {
-        return tuple.get( this.declaration ).getObject();
+        if ( cachedValue == ValueHandler.EMPTY ) {
+            this.cachedValue = tuple.get( this.declaration ).getObject(); 
+        }
+        return  this.cachedValue;
     }
+    
+    public void reset() {
+        this.cachedValue = ValueHandler.EMPTY;
+    }
+    
+    public String toString() {
+        return "[DeclarationVariable " + this.declaration + "]";
+    }
 
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + this.declaration.hashCode();
+        return result;
+    }
+
+    public boolean equals(Object object) {
+        if ( this == object ) {
+            return true;
+        }
+        
+        if ( object == null || getClass() != object.getClass() ) {
+            return false;
+        }
+        
+        final DeclarationVariable other = (DeclarationVariable) object;
+        
+        return this.declaration.equals( other.declaration );
+    }
+    
+    
+    
+    
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/GlobalVariable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/GlobalVariable.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/GlobalVariable.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -9,15 +9,50 @@
 public class GlobalVariable
     implements
     ValueHandler {
-    public String globalName;
 
+    private static final long serialVersionUID = 320L;
+
+    public String             globalName;
+    
+    private Object cachedValue = ValueHandler.EMPTY;
+
     public GlobalVariable(final String name) {
         this.globalName = name;
     }
 
     public Object getValue(final Tuple tuple,
                            final WorkingMemory wm) {
-        return wm.getGlobal( this.globalName );
+        if ( this.cachedValue == ValueHandler.EMPTY ) {
+            this.cachedValue = wm.getGlobal( this.globalName );
+        }
+        return this.cachedValue;
+    }
+    
+    public void reset() {
+        this.cachedValue = ValueHandler.EMPTY;
+    }    
+    
+    public String toString() {
+        return "[GlobalVariable name=" + this.globalName + "]";
+    }
 
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + this.globalName.hashCode();
+        return result;
     }
+
+    public boolean equals(Object object) {
+        if ( this == object ) {
+            return true;
+        }
+        if ( object == null || getClass() != object.getClass() ) {
+            return false;
+        }
+        
+        final GlobalVariable other = (GlobalVariable) object;
+        return this.globalName.equals( other.globalName );
+    }
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ListValue.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ListValue.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ListValue.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -13,8 +13,12 @@
 public class ListValue
     implements
     ValueHandler {
+    
+    private static final long serialVersionUID = 320L;
 
     private final List list;
+    
+    private Object cachedValue = ValueHandler.EMPTY;    
 
     public ListValue(final List list) {
         this.list = list;
@@ -22,13 +26,46 @@
 
     public Object getValue(final Tuple tuple,
                            final WorkingMemory wm) {
-        final List resolvedList = new ArrayList( this.list.size() );
-
-        for ( final Iterator it = this.list.iterator(); it.hasNext(); ) {
-            resolvedList.add( ((ValueHandler) it.next()).getValue( tuple,
-                                                                   wm ) );
+        if ( this.cachedValue == ValueHandler.EMPTY ) {        
+            final List resolvedList = new ArrayList( this.list.size() );
+    
+            for ( final Iterator it = this.list.iterator(); it.hasNext(); ) {
+                resolvedList.add( ((ValueHandler) it.next()).getValue( tuple,
+                                                                       wm ) );
+            }
+            
+            this.cachedValue = resolvedList;
         }
+        return this.cachedValue;
+    }
+    
+    public void reset() {
+        this.cachedValue = ValueHandler.EMPTY;
+    }
+    
+    public String toString() {
+        return "[ListValue list=" + this.list + "]";
+    }
 
-        return resolvedList;
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + this.list.hashCode();
+        return result;
     }
+
+    public boolean equals(Object object) {
+        if ( this == object ) {
+            return true;
+        }
+        
+        if ( object == null || getClass() != object.getClass()) {
+            return false;
+        }
+        
+        final ListValue other = (ListValue) object;
+        return this.list.equals( other.list );
+    }
+    
+    
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/LiteralValue.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/LiteralValue.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/LiteralValue.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -9,8 +9,10 @@
 public class LiteralValue
     implements
     ValueHandler {
+    
+    private static final long serialVersionUID = 320L;
 
-    private String value;
+    private String value;    
 
     public LiteralValue(final String value) {
         this.value = value;
@@ -20,5 +22,33 @@
                            final WorkingMemory wm) {
         return this.value;
     }
+    
+    public void reset() {
+        // N/A
+    }    
+    
+    public String toString() {
+        return "LiteralValue value=" + this.value + "]";
+    }
 
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * this.value.hashCode();
+        return result;
+    }
+
+    public boolean equals(Object object) {
+        if ( this == object ) {
+            return true;
+        }
+        if ( object == null || getClass() != object.getClass()) {
+            return false;
+        }
+        final LiteralValue other = (LiteralValue) object;
+        return this.value.equals( other.value );
+    }
+    
+    
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/MapValue.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/MapValue.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/MapValue.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -3,6 +3,7 @@
  */
 package org.drools.base.resolvers;
 
+import java.io.Serializable;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -12,8 +13,12 @@
 public class MapValue
     implements
     ValueHandler {
+    
+    private static final long serialVersionUID = 320L;
 
     private final KeyValuePair[] pairs;
+    
+    private Object cachedValue = ValueHandler.EMPTY;    
 
     public MapValue(final KeyValuePair[] pairs) {
         this.pairs = pairs;
@@ -21,21 +26,30 @@
 
     public Object getValue(final Tuple tuple,
                            final WorkingMemory wm) {
-        final Map map = new HashMap();
-
-        for ( int i = 0, length = this.pairs.length; i < length; i++ ) {
-            final ValueHandler key = this.pairs[i].getKey();
-            final ValueHandler value = this.pairs[i].getValue();
-            map.put( key.getValue( tuple,
-                                   wm ),
-                     value.getValue( tuple,
-                                     wm ) );
+        if ( this.cachedValue == ValueHandler.EMPTY ) {            
+            final Map map = new HashMap();
+    
+            for ( int i = 0, length = this.pairs.length; i < length; i++ ) {
+                final ValueHandler key = this.pairs[i].getKey();
+                final ValueHandler value = this.pairs[i].getValue();
+                map.put( key.getValue( tuple,
+                                       wm ),
+                         value.getValue( tuple,
+                                         wm ) );
+            }
+            this.cachedValue = map;
         }
 
-        return map;
+        return this.cachedValue;
     }
 
-    static class KeyValuePair {
+    public void reset() {
+        this.cachedValue = ValueHandler.EMPTY;
+    }    
+    
+    static class KeyValuePair implements Serializable {
+        private static final long serialVersionUID = 320L;
+        
         private ValueHandler key;
         private ValueHandler value;
 
@@ -60,6 +74,42 @@
         public ValueHandler getValue() {
             return this.value;
         }
+        
+        public String toString() {
+            return this.key + "/" + this.value;
+        }
 
+        public int hashCode() {
+            final int PRIME = 31;
+            int result = 1;
+            result = PRIME * result + this.key.hashCode();
+            result = PRIME * result + this.value.hashCode();
+            return result;
+        }                
+
+        public boolean equals(Object object) {
+            if ( this == object ) {
+                return true;
+            }
+            
+            if ( object == null ||  getClass() != object.getClass() ) {
+                return false;
+            }
+            final KeyValuePair other = (KeyValuePair) object;
+            return this.key.equals( other.key ) && this.value.equals( other.value );
+        }                        
     }
+    
+    public String toString() {
+        StringBuffer buffer = new StringBuffer();
+        buffer.append( "MapValue pairs=" );
+        for ( int i = 0, length = this.pairs.length; i < length; i++ ) {
+            buffer.append( this.pairs[i] );            
+        }
+        buffer.append( "]" );
+        
+        return buffer.toString();
+    }
+    
+    
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ValueHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ValueHandler.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/resolvers/ValueHandler.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -3,11 +3,32 @@
  */
 package org.drools.base.resolvers;
 
+import java.io.Serializable;
+
 import org.drools.WorkingMemory;
 import org.drools.spi.Tuple;
 
-public interface ValueHandler {
+public interface ValueHandler
+    extends
+    Serializable {
 
-    Object getValue(Tuple tuple,
-                    WorkingMemory wm);
+    public static final Object EMPTY = new Serializable() {
+                                    };
+
+    /**
+     * Returns a value resolved. Declarations are resolved from the tuple, globals from the working memory and literals are just returned as is.
+     * The returned value is cached until reset() is called.
+     * 
+     * @param tuple
+     * @param wm
+     * @return
+     */
+    public Object getValue(Tuple tuple,
+                           WorkingMemory wm);
+
+    /**
+     * NULL the cached value
+     *
+     */
+    public void reset();
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaGroupImpl.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -38,6 +38,9 @@
 public class AgendaGroupImpl
     implements
     AgendaGroup {
+    
+    private static final long        serialVersionUID = 320L;
+    
     private final String              name;
 
     /** Items in the agenda. */

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AgendaItem.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -45,7 +45,7 @@
     /**
      * 
      */
-    private static final long        serialVersionUID = -4040142320609432740L;
+    private static final long        serialVersionUID = 320L;
 
     /** The tuple. */
     private final Tuple              tuple;

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ScheduledAgendaItem.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -44,7 +44,7 @@
     /**
      * 
      */
-    private static final long        serialVersionUID = -860479286586425918L;
+    private static final long        serialVersionUID = 320L;
 
     private LinkedListNode           previous;
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ReflectiveVisitor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ReflectiveVisitor.java	2006-08-28 15:09:11 UTC (rev 5989)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ReflectiveVisitor.java	2006-08-28 15:39:54 UTC (rev 5990)
@@ -18,6 +18,7 @@
 
 import java.lang.reflect.Method;
 
+import org.drools.RuntimeDroolsException;
 import org.drools.Visitor;
 
 /**
@@ -45,7 +46,7 @@
                                (Object[]) null );
             }
         } catch ( final Exception e ) {
-            e.printStackTrace();
+            throw new RuntimeDroolsException(e.toString() + " : " + object, e.getCause());
         }
     }
 
@@ -86,7 +87,7 @@
             } catch ( final Exception e ) {
                 // Shouldn't happen as long as all Visitors extend this class
                 // and this class continues to implement visitObject(Object).
-                e.printStackTrace();
+                throw new RuntimeDroolsException(e.toString() + " : " + clazz, e.getCause());
             }
         }
         return method;




More information about the jboss-svn-commits mailing list