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 buff = Buffer.create 20
let fmt = formatter_of_buffer 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 %s todo: %a sat: %a _______________________ "
nid msg StateSet.print t StateSet.print d
in
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 = 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
and g = (h lsr 8) land 0xff
and b = (h lsr 16) land 0xff
in
r, g, b
let color 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 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 sat_arrays t tree ->
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 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
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
let first = T.first_child tree node in
let next = T.next_sibling tree node in
fprintf output
"%!"
s_node
s_node
x y
lbox
scolor
(if marked
then ";stroke-width:4"
else ";stroke-width:2;stroke-dasharray:2,2");
fprintf output "node%inode%inode%inode%i\n%!"
s_node (T.preorder tree first) s_node (T.preorder tree next)
s_node (T.preorder tree parent) s_node (T.preorder tree prevsib)
;
fprintf output "%s\n"
(x+10)
(y+15)
tcolor s_node tag;
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 prevsib == T.nil then
fprintf output "\n"
(x + lbox / 2) (y-18) (x + lbox / 2) (y);
if next != T.nil then
fprintf output "\n"
(x + lbox) (y+11) x_next (y+11);
end;
let maxw2, maxy2 = loop output next parent node x_next y in
maxw2, max maxy1 maxy2
end
else x, y
in
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 "\n\
\n\
\n\
\n\
\n\
\n\