Add a new option to choose tree model at runtime.
[tatoo.git] / src / html_trace.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 dup_rev_list l =
87   List.fold_left (fun acc e -> e::e::acc) [] l
88
89 let gen_trace (type s) = fun auto sat_arrays t tree ->
90   let sat_arrays = dup_rev_list sat_arrays in
91   let module T = (val (t) : Tree.S with type t = s) in
92   let root = T.root tree in
93   let sel = Ata.get_selecting_states auto in
94   let todos = Ata.get_states_by_rank auto in
95   let pr_sets = Pretty.print_list ~sep:", "
96     (fun fmt s -> fprintf fmt "'%a'" StateSet.print s)
97   in
98   let rec loop output node parent prevsib x y =
99     if node != T.nil then begin
100       let node_id = T.preorder tree node in
101       let marked, conf = get_conf sel sat_arrays node_id in
102       let scolor, tcolor = color conf, text_color conf in
103       let tag = QName.to_string (T.tag tree node) in
104       let lbox = (String.length tag + 2) * 10 in
105       let s_node = "node" ^ (string_of_int node_id) in
106       let first = T.first_child tree node in
107       let next = T.next_sibling tree node in
108       fprintf output "<script type='text/javascript'><![CDATA[ ";
109       let pass, max_active_pass, diff_list, full_list =
110         List.fold_left
111           (fun (pass, max_active_pass, diff_list, full_list) a ->
112             let todo = if pass >= Array.length todos then StateSet.empty
113               else todos.(pass)
114             in
115             let diff = StateSet.inter a.(node_id) todo in
116             let new_active_pass =
117               if diff == StateSet.empty then max_active_pass else pass
118             in
119             pass+1, new_active_pass, diff::diff_list, a.(node_id) :: full_list)
120           (0, -1, [], []) sat_arrays
121       in
122       fprintf output "tree['%s'] = { marked: %b, fc: 'node%i', ns: 'node%i', par: 'node%i', ps: 'node%i', max: %i, dlist: [%a], flist: [%a] };]]></script>\n"
123         s_node
124         marked
125         (T.preorder tree first) (T.preorder tree next)
126         (T.preorder tree parent) (T.preorder tree prevsib)
127         max_active_pass
128         pr_sets (List.rev diff_list)
129         pr_sets (List.rev full_list);
130       fprintf output
131         "<rect id=\"%s\" onclick=\"activate(\'%s\');\" x=\"%i\" y=\"%i\"\
132  width=\"%i\" height=\"22\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>%!"
133         s_node
134         s_node
135         x y
136         lbox
137         scolor
138         (if marked
139          then ";stroke-width:4"
140          else ";stroke-width:2;stroke-dasharray:2,2");
141       fprintf output "<text x=\"%i\" y=\"%i\" style=\"fill:%s;font-size:17;\
142 font-family:typewriter;\" onclick=\"activate(\'%s\');\" >%s</text>\n"
143         (x+10)
144         (y+15)
145         tcolor s_node tag;
146       let maxw1, maxy1 = loop output first node T.nil x (y + 40) in
147       let x_next = max (x+lbox) (maxw1+10) in
148       if node != root then begin
149         if prevsib == T.nil then
150           fprintf output  "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
151 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
152             (x + lbox / 2) (y-18) (x + lbox / 2) (y);
153         if next != T.nil then
154           fprintf output "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
155 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
156             (x + lbox) (y+11) x_next (y+11);
157       end;
158       let maxw2, maxy2 = loop output next parent node x_next y in
159       maxw2, max maxy1 maxy2
160     end
161     else x, y
162   in
163   ignore (Sys.command "mkdir -p tests/trace");
164   let ohtml_ = open_out "tests/trace/trace.html" in
165   let ohtml = formatter_of_out_channel ohtml_ in
166   fprintf ohtml "<html>\n\
167 <head><title></title>\n\
168 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\n\
169 <style>\n\
170 %s
171 </style>\n\
172 </head>\n\
173 <body>\n\
174 <div id='automata' >%a</div>\n\
175 <div id='data' > </div>\n\
176 <script type='text/javascript'>\n\
177 var tree = new Array();\n\
178 var current_node = null;
179 var nil_id = 'node%i';
180 var last_id = %i;
181 var rounds = %i;\n"
182     Trace_css.content
183     Ata.print auto
184     (T.preorder tree T.nil)
185     (T.size tree)
186     (List.length sat_arrays - 1);
187   fprintf ohtml "%s\n" Trace_js.content;
188   fprintf ohtml
189     "</script>\n\
190 <div id='tree'>
191 <div id='controls'><button onclick='activate_parent();'>↑</button>\n\
192 <button onclick='activate_previous();'>←</button>\n\
193 <button onclick='activate_first();'>↓</button>\n\
194 <button onclick='activate_next();'>→</button>\n\
195 <select id='relnodes' onchange='hide_nodes();'>\n\
196 <option value='-1'>All nodes</option>\n\
197 %a</select>\n\
198 </div>
199 <div id='svg'>\n
200    <svg id='svgimg' width='' height='' xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
201     (Pretty.print_range (fun fmt i ->
202       fprintf fmt "<option value='%i'>Relevant nodes on pass %i</option>\n" i i))
203     (0,(List.length sat_arrays - 1));
204   let maxw, maxh = loop ohtml (T.root tree) T.nil T.nil 50 50 in
205   fprintf ohtml "</svg>\n</div></div><script type='text/javascript'>\n\
206 window.onload = function () {\n\
207     var svg = document.getElementById('svgimg');\n\
208     svg.setAttribute('width', %i);\n\
209     svg.setAttribute('height', %i);\n\
210     activate('node0');\n\
211 };\n
212 </script></body></html>\n%!"
213     maxw maxh;
214   pp_print_flush ohtml ();
215   close_out ohtml_