0

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

我现在明白我需要使用 sparse 或 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;
   }
 }

当我运行我的主要课程时:

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

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

谢谢齐德

4

0 回答 0