X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=blobdiff_plain;f=src%2Fauto%2Feval.ml;h=cc9ed71074436f9d9acf4f20ed538f8c9d6f77dd;hp=80df253b47a3b173b787d5c8bd5677ce8470fb3c;hb=9b3611f8b650edf4183169a9c2c4317e13be536d;hpb=deb657ae0bc7163a0108e9e63d10921416063294 diff --git a/src/auto/eval.ml b/src/auto/eval.ml index 80df253..cc9ed71 100644 --- a/src/auto/eval.ml +++ b/src/auto/eval.ml @@ -14,70 +14,149 @@ (***********************************************************************) (* - Time-stamp: + Time-stamp: *) INCLUDE "utils.ml" open Format open Utils -module Make (T : Tree.Sig.S) = struct +module Make (T : Tree.Sig.S) : + sig + val eval : Ata.t -> T.t -> T.node -> T.node list + end + = struct - type cache = (int, StateSet.t) Hashtbl.t + type cache = StateSet.t Cache.N1.t + let get c t n = Cache.N1.find c (T.preorder t n) - let get c t n = - try Hashtbl.find c (T.preorder t n) - with Not_found -> StateSet.empty + let set c t n v = Cache.N1.add c (T.preorder t n) v - let set c t n v = Hashtbl.replace c (T.preorder t n) v + module Info = struct + type t = { is_left : bool; + is_right : bool; + has_left : bool; + has_right : bool; + kind : Tree.Common.NodeKind.t; + } + let equal a b = a = b + let hash a = Hashtbl.hash a + end - let eval_trans l ctx acc = - List.fold_left (fun (acct, accs) ((q, phi) as trs) -> - if StateSet.mem q accs then (acct, accs) else - if Ata.SFormula.eval ctx phi then - (acct, StateSet.add q accs) - else - (trs::acct, accs) - ) ([], acc) l + module NodeInfo = Hcons.Make(Info) + + let eval_form phi node_info fcs nss ps ss = + let open NodeInfo in + let open Info in + let rec loop phi = + begin match Ata.SFormula.expr phi with + Formula.True -> true + | Formula.False -> false + | Formula.Atom a -> + let p, b, q = Ata.Atom.node a in + let pos = + let open Ata in + match p with + | First_child -> StateSet.mem q fcs + | Next_sibling -> StateSet.mem q nss + | Parent | Previous_sibling -> StateSet.mem q ps + | Stay -> StateSet.mem q ss + | Is_first_child -> node_info.node.is_left + | Is_next_sibling -> node_info.node.is_right + | Is k -> k == node_info.node.kind + | Has_first_child -> node_info.node.has_left + | Has_next_sibling -> node_info.node.has_right + in + if Ata.is_move p && (not b) then + eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a; + b == pos + | Formula.And(phi1, phi2) -> loop phi1 && loop phi2 + | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2 + end + in + loop phi + + let eval_trans cache ltrs node_info fcs nss ps ss = + let i = (ltrs.Ata.TransList.id :> int) + and j = (node_info.NodeInfo.id :> int) + and k = (fcs.StateSet.id :> int) + and l = (nss.StateSet.id :> int) + and m = (ps.StateSet.id :> int) + and n = (ss.StateSet.id :> int) in + let res = Cache.N6.find cache i j k l m n in + if res == Cache.N6.dummy cache then + let res = + Ata.TransList.fold (fun trs (acct, accs) -> + let q, _, phi = Ata.Transition.node trs in + if StateSet.mem q accs then (acct, accs) else + if eval_form phi node_info fcs nss ps accs then + (acct, StateSet.add q accs) + else + (Ata.TransList.cons trs acct, accs) + ) ltrs (Ata.TransList.nil, ss) + in + Cache.N6.add cache i j k l m n res; res + else + res - let top_down_run auto tree node cache i = + let top_down_run auto tree node cache _i = let redo = ref false in - let rec loop node is_left = + let dummy2 = Ata.TransList.cons + (Ata.Transition.make (State.dummy,QNameSet.empty, Ata.SFormula.false_)) + Ata.TransList.nil + in + let dummy6 = (dummy2, StateSet.empty) in + let trans_cache6 = Cache.N6.create 17 dummy6 in + let trans_cache2 = Cache.N2.create 17 dummy2 in + let rec loop node = if node != T.nil then begin let parent = T.parent tree node in let fc = T.first_child tree node in let ns = T.next_sibling tree node in - let states0 = get cache tree node in let tag = T.tag tree node in - let trans0 = Ata.get_trans auto auto.Ata.states tag in - let parent_states = if parent == T.nil then auto.Ata.top_states else get cache tree parent in - let fc_states = if fc == T.nil then auto.Ata.bottom_states else get cache tree fc in - let ns_states = if ns == T.nil then auto.Ata.bottom_states else get cache tree ns in - let is_root = parent == T.nil in - let ctx0 = - if is_left then - Ata.make_ctx fc_states ns_states parent_states StateSet.empty states0 is_left is_root - else - Ata.make_ctx fc_states ns_states StateSet.empty parent_states states0 is_left is_root + let states0 = get cache tree node in + let trans0 = + let trs = + Cache.N2.find trans_cache2 + (tag.QName.id :> int) (auto.Ata.states.StateSet.id :> int) + in + if trs == dummy2 then + let trs = Ata.get_trans auto auto.Ata.states tag in + (Cache.N2.add + trans_cache2 + (tag.QName.id :> int) + (auto.Ata.states.StateSet.id :> int) trs; trs) + else trs + in + let ps = get cache tree parent in + let fcs = get cache tree fc in + let nss = get cache tree ns in + let node_info = NodeInfo.make + (Info.({ is_left = node == T.first_child tree parent; + is_right = node == T.next_sibling tree parent; + has_left = fc != T.nil; + has_right = ns != T.nil; + kind = T.kind tree node })) + in + let trans1, states1 = + eval_trans trans_cache6 trans0 node_info fcs nss ps states0 in - eprintf "-- [Iteration % 4d] --\n node: %a\n context: %a\n%!" - i T.print_node node Ata.print_ctx ctx0; - List.iter (fun (q, phi) -> eprintf " %a -> %a\n" State.print q Ata.SFormula.print phi) trans0; - eprintf "----------------------\n%!"; - let trans1, states1 = eval_trans trans0 ctx0 StateSet.empty in if states1 != states0 then set cache tree node states1; - let () = loop fc true in - let ctx1 = {ctx0 with Ata.left = (get cache tree fc) ; Ata.epsilon = states1 } in - let trans2, states2 = eval_trans trans1 ctx1 states1 in + let () = loop fc in + let fcs1 = get cache tree fc in + let trans2, states2 = + eval_trans trans_cache6 trans1 node_info fcs1 nss ps states1 + in if states2 != states1 then set cache tree node states2; - let () = loop ns false in - let ctx2 = { ctx1 with Ata.right = (get cache tree ns); Ata.epsilon = states2 } in - let _, states3 = eval_trans trans2 ctx2 states2 in + let () = loop ns in + let _, states3 = + eval_trans trans_cache6 trans2 node_info fcs1 (get cache tree ns) ps states2 + in if states3 != states2 then set cache tree node states3; if states0 != states3 && (not !redo) then redo := true end in - loop node true; + loop node; !redo let get_results auto tree node cache = @@ -95,7 +174,7 @@ module Make (T : Tree.Sig.S) = struct loop node [] let eval auto tree node = - let cache = Hashtbl.create 511 in + let cache = Cache.N1.create (T.size tree) StateSet.empty in let redo = ref true in let iter = ref 0 in while !redo do