import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.nio.dot.DOTExporter;
import org.jgrapht.nio.graphml.GraphMLExporter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class GraphFileWriter {

    String filename;
    SimpleWeightedGraph graph;

    public GraphFileWriter(String filename, SimpleWeightedGraph graph) {
        if(!filename.matches(".*\\.gv")){
            filename = filename + ".gv";
        }
        this.filename = filename;
        this.graph = graph;
    }

    public void writeGraphToFile() {
        try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
        ){
            GraphMLExporter<SimpleWeightedGraph, BufferedWriter> exporter = new GraphMLExporter<>();
            exporter.exportGraph(graph, writer);
        } catch(IOException ex){
            System.out.println("Could not make new file named "+filename);
            System.err.println(ex);
        }
    }
}
