Author: ruschein
Date: 2011-09-15 08:59:03 -0700 (Thu, 15 Sep 2011)
New Revision: 26807

Modified:
   
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/Equation.java
   
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/EquationParser.java
   
core3/api/trunk/equations-api/src/test/java/org/cytoscape/equations/EquationTest.java
   
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationCompilerImpl.java
   
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationParserImpl.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/EqnSupport.java
Log:
Fixed a problem with default values for variable references in equations.

Modified: 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/Equation.java
===================================================================
--- 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/Equation.java
   2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/Equation.java
   2011-09-15 15:59:03 UTC (rev 26807)
@@ -1,7 +1,7 @@
 /*
   File: Equation.java
 
-  Copyright (c) 2010, The Cytoscape Consortium (www.cytoscape.org)
+  Copyright (c) 2010-2011, The Cytoscape Consortium (www.cytoscape.org)
 
   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published
@@ -38,27 +38,31 @@
 public final class Equation {
        private final String equation;
        private final Set<String> variableReferences;
+       private final Map<String, Object> defaultVariableValues;
        private final Object[] code;
        private final int[] sourceLocations;
        private final Class type;
 
        /**
         *  Constructs an <code>Equation</code>.
-        *  @param equation            the string representing this equation
-        *  @param variableReferences  other variables that are referenced by 
this equation
-        *  @param code                the instruction sequence representing 
the compiled equation
-        *  @param sourceLocations     the starting points in the original 
equation for each node in the parse tree that resulted from
-        *                             the original equation
-        *  @param type                the type of the equation, String.class, 
Boolean.class or Double.class
+        *  @param equation              the string representing this equation
+        *  @param variableReferences    other variables that are referenced by 
this equation
+        *  @param defaultVariableValues 
+        *  @param code                  the instruction sequence representing 
the compiled equation
+        *  @param sourceLocations       the starting points in the original 
equation for each node
+        *                               in the parse tree that resulted from 
the original equation
+        *  @param type                  the type of the equation, 
String.class, Boolean.class or Double.class
         */
-       public Equation(final String equation, final Set<String> 
variableReferences, final Object[] code,
-                      final int[] sourceLocations, final Class type)
+       public Equation(final String equation, final Set<String> 
variableReferences,
+                       final Map<String, Object> defaultVariableValues, final 
Object[] code,
+                       final int[] sourceLocations, final Class type)
        {
-               this.equation           = equation;
-               this.variableReferences = variableReferences;
-               this.code               = code;
-               this.sourceLocations    = sourceLocations;
-               this.type               = type;
+               this.equation              = equation;
+               this.variableReferences    = variableReferences;
+               this.defaultVariableValues = defaultVariableValues;
+               this.code                  = code;
+               this.sourceLocations       = sourceLocations;
+               this.type                  = type;
        }
 
        /** Returns a textual representation of an <code>Equation</code>.
@@ -86,6 +90,11 @@
         */
        public Set<String> getVariableReferences() { return variableReferences; 
}
 
+       /** Returns default values for variable references, if any.
+        *  @return a map of variable names to their default values, if any
+        */
+       public Map<String, Object> getDefaultVariableValues() { return 
defaultVariableValues; }
+
        /** Returns the compiled code (not Java byte code!) for the VM 
representing this <code>Equation</code>.
         *  @return the code representing this <code>Equation</code>
         */

Modified: 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/EquationParser.java
===================================================================
--- 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/EquationParser.java
     2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/api/trunk/equations-api/src/main/java/org/cytoscape/equations/EquationParser.java
     2011-09-15 15:59:03 UTC (rev 26807)
@@ -40,43 +40,48 @@
         *  @param func  the function that will be registered
         *  @throws IllegalArgumentException will be thrown if "func" is null 
or the function has previously been registered
         */
-       public void registerFunction(final Function func) throws 
IllegalArgumentException;
+       void registerFunction(final Function func) throws 
IllegalArgumentException;
 
        /**
         *  @return the function associated with the name "functionName" or 
null if no such function exists
         */
-       public Function getFunction(final String functionName);
+       Function getFunction(final String functionName);
 
        /**
         *  @return the set of currently registered functions
         */
-       public Set<Function> getRegisteredFunctions();
+       Set<Function> getRegisteredFunctions();
 
        /**
         *  @param eqn                  a valid attribute equation which must 
start with an equal sign
         *  @param attribNameToTypeMap  a list of existing attribute names and 
their types
         *  @return true if the parse succeeded otherwise false
         */
