显示图形流路径

Displaying a graphstream Path

我有一个 graphstream 图,每个节点和边都有一些属性。然后,我使用以下代码获取两个节点之间的最短路径:

AStar astar = new AStar(graph);
astar.setCosts(new DistanceCosts());
astar.setSource(fromNodeIdentifierString);
astar.setTarget(toNodeIdentifierString);
astar.compute();
org.graphstream.graph.Path p = astar.getShortestPath();

然后我如何从图中删除所有不在路径上的节点 - 或者以其他方式将路径变成图?

我试过了

graph.clear()
for (Node n : p.getNodeSet())
{
    graph.addNode(n);
}

但显然这不起作用,因为您不能添加节点对象,只能创建具有给定 ID 的新节点。我是否真的必须将整个路径重新创建为节点和边缘,并重新创建所有属性等?或者遍历图中的所有节点并删除那些 ID 与路径中的节点不匹配的节点?肯定有更有效的方法从路径获取图形吗?

List<org.graphstream.graph.Node> nodes = graph.nodes().collect(Collectors.toList());
nodes.removeAll(p.getNodeSet());
nodes.forEach
(
    node -> graph.removeNode(node)
);