Silence an unused variable warning.
[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 scolor, tcolor =
89         let { sat ; todo; _ } =
90           match M.find last_round m with
91             [] -> { sat = StateSet.empty; todo= StateSet.empty; msg = "ERROR" }
92           | [ e ] -> e
93           | l -> List.hd (List.tl (List.rev l))
94         in
95         let c = (last_round, StateSet.union sat todo, StateSet.empty) in color c, text_color c
96       in
97       let tag = QName.to_string (T.tag tree node) in
98       let lbox = (String.length tag + 2) * 10 in
99       let s_node = "node" ^ (string_of_int node_id) in
100       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%!"
101         s_node s_node x y lbox scolor (if marked then ";stroke-width:4" else ";stroke-width:2;stroke-dasharray:2,2");
102       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;
103       fprintf ohtml "data['%s'] = new Array();\n" s_node;
104       M.iter
105         (fun i l ->
106           let msg = String.concat "" (List.rev_map (fun x -> x.msg) l) in
107           fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i msg)
108         m;
109       let first = T.first_child tree node in
110       let maxw1, maxy1 = loop osvg ohtml first node x (y + 40) in
111       let next = T.next_sibling tree node in
112       let x_next = max (x+lbox) (maxw1+10) in
113       if node != root then begin
114         if node == T.first_child tree parent then
115           fprintf osvg  "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
116             (x + lbox / 2) (y-20) (x + lbox / 2) (y);
117         if next != T.nil then
118           fprintf osvg "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
119             (x + lbox) (y+10) x_next (y+10);
120       end;
121       let maxw2, maxy2 = loop osvg ohtml next node x_next y in
122       maxw2, max maxy1 maxy2
123     end
124     else x, y
125   in
126   ignore (Sys.command "mkdir -p tests/trace");
127   let osvg_ = open_out "tests/trace/trace.svg" in
128   let ohtml_ = open_out "tests/trace/trace.html" in
129   let osvg = formatter_of_out_channel osvg_ in
130   let ohtml = formatter_of_out_channel ohtml_ in
131   fprintf ohtml "<html>\
132 <head><title></title>
133 <link rel='stylesheet' type='text/css' href='trace.css' />\
134 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
135 </head>\
136 <body>\
137 <div id='automata' >%a
138 </div>
139 <div id='data' > </div>\
140 <script type='text/javascript'>"
141 Ata.print auto;
142   let maxw, maxh = loop osvg ohtml (T.root tree) T.nil 50 50 in
143   pp_print_flush osvg ();
144   close_out osvg_;
145   fprintf ohtml "var activate = function (id) {\
146   var d = document.getElementById('data');
147   var msg = '';
148   for (i=0; i < data[id].length; i++)
149      msg += ('<p>round: ' + i + ':<br/>') + data[id][i] + '</p>\\n';
150   d.innerHTML = msg;
151   return;
152   };\n";
153   fprintf ohtml "</script>\n<div id='svg'><svg width=\"%i\" height=\"%i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
154     maxw maxh;
155   let fi = open_in "tests/trace/trace.svg" in
156   try
157     while true do
158       let s = input_line fi in
159       fprintf ohtml "%s\n" s;
160     done
161   with
162     End_of_file ->
163       fprintf ohtml "</svg>\n</div></body></html>\n%!";
164       pp_print_flush ohtml ();
165       close_out ohtml_;
166       close_in fi