-       public boolean parse(final String eqn, final Map<String, Class<?>> 
attribNameToTypeMap);
+       boolean parse(final String eqn, final Map<String, Class<?>> 
attribNameToTypeMap);
 
        /**
         *  @return the result type of the parsed equation if the parse 
succeeded, otherwise null
         */
-       public Class<?> getType();
+       Class<?> getType();
 
        /**
         *  If parse() failed, this will return the last error messages.
         *  @return the last error message of null
         */
-       public String getErrorMsg();
+       String getErrorMsg();
 
        /**
         *  @return all the variable names that have been detected in the most 
recently parsed equation
         */
-       public Set<String> getVariableReferences();
+       Set<String> getVariableReferences();
 
        /**
+        *  @return a map of variable names to their default values, if any
+        */
+       Map<String, Object> getDefaultVariableValues();
+
+       /**
         *  @return the parse tree.  Must only be called if parse() returns 
true!
         */
-       public Node getParseTree();
+       Node getParseTree();
 }

Modified: 
core3/api/trunk/equations-api/src/test/java/org/cytoscape/equations/EquationTest.java
===================================================================
--- 
core3/api/trunk/equations-api/src/test/java/org/cytoscape/equations/EquationTest.java
       2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/api/trunk/equations-api/src/test/java/org/cytoscape/equations/EquationTest.java
       2011-09-15 15:59:03 UTC (rev 26807)
@@ -3,6 +3,7 @@
 
 import java.util.HashSet;
 import java.util.Set;
+import java.util.TreeMap;
 
 import static org.junit.Assert.*;
 import org.junit.Before;
@@ -22,7 +23,8 @@
                variableReferences.add("B");
                code = new Object[2];
                sourceLocations = new int[] { 12, 117 };
-               eqn = new Equation("=$A+$B", variableReferences, code, 
sourceLocations, Long.class);
+               eqn = new Equation("=$A+$B", variableReferences, new 
TreeMap<String, Object>(), code,
+                                  sourceLocations, Long.class);
        }
 
        @Test
@@ -52,7 +54,9 @@
 
        @Test
        public void testEqualsWithExpectedSuccess() {
-               final Equation other = new Equation("=$A+$B", 
variableReferences, code, sourceLocations, Long.class);
+               final Equation other =
+                       new Equation("=$A+$B", variableReferences, new 
TreeMap<String, Object>(),
+                                    code, sourceLocations, Long.class);
                assertTrue("equals() failed!", eqn.equals(other));
        }
 

Modified: 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationCompilerImpl.java
===================================================================
--- 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationCompilerImpl.java
    2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationCompilerImpl.java
    2011-09-15 15:59:03 UTC (rev 26807)
@@ -76,7 +76,9 @@
                        code[i]            = codeAndSourceLocation.getCode();
                        sourceLocations[i] = 
codeAndSourceLocation.getSourceLocation();
                }
-               this.equation = new Equation(equation, 
parser.getVariableReferences(), code, sourceLocations, parser.getType());
+               this.equation = new Equation(equation, 
parser.getVariableReferences(),
+                                            parser.getDefaultVariableValues(), 
code, sourceLocations,
+                                            parser.getType());
 
                errorMsg = null;
                return true;
