Silence an unused variable warning.
[tatoo.git] / src / html.ml
index c7990b3..21b33e6 100644 (file)
@@ -1,40 +1,81 @@
+INCLUDE "utils.ml"
+
 open Format
 module M = Map.Make(struct type t = int let compare = compare end)
 
+type info = { sat : StateSet.t;
+              todo : StateSet.t;
+              msg : string;
+            }
 let info = Hashtbl.create 2017
 let final = Hashtbl.create 2017
 
 let max_round = ref 0
 
-let add_info (nodeid:int) (i:int) s =
-  if i > !max_round then max_round := i;
-  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 =
-  let () = pp_print_flush fmt ();
-    Buffer.clear buff
+
+let trace ?(msg="") nid r t d  =
+  if r > !max_round then max_round := r;
+  let m = try Hashtbl.find info nid with Not_found -> M.empty in
+  let () = pp_print_flush fmt () in
+  let _ = fprintf fmt
+    "node: %i<br/>%s<br/>todo: %a<br/>sat: %a<br/>_______________________<br/>"
+    nid msg  StateSet.print t StateSet.print d
   in
-  kfprintf (fun fmt ->
-    pp_print_flush fmt ();
-    let s = Buffer.contents buff in
-    add_info nodeid i s) fmt
+  let () = pp_print_flush fmt () in
+  let msg = Buffer.contents buff in
+  let () = Buffer.clear buff in
+  let old_inf = try M.find r m with Not_found -> [] in
+  let m' = M.add r ({ sat = d; todo = t; msg = msg }::old_inf) m in
+  Hashtbl.replace info nid m'
 
 let finalize_node n r b =
   Hashtbl.replace final n (b,r)
+module K =
+struct
+  type t = int * StateSet.t * StateSet.t
+  let hash (a,b,c) = HASHINT3(a, (b.StateSet.id :> int), (c.StateSet.id :> int) )
+  let equal ((a1,b1,c1) as x) ((a2,b2,c2) as y) =
+    x == y || (a1 == a2 && b1 == b2 && c1 == c2)
+end
+
+module CTable = Hashtbl.Make (K)
+
+let ctable = CTable.create 20
+let rgb x =
+  let h = K.hash x in
+  let r = h land 0xff
+  and g = (h lsr 8) land 0xff
+  and b = (h lsr 16) land 0xff
+  in
+  r, g, b
+let color ((a,b,c) as x) =
+  try
+    CTable.find ctable x
+  with
+    Not_found ->
+      let r,g,b = rgb x in
+      let s = "rgb(" ^ (string_of_int r) ^ ","
+        ^ (string_of_int g) ^ ","
+        ^ (string_of_int b) ^ ")"
+      in
+      CTable.add ctable x s;
+      s
+let text_color x =
+  let r,g,b = rgb x in
+  let av = (r + g + b) / 3 in
+  if av > 128 then "rgb(0,0,0)"
+  else "rgb(255,255,255)"
 
 
-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)
@@ -44,56 +85,74 @@ let gen_trace (type s)  = (); fun t tree ->
       let marked, last_round = try Hashtbl.find final node_id with Not_found ->
         Printf.eprintf ">>> %i\n%!" node_id; false, !max_round;
       in
+      let scolor, tcolor =
+        let { sat ; todo; _ } =
+          match M.find last_round m with
+            [] -> { sat = StateSet.empty; todo= StateSet.empty; msg = "ERROR" }
+          | [ e ] -> e
+          | l -> List.hd (List.tl (List.rev l))
+        in
+        let c = (last_round, StateSet.union sat todo, StateSet.empty) in color c, text_color c
+      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 odot "%s[ id=\"%s\" label=\"%s\" style=filled fillcolor=\"%f,1.0,1.0\"\
-shape=\"%s\" ];\n"
-        s_node
-        s_node
-        (QName.to_string (T.tag tree node))
-        (1.0 -. (float (last_round+1) /. float (!max_round+1)))
-        (if marked then "oval" else "box") ;
+      fprintf osvg "<rect id=\"%s\" onclick=\"activate(\'%s\');\" x=\"%i\" y=\"%i\" width=\"%i\" height=\"20\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>\n%!"
+        s_node s_node x y lbox scolor (if marked then ";stroke-width:4" else ";stroke-width:2;stroke-dasharray:2,2");
+      fprintf osvg "<text x=\"%i\" y=\"%i\" style=\"fill:%s;font-size:17;font-family:typewriter;\" onclick=\"activate(\'%s\');\" >%s</text>\n" (x+10) (y+15) tcolor 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)
+        (fun i l ->
+          let msg = String.concat "" (List.rev_map (fun x -> x.msg) l) in
+          fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i msg)
         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  "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
+            (x + lbox / 2) (y-20) (x + lbox / 2) (y);
+        if next != T.nil then
+          fprintf osvg "<line x1=\"%i\" y1=\"%i\" x2=\"%i\" y2=\"%i\" style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\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 "<html>\
 <head><title></title>
 <link rel='stylesheet' type='text/css' href='trace.css' />\
 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
 </head>\
 <body>\
+<div id='automata' >%a
+</div>
 <div id='data' > </div>\
-<script type='text/javascript'>";
-  loop odot ohtml (T.root tree) (T.nil);
-  fprintf odot "\n}\n%!";
-  pp_print_flush odot ();
-  close_out odot_;
-  ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
-  ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
+<script type='text/javascript'>"
+Ata.print auto;
+  let maxw, maxh = loop osvg ohtml (T.root tree) T.nil 50 50 in
+  pp_print_flush osvg ();
+  close_out osvg_;
   fprintf ohtml "var activate = function (id) {\
   var d = document.getElementById('data');
   var msg = '';
   for (i=0; i < data[id].length; i++)
-     msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
+     msg += ('<p>round: ' + i + ':<br/>') + data[id][i] + '</p>\\n';
   d.innerHTML = msg;
   return;
   };\n";
-
-  fprintf ohtml "</script>\n<div id='svg'>\n";
-  let fi = open_in "tests/trace/trace2.svg" in
+  fprintf ohtml "</script>\n<div id='svg'><svg width=\"%i\" height=\"%i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
+    maxw maxh;
+  let fi = open_in "tests/trace/trace.svg" in
   try
     while true do
       let s = input_line fi in
@@ -101,8 +160,7 @@ shape=\"%s\" ];\n"
     done
   with
     End_of_file ->
-      fprintf ohtml "</div></body></html>\n%!";
+      fprintf ohtml "</svg>\n</div></body></html>\n%!";
       pp_print_flush ohtml ();
       close_out ohtml_;
       close_in fi
-