Search in sources :

Example 11 with IdentDescriptor

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

the class DegreeTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new Degree(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=DEGREE(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 IdentDescriptor

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

the class EqnSupport method evalEquation.

static Object evalEquation(final Equation equation, final Object key, final Interpreter interpreter, final Set<String> currentlyActiveAttributes, final String columnName, final Appendable lastInternalError, final CyTableImpl tableImpl) {
    if (currentlyActiveAttributes.contains(columnName)) {
        currentlyActiveAttributes.clear();
        try {
            lastInternalError.append("Recursive equation evaluation of \"" + columnName + "\".");
        } catch (Exception e) {
        // Intentionally empty!
        }
        return null;
    } else
        currentlyActiveAttributes.add(columnName);
    final Collection<String> attribReferences = equation.getVariableReferences();
    final Map<String, Object> defaultValues = equation.getDefaultVariableValues();
    final Map<String, IdentDescriptor> nameToDescriptorMap = new TreeMap<String, IdentDescriptor>();
    for (final String attribRef : attribReferences) {
        if (attribRef.equals("ID")) {
            nameToDescriptorMap.put("ID", new IdentDescriptor(key));
            continue;
        }
        Object attribValue = tableImpl.getValue(key, attribRef);
        if (attribValue == null) {
            final Object defaultValue = defaultValues.get(attribRef);
            if (defaultValue != null)
                attribValue = defaultValue;
            else {
                currentlyActiveAttributes.clear();
                try {
                    lastInternalError.append("Missing value for referenced column \"" + attribRef + "\".");
                } catch (Exception e) {
                // Intentionally empty!
                }
                logger.warn("Missing value for \"" + attribRef + "\" while evaluating an equation (ID:" + key + ", column name:" + columnName + ")");
                return null;
            }
        }
        try {
            nameToDescriptorMap.put(attribRef, new IdentDescriptor(attribValue));
        } catch (final Exception e) {
            currentlyActiveAttributes.clear();
            try {
                lastInternalError.append("Bad column reference to \"" + attribRef + "\".");
            } catch (Exception e2) {
            // Intentionally empty!
            }
            logger.warn("Bad column reference to \"" + attribRef + "\" while evaluating an equation (ID:" + key + ", column name:" + columnName + ")");
            return null;
        }
    }
    try {
        final Object result = interpreter.execute(equation, nameToDescriptorMap);
        currentlyActiveAttributes.remove(columnName);
        return result;
    } catch (final Exception e) {
        currentlyActiveAttributes.clear();
        try {
            lastInternalError.append(e.getMessage());
        } catch (Exception e2) {
        // Intentionally empty!
        }
        logger.warn("Error while evaluating an equation: " + e.getMessage() + " (ID:" + key + ", column name:" + columnName + ")");
        return null;
    }
}
Also used : TreeMap(java.util.TreeMap) IdentDescriptor(org.cytoscape.equations.IdentDescriptor)

Example 13 with IdentDescriptor

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

the class InDegreeTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new InDegree(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=INDEGREE(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 14 with IdentDescriptor

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

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

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