2 module M = Map.Make(struct type t = int let compare = compare end)
4 let info = Hashtbl.create 2017
6 let max_round = ref ~-1
8 let add_info (nodeid:int) (i:int) (oldi: int) s =
9 if i > !max_round then max_round := i;
10 if i <= oldi || oldi < 0 then begin
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
21 let trace nodeid i oldi =
22 let () = pp_print_flush fmt ();
26 pp_print_flush fmt ();
27 let s = Buffer.contents buff in
28 add_info nodeid i oldi s) fmt
31 let gen_trace (type s) = (); fun t tree ->
32 let module T = (val (t) : Tree.S with type t = s) in
33 let rec loop odot ohtml node parent =
34 if node == T.nil then () else begin
37 Hashtbl.find info (T.preorder tree node)
38 with Not_found -> M.empty
40 let last_round = try fst (M.max_binding m) with Not_found -> 0 in
41 let s_node = "node" ^ (string_of_int (T.preorder tree node)) in
42 fprintf odot "%s[ id=\"%s\" label=\"%s\" style=filled fillcolor=\"%f,1.0,1.0\"];\n"
43 s_node s_node (QName.to_string (T.tag tree node))
44 (0.2 *. (1.0 -. (float last_round /. float !max_round))) ;
45 fprintf ohtml "data['%s'] = new Array();\n" s_node;
47 (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s)
49 if parent != T.nil then
50 fprintf odot "node%i -> %s;\n"
51 (T.preorder tree parent) s_node;
52 loop odot ohtml (T.first_child tree node) node;
53 loop odot ohtml (T.next_sibling tree node) parent
56 ignore (Sys.command "mkdir -p tests/trace");
57 let odot_ = open_out "tests/trace/trace.dot" in
58 let ohtml_ = open_out "tests/trace/trace.html" in
59 let odot = formatter_of_out_channel odot_ in
60 let ohtml = formatter_of_out_channel ohtml_ in
61 fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;";
62 fprintf ohtml "<html>\
64 <link rel='stylesheet' type='text/css' href='trace.css' />\
65 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
68 <div id='data' > </div>\
69 <script type='text/javascript'>";
70 loop odot ohtml (T.root tree) (T.nil);
71 fprintf odot "\n}\n%!";
72 pp_print_flush odot ();
74 ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
75 ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
76 fprintf ohtml "var activate = function (id) {\
77 var d = document.getElementById('data');
79 for (i=0; i < data[id].length; i++)
80 msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
85 fprintf ohtml "</script>\n<div id='svg'>\n";
86 let fi = open_in "tests/trace/trace2.svg" in
89 let s = input_line fi in
90 fprintf ohtml "%s\n" s;
94 fprintf ohtml "</div></body></html>\n%!";
95 pp_print_flush ohtml ();