我正在尝试创建一个动态更新的 Prefuse 图,其中定期添加和删除节点。我已经能够很好地添加节点和边,然后删除它们,但是当我尝试将边添加到下一组节点时出现异常。有谁知道从 Prefuse 图中删除节点的任何陷阱?或者,有谁知道动态与 Prefuse 图交互的更好方法?
(请注意,显示更新正常;显示似乎始终与图形的内容匹配。这不是Prefuse Toolkit 的欺骗:动态添加节点和边)
代码摘录:
// Set up edge and node tables
Schema node_schema = new Schema();
Schema edge_schema = new Schema();
node_schema.addColumn("node_info", NodeInfo.class);
edge_schema.addColumn("source", int.class, 0);
edge_schema.addColumn("target", int.class, 0);
edge_schema.addColumn("edge_info", EdgeInfo.class);
graph_nodes = node_schema.instantiate();
graph_edges = edge_schema.instantiate();
...
// Set up visualization
graph = new Graph(graph_nodes, graph_edges, false);
vis_panel = new MyVisPanel(graph);
...
// Add nodes & edges
int new_node_1 = graph_nodes.addRow();
graph_nodes.set(new_node_1, "node_info", new NodeInfo());
int new_node_2 = graph_nodes.addRow();
graph_nodes.set(new_node_2, "node_info", new NodeInfo());
int new_edge = graph_edges.addRow();
graph_edges.set(new_edge, "target", new_node_1);
graph_edges.set(new_edge, "source", new_node_2);
...
// Remove nodes
TupleSet nodes = graph.getNodes();
Iterator iter = nodes.tuples(my_predicate);
while(iter.hasNext())
{
int row_to_delete = ((Tuple) iter.next()).getRow();
if(graph.removeNode(row_to_delete))
{
System.out.println("Removed row " + row_to_delete);
}
}
...
// Add nodes & edges again
int new_node_1 = graph_nodes.addRow();
graph_nodes.set(new_node_1, "node_info", new NodeInfo());
int new_node_2 = graph_nodes.addRow();
graph_nodes.set(new_node_2, "node_info", new NodeInfo());
int new_edge = graph_edges.addRow(); // <<-- Exception is thrown here
graph_edges.set(new_edge, "target", new_node_1);
graph_edges.set(new_edge, "source", new_node_2);
我得到的例外是:
java.lang.IllegalArgumentException: Row index out of bounds: -1
at prefuse.data.column.IntColumn.getInt(Unknown Source)
at prefuse.data.Table.getInt(Unknown Source)
at prefuse.data.Graph.updateDegrees(Unknown Source)
at prefuse.data.Graph.updateDegrees(Unknown Source)
at prefuse.data.Graph$Listener.tableChanged(Unknown Source)
at prefuse.data.Table.fireTableEvent(Unknown Source)
at prefuse.data.Table.addRow(Unknown Source)
at my_pkg.addSomeNodesFunction(my_code.java:99)
我没有使用标准的 Graph.addNode() 和 addEdge() 方法,因为这样设置边缘信息似乎更难。(如果有更好的方法,请告诉我!)删除成功,在那个 graph.getNodeCount() 和 graph.getEdgeCount() 删除节点后返回较小的数字。
有任何想法吗?