Refactor the run module, moving out of the Make functor everything that can be moved...
[tatoo.git] / src / html.ml
index 21b33e6..f5aa8b2 100644 (file)
@@ -36,15 +36,21 @@ 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)
+  type t = StateSet.t list
+  let hash l =
+    List.fold_left (fun acc set ->
+      HASHINT2(acc, (set.StateSet.id :> int))) 17 l
+
+  let equal l1 l2 =
+    try
+      List.for_all2 (==) l1 l2
+    with _ -> false
 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
@@ -52,7 +58,8 @@ let rgb x =
   and b = (h lsr 16) land 0xff
   in
   r, g, b
-let color ((a,b,c) as x) =
+
+let color x =
   try
     CTable.find ctable x
   with
@@ -64,103 +71,131 @@ let color ((a,b,c) as x) =
       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 get_conf sel l i =
+  List.fold_left (fun (accb,accl) a ->
+    accb || StateSet.intersect a.(i) sel,
+    a.(i) :: accl) (false,[]) l
 
-let gen_trace (type s) = fun auto t tree ->
+let gen_trace (type s) = fun auto sat_arrays t tree ->
   let module T = (val (t) : Tree.S with type t = s) in
   let root = T.root tree in
-  let rec loop osvg ohtml node parent x y =
+  let sel = Ata.get_selecting_states auto in
+  let rec loop output 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 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 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 marked, conf = get_conf sel sat_arrays node_id in
+      let scolor, tcolor = color conf, text_color conf 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 "<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 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;
+      fprintf output
+        "<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 output "<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;
       let first = T.first_child tree node in
-      let maxw1, maxy1 = loop osvg ohtml first node x (y + 40) in
+      let maxw1, maxy1 = loop output 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"
+          fprintf output  "<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"
+          fprintf output "<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
+      let maxw2, maxy2 = loop output next node x_next y in
       maxw2, max maxy1 maxy2
     end
     else x, y
   in
   ignore (Sys.command "mkdir -p tests/trace");
-  let osvg_ = open_out "tests/trace/trace.svg" in
   let ohtml_ = open_out "tests/trace/trace.html" in
-  let osvg = formatter_of_out_channel osvg_ in
   let ohtml = formatter_of_out_channel ohtml_ in
   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'/>\
+<style>\
+div#data {\
+    position: absolute;\
+    top: 0%%;\
+    left: 50%%;\
+    width: 50%%;\
+    height: 50%%;\
+    overflow: auto;\
+}\
+div#svg {\
+    position: absolute;\
+    top: 50%%;\
+    left: 0%%;\
+    width: 100%%;\
+    height: 50%%;\
+    overflow: auto;\
+}\
+\
+div#automata {\
+    white-space: pre;\
+    overflow: auto;\
+    position: absolute;\
+    width: 50%%;\
+    top: 0%%;\
+    left: 0%%;\
+    height: 50%%;\
+}\
+</style>\
 </head>\
 <body>\
 <div id='automata' >%a
 </div>
-<div id='data' > </div>\
-<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_;
+<div id='data' > </div>\n\
+<script type='text/javascript'>\n\
+var data = new Array();\n\
+var rounds = %i;\n"
+    Ata.print auto
+    (List.length sat_arrays);
+  List.iteri (fun i a ->
+    fprintf ohtml "data[%i] = new Array();\n" i;
+    Array.iteri (fun id set ->
+      fprintf ohtml "data[%i]['node%i'] = \"%a\";\n"
+      i id StateSet.print set) a) (List.rev sat_arrays);
   fprintf ohtml "var activate = function (id) {\
   var d = document.getElementById('data');
   var msg = '';
-  for (i=0; i < data[id].length; i++)
-     msg += ('<p>round: ' + i + ':<br/>') + data[id][i] + '</p>\\n';
+  for (i=0; i < rounds; i++)
+     msg += ('<p>round: ' + i + ':<br/>') + data[i][id] + '</p>\\n';
   d.innerHTML = msg;
   return;
   };\n";
-  fprintf ohtml "</script>\n<div id='svg'><svg width=\"%i\" height=\"%i\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
+  fprintf ohtml "</script>\n<div id='svg'><svg id='svgimg' width='' height='' xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n";
+  let maxw, maxh = loop ohtml (T.root tree) T.nil 50 50 in
+  fprintf ohtml "</svg>\n<script type=\"text/javascript\">window.onload = function () {\
+  var svg = document.getElementById('svgimg');
+  svg.setAttribute('width', %i);
+  svg.setAttribute('height', %i);
+};\
+</script>\
+</div></body></html>\n%!"
     maxw maxh;
-  let fi = open_in "tests/trace/trace.svg" in
-  try
-    while true do
-      let s = input_line fi in
-      fprintf ohtml "%s\n" s;
-    done
-  with
-    End_of_file ->
-      fprintf ohtml "</svg>\n</div></body></html>\n%!";
-      pp_print_flush ohtml ();
-      close_out ohtml_;
-      close_in fi
+  pp_print_flush ohtml ();
+  close_out ohtml_