Search in sources :

Example 21 with IdentDescriptor

use of org.cytoscape.equations.IdentDescriptor in project cytoscape-impl by cytoscape.

the class Framework method executeTest.

/**
 *  Execute a test that should succeed at compile time and runtime.
 *  @return true if the test compiled and ran and produced the expected result
 */
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());
    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>();
    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;
    }
    final Interpreter interpreter = new InterpreterImpl();
    final Object actualResult;
    try {
        actualResult = interpreter.execute(compiler.getEquation(), nameToDescriptorMap);
    } catch (final Exception e) {
        System.err.println("caught runtime error: " + e.getMessage());
        return false;
    }
    if (!areEqual(actualResult, expectedResult)) {
        System.err.println("[" + equation + "] expected: " + expectedResult + ", found: " + actualResult);
        return false;
    }
    return true;
}
Also used : Interpreter(org.cytoscape.equations.Interpreter) HashMap(java.util.HashMap) InterpreterImpl(org.cytoscape.equations.internal.interpreter.InterpreterImpl) IdentDescriptor(org.cytoscape.equations.IdentDescriptor)

Aggregations

IdentDescriptor (org.cytoscape.equations.IdentDescriptor)21 HashMap (java.util.HashMap)18 Test (org.junit.Test)16 Interpreter (org.cytoscape.equations.Interpreter)7 InterpreterImpl (org.cytoscape.equations.internal.interpreter.InterpreterImpl)7 Equation (org.cytoscape.equations.Equation)5 EquationCompilerImpl (org.cytoscape.equations.internal.EquationCompilerImpl)5 EquationParserImpl (org.cytoscape.equations.internal.EquationParserImpl)5 TreeMap (java.util.TreeMap)1 Function (org.cytoscape.equations.Function)1