Search in sources :

Example 16 with IdentDescriptor

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

the class InterpreterTest method testMixedModeArithmetic.

@Test
public void testMixedModeArithmetic() throws Exception {
    final Map<String, Class<?>> attribNameToTypeMap = new HashMap<String, Class<?>>();
    attribNameToTypeMap.put("x", Long.class);
    final Map<String, IdentDescriptor> nameToDescriptorMap = new HashMap<String, IdentDescriptor>();
    nameToDescriptorMap.put("x", new IdentDescriptor(new Long(3)));
    assertTrue(compiler.compile("=$x + 2.0", attribNameToTypeMap));
    assertEquals(new Double(5.0), interpreter.execute(compiler.getEquation(), nameToDescriptorMap));
    assertTrue(compiler.compile("=TRUE + TRUE", attribNameToTypeMap));
    assertEquals(new Double(2.0), interpreter.execute(compiler.getEquation(), nameToDescriptorMap));
}
Also used : HashMap(java.util.HashMap) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 17 with IdentDescriptor

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

the class InterpreterTest method testSimpleExpr.

@Test
public void testSimpleExpr() throws Exception {
    final Map<String, Class<?>> attribNameToTypeMap = new HashMap<String, Class<?>>();
    attribNameToTypeMap.put("BOB", Double.class);
    assertTrue(compiler.compile("=42 - 12 + 3 * (4 - 2) + ${BOB:12}", attribNameToTypeMap));
    final Map<String, IdentDescriptor> nameToDescriptorMap = new HashMap<String, IdentDescriptor>();
    nameToDescriptorMap.put("BOB", new IdentDescriptor(-10.0));
    assertEquals(new Double(26.0), interpreter.execute(compiler.getEquation(), nameToDescriptorMap));
}
Also used : HashMap(java.util.HashMap) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 18 with IdentDescriptor

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

the class InterpreterTest method testFunctionWithBadRuntimeReturnType.

@Test
public void testFunctionWithBadRuntimeReturnType() throws Exception {
    final Function badReturnFunction = new BadReturnFunction();
    if (// Avoid duplicate registration!
    parser.getFunction(badReturnFunction.getName()) == null)
        parser.registerFunctionInternal(badReturnFunction);
    final Map<String, Class<?>> attribNameToTypeMap = new HashMap<String, Class<?>>();
    assertTrue(compiler.compile("=BAD()", attribNameToTypeMap));
    final Map<String, IdentDescriptor> nameToDescriptorMap = new HashMap<String, IdentDescriptor>();
    try {
        interpreter.execute(compiler.getEquation(), nameToDescriptorMap);
    } catch (final IllegalStateException e) {
    // If we get here, everything is as expected and we let the test pass!
    }
}
Also used : Function(org.cytoscape.equations.Function) HashMap(java.util.HashMap) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 19 with IdentDescriptor

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

the class InterpreterImpl method aref.

private void aref() throws EmptyStackException {
    final String attribName = (String) argumentStack.pop();
    final IdentDescriptor identDescriptor = variableNameToDescriptorMap.get(attribName);
    if (identDescriptor == null)
        throw new IllegalStateException("unknown column reference: \"" + attribName + "\" (1).");
    final Object value = identDescriptor.getValue();
    if (value == null)
        throw new IllegalStateException("undefined column reference: \"" + attribName + "\".");
    argumentStack.push(value);
}
Also used : IdentDescriptor(org.cytoscape.equations.IdentDescriptor)

Example 20 with IdentDescriptor

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

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