use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class EqnSupport method topoSortAttribReferences.
/**
* @return an in-order list of attribute names that will have to be evaluated before "columnName" can be evaluated
*/
private List<String> topoSortAttribReferences(final Object key, final String columnName, final CyTableImpl tableImpl) {
final Object equationCandidate = tableImpl.getValueOrEquation(key, columnName);
if (!(equationCandidate instanceof Equation))
return new ArrayList<String>();
final Equation equation = (Equation) equationCandidate;
final Set<String> attribReferences = equation.getVariableReferences();
if (attribReferences.size() == 0)
return new ArrayList<String>();
final Set<String> alreadyProcessed = new TreeSet<String>();
alreadyProcessed.add(columnName);
final List<TopoGraphNode> dependencies = new ArrayList<TopoGraphNode>();
for (final String attribReference : attribReferences) followReferences(key, attribReference, alreadyProcessed, dependencies, tableImpl);
final List<TopoGraphNode> topoOrder = TopologicalSort.sort(dependencies);
final List<String> retVal = new ArrayList<String>();
for (final TopoGraphNode node : topoOrder) {
final AttribTopoGraphNode attribTopoGraphNode = (AttribTopoGraphNode) node;
final String nodeName = attribTopoGraphNode.getNodeName();
if (nodeName.equals(columnName))
return retVal;
else
retVal.add(nodeName);
}
// We should never get here because "columnName" should have been found in the for-loop above!
throw new IllegalStateException("\"" + columnName + "\" was not found in the toplogical order, which should be impossible.");
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class EqnSupport method followReferences.
/**
* Helper function for topoSortAttribReferences() performing a depth-first search of equation evaluation dependencies.
*/
private static void followReferences(final Object key, final String columnName, final Collection<String> alreadyProcessed, final Collection<TopoGraphNode> dependencies, final CyTableImpl tableImpl) {
// Already visited this attribute?
if (alreadyProcessed.contains(columnName))
return;
alreadyProcessed.add(columnName);
final Object equationCandidate = tableImpl.getValueOrEquation(key, columnName);
if (!(equationCandidate instanceof Equation))
return;
final Equation equation = (Equation) equationCandidate;
final Set<String> attribReferences = equation.getVariableReferences();
for (final String attribReference : attribReferences) followReferences(key, attribReference, alreadyProcessed, dependencies, tableImpl);
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class EqnSupport method scalarEquationIsCompatible.
static boolean scalarEquationIsCompatible(final Object equationCandidate, final Class<?> targetType) {
if (!(equationCandidate instanceof Equation))
return false;
final Equation equation = (Equation) equationCandidate;
final Class<?> eqType = equation.getType();
if (targetType == String.class)
// Everything can be turned into a String!
return true;
if (targetType == Boolean.class || Number.class.isAssignableFrom(targetType))
return eqType == Boolean.class || Number.class.isAssignableFrom(eqType);
return false;
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class BrowserTableModel method setValueAt.
@Override
public void setValueAt(final Object value, final int rowIndex, final int columnIndex) {
final String text = (String) value;
final CyRow row = getCyRow(rowIndex);
final String columnName = mapColumnIndexToColumnName(columnIndex);
final Class<?> columnType = dataTable.getColumn(columnName).getType();
final Class<?> listElementType = dataTable.getColumn(columnName).getListElementType();
if (text.isEmpty()) {
if (!row.isSet(columnName))
return;
row.set(columnName, null);
} else if (text.startsWith("=")) {
final Map<String, Class<?>> attrNameToTypeMap = TableBrowserUtil.getAttNameToTypeMap(dataTable, null);
if (compiler.compile(text, attrNameToTypeMap)) {
final Equation eqn = compiler.getEquation();
final Class<?> eqnType = eqn.getType();
// Is the equation type compatible with the column type?
if (eqnTypeIsCompatible(columnType, listElementType, eqnType)) {
row.set(columnName, eqn);
} else {
// The equation type is incompatible w/ the column type!
final Class<?> expectedType = columnType == Integer.class ? Long.class : columnType;
final String errorMsg = "Equation result type is " + getUnqualifiedName(eqnType) + ", column type is " + getUnqualifiedName(columnType) + ".";
final Equation errorEqn = compiler.getErrorEquation(text, expectedType, errorMsg);
row.set(columnName, errorEqn);
}
} else {
final Class<?> eqnType = columnType == Integer.class ? Long.class : columnType;
final String errorMsg = compiler.getLastErrorMsg();
final Equation errorEqn = compiler.getErrorEquation(text, eqnType, errorMsg);
row.set(columnName, errorEqn);
}
} else {
// Not an equation!
final List<Object> parsedData = TableBrowserUtil.parseCellInput(dataTable, columnName, value);
if (parsedData.get(0) != null)
row.set(columnName, parsedData.get(0));
else {
// Error!
showErrorWindow(parsedData.get(1).toString());
// + " should be an Integer (or the number is too big/small).");
}
}
final TableModelEvent event = new TableModelEvent(this, rowIndex, rowIndex, columnIndex);
fireTableChanged(event);
fireTableDataChanged();
}
use of org.cytoscape.equations.Equation in project cytoscape-impl by cytoscape.
the class InDegreeTest method test.
@Test
public void test() {
final EquationParserImpl parser = new EquationParserImpl(serviceRegistrar);
final EquationCompilerImpl compiler = new EquationCompilerImpl(parser);
parser.registerFunctionInternal(new InDegree(serviceRegistrar));
final Map<String, Class<?>> variableNameToTypeMap = new HashMap<String, Class<?>>();
if (!compiler.compile("=INDEGREE(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));
}
Aggregations