use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableImpl method setX.
private final void setX(final Object key, final String columnName, final Object value) {
if (columnName == null)
throw new NullPointerException("columnName must not be null.");
if (value == null)
throw new NullPointerException("value must not be null.");
final Object newValue;
final Object newRawValue;
final VirtualColumn virtColumn;
synchronized (lock) {
final String normalizedColName = normalizeColumnName(columnName);
if (types.get(normalizedColName) == null)
throw new IllegalArgumentException("column: '" + columnName + "' does not yet exist.");
final Class<?> columnType = types.get(normalizedColName).getType();
if (columnType == List.class) {
setListX(key, columnName, value);
return;
}
if (!(value instanceof Equation))
checkType(value);
virtColumn = virtualColumnMap.get(normalizedColName);
if (virtColumn != null) {
virtColumn.setValue(key, value);
newValue = virtColumn.getValue(key);
newRawValue = virtColumn.getRawValue(key);
} else {
ColumnData keyToValueMap = attributes.get(normalizedColName);
if (!columnType.isAssignableFrom(value.getClass()) && !EqnSupport.scalarEquationIsCompatible(value, columnType))
throw new IllegalArgumentException("value of \"" + columnName + "\" is not of type " + columnType);
if (value instanceof Equation) {
newRawValue = value;
final Equation equation = (Equation) value;
// TODO this is an implicit addRow - not sure if we want to refactor this or not
keyToValueMap.put(key, equation);
final StringBuilder errorMsg = new StringBuilder();
newValue = EqnSupport.evalEquation(equation, key, interpreter, currentlyActiveAttributes, columnName, errorMsg, this);
lastInternalError = errorMsg.toString();
if (newValue == null)
logger.warn("attempted premature evaluation evaluation for " + equation);
} else {
// TODO this is an implicit addRow - not sure if we want to refactor this or not
newRawValue = newValue = columnType.cast(value);
keyToValueMap.put(key, newValue);
}
}
}
if (fireEvents && virtColumn == null) {
// Fire an event for each table in the virtual column chain
fireVirtualColumnRowSetEvent(this, key, columnName, newValue, newRawValue, Collections.newSetFromMap(new IdentityHashMap<VirtualColumnInfo, Boolean>()));
}
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableTest method testSetEquationWithIncompatibleEquationReturnType.
@Test
public void testSetEquationWithIncompatibleEquationReturnType() {
table.createColumn("someDouble", Double.class, false);
table.createColumn("someOtherDouble", Double.class, false);
compiler.compile("=\"String\"", new HashMap<String, Class<?>>());
final Equation eqn = compiler.getEquation();
try {
attrs.set("someDouble", eqn);
fail();
} catch (IllegalArgumentException e) {
/* Intentionally empty. */
}
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableTest method testSetEquation.
@Test
public void testSetEquation() {
table.createColumn("someDouble", Double.class, false);
table.createColumn("someOtherDouble", Double.class, false);
compiler.compile("=6/3", new HashMap<String, Class<?>>());
final Equation eqn = compiler.getEquation();
attrs.set("someDouble", eqn);
assertTrue(attrs.isSet("someDouble"));
assertEquals(2.0, attrs.get("someDouble", Double.class).doubleValue(), 0.00001);
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class CyTableTest method testSetWithAnEvaluableCompatibleEquation.
@Test
public void testSetWithAnEvaluableCompatibleEquation() {
table.createColumn("strings", String.class, false);
final Map<String, Class<?>> varnameToTypeMap = new HashMap<String, Class<?>>();
compiler.compile("=\"one\"", new HashMap<String, Class<?>>());
final Equation eqn = compiler.getEquation();
attrs.set("strings", eqn);
Object last = eventHelper.getLastPayload();
assertNotNull(last);
assertTrue(last instanceof RowSetRecord);
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class EquationTest method testChainEquationsUsingArithmeticOperators.
@Test
public void testChainEquationsUsingArithmeticOperators() {
CyTableFactory factory = support.getTableFactory();
CyTable table = factory.createTable("MyTable2", "SUID", Long.class, true, true);
table.createColumn("c1", Integer.class, false);
table.createColumn("c2", Integer.class, false);
table.createColumn("c3", Integer.class, false);
Equation e2 = parseEquation("=$c1 + 1", table);
Equation e3 = parseEquation("=$c2 + 1", table);
CyRow row = table.getRow(1L);
row.set("c1", 1);
row.set("c2", e2);
row.set("c3", e3);
assertEquals(Integer.valueOf(1), row.get("c1", Integer.class));
assertEquals(Integer.valueOf(2), row.get("c2", Integer.class));
assertEquals(Integer.valueOf(3), row.get("c3", Integer.class));
}
Aggregations