Search in sources :

Example 1 with EquationParser

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

the class FormulaBuilderDialog method expressionIsValid.

/**
 *  Tests whether "expression" is valid given the possible argument types are "validArgTypes".
 *  @returns null, if "expression" is invalid, or, the type of "expression" if it was valid
 */
private Class<?> expressionIsValid(final List<Class<?>> validArgTypes, final String expression) {
    final Map<String, Class<?>> attribNamesAndTypes = new HashMap<String, Class<?>>();
    for (final CyColumn column : tableModel.getDataTable().getColumns()) attribNamesAndTypes.put(column.getName(), column.getType());
    final EquationParser parser = compiler.getParser();
    if (!parser.parse("=" + expression, attribNamesAndTypes)) {
        displayErrorMessage(parser.getErrorMsg());
        return null;
    }
    final Class<?> expressionType = parser.getType();
    if (validArgTypes.contains(expressionType))
        return expressionType;
    final StringBuilder errorMessage = new StringBuilder("Expression is of an incompatible data type (");
    errorMessage.append(getLastDotComponent(expressionType.toString()));
    errorMessage.append(") valid types are: ");
    for (int i = 0; i < validArgTypes.size(); ++i) {
        if (validArgTypes.get(i) == null)
            continue;
        errorMessage.append(getLastDotComponent(validArgTypes.get(i).toString()));
        if (i < validArgTypes.size() - 1)
            errorMessage.append(',');
    }
    displayErrorMessage(errorMessage.toString());
    return null;
}
Also used : HashMap(java.util.HashMap) EquationParser(org.cytoscape.equations.EquationParser) CyColumn(org.cytoscape.model.CyColumn)

Example 2 with EquationParser

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

the class FormulaBuilderDialog method getFunctionList.

private JList<Function> getFunctionList() {
    if (functionList == null) {
        DefaultListModel<Function> model = new DefaultListModel<>();
        functionList = new JList<>(model);
        functionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        functionList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                functionSelected();
            }
        });
        functionList.setCellRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                final Function fn = (Function) value;
                this.setText(fn.getName());
                this.setToolTipText(fn.getFunctionSummary());
                return this;
            }
        });
        final EquationParser parser = compiler.getParser();
        final List<Function> functions = new ArrayList<>(parser.getRegisteredFunctions());
        final Collator collator = Collator.getInstance(Locale.getDefault());
        Collections.sort(functions, new Comparator<Function>() {

            @Override
            public int compare(Function f1, Function f2) {
                return collator.compare(f1.getName(), f2.getName());
            }
        });
        final Class<?> requestedReturnType = getAttributeType(targetAttrName);
        int index = 0;
        for (final Function fn : functions) {
            if (returnTypeIsCompatible(requestedReturnType, fn.getReturnType()))
                model.add(index++, fn);
        }
    }
    return functionList;
}
Also used : EquationParser(org.cytoscape.equations.EquationParser) ListSelectionEvent(javax.swing.event.ListSelectionEvent) ArrayList(java.util.ArrayList) DefaultListModel(javax.swing.DefaultListModel) ListSelectionListener(javax.swing.event.ListSelectionListener) Collator(java.text.Collator) Function(org.cytoscape.equations.Function) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) Component(java.awt.Component)

Aggregations

EquationParser (org.cytoscape.equations.EquationParser)2 Component (java.awt.Component)1 Collator (java.text.Collator)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DefaultListCellRenderer (javax.swing.DefaultListCellRenderer)1 DefaultListModel (javax.swing.DefaultListModel)1 ListSelectionEvent (javax.swing.event.ListSelectionEvent)1 ListSelectionListener (javax.swing.event.ListSelectionListener)1 Function (org.cytoscape.equations.Function)1 CyColumn (org.cytoscape.model.CyColumn)1