GraphML With JUNG: Importing GraphML
This is a follow up to my earlier post on saving to GraphML using the JUNG library. It would make more sense if you browsed through that post before reading this one.
You first need to read from your file. This snippet creates a standard Java
which in turn is fed from a
FileReader that reads from the file specified by the
The problem is, the
GraphMLReader2 is just a smart text parser. For it to do anything useful,
you need to specify
Transformers for the vertices, edges, hyperedges and the graph itself.
We pump these transformers into the function reading the graph so it understands what exactly
to do after it reads these different components in from the GraphML file.
This transformer is used to parse the
<graph> tag in my GraphML file and create a new
Graph (it would really help if you had your GraphML file open at this point to understand
what the transformers are doing). This tag specifies the beginning of your graph, and whether
the graph is directed or undirected. The function
metadata.getEdgeDefault() in this case
"undirected" which is equal to the library-defined constant
metadata.getEdgeDefault().DIRECTED. So this transformer checks whether the graph in the file
is directed and returns the required
The vertex transformer is slightly more involved. I’ll list out the various parts:
MyVertexFactory: This is my custom
Factorythat generates objects of my custom
MyVertexFactory.getInstance(): This returns an object of the MyVertexFactory class that I can now use to access it’s methods.
v.setX(Double d)/v.setY(Double d): These are custom functions I’ve defined for my
MyVertexclass that set the
private int xand
private int yproperties of the current vertex to the specified value.
metadata.getProperty("y"): This gets the value of the x and y properties that I’ve stored in my GraphML file (we used the
addVertexDatafunction in the previous post to save these out).
The vertex transformer parses the vertex data from the file and reads in its x and y
coordinates, and then sets these properties on the newly created
Similar to the previous transformer, this edge transformer reads in the edge data from the
file and creates a new edge. The
getInstance() are analogous to those
just discussed for the
This is pretty much the same code as my edge transformer! My graphs didn’t have hyper-edges, but the GraphMLReader2 definition requires that you specify a hyperedge transformer too.
Finally, you create your graph reader object:
It’s a pretty long statement to create one object, so make sure you’ve got all your angle brackets in place.
Now you have your Graph object! Assuming you’ve worked with simple JUNG Graph objects enough to want to save them out, you should be able to take it from here.
There is a small catch though; the graph right now will be displayed without any regard to the
y properties associated with its vertex objects. So you’ll need to specify a
StaticLayout with a custom transformer, that reads in the x and y properties of each vertex
and places them accordingly on your canvas. You do that like this:
This transformer is required to convert a vertex object to a
Point2D object that the
layout needs to position its vertices. It creates a new
Point2D object by using my custom
getY() methods that return the x and y properties of the vertex.