use of org.cytoscape.equations.Equation 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.Equation 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.Equation in project cytoscape-impl by cytoscape.
the class CSVCyReader method createTable.
CyTable createTable(CSVReader reader, TableInfo info) throws IOException, SecurityException {
final ColumnInfo[] columns = info.getColumns();
final CyTableFactory tableFactory = serviceRegistrar.getService(CyTableFactory.class);
final CyTable table = tableFactory.createTable(info.getTitle(), columns[0].getName(), columns[0].getType(), info.isPublic(), true);
final Map<String, Class<?>> variableNameToTypeMap = new HashMap<>();
for (final ColumnInfo colInfo : columns) variableNameToTypeMap.put(colInfo.getName(), colInfo.getType() == Integer.class ? Long.class : colInfo.getType());
for (int i = 1; i < columns.length; i++) {
ColumnInfo column = columns[i];
Class<?> type = column.getType();
if (type.equals(List.class)) {
table.createListColumn(column.getName(), column.getListElementType(), !column.isMutable());
} else {
table.createColumn(column.getName(), type, !column.isMutable());
}
}
final EquationCompiler compiler = serviceRegistrar.getService(EquationCompiler.class);
String[] values = reader.readNext();
while (values != null) {
if (isCanceled)
return null;
Object key = parseValue(columns[0].getType(), null, values[0]);
CyRow row = table.getRow(key);
for (int i = 1; i < values.length; i++) {
ColumnInfo column = columns[i];
String name = column.getName();
final Class<?> columnType = column.getType();
final Class<?> columnListElementType = column.getListElementType();
if (handleEquations && values[i].startsWith("=")) {
final Class<?> expectedType = variableNameToTypeMap.remove(name);
try {
final Equation equation;
final Class<?> eqnType;
if (compiler.compile(values[i], variableNameToTypeMap)) {
eqnType = compiler.getEquation().getType();
if (EquationUtil.eqnTypeIsCompatible(columnType, columnListElementType, eqnType))
equation = compiler.getEquation();
else {
final String errorMsg = "Equation result type is " + EquationUtil.getUnqualifiedName(eqnType) + ", column type is " + EquationUtil.getUnqualifiedName(columnType) + ".";
equation = compiler.getErrorEquation(values[i], expectedType, errorMsg);
}
} else {
equation = compiler.getErrorEquation(values[i], expectedType, compiler.getLastErrorMsg());
}
row.set(name, equation);
} catch (final Exception e) {
throw new IOException(e.getMessage(), e.getCause());
}
variableNameToTypeMap.put(name, expectedType);
} else {
Object value = parseValue(columnType, columnListElementType, values[i]);
if (value != null)
row.set(name, value);
}
}
values = reader.readNext();
}
return table;
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CSVCyWriter method writeValues.
private void writeValues(CSVWriter writer, Collection<CyColumn> columns) {
for (CyRow row : table.getAllRows()) {
if (isCanceled)
return;
String[] values = new String[columns.size()];
int index = 0;
for (CyColumn column : columns) {
if (handleEquations) {
final Object rawValue = row.getRaw(column.getName());
if (rawValue instanceof Equation) {
values[index++] = rawValue.toString();
continue;
}
}
Class<?> type = column.getType();
if (type.equals(List.class)) {
StringBuilder builder = new StringBuilder();
boolean first = true;
List<?> list = row.getList(column.getName(), column.getListElementType());
if (list != null) {
for (Object value : list) {
if (!first) {
if (fileFilter.getExtensions().contains("csv"))
builder.append("|");
else
builder.append("\r");
}
if (value != null) {
builder.append(value);
}
first = false;
}
values[index] = builder.toString();
}
} else {
Object value = row.get(column.getName(), type);
if (value != null) {
values[index] = value.toString();
} else {
values[index] = null;
}
}
index++;
}
writer.writeNext(values);
}
}
Aggregations