1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-03-13 18:27:13 CET by Kim Nguyen>
24 module Make (T : Tree.Sig.S) :
26 val eval : Ata.t -> T.t -> T.node -> T.node list
30 type cache = (int, StateSet.t) Hashtbl.t
32 try Hashtbl.find c (T.preorder t n)
33 with Not_found -> StateSet.empty
35 let set c t n v = Hashtbl.replace c (T.preorder t n) v
38 type t = { is_left : bool;
42 kind : Tree.Common.NodeKind.t;
45 let hash a = Hashtbl.hash a
48 module NodeInfo = Hcons.Make(Info)
50 let eval_form phi node_info fcs nss ps ss =
54 begin match Ata.SFormula.expr phi with
56 | Formula.False -> false
58 let p, b, q = Ata.Atom.node a in
62 | First_child -> StateSet.mem q fcs
63 | Next_sibling -> StateSet.mem q nss
64 | Parent | Previous_sibling -> StateSet.mem q ps
65 | Stay -> StateSet.mem q ss
66 | Is_first_child -> node_info.node.is_left
67 | Is_next_sibling -> node_info.node.is_right
68 | Is k -> k == node_info.node.kind
69 | Has_first_child -> node_info.node.has_left
70 | Has_next_sibling -> node_info.node.has_right
72 if Ata.is_move p && (not b) then
73 eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
75 | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
76 | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
81 let eval_trans cache ltrs node_info fcs nss ps ss =
82 let i = (ltrs.Ata.TransList.id :> int)
83 and j = (node_info.NodeInfo.id :> int)
84 and k = (fcs.StateSet.id :> int)
85 and l = (nss.StateSet.id :> int)
86 and m = (ps.StateSet.id :> int)
87 and n = (ss.StateSet.id :> int) in
88 let res = Cache.N6.find cache i j k l m n in
89 if res == Cache.N6.dummy cache then
91 Ata.TransList.fold (fun trs (acct, accs) ->
92 let q, _, phi = Ata.Transition.node trs in
93 if StateSet.mem q accs then (acct, accs) else
94 if eval_form phi node_info fcs nss ps accs then
95 (acct, StateSet.add q accs)
97 (Ata.TransList.cons trs acct, accs)
98 ) ltrs (Ata.TransList.nil, ss)
100 Cache.N6.add cache i j k l m n res; res
104 let top_down_run auto tree node cache _i =
105 let redo = ref false in
106 let dummy2 = Ata.TransList.cons
107 (Ata.Transition.make (State.dummy,QNameSet.empty, Ata.SFormula.false_))
110 let dummy6 = (dummy2, StateSet.empty) in
111 let trans_cache6 = Cache.N6.create 17 dummy6 in
112 let trans_cache2 = Cache.N2.create 17 dummy2 in
114 if node != T.nil then begin
115 let parent = T.parent tree node in
116 let fc = T.first_child tree node in
117 let ns = T.next_sibling tree node in
118 let tag = T.tag tree node in
119 let states0 = get cache tree node in
122 Cache.N2.find trans_cache2
123 (tag.QName.id :> int) (auto.Ata.states.StateSet.id :> int)
125 if trs == dummy2 then
126 let trs = Ata.get_trans auto auto.Ata.states tag in
129 (tag.QName.id :> int)
130 (auto.Ata.states.StateSet.id :> int) trs; trs)
133 let ps = get cache tree parent in
134 let fcs = get cache tree fc in
135 let nss = get cache tree ns in
136 let node_info = NodeInfo.make
137 (Info.({ is_left = node == T.first_child tree parent;
138 is_right = node == T.next_sibling tree parent;
139 has_left = fc != T.nil;
140 has_right = ns != T.nil;
141 kind = T.kind tree node }))
143 let trans1, states1 =
144 eval_trans trans_cache6 trans0 node_info fcs nss ps states0
146 if states1 != states0 then set cache tree node states1;
148 let fcs1 = get cache tree fc in
149 let trans2, states2 =
150 eval_trans trans_cache6 trans1 node_info fcs1 nss ps states1
152 if states2 != states1 then set cache tree node states2;
155 eval_trans trans_cache6 trans2 node_info fcs1 (get cache tree ns) ps states2
157 if states3 != states2 then set cache tree node states3;
158 if states0 != states3 && (not !redo) then redo := true
164 let get_results auto tree node cache =
165 let rec loop node acc =
166 if node == T.nil then acc
168 let acc0 = loop (T.next_sibling tree node) acc in
169 let acc1 = loop (T.first_child tree node) acc0 in
171 if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
178 let eval auto tree node =
179 let cache = Hashtbl.create 511 in
180 let redo = ref true in
183 redo := top_down_run auto tree node cache !iter;
186 get_results auto tree node cache