2 module M = Map.Make(struct type t = int let compare = compare end)
4 let info = Hashtbl.create 2017
5 let final = Hashtbl.create 2017
9 let add_info (nodeid:int) (i:int) s =
10 if i > !max_round then max_round := i;
11 let m = try Hashtbl.find info nodeid with Not_found -> M.empty in
12 let old_s = try M.find i m with Not_found -> "" in
14 let m' = M.add i s' m in
15 Hashtbl.replace info nodeid m'
18 let buff = Buffer.create 20
19 let fmt = formatter_of_buffer buff
22 let () = pp_print_flush fmt ();
26 pp_print_flush fmt ();
27 let s = Buffer.contents buff in
28 add_info nodeid i s) fmt
30 let finalize_node n r b =
31 Hashtbl.replace final n (b,r)
34 let gen_trace (type s) = (); fun t tree ->
35 let module T = (val (t) : Tree.S with type t = s) in
36 let rec loop odot ohtml node parent =
37 if node == T.nil then () else begin
40 Hashtbl.find info (T.preorder tree node)
41 with Not_found -> M.empty
43 let node_id = T.preorder tree node in
44 let marked, last_round = try Hashtbl.find final node_id with Not_found ->
45 Printf.eprintf ">>> %i\n%!" node_id; false, !max_round;
47 let s_node = "node" ^ (string_of_int node_id) in
48 fprintf odot "%s[ id=\"%s\" label=\"%s\" style=filled fillcolor=\"%f,1.0,1.0\"\
52 (QName.to_string (T.tag tree node))
53 (1.0 -. (float (last_round+1) /. float (!max_round+1)))
54 (if marked then "oval" else "box") ;
55 fprintf ohtml "data['%s'] = new Array();\n" s_node;
57 (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s)
59 if parent != T.nil then
60 fprintf odot "node%i -> %s;\n"
61 (T.preorder tree parent) s_node;
62 loop odot ohtml (T.first_child tree node) node;
63 loop odot ohtml (T.next_sibling tree node) parent
66 ignore (Sys.command "mkdir -p tests/trace");
67 let odot_ = open_out "tests/trace/trace.dot" in
68 let ohtml_ = open_out "tests/trace/trace.html" in
69 let odot = formatter_of_out_channel odot_ in
70 let ohtml = formatter_of_out_channel ohtml_ in
71 fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;";
72 fprintf ohtml "<html>\
74 <link rel='stylesheet' type='text/css' href='trace.css' />\
75 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
78 <div id='data' > </div>\
79 <script type='text/javascript'>";
80 loop odot ohtml (T.root tree) (T.nil);
81 fprintf odot "\n}\n%!";
82 pp_print_flush odot ();
84 ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
85 ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
86 fprintf ohtml "var activate = function (id) {\
87 var d = document.getElementById('data');
89 for (i=0; i < data[id].length; i++)
90 msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
95 fprintf ohtml "</script>\n<div id='svg'>\n";
96 let fi = open_in "tests/trace/trace2.svg" in
99 let s = input_line fi in
100 fprintf ohtml "%s\n" s;
104 fprintf ohtml "</div></body></html>\n%!";
105 pp_print_flush ohtml ();