Search in sources :

Example 11 with Interpreter

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

the class OutDegreeTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new OutDegree(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=OUTDEGREE(101)", variableNameToTypeMap))
        fail(compiler.getLastErrorMsg());
    final Equation equation = compiler.getEquation();
    final Interpreter interpreter = new InterpreterImpl();
    final Map<String, IdentDescriptor> variableNameToDescriptorMap = new HashMap<String, IdentDescriptor>();
    assertEquals("Equation evaluation returned an unexpected result!", 3L, interpreter.execute(equation, variableNameToDescriptorMap));
}
Also used : EquationParserImpl(org.cytoscape.equations.internal.EquationParserImpl) EquationCompilerImpl(org.cytoscape.equations.internal.EquationCompilerImpl) Interpreter(org.cytoscape.equations.Interpreter) HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation) InterpreterImpl(org.cytoscape.equations.internal.interpreter.InterpreterImpl) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 12 with Interpreter

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

the class TargetIDTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new TargetID(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=TARGETID(11)", variableNameToTypeMap))
        fail(compiler.getLastErrorMsg());
    final Equation equation = compiler.getEquation();
    final Interpreter interpreter = new InterpreterImpl();
    final Map<String, IdentDescriptor> variableNameToDescriptorMap = new HashMap<String, IdentDescriptor>();
    assertEquals("Equation evaluation returned an unexpected result!", 101L, interpreter.execute(equation, variableNameToDescriptorMap));
}
Also used : EquationParserImpl(org.cytoscape.equations.internal.EquationParserImpl) EquationCompilerImpl(org.cytoscape.equations.internal.EquationCompilerImpl) Interpreter(org.cytoscape.equations.Interpreter) HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation) InterpreterImpl(org.cytoscape.equations.internal.interpreter.InterpreterImpl) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 13 with Interpreter

use of org.cytoscape.equations.Interpreter 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 14 with Interpreter

use of org.cytoscape.equations.Interpreter 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

Interpreter (org.cytoscape.equations.Interpreter)14 InterpreterImpl (org.cytoscape.equations.internal.interpreter.InterpreterImpl)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)6 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 EquationCompiler (org.cytoscape.equations.EquationCompiler)3 DummyCyEventHelper (org.cytoscape.event.DummyCyEventHelper)3 CyServiceRegistrar (org.cytoscape.service.util.CyServiceRegistrar)3 CyNetworkFactoryImpl (org.cytoscape.model.internal.CyNetworkFactoryImpl)2 CyRootNetworkImpl (org.cytoscape.model.internal.CyRootNetworkImpl)2 CyTableImpl (org.cytoscape.model.internal.CyTableImpl)2 CyNetworkNaming (org.cytoscape.session.CyNetworkNaming)2