@@ -101,7 +103,8 @@
                        throw new IllegalStateException("internal error in 
Equation.getErrorEquation().  This should *never* happen!");
 
                final Equation errorEquation = getEquation();
-               return new Equation(equation, 
errorEquation.getVariableReferences(), errorEquation.getCode(),
+               return new Equation(equation, 
errorEquation.getVariableReferences(),
+                                   errorEquation.getDefaultVariableValues(), 
errorEquation.getCode(),
                                    errorEquation.getSourceLocations(), type);
        }
 

Modified: 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationParserImpl.java
===================================================================
--- 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationParserImpl.java
      2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/impl/trunk/equations-impl/src/main/java/org/cytoscape/equations/internal/EquationParserImpl.java
      2011-09-15 15:59:03 UTC (rev 26807)
@@ -1,7 +1,7 @@
 /*
   File: EquationParserImpl.java
 
-  Copyright (c) 2010, The Cytoscape Consortium (www.cytoscape.org)
+  Copyright (c) 2010-2011, The Cytoscape Consortium (www.cytoscape.org)
 
   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published
@@ -35,6 +35,7 @@
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.cytoscape.equations.EquationParser;
@@ -53,6 +54,7 @@
        private Node parseTree;
        private Map<String, Class<?>> variableNameToTypeMap;
        private Set<String> variableReferences;
+       private Map<String, Object> defaultVariableValues;
        private Set<Function> registeredFunctions;
 
        public EquationParserImpl() {
@@ -100,6 +102,7 @@
                this.formula = formula;
                this.variableNameToTypeMap = variableNameToTypeMap;
                this.variableReferences = new TreeSet<String>();
+               this.defaultVariableValues = new TreeMap<String, Object>();
                this.tokeniser = new Tokeniser(formula.substring(1));
                this.lastErrorMessage = null;
 
@@ -139,6 +142,8 @@
 
        public Set<String> getVariableReferences() { return variableReferences; 
}
 
+       public Map<String, Object> getDefaultVariableValues() { return 
defaultVariableValues; }
+
        /**
         *  @return the parse tree.  Must only be called if parse() returns 
true!
         */
@@ -240,11 +245,12 @@
                        if (token != Token.IDENTIFIER)
                                throw new IllegalStateException(sourceLocation 
+ ": identifier expected!");
 
-                       final Class<?> varRefType = 
variableNameToTypeMap.get(tokeniser.getIdent());
+                       final String ident = tokeniser.getIdent();
+                       final Class<?> varRefType = 
variableNameToTypeMap.get(ident);
                        if (varRefType == null)
                                throw new IllegalStateException(sourceLocation 
+ ": unknown variable reference name: \""
-                                                               + 
tokeniser.getIdent() + "\"!");
-                       variableReferences.add(tokeniser.getIdent());
+                                                               + ident + 
"\"!");
+                       variableReferences.add(ident);
 
                        Object defaultValue = null;
                        if (usingOptionalBraces) {
@@ -273,6 +279,8 @@
 
                                if (token != Token.CLOSE_BRACE)
                                        throw new 
IllegalStateException(sourceLocation + ": closing brace expected!");
+
+                               defaultVariableValues.put(ident, defaultValue);
                        }
 
                        return new IdentNode(varRefStartPos, 
tokeniser.getIdent(), defaultValue, varRefType);

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/EqnSupport.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/EqnSupport.java
 2011-09-15 15:10:45 UTC (rev 26806)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/EqnSupport.java
 2011-09-15 15:59:03 UTC (rev 26807)
@@ -136,6 +136,7 @@
                        currentlyActiveAttributes.add(columnName);
 
                final Collection<String> attribReferences = 
equation.getVariableReferences();
+               final Map<String, Object> defaultValues = 
equation.getDefaultVariableValues();
 
                final Map<String, IdentDescriptor> nameToDescriptorMap = new 
TreeMap<String, IdentDescriptor>();
                for (final String attribRef : attribReferences) {
@@ -144,20 +145,25 @@
                                continue;
                        }
 
-                       final Object attribValue = tableImpl.getValue(key, 
attribRef);
+                       Object attribValue = tableImpl.getValue(key, attribRef);
                        if (attribValue == null) {
-                               currentlyActiveAttributes.clear();
-                               try {
-                                       lastInternalError.append(
-                                               "Missing value for referenced 
attribute \""
-                                               + attribRef + "\"!");
-                               } catch (Exception e) {
-                                       // Intentionally empty!
+                               final Object defaultValue = 
defaultValues.get(attribRef);
+                               if (defaultValue != null)
+                                       attribValue = defaultValue;
+                               else {
+                                       currentlyActiveAttributes.clear();
+                                       try {
+                                               lastInternalError.append(
+                                                       "Missing value for 
referenced attribute \""
+                                                       + attribRef + "\"!");
+                                       } catch (Exception e) {
+                                               // Intentionally empty!
+                                       }
+                                       logger.warn("Missing value for \"" + 
attribRef
+                                                   + "\" while evaluating an 
equation (ID:" + key
+                                                   + ", attribute name:" + 
columnName + ")");
+                                       return null;
                                }
-                               logger.warn("Missing value for \"" + attribRef
-                                           + "\" while evaluating an equation 
(ID:" + key
-                                           + ", attribute name:" + columnName 
+ ")");
-                               return null;
                        }
 
                        try {

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to