commit initial
Cette révision appartient à :
114
ocaml/affichage_auto.ml
Fichier normal
114
ocaml/affichage_auto.ml
Fichier normal
@@ -0,0 +1,114 @@
|
||||
#require "unix";;
|
||||
|
||||
module Affichage =
|
||||
struct
|
||||
let s_o_c a = if a = '"' then
|
||||
"\\\\guill"
|
||||
else if a = '\\' then
|
||||
"\\\\ligne"
|
||||
else if a = ' ' then
|
||||
"\\\\espace"
|
||||
else
|
||||
String.make 1 a;;
|
||||
|
||||
let s_o_l a =
|
||||
let lettre = s_o_c a
|
||||
in "\"" ^ lettre ^ "\"";;
|
||||
|
||||
let s_o_m (a, n) =
|
||||
let lettre = s_o_c a in
|
||||
"\"$\\text{" ^ lettre ^ "}_{" ^ (string_of_int n) ^ "}$\"";;
|
||||
|
||||
let dot_auto print string_of_lettre auto =
|
||||
let lien = "->" in
|
||||
print ("digraph G {\n");
|
||||
for x = 0 to auto.nQ - 1 do
|
||||
print ((string_of_int x) ^ "[style=\"state");
|
||||
if List.mem x auto.i then
|
||||
print ",initial";
|
||||
if List.mem x auto.f then
|
||||
print ",accepting";
|
||||
print ("\", label=\"" ^ (string_of_int x) ^ "\"];\n")
|
||||
done;
|
||||
List.iter (
|
||||
fun (q, a, q') ->
|
||||
print ((string_of_int q) ^ lien ^ (string_of_int q'));
|
||||
print (" [label=" ^ (string_of_lettre a) ^ "]");
|
||||
print ";\n"
|
||||
)
|
||||
auto.t;
|
||||
print "}\n";;
|
||||
|
||||
let auto_to_file fn string_of_lettre auto =
|
||||
let os = open_out fn in
|
||||
let print = output_string os in
|
||||
print "\\documentclass[tikz]{standalone}\n";
|
||||
print "\\usetikzlibrary{automata, positioning, shapes, snakes, arrows}\n";
|
||||
print "\\usepackage{dot2texi, amsmath}\n";
|
||||
print "\\def\\guill{\"}\n\\def\\ligne{\\textbackslash\\textbackslash}\n\\def\\espace{\\textvisiblespace}\n";
|
||||
print "\\begin{document}\n";
|
||||
print "\\begin{tikzpicture}[scale=.75,shorten >=1pt,node distance=1cm,auto, initial text={}]\n";
|
||||
print "\\begin{dot2tex}[tikz, codeonly, styleonly, options={-t raw}]\n";
|
||||
dot_auto (output_string os) string_of_lettre auto;
|
||||
print "\\end{dot2tex}\n";
|
||||
(*flush os;
|
||||
let fd_pipe_exit, fd_pipe_input = Unix.pipe () in
|
||||
let outch_input = Unix.out_channel_of_descr fd_pipe_input in
|
||||
let pid = Unix.create_process "dot2tex" [|"-f"; "tikz"; "-t"; "raw"; "--codeonly"; "--styleonly"|] (Unix.descr_of_out_channel os) fd_pipe_input Unix.stderr
|
||||
in
|
||||
dot_auto (output_string outch_input) string_of_lettre auto;
|
||||
flush outch_input;
|
||||
ignore (Unix.waitpid [Unix.WNOHANG] pid);
|
||||
flush os;*)
|
||||
print "\\end{tikzpicture}\n";
|
||||
print "\\end{document}";
|
||||
close_out os;;
|
||||
|
||||
|
||||
let run wait cmd =
|
||||
if wait then
|
||||
ignore (Unix.system cmd)
|
||||
else
|
||||
ignore (Unix.open_process cmd);;
|
||||
|
||||
let rundot fn =
|
||||
let bn = try Filename.chop_extension fn with Invalid_argument _ -> fn in
|
||||
let pdfn = bn^".pdf" in
|
||||
run true ("latexmk -xelatex -cd -shell-escape " ^ (Filename.quote fn));
|
||||
pdfn;;
|
||||
|
||||
let openPDF wait fn =
|
||||
run wait ("xdg-open " ^ (Filename.quote fn));;
|
||||
|
||||
let vizgraph wait fn =
|
||||
let pdfn = rundot fn in
|
||||
openPDF wait pdfn;;
|
||||
|
||||
let rec string_of_regexp sol = function
|
||||
| Vide -> "∅"
|
||||
| Epsilon -> "ε"
|
||||
| Lettre(x) -> sol x
|
||||
| Union([]) -> "∅"
|
||||
| Union([x]) -> string_of_regexp sol x
|
||||
| Union(le) -> "(" ^ (String.concat "|" (List.map (fun x -> string_of_regexp sol x) le)) ^ ")"
|
||||
| Concat(le) -> String.concat "" (List.map (fun x -> string_of_regexp sol x) le)
|
||||
| Etoile(x) -> (string_of_regexp sol x)^"*";;
|
||||
|
||||
|
||||
end
|
||||
|
||||
let string_of_char = String.make 1;;
|
||||
let string_of_charint (a, n) = (String.make 1 a) ^ (string_of_int n);;
|
||||
|
||||
let string_of_regexp = Affichage.string_of_regexp string_of_char;;
|
||||
let string_of_regexp_marques = Affichage.string_of_regexp string_of_charint;;
|
||||
|
||||
let afficher_auto a = let fn = (Filename.temp_file "auto" ".tex") in
|
||||
Affichage.auto_to_file fn Affichage.s_o_l a;
|
||||
Affichage.vizgraph false fn;;
|
||||
|
||||
let afficher_auto_marques a = let fn = (Filename.temp_file "auto" ".tex") in
|
||||
Affichage.auto_to_file fn Affichage.s_o_m a;
|
||||
Affichage.vizgraph false fn;;
|
||||
|
||||
let string_of_list f l = "[" ^ (String.concat "; " (List.map f l)) ^"]";;
|
||||
63
ocaml/affichage_graphe.ml
Fichier normal
63
ocaml/affichage_graphe.ml
Fichier normal
@@ -0,0 +1,63 @@
|
||||
#load "unix.cma";;
|
||||
|
||||
(* graphe_to_dot génère la représentation dot d'un graphe orienté ou non avec
|
||||
les infos issues de l'algo de Dijkstra. fn est le nom du fichier dans lequel on écrit
|
||||
(recommandé : "/tmp/quelquechose.dot")
|
||||
|
||||
vizgraph fn lance la compilation d'un fichier dot en un PDF puis affiche ce PDF *)
|
||||
|
||||
|
||||
let graphe_to_dot fn oriente graphe marques estimations parents =
|
||||
let os = open_out fn in
|
||||
let print = output_string os in
|
||||
if oriente then print "di";
|
||||
print "graph {\n";
|
||||
print "rankdir=\"LR\";\n";
|
||||
for x = 0 to Array.length graphe - 1 do
|
||||
print (string_of_int x);
|
||||
print "[label=\"";
|
||||
if marques.(x) then print "*";
|
||||
print (string_of_int x);
|
||||
print " (";
|
||||
if estimations.(x) = -1 then
|
||||
print "∞"
|
||||
else
|
||||
print (string_of_int estimations.(x));
|
||||
print ")\"];\n";
|
||||
List.iter
|
||||
(fun (y, w) -> if x < y || oriente then
|
||||
begin
|
||||
print (string_of_int x);
|
||||
if oriente then print " -> " else print "--";
|
||||
print (string_of_int y);
|
||||
print "[label=\"";
|
||||
print (string_of_int w);
|
||||
print "\" ";
|
||||
if parents.(y) = x || (not oriente && parents.(x) = y) then
|
||||
print ",color=\"#FF0000\", penwidth=\"3\"";
|
||||
print "];\n"
|
||||
end
|
||||
)
|
||||
graphe.(x)
|
||||
done;
|
||||
print "}";
|
||||
close_out os;;
|
||||
|
||||
let run wait cmd =
|
||||
if wait then
|
||||
ignore (Unix.system cmd)
|
||||
else
|
||||
ignore (Unix.open_process cmd);;
|
||||
|
||||
let rundot fn =
|
||||
let bn = try Filename.chop_extension fn with Invalid_argument _ -> fn in
|
||||
let pdfn = bn^".pdf" in
|
||||
run true ("dot -Tpdf " ^ (Filename.quote fn) ^ " -o " ^ (Filename.quote pdfn));
|
||||
pdfn;;
|
||||
|
||||
let openPDF wait fn =
|
||||
run wait ("evince " ^ (Filename.quote fn));;
|
||||
|
||||
let vizgraph wait fn =
|
||||
let pdfn = rundot fn in
|
||||
openPDF wait pdfn;;
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur