Search in sources :

Example 11 with Equation

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);
}
Also used : ArrayList(java.util.ArrayList) Equation(org.cytoscape.equations.Equation) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 12 with Equation

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);
}
Also used : RowSetRecord(org.cytoscape.model.events.RowSetRecord) CyColumn(org.cytoscape.model.CyColumn) Equation(org.cytoscape.equations.Equation) ColumnData(org.cytoscape.model.internal.column.ColumnData) CyRow(org.cytoscape.model.CyRow) RowsSetEvent(org.cytoscape.model.events.RowsSetEvent) CyTable(org.cytoscape.model.CyTable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with Equation

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));
}
Also used : EquationParserImpl(org.cytoscape.equations.internal.EquationParserImpl) EquationCompilerImpl(org.cytoscape.equations.internal.EquationCompilerImpl) Interpreter(org.cytoscape.equations.Interpreter) HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation) InterpreterImpl(org.cytoscape.equations.internal.interpreter.InterpreterImpl) IdentDescriptor(org.cytoscape.equations.IdentDescriptor) Test(org.junit.Test)

Example 14 with Equation

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());
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) CyColumn(org.cytoscape.model.CyColumn) Equation(org.cytoscape.equations.Equation) CyRow(org.cytoscape.model.CyRow) EquationCompiler(org.cytoscape.equations.EquationCompiler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with Equation

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;
    }
}
Also used : CyColumn(org.cytoscape.model.CyColumn) Equation(org.cytoscape.equations.Equation) ArrayList(java.util.ArrayList) List(java.util.List)

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