我需要随着时间的推移更改节点和边的属性。时间被分成时间段,每个时间段看起来都一样:检查每个节点和边缘是否有可能的变化,并在必要时编辑属性。具体来说,有数字属性,节点的大小和边的宽度基于属性值。最初,图形正确显示。节点和边具有假定的大小。但是随着时间的推移动态地改变属性值并不会改变元素的大小。如何确保属性更改也会更改图形可视化?
据我了解 Graphstream 文档和教程有源、汇和刀槽花纹(管道既是源又是汇)。源创建事件,接收器消耗它们。我使用作为源的 GridGenerator。我可以将图形添加为接收器并让生成器创建图形。我想,我必须在图表中添加一个接收器,因为更改图表元素的属性使其成为源。但是我用什么做水槽呢?graph.display()
返回一个查看器,但我不能将它添加为接收器,它说它与graph.addSink(sink)
. 尽管 Graphstream Docs 说 Viewer 是 sink 并且 Viewer 会自动添加为 sink。那为什么我在 UI 中看不到变化呢?我不明白。
生成图后,节点和边得到属性
public static void configureElements(Graph world) {
for (Node node : world.getEachNode()) {
double random = Math.random() * 100;
if (random < 20) {
// remove obstacles
world.removeNode(node)
} else if (random < 30) {
// node have rohstoffe
node.addAttribute("ui.class", "rohstoff");
node.addAttribute("ui.label", node.getId());
node.addAttribute("isRohstoff");
int capacity = (int) (Math.random() * maxCapacity);
node.addAttribute("capacity", capacity);ity);
// nodes size is based on capacity of rohstoffe
node.setAttribute("ui.size", node.getNumber("capacity") + 10);
} else if (random < 32) {
// node is a lager
node.addAttribute("ui.class", "lager");
node.addAttribute("ui.label", node.getId());
node.addAttribute("isLager");
node.addAttribute("lagerstand", 0);
// nodes size is based on capacity of the lager
node.setAttribute("ui.size", node.getNumber("lagerstand") + 10);
} else {
// normal node
node.addAttribute("isNode");
}
}
for (Edge edge : world.getEachEdge()) {
// add pheromones to edge
edge.addAttribute("pheromones", 0);
// edges size is based on number of pheromones
edge.setAttribute("ui.size", edge.getNumber("pheromones"));
}
}
在这里,我随着时间的推移动态更改节点属性
public void dropRohstoff(Node node) {
int oldRohstoff = (int) node.getNumber("rohstoff");
int newRohstoff = oldRohstoff++;
node.setAttribute("rohstoff", newRohstoff);
world.nodeAttributeChanged(world.getId(), (long) world.getStep(), node.getId(),"rohstoff", oldRohstoff, newRohstoff);
}
public void pickRohstoff(Node node) {
int oldCapacity = (int) node.getNumber("capacity");
int newCapicity = oldCapacity++;
node.setAttribute("capacity", newCapicity);
world.nodeAttributeChanged(world.getId(), (long) world.getStep(), node.getId(), "capacity", oldCapacity, newCapicity);
}
这里的边缘属性
public void evaporateAll() {
for (Edge edge : world.getEachEdge()) {
Double oldEvaporateRate = edge.getNumber("pheromones");
Double newEvaporateRate = oldEvaporateRate * (1.0 - evaporateRate);
edge.setAttribute("pheromones", newEvaporateRate);
world.edgeAttributeChanged(world.getId(), (long) world.getStep(), edge.getId(), "pheromones", oldEvaporateRate, newEvaporateRate);
}
}
有人知道我必须如何添加水槽吗?还是我错过了其他东西?