4 module M = Map.Make(struct type t = int let compare = compare end)
6 type info = { sat : StateSet.t;
10 let info = Hashtbl.create 2017
11 let final = Hashtbl.create 2017
16 let buff = Buffer.create 20
17 let fmt = formatter_of_buffer buff
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
25 "node: %i<br/>%s<br/>todo: %a<br/>sat: %a<br/>_______________________<br/>"
26 nid msg StateSet.print t StateSet.print d
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'
35 let finalize_node n r b =
36 Hashtbl.replace final n (b,r)
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)
45 module CTable = Hashtbl.Make (K)
47 let ctable = CTable.create 20
51 and g = (h lsr 8) land 0xff
52 and b = (h lsr 16) land 0xff
55 let color ((a,b,c) as x) =
61 let s = "rgb(" ^ (string_of_int r) ^ ","
62 ^ (string_of_int g) ^ ","
63 ^ (string_of_int b) ^ ")"
65 CTable.add ctable x s;
69 let av = (r + g + b) / 3 in
70 if av > 128 then "rgb(0,0,0)"
71 else "rgb(255,255,255)"
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
81 Hashtbl.find info (T.preorder tree node)
82 with Not_found -> M.empty
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;
89 let { sat ; todo; _ } =
90 match M.find last_round m with
91 [] -> { sat = StateSet.empty; todo= StateSet.empty; msg = "ERROR" }
93 | l -> List.hd (List.tl (List.rev l))
95 let c = (last_round, StateSet.union sat todo, StateSet.empty) in
98 let tag = QName.to_string (T.tag tree node) in
99 let lbox = (String.length tag + 2) * 10 in
100 let s_node = "node" ^ (string_of_int node_id) in
102 "<rect id=\"%s\" onclick=\"activate(\'%s\');\" x=\"%i\" y=\"%i\"\
103 width=\"%i\" height=\"20\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>\n%!"
110 then ";stroke-width:4"
111 else ";stroke-width:2;stroke-dasharray:2,2");
112 fprintf osvg "<text x=\"%i\" y=\"%i\" style=\"fill:%s;font-size:17;\
113 font-family:typewriter;\" onclick=\"activate(\'%s\');\" >%s</text>\n"
117 fprintf ohtml "data['%s'] = new Array();\n" s_node;
120 let msg = String.concat "" (List.rev_map (fun x -> x.msg) l) in
121 fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i msg)
123 let first = T.first_child tree node in
124 let maxw1, maxy1 = loop osvg ohtml first node x (y + 40) in
125 let next = T.next_sibling tree node in
126 let x_next = max (x+lbox) (maxw1+10) in
127 if node != root then begin
128 if node == T.first_child tree parent then
129 fprintf osvg "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
130 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
131 (x + lbox / 2) (y-20) (x + lbox / 2) (y);
132 if next != T.nil then
133 fprintf osvg "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\"\
134 style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
135 (x + lbox) (y+10) x_next (y+10);
137 let maxw2, maxy2 = loop osvg ohtml next node x_next y in
138 maxw2, max maxy1 maxy2
142 ignore (Sys.command "mkdir -p tests/trace");
143 let osvg_ = open_out "tests/trace/trace.svg" in
144 let ohtml_ = open_out "tests/trace/trace.html" in
145 let osvg = formatter_of_out_channel osvg_ in
146 let ohtml = formatter_of_out_channel ohtml_ in
147 fprintf ohtml "<html>\
148 <head><title></title>
149 <link rel='stylesheet' type='text/css' href='trace.css' />\
150 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
153 <div id='automata' >%a
155 <div id='data' > </div>\
156 <script type='text/javascript'>"
158 let maxw, maxh = loop osvg ohtml (T.root tree) T.nil 50 50 in
159 pp_print_flush osvg ();
161 fprintf ohtml "var activate = function (id) {\
162 var d = document.getElementById('data');
164 for (i=0; i < data[id].length; i++)
165 msg += ('<p>round: ' + i + ':<br/>') + data[id][i] + '</p>\\n';
169 fprintf ohtml "</script>\n<div id='svg'><svg width=\"%i\"\
170 height=\"%i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
172 let fi = open_in "tests/trace/trace.svg" in
175 let s = input_line fi in
176 fprintf ohtml "%s\n" s;
180 fprintf ohtml "</svg>\n</div></body></html>\n%!";
181 pp_print_flush ohtml ();