From: Sebastian M. <woo...@gm...> - 2013-07-23 12:53:55
|
Hi Thomas, below is my saveToGraphML method. I use providers to set vertex and edge IDs and weights. I use the edge label to save their weight. Not sure if that's the correct way, but it does the trick as long as you don't need the labels for other information. Best, Sebastian [code] /** * Saves a graph to GraphML format. * * @param graph * @param filename * @throws IOException */ public static void saveToGraphML(final Graph<String, MyWeightedEdge> graph, String filename) throws IOException { //GraphMLExporter<String, MyWeightedEdge> exporter = new GraphMLExporter<String, MyWeightedEdge>(); // In order to be able to export edge and node labels and IDs, we must implement providers for them VertexNameProvider<String> vertexIDProvider = new VertexNameProvider<String>() { @Override public String getVertexName(String vertex) { return vertex; } }; VertexNameProvider<String> vertexNameProvider = new VertexNameProvider<String>() { @Override public String getVertexName(String vertex) { return vertex; } }; EdgeNameProvider<MyWeightedEdge> edgeIDProvider = new EdgeNameProvider<MyWeightedEdge>() { @Override public String getEdgeName(MyWeightedEdge edge) { return graph.getEdgeSource(edge) + " > " + graph.getEdgeTarget(edge); } }; EdgeNameProvider<MyWeightedEdge> edgeLabelProvider = new EdgeNameProvider<MyWeightedEdge>() { @Override public String getEdgeName(MyWeightedEdge edge) { return graph.getEdgeWeight(edge); } }; GraphMLExporter<String, MyWeightedEdge> exporter = new GraphMLExporter<String, MyWeightedEdge>(vertexIDProvider, vertexNameProvider, edgeIDProvider, edgeLabelProvider); FileWriter fw = new FileWriter(filename); try { exporter.export(fw, graph); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } } [/code] On 07/23/2013 02:42 PM, Thomas Hupperich wrote: > Hello, > > I am using jgrapht for my research since a few months and came across > the export function. As newer versions of MS Visio do not support csv > file format and - more important - open formats are to be prefered, I > tried to export a graph with the built-in GraphMLExporter. > > Unfortunately there seems no vertex names or edge weights to be exported > (although I am sure, I am making a mistake here), because when I open > the exported .graphml file with yEd or Gephi there are no values of > vertexes and edges. > > I already read that there are NameProvider-classes but I could not > figure out how to use them or even if they are the way to go here. > > So, I would like to export a graph using GraphML where the vertexes have > names like created in the programm ("A", "B", "C") and the edges have > weights like set in the programm (1, 2, 3). > Here is a very small example class and I hope anybody could help me out > on this issue. > > Thank you for reading so far! > > Here is the programm code: > > import java.io.FileWriter; > import java.io.IOException; > import javax.xml.transform.TransformerConfigurationException; > import org.jgrapht.ext.GraphMLExporter; > import org.jgrapht.graph.DefaultWeightedEdge; > import org.jgrapht.graph.SimpleDirectedWeightedGraph; > import org.xml.sax.SAXException; > > public class WIC { > > public SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g; > > public WIC(){ > g = new > SimpleDirectedWeightedGraph<String,DefaultWeightedEdge>(DefaultWeightedEdge.class); > } > > private void test(){ > g.addVertex("A"); > g.addVertex("B"); > g.addVertex("C"); > makeEdge("A","B",1); > makeEdge("B","A",2); > makeEdge("A","C",3); > } > > private void makeEdge(String A, String B, int w){ > DefaultWeightedEdge e = g.addEdge(A,B); > g.setEdgeWeight(e, w); > } > > public static void main(String[] args){ > WIC wic = new WIC(); > wic.test(); > > GraphMLExporter VE = new GraphMLExporter(); > FileWriter PS; > try { > PS = new FileWriter("C:/TestData/graph.graphml"); > VE.export(PS, wic.g); > } catch (IOException | TransformerConfigurationException | > SAXException e) {e.printStackTrace();} > } > > } > > ------------------------------------------------------------------------------ > See everything from the browser to the database with AppDynamics > Get end-to-end visibility with application monitoring from AppDynamics > Isolate bottlenecks and diagnose root cause in seconds. > Start your free trial of AppDynamics Pro today! > http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk > _______________________________________________ > jgrapht-users mailing list > jgr...@li... > https://lists.sourceforge.net/lists/listinfo/jgrapht-users |