Author: ruschein
Date: 2010-05-27 14:42:28 -0700 (Thu, 27 May 2010)
New Revision: 20338

Added:
   corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Degrees.java
   corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Tan.java
   corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/
   
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java
Modified:
   corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java
   corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Cos.java
   corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Sin.java
Log:
New framework for the testing of built-in functions.

Modified: corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java    
2010-05-27 20:49:12 UTC (rev 20337)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java    
2010-05-27 21:42:28 UTC (rev 20338)
@@ -49,6 +49,7 @@
                        eqnParser.registerFunction(new Combin());
                        eqnParser.registerFunction(new Cos());
                        eqnParser.registerFunction(new Count());
+                       eqnParser.registerFunction(new Degrees());
                        eqnParser.registerFunction(new Exp());
                        eqnParser.registerFunction(new First());
                        eqnParser.registerFunction(new GeoMean());

Modified: corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Cos.java
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Cos.java      
2010-05-27 20:49:12 UTC (rev 20337)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Cos.java      
2010-05-27 21:42:28 UTC (rev 20338)
@@ -47,7 +47,7 @@
         *  Used to provide help for users.
         *  @return a description of what this function does
         */
-       public String getFunctionSummary() { return "Returns the cosine of a 
number."; }
+       public String getFunctionSummary() { return "Returns the cosine of an 
angle given in radians."; }
 
        /**
         *  Used to provide help for users.

Copied: 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Degrees.java 
(from rev 20337, 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Radians.java)
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Degrees.java  
                        (rev 0)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Degrees.java  
2010-05-27 21:42:28 UTC (rev 20338)
@@ -0,0 +1,99 @@
+/*
+  File: Degrees.java
+
+  Copyright (c) 2010, 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
+  by the Free Software Foundation; either version 2.1 of the License, or
+  any later version.
+
+  This library is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+  documentation provided hereunder is on an "as is" basis, and the
+  Institute for Systems Biology and the Whitehead Institute
+  have no obligations to provide maintenance, support,
+  updates, enhancements or modifications.  In no event shall the
+  Institute for Systems Biology and the Whitehead Institute
+  be liable to any party for direct, indirect, special,
+  incidental or consequential damages, including lost profits, arising
+  out of the use of this software and its documentation, even if the
+  Institute for Systems Biology and the Whitehead Institute
+  have been advised of the possibility of such damage.  See
+  the GNU Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with this library; if not, write to the Free Software Foundation,
+  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.equations.builtins;
+
+
+import java.util.ArrayList;
+import java.util.List;
+import org.cytoscape.equations.Function;
+import org.cytoscape.equations.FunctionUtil;
+
+
+public class Degrees implements Function {
+       /**
+        *  Used to parse the function string.  This name is treated in a 
case-insensitive manner!
+        *  @return the name by which you must call the function when used in 
an attribute equation.
+        */
+       public String getName() { return "DEGREES"; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of what this function does
+        */
+       public String getFunctionSummary() { return "Returns the value in 
degrees converted from radians."; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of how to use this function
+        */
+       public String getUsageDescription() { return "Call this with 
\"DEGREES(radians)\""; }
+
+       public Class getReturnType() { return Double.class; }
+
+       /**
+        *  @return Double.class or null if there is not exactly 1 arg or the 
arg is not of type Double, Long, String or Boolean
+        */
+       public Class validateArgTypes(final Class[] argTypes) {
+               if (argTypes.length != 1 || 
!FunctionUtil.isScalarArgType(argTypes[0]))
+                       return null;
+
+               return Double.class;
+       }
+
+       /**
+        *  @param args the function arguments which must be either one object 
of type Double or Long
+        *  @return the result of the function evaluation which is the natural 
logarithm of the first argument
+        */
+       public Object evaluateFunction(final Object[] args) {
+               final double angleInRadians = 
FunctionUtil.getArgAsDouble(args[0]);
+               return angleInRadians * 180.0 / Math.PI;
+       }
+
+       /**
+        *  Used with the equation builder.
+        *
+        *  @param leadingArgs the types of the arguments that have already 
been selected by the user.
+        *  @return the set of arguments (must be a collection of String.class, 
Long.class, Double.class,
+        *           Boolean.class and List.class) that are candidates for the 
next argument.  An empty
+        *           set indicates that no further arguments are valid.
+        */
+       public List<Class> getPossibleArgTypes(final Class[] leadingArgs) {
+               if (leadingArgs.length == 0) {
+                       final List<Class> possibleNextArgs = new 
ArrayList<Class>();
+                       possibleNextArgs.add(Double.class);
+                       possibleNextArgs.add(Long.class);
+                       possibleNextArgs.add(String.class);
+                       possibleNextArgs.add(Boolean.class);
+                       return possibleNextArgs;
+               }
+
+               return null;
+       }
+}

Modified: corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Sin.java
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Sin.java      
2010-05-27 20:49:12 UTC (rev 20337)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Sin.java      
2010-05-27 21:42:28 UTC (rev 20338)
@@ -47,7 +47,7 @@
         *  Used to provide help for users.
         *  @return a description of what this function does
         */
-       public String getFunctionSummary() { return "Returns the sine of a 
number."; }
+       public String getFunctionSummary() { return "Returns the sine of an 
angle given in radians."; }
 
        /**
         *  Used to provide help for users.

Copied: corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Tan.java 
(from rev 20337, 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Sin.java)
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Tan.java      
                        (rev 0)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Tan.java      
2010-05-27 21:42:28 UTC (rev 20338)
@@ -0,0 +1,104 @@
+/*
+  File: Tan.java
+
+  Copyright (c) 2010, 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
+  by the Free Software Foundation; either version 2.1 of the License, or
+  any later version.
+
+  This library is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+  documentation provided hereunder is on an "as is" basis, and the
+  Institute for Systems Biology and the Whitehead Institute
+  have no obligations to provide maintenance, support,
+  updates, enhancements or modifications.  In no event shall the
+  Institute for Systems Biology and the Whitehead Institute
+  be liable to any party for direct, indirect, special,
+  incidental or consequential damages, including lost profits, arising
+  out of the use of this software and its documentation, even if the
+  Institute for Systems Biology and the Whitehead Institute
+  have been advised of the possibility of such damage.  See
+  the GNU Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with this library; if not, write to the Free Software Foundation,
+  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.equations.builtins;
+
+
+import java.util.ArrayList;
+import java.util.List;
+import org.cytoscape.equations.Function;
+import org.cytoscape.equations.FunctionUtil;
+
+
+public class Tan implements Function {
+       /**
+        *  Used to parse the function string.  This name is treated in a 
case-insensitive manner!
+        *  @return the name by which you must call the function when used in 
an attribute equation.
+        */
+       public String getName() { return "TAN"; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of what this function does
+        */
+       public String getFunctionSummary() { return "Returns the tanget of an 
angle given in radians."; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of how to use this function
+        */
+       public String getUsageDescription() { return "Call this with 
\"TAN(radians)\""; }
+
+       public Class getReturnType() { return Double.class; }
+
+       /**
+        *  @return Double.class or null if there is not exactly 1 arg or the 
arg is not of type Double, Long, String or Boolean
+        */
+       public Class validateArgTypes(final Class[] argTypes) {
+               if (argTypes.length != 1 || 
!FunctionUtil.isScalarArgType(argTypes[0]))
+                       return null;
+
+               return Double.class;
+       }
+
+       /**
+        *  @param args the function arguments which must be either one object 
of type Double or Long
+        *  @return the result of the function evaluation which is the natural 
logarithm of the first argument
+        */
+       public Object evaluateFunction(final Object[] args) {
+               final double angleInRadians = 
FunctionUtil.getArgAsDouble(args[0]);
+
+               final double cos = Math.cos(angleInRadians);
+               if (cos == 0.0)
+                       throw new IllegalArgumentException("division by zero in 
call to TAN()!");
+
+               return Math.sin(angleInRadians) / cos;
+       }
+
+       /**
+        *  Used with the equation builder.
+        *
+        *  @param leadingArgs the types of the arguments that have already 
been selected by the user.
+        *  @return the set of arguments (must be a collection of String.class, 
Long.class, Double.class,
+        *           Boolean.class and List.class) that are candidates for the 
next argument.  An empty
+        *           set indicates that no further arguments are valid.
+        */
+       public List<Class> getPossibleArgTypes(final Class[] leadingArgs) {
+               if (leadingArgs.length == 0) {
+                       final List<Class> possibleNextArgs = new 
ArrayList<Class>();
+                       possibleNextArgs.add(Double.class);
+                       possibleNextArgs.add(Long.class);
+                       possibleNextArgs.add(String.class);
+                       possibleNextArgs.add(Boolean.class);
+                       return possibleNextArgs;
+               }
+
+               return null;
+       }
+}

Copied: 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java 
(from rev 20337, 
corelibs/trunk/equations/tests/org/cytoscape/equations/interpreter/InterpreterTest.java)
===================================================================
--- 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java  
                            (rev 0)
+++ 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java  
    2010-05-27 21:42:28 UTC (rev 20338)
@@ -0,0 +1,73 @@
+/*
+  File: Framework.java
+
+  Copyright (c) 2010, 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
+  by the Free Software Foundation; either version 2.1 of the License, or
+  any later version.
+
+  This library is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+  documentation provided hereunder is on an "as is" basis, and the
+  Institute for Systems Biology and the Whitehead Institute
+  have no obligations to provide maintenance, support,
+  updates, enhancements or modifications.  In no event shall the
+  Institute for Systems Biology and the Whitehead Institute
+  be liable to any party for direct, indirect, special,
+  incidental or consequential damages, including lost profits, arising
+  out of the use of this software and its documentation, even if the
+  Institute for Systems Biology and the Whitehead Institute
+  have been advised of the possibility of such damage.  See
+  the GNU Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with this library; if not, write to the Free Software Foundation,
+  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.equations.interpreter;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+import junit.framework.*;
+import org.cytoscape.equations.EqnCompiler;
+import org.cytoscape.equations.Function;
+import org.cytoscape.equations.Parser;
+import org.cytoscape.equations.builtins.*;
+
+
+class Framework {
+       static private class BadReturnFunction implements Function {
+               public String getName() { return "BAD"; }
+               public String getFunctionSummary() { return "Returns an invalid 
type at runtime."; }
+               public String getUsageDescription() { return "Call this with 
\"BAD()\"."; }
+               public Class getReturnType() { return Double.class; }
+               public Class validateArgTypes(final Class[] argTypes) { return 
argTypes.length == 0 ? Double.class : null; }
+               public Object evaluateFunction(final Object[] args) { return 
new Integer(1); }
+               public List<Class> getPossibleArgTypes(final Class[] 
leadingArgs) { return null; }
+       }
+
+       private final EqnCompiler compiler = new EqnCompiler();
+
+       boolean executeTest(final String equation, final Map<String, Object> 
variablesAndValues, final Object expectedResult)
+       {
+               final Map<String, Class> varNameToTypeMap = new HashMap<String, 
Class>();
+               for (final String variableName : variablesAndValues.keySet())
+                       varNameToTypeMap.put(variableName, 
variablesAndValues.get(variableName).getClass());
+
+               if (!compiler.compile(equation, varNameToTypeMap))
+                       return false;
+
+               final Map<String, IdentDescriptor> nameToDescriptorMap = new 
HashMap<String, IdentDescriptor>();
+               for (final String variableName : variablesAndValues.keySet())
+                       nameToDescriptorMap.put(variableName, new 
IdentDescriptor(variablesAndValues.get(variableName)));
+
+               return true;
+       }
+}

-- 
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