-1

I have a graph of dot format as String. I want to get its nodes, edges and their data for processing. I am looking for a Java library which processes a given dot format graph. An example would be appreciated.

digraph G { rankdir=TB
    V1 [a=1, b=2, label="V1"]; 
    V2 [a=4, b=0, label="V2"]; 
    V3 [a=1, b=3, label="V3"]; 
    V4 [a=3, b=7, label="V4"];

    V1 -> V2 [path=a, label="a"];  
    V2 -> V3 [path=b, label="b"]; 
    V1 -> V4 [path=c, label="c"];
    V2 -> V4 [path=d, label="d"];
}
David
  • 61
  • 1
  • 10
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, [describe the problem](http://meta.stackoverflow.com/questions/254393) and what has been done so far to solve it. – Joe C Apr 22 '18 at 16:41

1 Answers1

1

Assuming that you want to use the JGraphT - Library, you can use this class: http://jgrapht.org/javadoc/org/jgrapht/io/DOTImporter.html

You need to pass a VertexProvider and an EdgeProvider, which must return the a vertex and an edge; they can also contain additional logic for the other attributes:

SimpleDirectedGraph< String ,DefaultEdge> labeledGraph = new SimpleDirectedGraph<>(DefaultEdge.class);
DOTImporter<Integer,DefaultEdge> importer = new DOTImporter<>(
    (String label, Map<String, Attribute> attributes) -> {
          // handle the attributes here
         return label;
    },
    (from, to, label, attributes) -> {
         // handle the attributes here
         return labelGraph.addEdge(from, to);
    });
importer.importGraph(labelGraph, yourFileReader)
LyteFM
  • 506
  • 5
  • 10