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;
}
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;
}
Aggregations