use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class EquationTest method testEquationEvaluationThreadSafety.
@Test
public void testEquationEvaluationThreadSafety() throws Exception {
CyTableFactory factory = support.getTableFactory();
CyTable table = factory.createTable("MyTable", "SUID", Long.class, true, true);
// there was a bug using Integer at time of writing, so using Double
table.createColumn("c1", Double.class, false);
table.createColumn("e1", Double.class, false);
table.createColumn("e2", Double.class, false);
table.createColumn("e3", Double.class, false);
Equation e1 = parseEquation("=$c1", table);
Equation e2 = parseEquation("=$e1 + 1.0", table);
Equation e3 = parseEquation("=$e2 + 1.0", table);
// first test using only one row to make sure the equations work correctly
{
CyRow row = table.getRow(1L);
row.set("c1", 1.0);
row.set("e1", e1);
row.set("e2", e2);
row.set("e3", e3);
Double result = row.get("e3", Double.class);
assertEquals(3.0, result, 0);
}
// Ok now lets scale it up!
final int N = 1000;
for (int i = 1; i <= N; i++) {
CyRow row = table.getRow((long) i);
row.set("c1", 1.0);
row.set("e1", e1);
row.set("e2", e2);
row.set("e3", e3);
}
assertEquals(N, table.getRowCount());
List<Callable<Double>> taskList = new ArrayList<>(N);
for (int i = 1; i <= N; i++) {
final int n = i;
taskList.add(new Callable<Double>() {
public Double call() throws Exception {
CyRow row = table.getRow((long) n);
return row.get("e3", Double.class);
}
});
}
ExecutorService executor = Executors.newFixedThreadPool(20);
List<Future<Double>> futures = executor.invokeAll(taskList);
double result = 0.0;
for (Future<Double> f : futures) {
result += f.get();
}
executor.shutdown();
assertEquals(N * 3.0, result, 0);
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableImpl method setListX.
private final void setListX(final Object key, final String columnName, final Object value) {
Object newValue;
final Object rawValue;
synchronized (lock) {
final String normalizedColName = normalizeColumnName(columnName);
final CyColumn column = types.get(normalizedColName);
CyRow row = rows.get(key);
final Class<?> type = column.getListElementType();
if (value instanceof CyListImpl) {
rawValue = value;
} else if (value instanceof List) {
final List list = (List) value;
if (!list.isEmpty())
checkType(list.get(0));
List<?> listData = columnFactory.createList(type, list);
rawValue = new CyListImpl(type, listData, eventHelper, row, column, this);
} else if (!(value instanceof Equation)) {
throw new IllegalArgumentException("value is a " + value.getClass().getName() + " and not a List for column '" + columnName + "'.");
} else if (!EqnSupport.listEquationIsCompatible((Equation) value, type)) {
throw new IllegalArgumentException("value is not a List equation of a compatible type for column '" + columnName + "'.");
} else {
rawValue = value;
}
final VirtualColumn virtColumn = virtualColumnMap.get(normalizedColName);
if (virtColumn != null) {
virtColumn.setValue(key, rawValue);
newValue = virtColumn.getListValue(key);
} else {
ColumnData keyToValueMap = attributes.get(normalizedColName);
// TODO this is an implicit addRow - not sure if we want to refactor this or not
keyToValueMap.put(key, rawValue);
if (rawValue instanceof Equation) {
final StringBuilder errorMsg = new StringBuilder();
newValue = EqnSupport.evalEquation((Equation) rawValue, suid, interpreter, currentlyActiveAttributes, columnName, errorMsg, this);
lastInternalError = errorMsg.toString();
} else {
newValue = rawValue;
}
}
}
if (fireEvents)
eventHelper.addEventPayload((CyTable) this, new RowSetRecord(getRow(key), columnName, newValue, rawValue), RowsSetEvent.class);
}
use of org.cytoscape.equations.Equation 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.Equation in project cytoscape-impl by cytoscape.
the class ReadDataManager method parseAllEquations.
/**
* Should be called only after all XGMML attributes have been read.
*/
protected void parseAllEquations() {
final EquationCompiler equationCompiler = serviceRegistrar.getService(EquationCompiler.class);
for (Map.Entry<CyRow, Map<String, String>> entry : equations.entrySet()) {
CyRow row = entry.getKey();
Map<String, String> colEquationMap = entry.getValue();
Map<String, Class<?>> colNameTypeMap = new Hashtable<String, Class<?>>();
Collection<CyColumn> columns = row.getTable().getColumns();
for (CyColumn col : columns) {
colNameTypeMap.put(col.getName(), col.getType());
}
for (Map.Entry<String, String> colEqEntry : colEquationMap.entrySet()) {
String columnName = colEqEntry.getKey();
String formula = colEqEntry.getValue();
if (equationCompiler.compile(formula, colNameTypeMap)) {
Equation equation = equationCompiler.getEquation();
row.set(columnName, equation);
} else {
logger.error("Error parsing equation \"" + formula + "\": " + equationCompiler.getLastErrorMsg());
}
}
}
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableImpl method getListX.
private <T> List<T> getListX(final Object key, final String columnName, final Class<? extends T> listElementType, final List<T> defaultValue) {
synchronized (lock) {
final String normalizedColName = normalizeColumnName(columnName);
CyColumn type = types.get(normalizedColName);
if (type == null) {
logger.warn("'" + columnName + "' does not yet exist.");
return defaultValue;
}
final Class<?> expectedListElementType = type.getListElementType();
if (expectedListElementType == null) {
throw new IllegalArgumentException("'" + columnName + "' is not a List.");
}
if (expectedListElementType != listElementType) {
throw new IllegalArgumentException("invalid list element type for column '" + columnName + ", found: " + listElementType.getName() + ", expected: " + expectedListElementType.getName() + ".");
}
lastInternalError = null;
final VirtualColumn virtColumn = virtualColumnMap.get(normalizedColName);
final Object vl = getValueOrEquation(key, columnName, virtColumn);
if (virtColumn != null && vl == null)
return (List<T>) virtColumn.getListValue(key);
if (vl == null)
return getDefaultValue(columnName, defaultValue);
if (vl instanceof Equation) {
final StringBuilder errorMsg = new StringBuilder();
final Object result = EqnSupport.evalEquation((Equation) vl, key, interpreter, currentlyActiveAttributes, columnName, errorMsg, this);
lastInternalError = errorMsg.toString();
return (List) result;
} else
return (List) vl;
}
}
Aggregations