Search in sources :

Example 26 with Equation

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

the class OutDegreeTest method test.

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

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

the class TargetIDTest method test.

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

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

the class CSVCyReader method createTable.

CyTable createTable(CSVReader reader, TableInfo info) throws IOException, SecurityException {
    final ColumnInfo[] columns = info.getColumns();
    final CyTableFactory tableFactory = serviceRegistrar.getService(CyTableFactory.class);
    final CyTable table = tableFactory.createTable(info.getTitle(), columns[0].getName(), columns[0].getType(), info.isPublic(), true);
    final Map<String, Class<?>> variableNameToTypeMap = new HashMap<>();
    for (final ColumnInfo colInfo : columns) variableNameToTypeMap.put(colInfo.getName(), colInfo.getType() == Integer.class ? Long.class : colInfo.getType());
    for (int i = 1; i < columns.length; i++) {
        ColumnInfo column = columns[i];
        Class<?> type = column.getType();
        if (type.equals(List.class)) {
            table.createListColumn(column.getName(), column.getListElementType(), !column.isMutable());
        } else {
            table.createColumn(column.getName(), type, !column.isMutable());
        }
    }
    final EquationCompiler compiler = serviceRegistrar.getService(EquationCompiler.class);
    String[] values = reader.readNext();
    while (values != null) {
        if (isCanceled)
            return null;
        Object key = parseValue(columns[0].getType(), null, values[0]);
        CyRow row = table.getRow(key);
        for (int i = 1; i < values.length; i++) {
            ColumnInfo column = columns[i];
            String name = column.getName();
            final Class<?> columnType = column.getType();
            final Class<?> columnListElementType = column.getListElementType();
            if (handleEquations && values[i].startsWith("=")) {
                final Class<?> expectedType = variableNameToTypeMap.remove(name);
                try {
                    final Equation equation;
                    final Class<?> eqnType;
                    if (compiler.compile(values[i], variableNameToTypeMap)) {
                        eqnType = compiler.getEquation().getType();
                        if (EquationUtil.eqnTypeIsCompatible(columnType, columnListElementType, eqnType))
                            equation = compiler.getEquation();
                        else {
                            final String errorMsg = "Equation result type is " + EquationUtil.getUnqualifiedName(eqnType) + ", column type is " + EquationUtil.getUnqualifiedName(columnType) + ".";
                            equation = compiler.getErrorEquation(values[i], expectedType, errorMsg);
                        }
                    } else {
                        equation = compiler.getErrorEquation(values[i], expectedType, compiler.getLastErrorMsg());
                    }
                    row.set(name, equation);
                } catch (final Exception e) {
                    throw new IOException(e.getMessage(), e.getCause());
                }
                variableNameToTypeMap.put(name, expectedType);
            } else {
                Object value = parseValue(columnType, columnListElementType, values[i]);
                if (value != null)
                    row.set(name, value);
            }
        }
        values = reader.readNext();
    }
    return table;
}
Also used : HashMap(java.util.HashMap) Equation(org.cytoscape.equations.Equation) IOException(java.io.IOException) CyRow(org.cytoscape.model.CyRow) IOException(java.io.IOException) CyTableFactory(org.cytoscape.model.CyTableFactory) CyTable(org.cytoscape.model.CyTable) EquationCompiler(org.cytoscape.equations.EquationCompiler)

Example 29 with Equation

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

the class CSVCyWriter method writeValues.

private void writeValues(CSVWriter writer, Collection<CyColumn> columns) {
    for (CyRow row : table.getAllRows()) {
        if (isCanceled)
            return;
        String[] values = new String[columns.size()];
        int index = 0;
        for (CyColumn column : columns) {
            if (handleEquations) {
                final Object rawValue = row.getRaw(column.getName());
                if (rawValue instanceof Equation) {
                    values[index++] = rawValue.toString();
                    continue;
                }
            }
            Class<?> type = column.getType();
            if (type.equals(List.class)) {
                StringBuilder builder = new StringBuilder();
                boolean first = true;
                List<?> list = row.getList(column.getName(), column.getListElementType());
                if (list != null) {
                    for (Object value : list) {
                        if (!first) {
                            if (fileFilter.getExtensions().contains("csv"))
                                builder.append("|");
                            else
                                builder.append("\r");
                        }
                        if (value != null) {
                            builder.append(value);
                        }
                        first = false;
                    }
                    values[index] = builder.toString();
                }
            } else {
                Object value = row.get(column.getName(), type);
                if (value != null) {
                    values[index] = value.toString();
                } else {
                    values[index] = null;
                }
            }
            index++;
        }
        writer.writeNext(values);
    }
}
Also used : CyColumn(org.cytoscape.model.CyColumn) 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