Search in sources :

Example 21 with Equation

use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.

the class EqnSupport method topoSortAttribReferences.

/**
 *  @return an in-order list of attribute names that will have to be evaluated before "columnName" can be evaluated
 */
private List<String> topoSortAttribReferences(final Object key, final String columnName, final CyTableImpl tableImpl) {
    final Object equationCandidate = tableImpl.getValueOrEquation(key, columnName);
    if (!(equationCandidate instanceof Equation))
        return new ArrayList<String>();
    final Equation equation = (Equation) equationCandidate;
    final Set<String> attribReferences = equation.getVariableReferences();
    if (attribReferences.size() == 0)
        return new ArrayList<String>();
    final Set<String> alreadyProcessed = new TreeSet<String>();
    alreadyProcessed.add(columnName);
    final List<TopoGraphNode> dependencies = new ArrayList<TopoGraphNode>();
    for (final String attribReference : attribReferences) followReferences(key, attribReference, alreadyProcessed, dependencies, tableImpl);
    final List<TopoGraphNode> topoOrder = TopologicalSort.sort(dependencies);
    final List<String> retVal = new ArrayList<String>();
    for (final TopoGraphNode node : topoOrder) {
        final AttribTopoGraphNode attribTopoGraphNode = (AttribTopoGraphNode) node;
        final String nodeName = attribTopoGraphNode.getNodeName();
        if (nodeName.equals(columnName))
            return retVal;
        else
            retVal.add(nodeName);
    }
    // We should never get here because "columnName" should have been found in the for-loop above!
    throw new IllegalStateException("\"" + columnName + "\" was not found in the toplogical order, which should be impossible.");
}
Also used : TopoGraphNode(org.cytoscape.model.internal.tsort.TopoGraphNode) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Equation(org.cytoscape.equations.Equation)

Example 22 with Equation

use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.

the class EqnSupport method followReferences.

/**
 *  Helper function for topoSortAttribReferences() performing a depth-first search of equation evaluation dependencies.
 */
private static void followReferences(final Object key, final String columnName, final Collection<String> alreadyProcessed, final Collection<TopoGraphNode> dependencies, final CyTableImpl tableImpl) {
    // Already visited this attribute?
    if (alreadyProcessed.contains(columnName))
        return;
    alreadyProcessed.add(columnName);
    final Object equationCandidate = tableImpl.getValueOrEquation(key, columnName);
    if (!(equationCandidate instanceof Equation))
        return;
    final Equation equation = (Equation) equationCandidate;
    final Set<String> attribReferences = equation.getVariableReferences();
    for (final String attribReference : attribReferences) followReferences(key, attribReference, alreadyProcessed, dependencies, tableImpl);
}
Also used : Equation(org.cytoscape.equations.Equation)

Example 23 with Equation

use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.

the class EqnSupport method scalarEquationIsCompatible.

static boolean scalarEquationIsCompatible(final Object equationCandidate, final Class<?> targetType) {
    if (!(equationCandidate instanceof Equation))
        return false;
    final Equation equation = (Equation) equationCandidate;
    final Class<?> eqType = equation.getType();
    if (targetType == String.class)
        // Everything can be turned into a String!
        return true;
    if (targetType == Boolean.class || Number.class.isAssignableFrom(targetType))
        return eqType == Boolean.class || Number.class.isAssignableFrom(eqType);
    return false;
}
Also used : Equation(org.cytoscape.equations.Equation)

Example 24 with Equation

use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.

the class BrowserTableModel method setValueAt.

@Override
public void setValueAt(final Object value, final int rowIndex, final int columnIndex) {
    final String text = (String) value;
    final CyRow row = getCyRow(rowIndex);
    final String columnName = mapColumnIndexToColumnName(columnIndex);
    final Class<?> columnType = dataTable.getColumn(columnName).getType();
    final Class<?> listElementType = dataTable.getColumn(columnName).getListElementType();
    if (text.isEmpty()) {
        if (!row.isSet(columnName))
            return;
        row.set(columnName, null);
    } else if (text.startsWith("=")) {
        final Map<String, Class<?>> attrNameToTypeMap = TableBrowserUtil.getAttNameToTypeMap(dataTable, null);
        if (compiler.compile(text, attrNameToTypeMap)) {
            final Equation eqn = compiler.getEquation();
            final Class<?> eqnType = eqn.getType();
            // Is the equation type compatible with the column type?
            if (eqnTypeIsCompatible(columnType, listElementType, eqnType)) {
                row.set(columnName, eqn);
            } else {
                // The equation type is incompatible w/ the column type!
                final Class<?> expectedType = columnType == Integer.class ? Long.class : columnType;
                final String errorMsg = "Equation result type is " + getUnqualifiedName(eqnType) + ", column type is " + getUnqualifiedName(columnType) + ".";
                final Equation errorEqn = compiler.getErrorEquation(text, expectedType, errorMsg);
                row.set(columnName, errorEqn);
            }
        } else {
            final Class<?> eqnType = columnType == Integer.class ? Long.class : columnType;
            final String errorMsg = compiler.getLastErrorMsg();
            final Equation errorEqn = compiler.getErrorEquation(text, eqnType, errorMsg);
            row.set(columnName, errorEqn);
        }
    } else {
        // Not an equation!
        final List<Object> parsedData = TableBrowserUtil.parseCellInput(dataTable, columnName, value);
        if (parsedData.get(0) != null)
            row.set(columnName, parsedData.get(0));
        else {
            // Error!
            showErrorWindow(parsedData.get(1).toString());
        // + " should be an Integer (or the number is too big/small).");
        }
    }
    final TableModelEvent event = new TableModelEvent(this, rowIndex, rowIndex, columnIndex);
    fireTableChanged(event);
    fireTableDataChanged();
}
Also used : TableModelEvent(javax.swing.event.TableModelEvent) Equation(org.cytoscape.equations.Equation) ArrayList(java.util.ArrayList) List(java.util.List) ValidatedObjectAndEditString(org.cytoscape.browser.internal.util.ValidatedObjectAndEditString) CyRow(org.cytoscape.model.CyRow) Map(java.util.Map)

Example 25 with Equation

use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.

the class InDegreeTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new InDegree(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=INDEGREE(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)

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