0

Ok so here is a clearer explanation :

I have now understood that I need to use sparse ou SparseMultigraph type to be able to have bidirectional edges so I have changed my GraphML class as such :

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

}

And then changed my tree builder class constructor to 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;
   }
 }

when I run my Main class :

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

I don't get an error but edges are not present (node are their in the graph but not properly displayed).

Thanks Zied

Ziwdigforbugs
  • 1,185
  • 8
  • 24
  • 41
  • You have provided neither a clear explanation of the error you're getting nor a small self contained example that demonstrates it. – Joshua O'Madadhain Nov 21 '13 at 16:58
  • Your comment about SparseMultigraph makes no sense: you say you want "bidirectional edges" (although you don't make it clear what you mean by this). SparseMultigraph supports both directed and undirected edges; maybe that's what you meant? In any case, you need to find out whether the edges are there in the graph built by GraphMLReader (if not, that suggests that your input file may be buggy; if so, then the bug is likely in your building of the tree or perhaps in the rendering of the graph). In short: there are a lot of potential places for bugs; you need to investigate them. – Joshua O'Madadhain Nov 26 '13 at 17:48
  • Thanks fo your answer Joshua, my bug was that I was using a directed graph instance. This type of graph does nor support bidirectional edges (A-->B and B-->A) by changing the graph to a sparse one I had to change the way my tree is built. I have managed to draw the graph now. – Ziwdigforbugs Nov 29 '13 at 09:36
  • Directed graphs do in general allow antiparallel edges (the proper term for paired edges as you describe). Trees, which are a subtype of directed graph, do not, for hopefully obvious reasons. – Joshua O'Madadhain Dec 01 '13 at 02:13

0 Answers0