FordFulkersonLeChameau/src/display.ml
2023-01-14 14:34:40 +01:00

78 lines
2.3 KiB
OCaml

open Graph
open Printf
open Gfile
(*exports an int graph to an output format
and writes it to the the path given as second parameter*)
let exportBase g path =
let str = e_fold (*goes over all edges*)
g
(fun acc id1 id2 label -> acc ^ "\n "
^ (string_of_int id1) ^ " -> "
^ (string_of_int id2) ^ " [label = \""
^ (string_of_int label) ^ "\"];" )
""
in
(*adds the header for the output file*)
let startStr =
"
digraph finite_state_machine {\n
fontname=\"Helvetica,Arial,sans-serif\"\n
node [fontname=\"Helvetica,Arial,sans-serif\"]\n
edge [fontname=\"Helvetica,Arial,sans-serif\"]\n
rankdir=LR;\n
node [shape = circle];\n
"
in
let finishStr = "\n}" in
(*final string from the 3 parts*)
let totalStr = startStr ^ str ^ finishStr in
(*writes to the file*)
let ff = open_out path in
fprintf ff "%s%!" totalStr;
close_out ff;
()
(*exports an int graph with a source and sink to an output format showing the difference (ex : 5/12)
and writes it to the the path given as second parameter*)
let exportDifference gStart gEnd path =
let findUsedCapacity gEnd id1 id2 capaStart =
let newEdge = find_arc gEnd id1 id2 in
match newEdge with
| None -> capaStart
| Some a -> capaStart - a
in
let str = e_fold
gStart
(fun x id1 id2 label -> x ^ "\n "
^ (string_of_int id1) ^ " -> "
^ (string_of_int id2) ^ " [label = \""
^ string_of_int (findUsedCapacity gEnd id1 id2 label) ^ "/"
^ (string_of_int label) ^"\"];" )
""
in
let startStr =
"
digraph finite_state_machine {\n
fontname=\"Helvetica,Arial,sans-serif\"\n
node [fontname=\"Helvetica,Arial,sans-serif\"]\n
edge [fontname=\"Helvetica,Arial,sans-serif\"]\n
rankdir=LR;\n
node [shape = circle];\n
"
in
let finishStr = "\n}" in
let totalStr = startStr ^ str ^ finishStr in
let ff = open_out path in
fprintf ff "%s%!" totalStr;
close_out ff;
()