Search in sources :

Example 16 with Equation

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>()));
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) Equation(org.cytoscape.equations.Equation) ColumnData(org.cytoscape.model.internal.column.ColumnData)

Example 17 with Equation

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. */
    }
}
Also used : Equation(org.cytoscape.equations.Equation) Test(org.junit.Test)

Example 18 with Equation

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);
}
Also used : Equation(org.cytoscape.equations.Equation) Test(org.junit.Test)

Example 19 with Equation

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);
}
Also used : RowSetRecord(org.cytoscape.model.events.RowSetRecord) HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation) Test(org.junit.Test)

Example 20 with Equation

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));
}
Also used : Equation(org.cytoscape.equations.Equation) Test(org.junit.Test)

Aggregations

Equation (org.cytoscape.equations.Equation)29 Test (org.junit.Test)12 HashMap (java.util.HashMap)10 CyRow (org.cytoscape.model.CyRow)8 ArrayList (java.util.ArrayList)6 IdentDescriptor (org.cytoscape.equations.IdentDescriptor)5 Interpreter (org.cytoscape.equations.Interpreter)5 EquationCompilerImpl (org.cytoscape.equations.internal.EquationCompilerImpl)5 EquationParserImpl (org.cytoscape.equations.internal.EquationParserImpl)5 InterpreterImpl (org.cytoscape.equations.internal.interpreter.InterpreterImpl)5 CyColumn (org.cytoscape.model.CyColumn)5 CyTable (org.cytoscape.model.CyTable)5 EquationCompiler (org.cytoscape.equations.EquationCompiler)4 List (java.util.List)3 RowSetRecord (org.cytoscape.model.events.RowSetRecord)3 Map (java.util.Map)2 ValidatedObjectAndEditString (org.cytoscape.browser.internal.util.ValidatedObjectAndEditString)2 ColumnData (org.cytoscape.model.internal.column.ColumnData)2 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1