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 (***********************************************************************)
22 type stats = { run : int;
24 fetch_trans_cache_access : int;
25 fetch_trans_cache_hit : int;
26 eval_trans_cache_access : int;
27 eval_trans_cache_hit : int;
30 let fetch_trans_cache_hit = ref 0
31 let fetch_trans_cache_access = ref 0
32 let eval_trans_cache_hit = ref 0
33 let eval_trans_cache_access = ref 0
34 let reset_stat_counters () =
35 fetch_trans_cache_hit := 0;
36 fetch_trans_cache_access := 0;
37 eval_trans_cache_hit := 0;
38 eval_trans_cache_access := 0
41 module Make (T : Tree.S) =
46 (* Pack into an integer the result of the is_* and has_ predicates
58 let is_left (s : t) : bool =
61 let is_right (s : t) : bool =
64 let has_left (s : t) : bool =
67 let has_right (s : t) : bool =
70 let kind (s : t) : Tree.NodeKind.t =
73 let make is_left is_right has_left has_right kind =
74 (int_of_bool is_left) lor
75 ((int_of_bool is_right) lsl 1) lor
76 ((int_of_bool has_left) lsl 2) lor
77 ((int_of_bool has_right) lsl 3) lor
78 ((Obj.magic kind) lsl 4)
81 let dummy_set = StateSet.singleton State.dummy
86 type sat_array = StateSet.t array list
87 DEFINE IFHTML(a,b) = (a)
89 type sat_array = StateSet.t array
90 DEFINE IFHTML(a,b) = (b)
93 if i < 0 then StateSet.empty else
94 Array.unsafe_get (IFHTML(List.hd a, a)) i
95 let unsafe_set a i v old_v =
97 Array.unsafe_set (IFHTML(List.hd a, a)) i v
101 (* The argument of the run *)
103 (* The automaton to be run *)
104 mutable sat: sat_array;
105 (* A mapping from node preorders to states satisfied at that node *)
107 (* Number of run we have performed *)
108 mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
109 (* A cache from states * label to list of transitions *)
110 mutable td_cache : StateSet.t Cache.N6.t;
111 mutable bu_cache : StateSet.t Cache.N6.t;
112 (* Two 6-way caches used during the top-down and bottom-up phase
113 label * self-set * fc-set * ns-set * parent-set * node-shape -> self-set
115 node_summaries: (int, int16_unsigned_elt, c_layout) Array1.t;
119 let dummy_form = Ata.Formula.stay State.dummy
122 let len = T.size tree in
126 sat = (let a = Array.create len StateSet.empty in
129 fetch_trans_cache = Cache.N2.create dummy_form;
130 td_cache = Cache.N6.create dummy_set;
131 bu_cache = Cache.N6.create dummy_set;
132 node_summaries = let ba = Array1.create int16_unsigned c_layout len in
136 let get_form fetch_trans_cache auto tag q =
138 incr fetch_trans_cache_access;
139 Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
141 if phi == dummy_form then
142 let phi = Ata.get_form auto tag q in
146 (tag.QName.id :> int)
150 incr fetch_trans_cache_hit;
155 let eval_form phi fcs nss ps ss summary =
158 begin match Formula.expr phi with
159 | Boolean.False -> false
160 | Boolean.True -> true
161 | Boolean.Atom (a, b) ->
163 let open NodeSummary in
164 match a.Atom.node with
166 b && StateSet.mem q (
169 | `Next_sibling -> nss
170 | `Parent | `Previous_sibling -> ps
173 | Is_first_child -> b == is_left summary
174 | Is_next_sibling -> b == is_right summary
175 | Is k -> b == (k == kind summary)
176 | Has_first_child -> b == has_left summary
177 | Has_next_sibling -> b == has_right summary
179 | Boolean.And(phi1, phi2) -> loop phi1 && loop phi2
180 | Boolean.Or (phi1, phi2) -> loop phi1 || loop phi2
186 let eval_trans_aux auto fetch_trans_cache tag fcs nss ps sat todo summary =
187 StateSet.fold (fun q (a_sat) ->
189 get_form fetch_trans_cache auto tag q
191 if eval_form phi fcs nss ps a_sat summary then
197 let rec eval_trans_fix auto fetch_trans_cache tag fcs nss ps sat todo summary =
199 eval_trans_aux auto fetch_trans_cache tag fcs nss ps sat todo summary
201 if new_sat == sat then sat else
202 eval_trans_fix auto fetch_trans_cache tag fcs nss ps new_sat todo summary
205 let eval_trans auto fetch_trans_cache eval_cache tag fcs nss ps ss todo summary =
206 let fcsid = (fcs.StateSet.id :> int) in
207 let nssid = (nss.StateSet.id :> int) in
208 let psid = (ps.StateSet.id :> int) in
209 let ssid = (ss.StateSet.id :> int) in
210 let tagid = (tag.QName.id :> int) in
211 let res = Cache.N6.find eval_cache tagid summary ssid fcsid nssid psid in
212 incr eval_trans_cache_access;
213 if res != dummy_set then begin incr eval_trans_cache_hit; res end
215 eval_trans_fix auto fetch_trans_cache tag fcs nss ps ss todo summary
217 Cache.N6.add eval_cache tagid summary ssid fcsid nssid psid new_sat;
224 let tree = run.tree in
225 let auto = run.auto in
226 let states_by_rank = Ata.get_states_by_rank auto in
227 let td_todo = states_by_rank.(i) in
228 let bu_todo = if i + 1 = Array.length states_by_rank then StateSet.empty
232 let rec loop_td_and_bu node parent parent_sat =
236 let node_id = T.preorder tree node in
237 let fc = T.first_child tree node in
238 let ns = T.next_sibling tree node in
239 let tag = T.tag tree node in
240 (* We enter the node from its parent *)
242 let s = Array1.unsafe_get run.node_summaries node_id in
243 if s != 0 then s else
246 (node == T.first_child tree parent) (*is_left *)
247 (node == T.next_sibling tree parent)(*is_right *)
248 (fc != T.nil) (* has_left *)
249 (ns != T.nil) (* has_right *)
250 (T.kind tree node) (* kind *)
252 run.node_summaries.{node_id} <- s; s
254 let status0 = unsafe_get run.sat node_id in
255 (* get the node_statuses for the first child, next sibling and parent *)
256 let fcs = unsafe_get run.sat (T.preorder tree fc) in
257 let nss = unsafe_get run.sat (T.preorder tree ns) in
258 (* evaluate the transitions with all this statuses *)
260 eval_trans auto run.fetch_trans_cache run.td_cache tag fcs nss
262 status0 td_todo summary
264 (* update the cache if the status of the node changed
265 unsafe_set run.sat node_id status1 status0;*)
266 let fcs1 = loop_td_and_bu fc node status1 in
267 if bu_todo == StateSet.empty then begin
268 unsafe_set run.sat node_id status1 status0; (* write the td_states *)
269 loop_td_and_bu ns node status1 (* tail call *)
271 let nss1 = loop_td_and_bu ns node status1 in
273 eval_trans auto run.fetch_trans_cache run.bu_cache tag fcs1 nss1
275 status1 bu_todo summary
277 unsafe_set run.sat node_id status2 status0;
281 let _ = loop_td_and_bu (T.root tree) T.nil StateSet.empty in
282 run.pass <- run.pass + 2
285 let get_results run =
286 let cache = IFHTML((List.hd run.sat), run.sat) in
287 let auto = run.auto in
288 let tree = run.tree in
289 let sel_states = Ata.get_selecting_states auto in
290 let rec loop node acc =
291 if node == T.nil then acc
293 let acc0 = loop (T.next_sibling tree node) acc in
294 let acc1 = loop (T.first_child tree node) acc0 in
296 if StateSet.intersect
297 cache.(T.preorder tree node)(* NodeStatus.node.sat *)
298 sel_states then node::acc1
301 loop (T.root tree) []
304 let get_full_results run =
305 let cache = IFHTML((List.hd run.sat), run.sat) in
306 let auto = run.auto in
307 let tree = run.tree in
308 let res_mapper = Hashtbl.create MED_H_SIZE in
311 (fun q -> Hashtbl.add res_mapper q [])
312 (Ata.get_selecting_states auto)
314 let dummy = [ T.nil ] in
315 let res_mapper = Cache.N1.create dummy in
318 (fun q -> Cache.N1.add res_mapper (q :> int) [])
319 (Ata.get_selecting_states auto)
322 if node != T.nil then
323 let () = loop (T.next_sibling tree node) in
324 let () = loop (T.first_child tree node) in
327 let res = Cache.N1.find res_mapper (q :> int) in
329 Cache.N1.add res_mapper (q :> int) (node::res)
331 cache.(T.preorder tree node)(* NodeStatus.node.sat *)
335 (fun q acc -> (q, Cache.N1.find res_mapper (q :> int))::acc)
336 (Ata.get_selecting_states auto) [])
339 let prepare_run run list =
340 let tree = run.tree in
341 let auto = run.auto in
342 let sat = IFHTML((List.hd run.sat), run.sat) in
343 let sat0 = Ata.get_starting_states auto in
344 List.iter (fun node ->
345 let node_id = T.preorder tree node in
346 sat.(node_id) <- sat0) list
348 let tree_size = ref 0
350 let compute_run auto tree nodes =
352 tree_size := T.size tree;
353 let run = make auto tree in
354 prepare_run run nodes;
355 let rank = Ata.get_max_rank auto in
356 while run.pass <= rank do
358 IFHTML((run.sat <- (Array.copy (List.hd run.sat)) :: run.sat), ());
359 run.td_cache <- Cache.N6.create dummy_set;
360 run.bu_cache <- Cache.N6.create dummy_set;
362 pass := Ata.get_max_rank auto + 1;
363 IFHTML(Html.gen_trace auto run.sat (module T : Tree.S with type t = T.t) tree ,());
366 let full_eval auto tree nodes =
367 let r = compute_run auto tree nodes in
370 let eval auto tree nodes =
371 let r = compute_run auto tree nodes in
375 tree_size = !tree_size;
377 fetch_trans_cache_access = !fetch_trans_cache_access;
378 fetch_trans_cache_hit = !fetch_trans_cache_hit;
379 eval_trans_cache_access = !eval_trans_cache_access;
380 eval_trans_cache_hit = !eval_trans_cache_hit;