X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=blobdiff_plain;f=src%2Fhtml.ml;h=d59265813564e1f259b7aeb14c183ac8d82a4ab1;hp=4119f23f54e3221d9f88f363ed0cfbaa1fdd20b4;hb=20ef25a27a326b250ae7f32997fa6d249a6b1751;hpb=556c8805fcfd27f485bdd63cd704e4df7eac8a06 diff --git a/src/html.ml b/src/html.ml index 4119f23..d592658 100644 --- a/src/html.ml +++ b/src/html.ml @@ -2,88 +2,108 @@ open Format module M = Map.Make(struct type t = int let compare = compare end) let info = Hashtbl.create 2017 +let final = Hashtbl.create 2017 -let max_round = ref ~-1 +let max_round = ref 0 -let add_info (nodeid:int) (i:int) (oldi: int) s = +let add_info (nodeid:int) (i:int) s = if i > !max_round then max_round := i; - if i <= oldi || oldi < 0 then begin - let m = try Hashtbl.find info nodeid with Not_found -> M.empty in - let old_s = try M.find i m with Not_found -> "" in - let s' = old_s ^ s in - let m' = M.add i s' m in - Hashtbl.replace info nodeid m' - end + let m = try Hashtbl.find info nodeid with Not_found -> M.empty in + let old_s = try M.find i m with Not_found -> "" in + let s' = old_s ^ s in + let m' = M.add i s' m in + Hashtbl.replace info nodeid m' + let buff = Buffer.create 20 let fmt = formatter_of_buffer buff -let trace nodeid i oldi = +let trace nodeid i = let () = pp_print_flush fmt (); Buffer.clear buff in kfprintf (fun fmt -> pp_print_flush fmt (); let s = Buffer.contents buff in - add_info nodeid i oldi s) fmt + add_info nodeid i s) fmt + +let finalize_node n r b = + Hashtbl.replace final n (b,r) -let gen_trace (type s) = (); fun t tree -> +let gen_trace (type s) = fun auto t tree -> let module T = (val (t) : Tree.S with type t = s) in - let rec loop odot ohtml node parent = - if node == T.nil then () else begin + let root = T.root tree in + let rec loop osvg ohtml node parent x y = + if node != T.nil then begin let m = try Hashtbl.find info (T.preorder tree node) with Not_found -> M.empty in - let last_round = try fst (M.max_binding m) with Not_found -> 0 in - let s_node = "node" ^ (string_of_int (T.preorder tree node)) in - fprintf odot "%s[ id=\"%s\" label=\"%s\" style=filled fillcolor=\"%f,1.0,1.0\"];\n" - s_node s_node (QName.to_string (T.tag tree node)) - (0.2 *. (1.0 -. (float last_round /. float !max_round))) ; + let node_id = T.preorder tree node in + let marked, last_round = try Hashtbl.find final node_id with Not_found -> + Printf.eprintf ">>> %i\n%!" node_id; false, !max_round; + in + let pc = if !max_round == 0 then 1. else float_of_int last_round /. float_of_int !max_round in + let color = int_of_float (255. *. (1. -. pc)) in + let tag = QName.to_string (T.tag tree node) in + let lbox = (String.length tag + 2) * 10 in + let s_node = "node" ^ (string_of_int node_id) in + fprintf osvg "\n%!" + s_node s_node x y lbox color color (if marked then "" else ";stroke-dasharray:2,2"); + fprintf osvg "%s\n" (x+10) (y+15) s_node tag; fprintf ohtml "data['%s'] = new Array();\n" s_node; M.iter (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s) m; - if parent != T.nil then - fprintf odot "node%i -> %s;\n" - (T.preorder tree parent) s_node; - loop odot ohtml (T.first_child tree node) node; - loop odot ohtml (T.next_sibling tree node) parent + let first = T.first_child tree node in + let maxw1, maxy1 = loop osvg ohtml first node x (y + 40) in + let next = T.next_sibling tree node in + let x_next = max (x+lbox) (maxw1+10) in + if node != root then begin + if node == T.first_child tree parent then + fprintf osvg "\n" + (x + lbox / 2) (y-20) (x + lbox / 2) (y); + if next != T.nil then + fprintf osvg "\n" + (x + lbox) (y+10) x_next (y+10); + end; + let maxw2, maxy2 = loop osvg ohtml next node x_next y in + maxw2, max maxy1 maxy2 end + else x, y in ignore (Sys.command "mkdir -p tests/trace"); - let odot_ = open_out "tests/trace/trace.dot" in + let osvg_ = open_out "tests/trace/trace.svg" in let ohtml_ = open_out "tests/trace/trace.html" in - let odot = formatter_of_out_channel odot_ in + let osvg = formatter_of_out_channel osvg_ in let ohtml = formatter_of_out_channel ohtml_ in - fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;"; fprintf ohtml "\ \ \ \ \ +
%a +
\ -\n
\n"; - let fi = open_in "tests/trace/trace2.svg" in + fprintf ohtml "\n
\n" + maxw maxh; + let fi = open_in "tests/trace/trace.svg" in try while true do let s = input_line fi in @@ -91,8 +111,9 @@ let gen_trace (type s) = (); fun t tree -> done with End_of_file -> - fprintf ohtml "
\n%!"; + fprintf ohtml "\n
\n%!"; pp_print_flush ohtml (); close_out ohtml_; close_in fi +