Refactor the tracing code, store the whole tree structure in a javascript array and...
[tatoo.git] / src / html_trace.ml
index f5aa8b2..7ce3f39 100644 (file)
@@ -83,11 +83,19 @@ let get_conf sel l i =
     accb || StateSet.intersect a.(i) sel,
     a.(i) :: accl) (false,[]) l
 
+let dup_rev_list l =
+  List.fold_left (fun acc e -> e::e::acc) [] l
+
 let gen_trace (type s) = fun auto sat_arrays t tree ->
+  let sat_arrays = dup_rev_list sat_arrays in
   let module T = (val (t) : Tree.S with type t = s) in
   let root = T.root tree in
   let sel = Ata.get_selecting_states auto in
-  let rec loop output node parent x y =
+  let todos = Ata.get_states_by_rank auto in
+  let pr_sets = Pretty.print_list ~sep:", "
+    (fun fmt s -> fprintf fmt "'%a'" StateSet.print s)
+  in
+  let rec loop output node parent prevsib x y =
     if node != T.nil then begin
       let node_id = T.preorder tree node in
       let marked, conf = get_conf sel sat_arrays node_id in
@@ -95,9 +103,33 @@ let gen_trace (type s) = fun auto sat_arrays t tree ->
       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
+      let first = T.first_child tree node in
+      let next = T.next_sibling tree node in
+      fprintf output "<script type='text/javascript'><![CDATA[ ";
+      let pass, max_active_pass, diff_list, full_list =
+        List.fold_left
+          (fun (pass, max_active_pass, diff_list, full_list) a ->
+            let todo = if pass >= Array.length todos then StateSet.empty
+              else todos.(pass)
+            in
+            let diff = StateSet.inter a.(node_id) todo in
+            let new_active_pass =
+              if diff == StateSet.empty then max_active_pass else pass
+            in
+            pass+1, new_active_pass, diff::diff_list, a.(node_id) :: full_list)
+          (0, -1, [], []) sat_arrays
+      in
+      fprintf output "tree['%s'] = { marked: %b, fc: 'node%i', ns: 'node%i', par: 'node%i', ps: 'node%i', max: %i, dlist: [%a], flist: [%a] };]]></script>\n"
+        s_node
+        marked
+        (T.preorder tree first) (T.preorder tree next)
+        (T.preorder tree parent) (T.preorder tree prevsib)
+        max_active_pass
+        pr_sets (List.rev diff_list)
+        pr_sets (List.rev full_list);
       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%!"
+ width=\"%i\" height=\"22\" style=\"fill:%s;stroke:rgb(0,0,0)%s\"/>%!"
         s_node
         s_node
         x y
@@ -111,21 +143,19 @@ 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 output first node x (y + 40) in
-      let next = T.next_sibling tree node in
+      let maxw1, maxy1 = loop output first node T.nil x (y + 40) in
       let x_next = max (x+lbox) (maxw1+10) in
       if node != root then begin
-        if node == T.first_child tree parent then
+        if prevsib == T.nil then
           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);
+            (x + lbox / 2) (y-18) (x + lbox / 2) (y);
         if next != T.nil then
           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);
+            (x + lbox) (y+11) x_next (y+11);
       end;
-      let maxw2, maxy2 = loop output next node x_next y in
+      let maxw2, maxy2 = loop output next parent node x_next y in
       maxw2, max maxy1 maxy2
     end
     else x, y
@@ -133,69 +163,53 @@ style=\"stroke:rgb(0,0,0);stroke-width:2\"/>\n"
   ignore (Sys.command "mkdir -p tests/trace");
   let ohtml_ = open_out "tests/trace/trace.html" in
   let ohtml = formatter_of_out_channel ohtml_ in
-  fprintf ohtml "<html>\
-<head><title></title>
-<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>
+  fprintf ohtml "<html>\n\
+<head><title></title>\n\
+<meta http-equiv='content-type' content='text/html;charset=utf-8'/>\n\
+<style>\n\
+%s
+</style>\n\
+</head>\n\
+<body>\n\
+<div id='automata' >%a</div>\n\
 <div id='data' > </div>\n\
 <script type='text/javascript'>\n\
-var data = new Array();\n\
+var tree = new Array();\n\
+var current_node = null;
+var nil_id = 'node%i';
+var last_id = %i;
 var rounds = %i;\n"
+    Trace_css.content
     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 < 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 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%!"
+    (T.preorder tree T.nil)
+    (T.size tree)
+    (List.length sat_arrays - 1);
+  fprintf ohtml "%s\n" Trace_js.content;
+  fprintf ohtml
+    "</script>\n\
+<div id='tree'>
+<div id='controls'><button onclick='activate_parent();'>↑</button>\n\
+<button onclick='activate_previous();'>←</button>\n\
+<button onclick='activate_first();'>↓</button>\n\
+<button onclick='activate_next();'>→</button>\n\
+<select id='relnodes' onchange='hide_nodes();'>\n\
+<option value='-1'>All nodes</option>\n\
+%a</select>\n\
+</div>
+<div id='svg'>\n
+   <svg id='svgimg' width='' height='' xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n"
+    (Pretty.print_range (fun fmt i ->
+      fprintf fmt "<option value='%i'>Relevant nodes on pass %i</option>\n" i i))
+    (0,(List.length sat_arrays - 1));
+  let maxw, maxh = loop ohtml (T.root tree) T.nil T.nil 50 50 in
+  fprintf ohtml "</svg>\n</div></div><script type='text/javascript'>\n\
+window.onload = function () {\n\
+    var svg = document.getElementById('svgimg');\n\
+    svg.setAttribute('width', %i);\n\
+    svg.setAttribute('height', %i);\n\
+    activate('node0');\n\
+};\n
+</script></body></html>\n%!"
     maxw maxh;
   pp_print_flush ohtml ();
   close_out ohtml_