在一对节点之间绘制一个具有两个以上顶点的JUNG图

时间:2013-11-20 15:20:04

标签: jung graphml

好的,这里有一个更清晰的解释:

我现在已经明白我需要使用稀疏的ou SparseMultigraph类型才能有双向边缘,所以我已经改变了我的GraphML类:

 class GraphML
           {
              public GraphML(String filename) throws ParserConfigurationException, SAXException, IOException
{
    //Step 1 we make a new GraphML Reader. We want a directed Graph of type node and edge.
    GraphMLReader<SparseMultigraph<node, edge>, node, edge> gmlr =
        new GraphMLReader<SparseMultigraph<node, edge>, node, edge>(new VertexFactory(), new EdgeFactory());

    //Next we need a Graph to store the data that we are reading in from GraphML. This is also a directed Graph
    // because it needs to match to the type of graph we are reading in.
    final SparseMultigraph<node, edge> graph = new SparseMultigraph<node, edge>();

    gmlr.load(filename, graph);
   // gmlr.load(filename, graph); //Here we read in our graph. filename is our .graphml file, and graph is where we
    // will store our graph.

    BidiMap<node, String> vertex_ids = gmlr.getVertexIDs();  //The vertexIDs are stored in a BidiMap.
    Map<String, GraphMLMetadata<node>> vertex_color = gmlr.getVertexMetadata(); //Our vertex Metadata is stored in a map.
    Map<String, GraphMLMetadata<edge>> edge_meta = gmlr.getEdgeMetadata(); // Our edge Metadata is stored in a map.

    // Here we iterate through our vertices, n, and we set the value and the color of our nodes from the data we have
    // in the vertex_ids map and vertex_color map.
    for (node n : graph.getVertices())
    {
        n.setValue(vertex_ids.get(n)); //Set the value of the node to the vertex_id which was read in from the GraphML Reader.
        n.setColor(vertex_color.get("d0").transformer.transform(n)); // Set the color, which we get from the Map, vertex_color.
        //Let's print out the data so we can get a good understanding of what we've got going on.
        System.out.println("ID: "+n.getID()+", Value: "+n.getValue()+", Color: "+n.getColor());
    }

    // Just as we added the vertices to the graph, we add the edges as well.
    for (edge e : graph.getEdges())
    {
        e.setValue(edge_meta.get("d1").transformer.transform(e)); //Set the edge's value.
        System.out.println("Edge ID: "+e.getID());
    }

    TreeBuilder treeBuilder = new TreeBuilder(graph);




    // create a simple graph for the demo:
    //First we make a VisualizationViewer, of type node, edge. We give it our Layout, and the Layout takes a graph in it's constructor.
    //VisualizationViewer<node, edge> vv = new VisualizationViewer<node, edge>(new FRLayout<node, edge>(graph));

    VisualizationViewer<node, edge> vv = new VisualizationViewer<node, edge>(new TreeLayout<node, edge>(treeBuilder.getTree()));

    //Next we set some rendering properties. First we want to color the vertices, so we provide our own vertexPainter.
    vv.getRenderContext().setVertexFillPaintTransformer(new vertexPainter());
    //Then we want to provide labels to our nodes, Jung provides a nice function which makes the graph use a vertex's ToString function
    //as it's way of labelling. We do the same for the edge. Look at the edge and node classes for their ToString function.
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<node>());
    vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<edge>());

    // Next we do some Java stuff, we create a frame to hold the graph
    final JFrame frame = new JFrame();
    frame.setTitle("GraphMLReader for Trees - Reading in Attributes"); //Set the title of our window.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Give a close operation.

    //Here we get the contentPane of our frame and add our a VisualizationViewer, vv.
    frame.getContentPane().add(vv);

    //Finally, we pack it to make sure it is pretty, and set the frame visible. Voila.
    frame.pack();
    frame.setVisible(true);
}

}

然后将我的树构建器类构造函数更改为SparseMultigraph:

public class TreeBuilder
 {
   DelegateForest<node,edge> mTree;
   TreeBuilder(SparseMultigraph<node, edge> graph)
      {
        mTree = new DelegateForest<node, edge>();
        for (node n : graph.getVertices())
         {
            mTree.addVertex(n);
         }
        for (edge e : graph.getEdges())
        {
        mTree.addEdge(e, graph.getSource(e),graph.getDest(e));
        }
       }

    public DelegateForest<node, edge> getTree()
   {
    return mTree;
   }
 }

当我运行我的Main课程时:

public class Main
 {
    public static void main(String[] args) throws ParserConfigurationException,  SAXException, IOException
    {
        String filename = "attributes.graphml";
          if(args.length > 0)
            filename = args[0];
            new GraphML(filename);
    }
  }

我没有收到错误但边缘不存在(节点是图中的但未正确显示)。

由于 Zied

0 个答案:

没有答案