Search in sources :

Example 1 with Equation

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

the class SourceIDTest method test.

@Test
public void test() {
    final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
    final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
    parser.registerFunctionInternal(new SourceID(serviceRegistrar));
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compiler.compile("=SOURCEID(11)", 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!", 101L, 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 2 with Equation

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

the class EquationCompilerImpl method compile.

public boolean compile(final String equation, final Map<String, Class<?>> variableNameToTypeMap) {
    this.equation = null;
    this.errorMsg = null;
    if (!parser.parse(equation, variableNameToTypeMap)) {
        errorMsg = parser.getErrorMsg();
        return false;
    }
    final TreeNode parseTree = parser.getParseTree();
    final Stack<CodeAndSourceLocation> codeStack = new Stack<CodeAndSourceLocation>();
    try {
        parseTree.genCode(codeStack);
    } catch (final IllegalStateException e) {
        errorMsg = e.getCause().toString();
        return false;
    }
    final Object[] code = new Object[codeStack.size()];
    final int[] sourceLocations = new int[codeStack.size()];
    for (int i = code.length - 1; i >= 0; --i) {
        final CodeAndSourceLocation codeAndSourceLocation = codeStack.pop();
        code[i] = codeAndSourceLocation.getCode();
        sourceLocations[i] = codeAndSourceLocation.getSourceLocation();
    }
    this.equation = new Equation(equation, parser.getVariableReferences(), parser.getDefaultVariableValues(), code, sourceLocations, parser.getType());
    errorMsg = null;
    return true;
}
Also used : TreeNode(org.cytoscape.equations.TreeNode) Equation(org.cytoscape.equations.Equation) CodeAndSourceLocation(org.cytoscape.equations.CodeAndSourceLocation) Stack(java.util.Stack)

Example 3 with Equation

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

the class EquationCompilerImpl method getErrorEquation.

/**
 *  A factory method that returns an Equation that always fails at runtime.
 *
 *  @param equation      an arbitrary string that is usually a syntactically invalid equation
 *  @param type          the return type of the error equation
 *  @param errorMessage  the runtime error message that the returned equation will produce
 */
public Equation getErrorEquation(final String equation, final Class<?> type, final String errorMessage) {
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
    if (!compile("=ERROR(\"" + escapeQuotes(errorMessage) + "\")", variableNameToTypeMap))
        throw new IllegalStateException("internal error in Equation.getErrorEquation().  This should *never* happen.");
    final Equation errorEquation = getEquation();
    return new Equation(equation, errorEquation.getVariableReferences(), errorEquation.getDefaultVariableValues(), errorEquation.getCode(), errorEquation.getSourceLocations(), type);
}
Also used : HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation)

Example 4 with Equation

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

the class BrowserTableModel method getValidatedObjectAndEditString.

private ValidatedObjectAndEditString getValidatedObjectAndEditString(final CyRow row, final String columnName) {
    if (row == null)
        return null;
    Object raw = row.getRaw(columnName);
    if (raw == null) {
        CyColumn column = row.getTable().getColumn(columnName);
        if (column != null)
            raw = column.getDefaultValue();
    }
    if (raw == null)
        return null;
    // Optimisation hack:
    final boolean isEquation = raw instanceof Equation;
    final Object cooked = !isEquation ? raw : getColumnValue(row, columnName);
    final String editString = createEditString(raw);
    if (cooked != null)
        return new ValidatedObjectAndEditString(cooked, editString, isEquation);
    final String lastInternalError = dataTable.getLastInternalError();
    return new ValidatedObjectAndEditString(cooked, editString, lastInternalError, isEquation);
}
Also used : ValidatedObjectAndEditString(org.cytoscape.browser.internal.util.ValidatedObjectAndEditString) CyColumn(org.cytoscape.model.CyColumn) Equation(org.cytoscape.equations.Equation) ValidatedObjectAndEditString(org.cytoscape.browser.internal.util.ValidatedObjectAndEditString)

Example 5 with Equation

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

the class FormulaBuilderDialog method updateCells.

private boolean updateCells(final StringBuilder errorMessage) {
    String formula = getFormulaTextField().getText();
    if (formula.charAt(formula.length() - 1) != ')')
        formula = formula + ")";
    final int cellColum = table.convertColumnIndexToModel(table.getSelectedColumn());
    final String attribName = tableModel.getColumnName(cellColum);
    final CyTable attribs = tableModel.getDataTable();
    final Equation equation = compileEquation(attribs, attribName, formula, errorMessage);
    if (equation == null)
        return false;
    switch(applicationDomain) {
        case CURRENT_CELL:
            final int cellRow = table.convertRowIndexToModel(table.getSelectedRow());
            tableModel.setValueAt(formula, cellRow, cellColum);
            break;
        case CURRENT_SELECTION:
            final Collection<CyRow> selectedRows = tableModel.getDataTable().getMatchingRows(CyNetwork.SELECTED, true);
            for (final CyRow selectedRow : selectedRows) {
                if (!setAttribute(selectedRow, attribName, equation, errorMessage))
                    return false;
            }
            break;
        case ENTIRE_ATTRIBUTE:
            final List<CyRow> rows = tableModel.getDataTable().getAllRows();
            for (final CyRow row : rows) {
                if (!setAttribute(row, attribName, equation, errorMessage))
                    return false;
            }
            break;
        default:
            throw new IllegalStateException("unknown application domain: " + applicationDomain + ".");
    }
    return true;
}
Also used : CyTable(org.cytoscape.model.CyTable) Equation(org.cytoscape.equations.Equation) CyRow(org.cytoscape.model.CyRow)

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