Implement set-theoretic operation on 2WSATA (union, intersection,
[tatoo.git] / src / html.ml
1 open Format
2 module M = Map.Make(struct type t = int let compare = compare end)
3
4 let info = Hashtbl.create 2017
5 let final = Hashtbl.create 2017
6
7 let max_round = ref 0
8
9 let add_info (nodeid:int) (i:int) s =
10   if i > !max_round then max_round := i;
11   let m = try Hashtbl.find info nodeid with Not_found -> M.empty in
12   let old_s = try M.find i m with Not_found -> "" in
13   let s' = old_s ^ s in
14   let m' = M.add i s' m in
15   Hashtbl.replace info nodeid m'
16
17
18 let buff = Buffer.create 20
19 let fmt = formatter_of_buffer buff
20
21 let trace nodeid i =
22   let () = pp_print_flush fmt ();
23     Buffer.clear buff
24   in
25   kfprintf (fun fmt ->
26     pp_print_flush fmt ();
27     let s = Buffer.contents buff in
28     add_info nodeid i s) fmt
29
30 let finalize_node n r b =
31   Hashtbl.replace final n (b,r)
32
33
34 let gen_trace (type s)  = (); fun t tree ->
35   let module T = (val (t) : Tree.S with type t = s) in
36   let rec loop odot ohtml node parent =
37     if node == T.nil then () else begin
38       let m =
39         try
40           Hashtbl.find info (T.preorder tree node)
41         with Not_found -> M.empty
42       in
43       let node_id = T.preorder tree node in
44       let marked, last_round = try Hashtbl.find final node_id with Not_found ->
45         Printf.eprintf ">>> %i\n%!" node_id; false, !max_round;
46       in
47       let s_node = "node" ^ (string_of_int node_id) in
48       fprintf odot "%s[ id=\"%s\" label=\"%s\" style=filled fillcolor=\"%f,1.0,1.0\"\
49 shape=\"%s\" ];\n"
50         s_node
51         s_node
52         (QName.to_string (T.tag tree node))
53         (1.0 -. (float (last_round+1) /. float (!max_round+1)))
54         (if marked then "oval" else "box") ;
55       fprintf ohtml "data['%s'] = new Array();\n" s_node;
56       M.iter
57         (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s)
58         m;
59       if parent != T.nil then
60         fprintf odot "node%i -> %s;\n"
61           (T.preorder tree parent) s_node;
62       loop odot ohtml (T.first_child tree node) node;
63       loop odot ohtml (T.next_sibling tree node) parent
64     end
65   in
66   ignore (Sys.command "mkdir -p tests/trace");
67   let odot_ = open_out "tests/trace/trace.dot" in
68   let ohtml_ = open_out "tests/trace/trace.html" in
69   let odot = formatter_of_out_channel odot_ in
70   let ohtml = formatter_of_out_channel ohtml_ in
71   fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;";
72   fprintf ohtml "<html>\
73 <head><title></title>
74 <link rel='stylesheet' type='text/css' href='trace.css' />\
75 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
76 </head>\
77 <body>\
78 <div id='data' > </div>\
79 <script type='text/javascript'>";
80   loop odot ohtml (T.root tree) (T.nil);
81   fprintf odot "\n}\n%!";
82   pp_print_flush odot ();
83   close_out odot_;
84   ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
85   ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
86   fprintf ohtml "var activate = function (id) {\
87   var d = document.getElementById('data');
88   var msg = '';
89   for (i=0; i < data[id].length; i++)
90      msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
91   d.innerHTML = msg;
92   return;
93   };\n";
94
95   fprintf ohtml "</script>\n<div id='svg'>\n";
96   let fi = open_in "tests/trace/trace2.svg" in
97   try
98     while true do
99       let s = input_line fi in
100       fprintf ohtml "%s\n" s;
101     done
102   with
103     End_of_file ->
104       fprintf ohtml "</div></body></html>\n%!";
105       pp_print_flush ohtml ();
106       close_out ohtml_;
107       close_in fi
108