Search in sources :

Example 16 with InterpreterImpl

use of org.cytoscape.equations.internal.interpreter.InterpreterImpl in project cytoscape-impl by cytoscape.

the class Framework method executeTestExpectFailure.

/**
 *  Excecute a test that should fail at either compile time or runtime.
 *  @return true if the test fails at compile time or runtime, otherwise false
 */
static boolean executeTestExpectFailure(final String equation, final Map<String, Object> variablesAndValues) {
    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 true;
        }
    } catch (final Exception e) {
        System.err.println("Error while compiling \"" + equation + "\": " + e.getMessage());
        return true;
    }
    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 true;
    }
    final Interpreter interpreter = new InterpreterImpl();
    try {
        final Object result = interpreter.execute(compiler.getEquation(), nameToDescriptorMap);
        // We should never get here!
        return false;
    } catch (final Exception e) {
        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)

Example 17 with InterpreterImpl

use of org.cytoscape.equations.internal.interpreter.InterpreterImpl 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

InterpreterImpl (org.cytoscape.equations.internal.interpreter.InterpreterImpl)17 Interpreter (org.cytoscape.equations.Interpreter)14 EquationCompilerImpl (org.cytoscape.equations.internal.EquationCompilerImpl)9 EquationParserImpl (org.cytoscape.equations.internal.EquationParserImpl)9 HashMap (java.util.HashMap)7 IdentDescriptor (org.cytoscape.equations.IdentDescriptor)7 Test (org.junit.Test)7 Equation (org.cytoscape.equations.Equation)5 CyNetworkManagerImpl (org.cytoscape.model.internal.CyNetworkManagerImpl)5 CyNetworkTableManagerImpl (org.cytoscape.model.internal.CyNetworkTableManagerImpl)5 CyTableManagerImpl (org.cytoscape.model.internal.CyTableManagerImpl)5 Before (org.junit.Before)5 CyTableFactoryImpl (org.cytoscape.model.internal.CyTableFactoryImpl)4 CyTableImpl (org.cytoscape.model.internal.CyTableImpl)4 CyServiceRegistrar (org.cytoscape.service.util.CyServiceRegistrar)4 EquationCompiler (org.cytoscape.equations.EquationCompiler)3 DummyCyEventHelper (org.cytoscape.event.DummyCyEventHelper)3 CyRow (org.cytoscape.model.CyRow)2 CyNetworkFactoryImpl (org.cytoscape.model.internal.CyNetworkFactoryImpl)2 CyRootNetworkImpl (org.cytoscape.model.internal.CyRootNetworkImpl)2