use of cbit.util.graph.Edge in project vcell by virtualcell.
the class MathSystemHash method getDependencyGraph.
/**
* Insert the method's description here.
* Creation date: (4/12/2002 3:45:17 PM)
* @return cbit.vcell.math.Variable[]
* @param variables cbit.vcell.math.Variable[]
*/
public Graph getDependencyGraph(Symbol[] symbols) throws MathSystemHash.UnresolvedException {
//
// add the nodes for each "Symbol"
//
Graph graph = new Graph();
for (int i = 0; i < symbols.length; i++) {
Node node = new Node(symbols[i].getName(), symbols[i]);
graph.addNode(node);
}
//
// add an edge for each dependency of a Symbol (due to it's associated expression implied equation ... e.g. Rate, IC).
//
Node[] nodes = graph.getNodes();
for (int i = 0; i < nodes.length; i++) {
Symbol symbol = (Symbol) nodes[i].getData();
if (symbol.getExpression() != null) {
String[] symbolNames = symbol.getExpression().getSymbols();
for (int j = 0; symbolNames != null && j < symbolNames.length; j++) {
//
// find node that this symbol references
//
Node symbolNode = graph.getNode(symbolNames[j]);
if (symbolNode == null) {
throw new RuntimeException("symbol " + symbol.getName() + " references unknown symbol '" + symbolNames[j] + "'");
}
Edge dependency = new Edge(nodes[i], symbolNode, symbol.getName() + "->" + ((Symbol) symbolNode.getData()).getName());
graph.addEdge(dependency);
}
}
if (symbol instanceof VariableReference) {
VariableReference varRef = (VariableReference) symbol;
Node referencingVarNode = graph.getNode(varRef.variable.getName());
if (referencingVarNode == null) {
throw new RuntimeException("symbol " + symbol.getName() + " references unknown variable '" + varRef.variable.getName() + "'");
}
Edge dependency = new Edge(referencingVarNode, nodes[i], varRef.variable.getName() + "->" + symbol.getName());
graph.addEdge(dependency);
}
}
return graph;
}
Aggregations