如何将AttributeChangedListener从Graphstream附加到图上?

时间:2019-04-22 12:44:37

标签: java events graphstream

我需要随时间更改节点和边的属性。时间分为多个时间段,每个时间段看起来都相同:检查每个节点和边是否可能发生更改,并在必要时编辑属性。具体来说,有数字属性,节点的大小和边缘的宽度基于属性值。最初,图形显示正确。节点和边具有假定的大小。但是随着时间的推移动态更改属性值不会更改元素的大小。我如何确定属性更改也会改变图形的可视化效果?

据我了解Graphstream Docs和教程,有源,接收器和刀槽花纹(管道既是源也是接收器)。源创建事件,接收器消耗事件。我使用的是GridGenerator。我可以将图形添加为接收器,并让生成器创建图形。我认为,然后必须向图形添加一个接收器,因为更改图形元素的属性使其成为源。但是我怎么用做水槽呢? graph.display()返回查看器,但我不能将其添加为接收器,它说它与graph.addSink(sink)的参数不兼容。即使Graphstream Docs说查看器是一个接收器,并且查看器也会自动添加为接收器。那么为什么我看不到用户界面中的更改?我不明白。

生成图后,节点和边会获得那里的属性

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"));
  }
}

在这里,我会随着时间动态更改node属性

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);
  }
}

有人知道我必须添加水槽吗?还是我想念其他东西?

1 个答案:

答案 0 :(得分:0)

首先,您可以像这样将ViewerPipe与查看器一起使用:

ViewerPipe pipeIn = viewer.newViewerPipe();

这样,您可以向图形添加一个接收器:

pipeIn.addAttributeSink( graph );
pipeIn.pump();

此外,如果您想动态调整大小,请不要忘记将css属性添加到节点:

size-mode: dyn-size;

这是graphstream 1.3的一个最小示例

public class Issue {
    public static void main(String args[]) {
        System.setProperty( "org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer" );
        new Issue();
    }

    protected boolean loop = true;

    public Issue() {
        Graph graph = new MultiGraph("main graph");
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        ViewerPipe pipeIn = viewer.newViewerPipe();
        viewer.addView("view1", new J2DGraphRenderer());

        graph.addAttribute("ui.antialias");

        pipeIn.addAttributeSink( graph );
        pipeIn.pump();

        Node A = graph.addNode("A");
        Node B = graph.addNode("B");
        Node C = graph.addNode("C");

        graph.addEdge("AB", "A", "B", true);
        graph.addEdge("BC", "B", "C", true);
        graph.addEdge("CA", "C", "A", true);

        A.addAttribute("xyz", 0, 1, 0);
        B.addAttribute("xyz", 1, 0, 0);
        C.addAttribute("xyz", -1, 0, 0);

        A.setAttribute("ui.label", "A");
        B.setAttribute("ui.label", "B");
        C.setAttribute("ui.label", "C");

        graph.addAttribute("ui.stylesheet", styleSheet);

        float color = 0;
        float dir = 0.01f;

        float size = 20f;
        float sizeInc = 1f;

        while( loop ) {
            pipeIn.pump();
            sleep( 40 );
            A.setAttribute( "ui.size", size );

            size += sizeInc;

            if( size > 50 ) {
                sizeInc = -1f; size = 50f;
            } else if( size < 20 ) {
                sizeInc = 1f; size = 20f;
            }
            System.out.println(size);
        }

        System.exit(0);
    }

    protected void sleep( long ms ) {
        try { Thread.sleep( ms ) ; } 
        catch (InterruptedException e) { e.printStackTrace(); }
    }

    private String styleSheet = 
            "graph {"+
            "   canvas-color: white;"+
            "       fill-mode: gradient-radial;"+
            "       fill-color: white, #EEEEEE;"+
            "       padding: 60px;"+
            "   }"+
            "node {"+
            "   size-mode: dyn-size;"+
            "   shape: circle;"+
            "   size: 20px;"+
            "   fill-mode: plain;"+
            "   fill-color: #CCC;"+
            "   stroke-mode: plain;"+
            "   stroke-color: black;"+
            "   stroke-width: 1px;"+
            "}";
}

您可以在graphstream 2.0 swing中找到示例here,在graphstream 2.0 javafx中找到here,或者在graphstream 1.3中(在scala中)找到here