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