Refactor HTML tracing utility. Color the nodes that are in the same
[tatoo.git] / src / html.ml
1 INCLUDE "utils.ml"
2
3 open Format
4 module M = Map.Make(struct type t = int let compare = compare end)
5
6 type info = { sat : StateSet.t;
7               todo : StateSet.t;
8               msg : string;
9             }
10 let info = Hashtbl.create 2017
11 let final = Hashtbl.create 2017
12
13 let max_round = ref 0
14
15
16 let buff = Buffer.create 20
17 let fmt = formatter_of_buffer buff
18
19
20 let trace ?(msg="") nid r t d  =
21   if r > !max_round then max_round := r;
22   let m = try Hashtbl.find info nid with Not_found -> M.empty in
23   let () = pp_print_flush fmt () in
24   let _ = fprintf fmt
25     "node: %i<br/>%s<br/>todo: %a<br/>sat: %a<br/>_______________________<br/>"
26     nid msg  StateSet.print t StateSet.print d
27   in
28   let () = pp_print_flush fmt () in
29   let msg = Buffer.contents buff in
30   let () = Buffer.clear buff in
31   let old_inf = try M.find r m with Not_found -> [] in
32   let m' = M.add r ({ sat = d; todo = t; msg = msg }::old_inf) m in
33   Hashtbl.replace info nid m'
34
35 let finalize_node n r b =
36   Hashtbl.replace final n (b,r)
37 module K = 
38 struct
39   type t = int * StateSet.t * StateSet.t
40   let hash (a,b,c) = HASHINT3(a, (b.StateSet.id :> int), (c.StateSet.id :> int) )
41   let equal ((a1,b1,c1) as x) ((a2,b2,c2) as y) =
42     x == y || (a1 == a2 && b1 == b2 && c1 == c2)
43 end
44
45 module CTable = Hashtbl.Make (K)
46
47 let ctable = CTable.create 20
48 let rgb x =
49   let h = K.hash x in
50   let r = h land 0xff
51   and g = (h lsr 8) land 0xff
52   and b = (h lsr 16) land 0xff
53   in
54   r, g, b
55 let color ((a,b,c) as x) =
56   try
57     CTable.find ctable x
58   with
59     Not_found ->
60       let r,g,b = rgb x in
61       let s = "rgb(" ^ (string_of_int r) ^ ","
62         ^ (string_of_int g) ^ ","
63         ^ (string_of_int b) ^ ")"
64       in
65       CTable.add ctable x s;
66       s
67 let text_color x =
68   let r,g,b = rgb x in
69   let av = (r + g + b) / 3 in
70   if av > 128 then "rgb(0,0,0)"
71   else "rgb(255,255,255)"
72
73
74 let gen_trace (type s) = fun auto t tree ->
75   let module T = (val (t) : Tree.S with type t = s) in
76   let root = T.root tree in
77   let rec loop osvg ohtml node parent x y =
78     if node != T.nil then begin
79       let m =
80         try
81           Hashtbl.find info (T.preorder tree node)
82         with Not_found -> M.empty
83       in
84       let node_id = T.preorder tree node in
85       let marked, last_round = try Hashtbl.find final node_id with Not_found ->
86         Printf.eprintf ">>> %i\n%!" node_id; false, !max_round;
87       in
88       let pc = if !max_round == 0 then 1. else float_of_int last_round /. float_of_int !max_round in
89       let scolor, tcolor =
90         let { sat ; todo; _ } = List.hd(M.find last_round m) in
91         let c = (last_round, sat, todo) in color c, text_color c
92       in
93       let tag = QName.to_string (T.tag tree node) in
94       let lbox = (String.length tag + 2) * 10 in
95       let s_node = "node" ^ (string_of_int node_id) in
96       fprintf osvg "<rect id=\"%s\" onclick=\"activate(\'%s\');\" x=\"%i\" y=\"%i\" width=\"%i\" height=\"20\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>\n%!"
97         s_node s_node x y lbox scolor (if marked then ";stroke-width:4" else ";stroke-width:2;stroke-dasharray:2,2");
98       fprintf osvg "<text x=\"%i\" y=\"%i\" style=\"fill:%s;font-size:17;font-family:typewriter;\" onclick=\"activate(\'%s\');\" >%s</text>\n" (x+10) (y+15) tcolor s_node tag;
99       fprintf ohtml "data['%s'] = new Array();\n" s_node;
100       M.iter
101         (fun i l ->
102           let msg = String.concat "" (List.rev_map (fun x -> x.msg) l) in
103           fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i msg)
104         m;
105       let first = T.first_child tree node in
106       let maxw1, maxy1 = loop osvg ohtml first node x (y + 40) in
107       let next = T.next_sibling tree node in
108       let x_next = max (x+lbox) (maxw1+10) in
109       if node != root then begin
110         if node == T.first_child tree parent then
111           fprintf osvg  "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
112             (x + lbox / 2) (y-20) (x + lbox / 2) (y);
113         if next != T.nil then
114           fprintf osvg "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
115             (x + lbox) (y+10) x_next (y+10);
116       end;
117       let maxw2, maxy2 = loop osvg ohtml next node x_next y in
118       maxw2, max maxy1 maxy2
119     end
120     else x, y
121   in
122   ignore (Sys.command "mkdir -p tests/trace");
123   let osvg_ = open_out "tests/trace/trace.svg" in
124   let ohtml_ = open_out "tests/trace/trace.html" in
125   let osvg = formatter_of_out_channel osvg_ in
126   let ohtml = formatter_of_out_channel ohtml_ in
127   fprintf ohtml "<html>\
128 <head><title></title>
129 <link rel='stylesheet' type='text/css' href='trace.css' />\
130 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
131 </head>\
132 <body>\
133 <div id='automata' >%a
134 </div>
135 <div id='data' > </div>\
136 <script type='text/javascript'>"
137 Ata.print auto;
138   let maxw, maxh = loop osvg ohtml (T.root tree) T.nil 50 50 in
139   pp_print_flush osvg ();
140   close_out osvg_;
141   fprintf ohtml "var activate = function (id) {\
142   var d = document.getElementById('data');
143   var msg = '';
144   for (i=0; i < data[id].length; i++)
145      msg += ('<p>round: ' + i + ':<br/>') + data[id][i] + '</p>\\n';
146   d.innerHTML = msg;
147   return;
148   };\n";
149   fprintf ohtml "</script>\n<div id='svg'><svg width=\"%i\" height=\"%i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
150     maxw maxh;
151   let fi = open_in "tests/trace/trace.svg" in
152   try
153     while true do
154       let s = input_line fi in
155       fprintf ohtml "%s\n" s;
156     done
157   with
158     End_of_file ->
159       fprintf ohtml "</svg>\n</div></body></html>\n%!";
160       pp_print_flush ohtml ();
161       close_out ohtml_;
162       close_in fi
163
164