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));
}
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;
}
}
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));
}
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));
}
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));
}
Aggregations