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