diff --git a/src/main/java/GraphData.java b/src/main/java/GraphData.java index 44b75ae..97a1c21 100644 --- a/src/main/java/GraphData.java +++ b/src/main/java/GraphData.java @@ -1,5 +1,60 @@ +import org.jgrapht.*; +import org.jgrapht.graph.*; +import org.jgrapht.nio.dot.DOTExporter; +import org.jgrapht.nio.dot.DOTImporter; +import org.jgrapht.traverse.*; +import org.jgrapht.io.*; + +import java.io.*; +import java.io.File; +import java.net.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; + public class GraphData { - public static void main(String[] args) { - System.out.println("Hello") + private Graph graphObject = new DefaultDirectedGraph<>(DefaultEdge.class); + + public void parseGraph(String filepath) throws IOException { + + // Import the graph from file + DOTImporter dotImporter = new DOTImporter<>(); + dotImporter.setVertexFactory(label -> label); + String fileContent = Files.readString(Paths.get(filepath)); + dotImporter.importGraph(graphObject, new StringReader(fileContent)); + System.out.println("Graph successfully parsed!"); + } + + @Override + public String toString() { + String opt = ""; + opt+="Number of nodes: "+graphObject.vertexSet().size()+"\n"; + opt+="Node labels: "+graphObject.vertexSet()+"\n"; + opt+="Number of edges: "+graphObject.edgeSet().size()+"\n"; + StringBuilder edges = new StringBuilder(); + for (DefaultEdge e: graphObject.edgeSet()) { + edges.append(e.toString().replace(":", "->")); + edges.append(", "); + } + edges.deleteCharAt(edges.length() - 1); + edges.deleteCharAt(edges.length() - 1); + opt+="Node and edge directions: "+edges+"\n"; + return opt; + } + + public void outputGraph(String filepath) throws IOException { + String opt = toString(); + Files.writeString(Paths.get(filepath), opt, StandardCharsets.ISO_8859_1); + } + public static void main(String[] args) throws IOException { + GraphData graphApi = new GraphData(); + graphApi.parseGraph("src/main/example.dot"); + System.out.println(graphApi.toString()); + graphApi.outputGraph("src/main/output.txt"); + + + + } }