+ let choose_jump tagset qtags1 qtagsn a f_nil f_text f_t1 f_s1 f_tn f_sn f_notext =
+ let tags1,hastext1,fin1 = inter_text tagset (tags a qtags1) in
+ let tagsn,hastextn,finn = inter_text tagset (tags a qtagsn) in
+ if (hastext1||hastextn) then f_text (* jumping to text nodes doesn't work really well *)
+ else if (Ptset.Int.is_empty tags1) && (Ptset.Int.is_empty tagsn) then f_nil
+ else if (Ptset.Int.is_empty tagsn) then
+ if (Ptset.Int.is_singleton tags1)
+ then (* TaggedChild/Sibling *)
+ let tag = (Ptset.Int.choose tags1) in mk_app_fun f_t1 tag (Tag.to_string tag)
+ else (* SelectChild/Sibling *)
+ mk_app_fun f_s1 tags1 (string_of_ts tags1)
+ else if (Ptset.Int.is_empty tags1) then
+ if (Ptset.Int.is_singleton tagsn)
+ then (* TaggedDesc/Following *)
+ let tag = (Ptset.Int.choose tagsn) in mk_app_fun f_tn tag (Tag.to_string tag)
+ else (* SelectDesc/Following *)
+ mk_app_fun f_sn tagsn (string_of_ts tagsn)
+ else f_notext
+
+ let choose_jump_down a b c d =
+ choose_jump a b c d
+ (mk_fun (Tree.mk_nil) "Tree.mk_nil")
+ (mk_fun (Tree.text_below) "Tree.text_below")
+ (mk_fun (fun _ -> Tree.node_child) "[TaggedChild]Tree.node_child") (* !! no tagged_child in Tree.ml *)
+ (mk_fun (fun _ -> Tree.node_child) "[SelectChild]Tree.node_child") (* !! no select_child in Tree.ml *)
+ (mk_fun (Tree.tagged_desc) "Tree.tagged_desc")
+ (mk_fun (fun _ -> Tree.node_child ) "[SelectDesc]Tree.node_child") (* !! no select_desc *)
+ (mk_fun (Tree.node_child) "Tree.node_child")
+
+ let choose_jump_next a b c d =
+ choose_jump a b c d
+ (mk_fun (fun t _ -> Tree.mk_nil t) "Tree.mk_nil2")
+ (mk_fun (Tree.text_next) "Tree.text_next")
+ (mk_fun (fun _ -> Tree.node_sibling_ctx) "[TaggedSibling]Tree.node_sibling_ctx")(* !! no tagged_sibling in Tree.ml *)
+ (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectSibling]Tree.node_sibling_ctx")(* !! no select_sibling in Tree.ml *)
+ (mk_fun (Tree.tagged_foll_ctx) "Tree.tagged_foll_ctx")
+ (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectFoll]Tree.node_sibling_ctx")(* !! no select_foll *)
+ (mk_fun (Tree.node_sibling_ctx) "Tree.node_sibling_ctx")
+
+ let get_trans slist tag a t =
+ try
+ Hashtbl.find td_trans (tag,SList.hash slist)
+ with
+ | Not_found ->
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ SList.fold
+ (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
+ let fl,ll,rr,ca,da,sa,fa =
+ StateSet.fold
+ (fun q acc ->
+ List.fold_left
+ (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
+ (ts,t) ->
+ if (TagSet.mem tag ts)
+ then
+ let _,_,f,_ = Transition.node t in
+ let (child,desc,below),(sibl,foll,after) = Formula.st f in
+ (Formlist.cons t fl_acc,
+ StateSet.union ll_acc below,
+ StateSet.union rl_acc after,
+ StateSet.union child c_acc,
+ StateSet.union desc d_acc,
+ StateSet.union sibl s_acc,
+ StateSet.union foll f_acc)
+ else acc ) acc (
+ try Hashtbl.find a.trans q
+ with
+ Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
+ q;[]
+ )
+
+ ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
+ in fl::fll_acc, (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
+ slist ([],SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+ in
+ (* Logic to chose the first and next function *)
+ let tags_below,tags_after = Tree.tags t tag in
+ let first = choose_jump_down tags_below ca da a
+ and next = choose_jump_next tags_after sa fa a in
+ let v = (fl_list,llist,rlist,first,next) in
+ Hashtbl.add td_trans (tag, SList.hash slist) v; v
+
+ let merge rb rb1 rb2 mark t res1 res2 =
+ if rb
+ then
+ let res1 = if rb1 then res1 else RS.empty
+ and res2 = if rb2 then res2 else RS.empty
+ in
+ if mark then RS.cons t (RS.concat res1 res2)
+ else RS.concat res1 res2
+ else RS.empty
+
+ let empty_size n =
+ let rec loop acc = function 0 -> acc
+ | n -> loop (SList.cons StateSet.empty acc) (n-1)
+ in loop SList.nil n
+
+ let top_down ?(noright=false) a t slist ctx slot_size =
+ let pempty = empty_size slot_size in
+ let eval_fold2_slist fll sl1 sl2 res1 res2 t =
+ let res = Array.copy res1 in
+ let rec fold l1 l2 fll i aq =
+ match SList.node l1,SList.node l2, fll with
+ | SList.Cons(s1,ll1),
+ SList.Cons(s2,ll2),
+ fl::fll ->
+ let r',rb,rb1,rb2,mark = eval_formlist s1 s2 fl in
+ let _ = res.(i) <- merge rb rb1 rb2 mark t res1.(i) res2.(i)
+ in
+ fold ll1 ll2 fll (i+1) (SList.cons r' aq)
+ | SList.Nil, SList.Nil,[] -> aq,res
+ | _ -> assert false
+ in
+ fold sl1 sl2 fll 0 SList.nil
+ in
+ let null_result() = (pempty,Array.make slot_size RS.empty) in
+ let rec loop t slist ctx =
+ if Tree.is_nil t then null_result()
+ else
+ let tag = Tree.tag t in
+ let fl_list,llist,rlist,first,next = get_trans slist tag a t in
+ let sl1,res1 = loop (first t) llist t in
+ let sl2,res2 = loop (next t ctx) rlist ctx in
+ let res = eval_fold2_slist fl_list sl1 sl2 res1 res2 t
+ in
+ D_IGNORE_(
+ register_trace t (slist,(fst res),sl1,sl2,fl_list,first,next,ctx),
+ res)
+ in
+ let loop_no_right t slist ctx =
+ if Tree.is_nil t then null_result()
+ else
+ let tag = Tree.tag t in
+ let fl_list,llist,rlist,first,next = get_trans slist tag a t in
+ let sl1,res1 = loop (first t) llist t in
+ let sl2,res2 = null_result() in
+ let res = eval_fold2_slist fl_list sl1 sl2 res1 res2 t
+ in
+ D_IGNORE_(
+ register_trace t (slist,(fst res),sl1,sl2,fl_list,first,next,ctx),
+ res)
+ in
+ (if noright then loop_no_right else loop) t slist ctx
+
+
+ let run_top_down a t =
+ let init = SList.cons a.init SList.nil in
+ let _,res = top_down a t init t 1
+ in
+ D_IGNORE_(
+ output_trace a t "trace.html"
+ (RS.fold (fun t a -> IntSet.add (Tree.id t) a) res.(0) IntSet.empty),
+ res.(0))
+ ;;
+
+ module Configuration =
+ struct
+ module Ptss = Set.Make(StateSet)
+ module IMap = Map.Make(StateSet)
+ type t = { hash : int;
+ sets : Ptss.t;
+ results : RS.t IMap.t }
+ let empty = { hash = 0;
+ sets = Ptss.empty;
+ results = IMap.empty;
+ }
+ let is_empty c = Ptss.is_empty c.sets
+ let add c s r =
+ if Ptss.mem s c.sets then
+ { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
+ else
+ { hash = HASHINT2(c.hash,Ptset.Int.uid s);
+ sets = Ptss.add s c.sets;
+ results = IMap.add s r c.results
+ }
+
+ let pr fmt c = Format.fprintf fmt "{";
+ Ptss.iter (fun s -> StateSet.print fmt s;
+ Format.fprintf fmt " ") c.sets;
+ Format.fprintf fmt "}\n%!";
+ IMap.iter (fun k d ->
+ StateSet.print fmt k;
+ Format.fprintf fmt "-> %i\n" (RS.length d)) c.results;
+ Format.fprintf fmt "\n%!"
+
+ let merge c1 c2 =
+ let acc1 = IMap.fold (fun s r acc ->
+ IMap.add s
+ (try
+ RS.concat r (IMap.find s acc)
+ with
+ | Not_found -> r) acc) c1.results IMap.empty
+ in
+ let imap =
+ IMap.fold (fun s r acc ->
+ IMap.add s
+ (try
+ RS.concat r (IMap.find s acc)
+ with
+ | Not_found -> r) acc) c2.results acc1