Author: ruschein
Date: 2010-05-27 16:07:06 -0700 (Thu, 27 May 2010)
New Revision: 20339

Added:
   
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/ListToString.java
   
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/ListToStringTest.java
   corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/TanTest.java
Modified:
   corelibs/trunk/equations/src/org/cytoscape/equations/FunctionUtil.java
   corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java
   
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java
Log:
Added new built-in LISTTOSTRING().

Modified: corelibs/trunk/equations/src/org/cytoscape/equations/FunctionUtil.java
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/FunctionUtil.java      
2010-05-27 21:42:28 UTC (rev 20338)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/FunctionUtil.java      
2010-05-27 23:07:06 UTC (rev 20339)
@@ -32,8 +32,16 @@
 
 import java.util.Arrays;
 import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.Stack;
+import java.util.Vector;
+import java.util.concurrent.CopyOnWriteArrayList;
 
+import javax.management.AttributeList;
+import javax.management.relation.RoleList;
+import javax.management.relation.RoleUnresolvedList;
 
+
 /**
  *  A collection of static methods that may be useful for the implementation 
of built-in functions.
  */
@@ -209,4 +217,28 @@
        static public boolean isScalarArgType(final Class type) {
                return type == Double.class || type == Long.class || type == 
String.class || type == Boolean.class;
        }
+
+       /**
+        *  @return true if "listClassCandidate" is an implementer of interface 
List, else false
+        */
+       static public boolean someKindOfList(final Class listClassCandidate) {
+               if (listClassCandidate == ArrayList.class)
+                       return true;
+               if (listClassCandidate == Vector.class)
+                       return true;
+               if (listClassCandidate == Stack.class)
+                       return true;
+               if (listClassCandidate == AttributeList.class)
+                       return true;
+               if (listClassCandidate == CopyOnWriteArrayList.class)
+                       return true;
+               if (listClassCandidate == LinkedList.class)
+                       return true;
+               if (listClassCandidate == RoleList.class)
+                       return true;
+               if (listClassCandidate == RoleUnresolvedList.class)
+                       return true;
+
+               return false;
+       }
 }

Modified: corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java
===================================================================
--- corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java    
2010-05-27 21:42:28 UTC (rev 20338)
+++ corelibs/trunk/equations/src/org/cytoscape/equations/Parser.java    
2010-05-27 23:07:06 UTC (rev 20339)
@@ -60,6 +60,7 @@
                        eqnParser.registerFunction(new Last());
                        eqnParser.registerFunction(new Left());
                        eqnParser.registerFunction(new Len());
+                       eqnParser.registerFunction(new ListToString());
                        eqnParser.registerFunction(new Ln());
                        eqnParser.registerFunction(new Log());
                        eqnParser.registerFunction(new Max());
@@ -82,6 +83,7 @@
                        eqnParser.registerFunction(new StDev());
                        eqnParser.registerFunction(new Sqrt());
                        eqnParser.registerFunction(new Substitute());
+                       eqnParser.registerFunction(new Tan());
                        eqnParser.registerFunction(new Trunc());
                        eqnParser.registerFunction(new UCase());
                        eqnParser.registerFunction(new Value());

Copied: 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/ListToString.java 
(from rev 20310, 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/Abs.java)
===================================================================
--- 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/ListToString.java 
                            (rev 0)
+++ 
corelibs/trunk/equations/src/org/cytoscape/equations/builtins/ListToString.java 
    2010-05-27 23:07:06 UTC (rev 20339)
