Refactor the run module, moving out of the Make functor everything that can be moved...
[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 = StateSet.t list
40   let hash l =
41     List.fold_left (fun acc set ->
42       HASHINT2(acc, (set.StateSet.id :> int))) 17 l
43
44   let equal l1 l2 =
45     try
46       List.for_all2 (==) l1 l2
47     with _ -> false
48 end
49
50 module CTable = Hashtbl.Make (K)
51
52 let ctable = CTable.create 20
53
54 let rgb x =
55   let h = K.hash x in
56   let r = h land 0xff
57   and g = (h lsr 8) land 0xff
58   and b = (h lsr 16) land 0xff
59   in
60   r, g, b
61
62 let color x =
63   try
64     CTable.find ctable x
65   with
66     Not_found ->
67       let r,g,b = rgb x in
68       let s = "rgb(" ^ (string_of_int r) ^ ","
69         ^ (string_of_int g) ^ ","
70         ^ (string_of_int b) ^ ")"
71       in
72       CTable.add ctable x s;
73       s
74
75 let text_color x =
76   let r,g,b = rgb x in
77   let av = (r + g + b) / 3 in
78   if av > 128 then "rgb(0,0,0)"
79   else "rgb(255,255,255)"
80
81 let get_conf sel l i =
82   List.fold_left (fun (accb,accl) a ->
83     accb || StateSet.intersect a.(i) sel,
84     a.(i) :: accl) (false,[]) l
85
86 let gen_trace (type s) = fun auto sat_arrays t tree ->
87   let module T = (val (t) : Tree.S with type t = s) in
88   let root = T.root tree in
89   let sel = Ata.get_selecting_states auto in
90   let rec loop output node parent x y =
91     if node != T.nil then begin
92       let node_id = T.preorder tree node in
93       let marked, conf = get_conf sel sat_arrays node_id in
94       let scolor, tcolor = color conf, text_color conf in
95       let tag = QName.to_string (T.tag tree node) in
96       let lbox = (String.length tag + 2) * 10 in
97       let s_node = "node" ^ (string_of_int node_id) in
98       fprintf output
99         "<rect id=\"%s\" onclick=\"activate(\'%s\');\" x=\"%i\" y=\"%i\"\
100  width=\"%i\" height=\"20\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>\n%!"
101         s_node
102         s_node
103         x y
104         lbox
105         scolor
106         (if marked
107          then ";stroke-width:4"
108          else ";stroke-width:2;stroke-dasharray:2,2");
109       fprintf output "<text x=\"%i\" y=\"%i\" style=\"fill:%s;font-size:17;\
110 font-family:typewriter;\" onclick=\"activate(\'%s\');\" >%s</text>\n"
111         (x+10)
112         (y+15)
113         tcolor s_node tag;
114       let first = T.first_child tree node in
115       let maxw1, maxy1 = loop output first node x (y + 40) in
116       let next = T.next_sibling tree node in
117       let x_next = max (x+lbox) (maxw1+10) in
118       if node != root then begin
119         if node == T.first_child tree parent then
120           fprintf output  "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
121 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
122             (x + lbox / 2) (y-20) (x + lbox / 2) (y);
123         if next != T.nil then
124           fprintf output "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
125 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
126             (x + lbox) (y+10) x_next (y+10);
127       end;
128       let maxw2, maxy2 = loop output next node x_next y in
129       maxw2, max maxy1 maxy2
130     end
131     else x, y
132   in
133   ignore (Sys.command "mkdir -p tests/trace");
134   let ohtml_ = open_out "tests/trace/trace.html" in
135   let ohtml = formatter_of_out_channel ohtml_ in
136   fprintf ohtml "<html>\
137 <head><title></title>
138 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
139 <style>\
140 div#data {\
141     position: absolute;\
142     top: 0%%;\
143     left: 50%%;\
144     width: 50%%;\
145     height: 50%%;\
146     overflow: auto;\
147 }\
148 div#svg {\
149     position: absolute;\
150     top: 50%%;\
151     left: 0%%;\
152     width: 100%%;\
153     height: 50%%;\
154     overflow: auto;\
155 }\
156 \
157 div#automata {\
158     white-space: pre;\
159     overflow: auto;\
160     position: absolute;\
161     width: 50%%;\
162     top: 0%%;\
163     left: 0%%;\
164     height: 50%%;\
165 }\
166 </style>\
167 </head>\
168 <body>\
169 <div id='automata' >%a
170 </div>
171 <div id='data' > </div>\n\
172 <script type='text/javascript'>\n\
173 var data = new Array();\n\
174 var rounds = %i;\n"
175     Ata.print auto
176     (List.length sat_arrays);
177   List.iteri (fun i a ->
178     fprintf ohtml "data[%i] = new Array();\n" i;
179     Array.iteri (fun id set ->
180       fprintf ohtml "data[%i]['node%i'] = \"%a\";\n"
181       i id StateSet.print set) a) (List.rev sat_arrays);
182   fprintf ohtml "var activate = function (id) {\
183   var d = document.getElementById('data');
184   var msg = '';
185   for (i=0; i < rounds; i++)
186      msg += ('<p>round: ' + i + ':<br/>') + data[i][id] + '</p>\\n';
187   d.innerHTML = msg;
188   return;
189   };\n";
190   fprintf ohtml "</script>\n<div id='svg'><svg id='svgimg' width='' height='' xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n";
191   let maxw, maxh = loop ohtml (T.root tree) T.nil 50 50 in
192   fprintf ohtml "</svg>\n<script type=\"text/javascript\">window.onload = function () {\
193   var svg = document.getElementById('svgimg');
194   svg.setAttribute('width', %i);
195   svg.setAttribute('height', %i);
196 };\
197 </script>\
198 </div></body></html>\n%!"
199     maxw maxh;
200   pp_print_flush ohtml ();
201   close_out ohtml_