Skip to content

Commit

Permalink
Implement first feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
theViz343 committed Oct 7, 2023
1 parent 00ee3f8 commit 41c210e
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
@@ -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<String, DefaultEdge> graphObject = new DefaultDirectedGraph<>(DefaultEdge.class);

public void parseGraph(String filepath) throws IOException {

// Import the graph from file
DOTImporter<String, DefaultEdge> 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");




}
}

0 comments on commit 41c210e

Please sign in to comment.