@@ -0,0 +1,110 @@
+/*
+  File: ListToString.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 ListToString 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 "LISTTOSTRING"; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of what this function does
+        */
+       public String getFunctionSummary() { return "Converts a list to a 
string, given a separator."; }
+
+       /**
+        *  Used to provide help for users.
+        *  @return a description of how to use this function
+        */
+       public String getUsageDescription() { return "Call this with 
\"LISTTOSTRING(list,separator)\""; }
+
+       public Class getReturnType() { return String.class; }
+
+       /**
+        *  @return Double.class or null if there is not exactly 1 arg or the 
arg is not of type Double or Long
+        */
+       public Class validateArgTypes(final Class[] argTypes) {
+               if (argTypes.length != 2 || 
!FunctionUtil.someKindOfList(argTypes[0]) || argTypes[1] != String.class)
+                       return null;
+
+               return String.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 List list = (List)args[0];
+               final String separator = (String)args[1];
+
+               final StringBuilder result = new StringBuilder();
+               int count = 0;
+               for (final Object listEntry : list) {
+                       result.append(listEntry.toString());
+                       if (++count < list.size())
+                               result.append(separator);
+               }
+
+               return result.toString();
+       }
+
+       /**
+        *  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) {
+               final List<Class> possibleNextArgs = new ArrayList<Class>();
+               if (leadingArgs.length == 0) {
+                       possibleNextArgs.add(List.class);
+                       return possibleNextArgs;
+               }
+               else if (leadingArgs.length == 1) {
+                       possibleNextArgs.add(String.class);
+                       return possibleNextArgs;
+               }
+
+               return null;
+       }
+}

Modified: 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java
===================================================================
--- 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java  
    2010-05-27 21:42:28 UTC (rev 20338)
+++ 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/Framework.java  
    2010-05-27 23:07:06 UTC (rev 20339)
@@ -27,19 +27,18 @@
   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;
+package org.cytoscape.equations.builtins;
 
 
-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.*;
+import org.cytoscape.equations.interpreter.IdentDescriptor;
+import org.cytoscape.equations.interpreter.Interpreter;
 
 
 class Framework {
@@ -53,21 +52,51 @@
                public List<Class> getPossibleArgTypes(final Class[] 
leadingArgs) { return null; }
        }
 
-       private final EqnCompiler compiler = new EqnCompiler();
+       private static final EqnCompiler compiler = new EqnCompiler();
 
-       boolean executeTest(final String equation, final Map<String, Object> 
variablesAndValues, final Object expectedResult)
-       {
+       static {
+               Parser.getParser().registerFunction(new BadReturnFunction());
+       }
+
+       static 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))
+               
+               try {
+                       if (!compiler.compile(equation, varNameToTypeMap)) {
+                               System.err.println("Error while compiling \"" + 
equation + "\": " + compiler.getLastErrorMsg());
+                               return false;
+                       }
+               } catch (final Exception e) {
+                       System.err.println("Error while compiling \"" + 
equation + "\": " + e.getMessage());
                        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)));
+               try {
+                       for (final String variableName : 
variablesAndValues.keySet())
+                               nameToDescriptorMap.put(variableName, new 
IdentDescriptor(variablesAndValues.get(variableName)));
+               } catch (final Exception e) {
+                       System.err.println("Error while processing variables 
for \"" + equation + "\": " + e.getMessage());
+                       return false;
+               }
 
-               return true;
+               final Interpreter interpreter = new 
Interpreter(compiler.getEquation(), nameToDescriptorMap);
+               try {
+                       final Object actualResult = interpreter.run();
+                       if (!actualResult.equals(expectedResult)) {
+                               System.err.println("[" + equation + "] 
expected: " + expectedResult + ", found: " + actualResult);
+                               return false;
+                       } else
+                               return true;
+               } catch (final Exception e) {
+                       return false;
+               }
        }
+
+       static boolean executeTest(final String equation, final Object 
expectedResult) {
+               final Map<String, Object> variablesAndValues = new 
HashMap<String, Object>();
+               return executeTest(equation, variablesAndValues, 
expectedResult);
+       }
 }

Copied: 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/ListToStringTest.java
 (from rev 20337, 
corelibs/trunk/equations/tests/org/cytoscape/equations/interpreter/InterpreterTest.java)
===================================================================
--- 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/ListToStringTest.java
                               (rev 0)
+++ 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/ListToStringTest.java
       2010-05-27 23:07:06 UTC (rev 20339)
@@ -0,0 +1,53 @@
+/*
+  File: ListToStringTest.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.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.*;
+
+
+public class ListToStringTest extends TestCase {
+       public void testAll() throws Exception {
+               final List<Object> list = new ArrayList<Object>();
+               list.add("Fred");
+               list.add(Long.valueOf(3L));
+               list.add(Double.valueOf(1.3));
+
+               final Map<String, Object> variablesAndValues = new 
HashMap<String, Object>();
+               variablesAndValues.put("LIST", list);
+
+               assertTrue(Framework.executeTest("=LISTTOSTRING($LIST,\",\")", 
variablesAndValues, new String("Fred,3,1.3")));
+       }
+}

Copied: 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/TanTest.java 
(from rev 20337, 
corelibs/trunk/equations/tests/org/cytoscape/equations/interpreter/InterpreterTest.java)
===================================================================
--- 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/TanTest.java    
                            (rev 0)
+++ 
corelibs/trunk/equations/tests/org/cytoscape/equations/builtins/TanTest.java    
    2010-05-27 23:07:06 UTC (rev 20339)
@@ -0,0 +1,40 @@
+/*
+  File: TanTest.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 junit.framework.*;
+
+
+public class TanTest extends TestCase {
+       public void testAll() throws Exception {
+               assertTrue(Framework.executeTest("=TAN(1.047197551)", new 
Double(1.7320508067824862)));
+       }
+}

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