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 (***********************************************************************)
23 type stats = { mutable pass : int;
26 mutable fetch_trans_cache_access : int;
27 mutable fetch_trans_cache_hit : int;
28 mutable eval_trans_cache_access : int;
29 mutable eval_trans_cache_hit : int;
30 mutable nodes_per_run : int list;
35 (* Pack into an integer the result of the is_* and has_ predicates
47 let is_left (s : t) : bool =
50 let is_right (s : t) : bool =
53 let has_left (s : t) : bool =
56 let has_right (s : t) : bool =
59 let kind (s : t) : Tree.NodeKind.t =
62 let make is_left is_right has_left has_right kind =
63 (int_of_bool is_left) lor
64 ((int_of_bool is_right) lsl 1) lor
65 ((int_of_bool has_left) lsl 2) lor
66 ((int_of_bool has_right) lsl 3) lor
67 ((Obj.magic kind) lsl 4)
70 let dummy_set = StateSet.singleton State.dummy
76 type sat_array = StateSet.t array list
77 DEFINE IFHTML(a,b) = (a)
79 type sat_array = StateSet.t array
80 DEFINE IFHTML(a,b) = (b)
84 if i < 0 then StateSet.empty else
85 Array.unsafe_get (IFHTML(List.hd a, a)) i
87 let unsafe_set a i v old_v =
89 Array.unsafe_set (IFHTML(List.hd a, a)) i v
93 (* The argument of the run *)
95 (* The automaton to be run *)
96 mutable sat: sat_array;
97 (* A mapping from node preorders to states satisfied at that node *)
99 (* Number of run we have performed *)
100 mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
101 (* A cache from states * label to list of transitions *)
102 mutable td_cache : StateSet.t Cache.N6.t;
103 mutable bu_cache : StateSet.t Cache.N6.t;
104 (* Two 6-way caches used during the top-down and bottom-up phase
105 label * self-set * fc-set * ns-set * parent-set * node-shape -> self-set
107 node_summaries: (int, int16_unsigned_elt, c_layout) Array1.t;
111 let dummy_form = Ata.Formula.stay State.dummy
113 let get_form run tag q =
114 let auto = run.auto in
115 let fetch_trans_cache = run.fetch_trans_cache in
116 let stats = run.stats in
118 stats.fetch_trans_cache_access <- stats.fetch_trans_cache_access + 1;
119 Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
121 if phi == dummy_form then
122 let phi = Ata.get_form auto tag q in
126 (tag.QName.id :> int)
130 stats.fetch_trans_cache_hit <- stats.fetch_trans_cache_hit + 1;
135 let eval_form phi fcs nss ps ss summary =
138 begin match Formula.expr phi with
139 | Boolean.False -> false
140 | Boolean.True -> true
141 | Boolean.Atom (a, b) ->
143 let open NodeSummary in
144 match a.Atom.node with
146 b && StateSet.mem q (
149 | `Next_sibling -> nss
150 | `Parent | `Previous_sibling -> ps
153 | Is_first_child -> b == is_left summary
154 | Is_next_sibling -> b == is_right summary
155 | Is k -> b == (k == kind summary)
156 | Has_first_child -> b == has_left summary
157 | Has_next_sibling -> b == has_right summary
159 | Boolean.And(phi1, phi2) -> loop phi1 && loop phi2
160 | Boolean.Or (phi1, phi2) -> loop phi1 || loop phi2
166 let eval_trans_aux run tag summary fcs nss ps sat todo =
167 StateSet.fold (fun q (a_sat) ->
171 if eval_form phi fcs nss ps a_sat summary then
177 let rec eval_trans_fix run tag summary fcs nss ps sat todo =
179 eval_trans_aux run tag summary fcs nss ps sat todo
181 if new_sat == sat then sat else
182 eval_trans_fix run tag summary fcs nss ps new_sat todo
185 let eval_trans run trans_cache tag summary fcs nss ps ss todo =
186 let stats = run.stats in
187 let fcsid = (fcs.StateSet.id :> int) in
188 let nssid = (nss.StateSet.id :> int) in
189 let psid = (ps.StateSet.id :> int) in
190 let ssid = (ss.StateSet.id :> int) in
191 let tagid = (tag.QName.id :> int) in
193 let res = Cache.N6.find trans_cache tagid summary ssid fcsid nssid psid in
194 stats.eval_trans_cache_access <- 1 + stats.eval_trans_cache_access;
195 if res != dummy_set then begin
196 stats.eval_trans_cache_hit <- 1 + stats.eval_trans_cache_hit;
198 end else let new_sat =
199 eval_trans_fix run tag summary fcs nss ps ss todo
201 Cache.N6.add trans_cache tagid summary ssid fcsid nssid psid new_sat;
205 module Make (T : Tree.S) (L : Node_list.S with type node = T.node) =
209 let len = T.size tree in
210 let ba = Array1.create int16_unsigned c_layout len in
215 sat = (let a = Array.create len StateSet.empty in
218 fetch_trans_cache = Cache.N2.create dummy_form;
219 td_cache = Cache.N6.create dummy_set;
220 bu_cache = Cache.N6.create dummy_set;
226 fetch_trans_cache_access = 0;
227 fetch_trans_cache_hit = 0;
228 eval_trans_cache_access = 0;
229 eval_trans_cache_hit = 0;
235 let top_down run update_res =
236 let num_visited = ref 0 in
238 let tree = run.tree in
239 let auto = run.auto in
240 let states_by_rank = Ata.get_states_by_rank auto in
241 let td_todo = states_by_rank.(i) in
243 if i == Array.length states_by_rank - 1 then StateSet.empty
247 let td_only = bu_todo == StateSet.empty in
248 let root = T.root tree in
249 let last_run = i >= Array.length states_by_rank - 2 in
250 let rec loop_td_and_bu node parent parent_sat =
251 if node == T.nil then StateSet.empty
254 let tag = T.tag tree node in
255 let node_id = T.preorder tree node in
256 let fc = T.first_child tree node in
257 let ns = T.next_sibling tree node in
258 (* We enter the node from its parent *)
260 let s = Array1.unsafe_get run.node_summaries node_id in
261 if s != 0 then s else
264 NodeSummary.make false false (fc != T.nil) (ns != T.nil) (T.kind tree node)
266 let is_left = node == T.first_child tree parent in
270 (fc != T.nil) (* has_left *)
271 (ns != T.nil) (* has_right *)
272 (T.kind tree node) (* kind *)
274 run.node_summaries.{node_id} <- s; s
276 let status0 = unsafe_get run.sat node_id in
277 (* get the node_statuses for the first child, next sibling and parent *)
278 (* evaluate the transitions with all this statuses *)
283 (unsafe_get run.sat (T.preorder tree fc))
284 (unsafe_get run.sat (T.preorder tree ns))
288 (* update the cache if the status of the node changed
289 unsafe_set run.sat node_id status1 status0;*)
290 if td_only then begin
291 unsafe_set run.sat node_id status1 status0; (* write the td_states *)
292 update_res false status1 node;
293 let _ = loop_td_and_bu fc node status1 in
294 loop_td_and_bu ns node status1 (* tail call *)
298 let nss1 = loop_td_and_bu ns node status1 in
299 let fcs1 = loop_td_and_bu fc node status1 in
302 let fcs1 = loop_td_and_bu fc node status1 in
303 let nss1 = loop_td_and_bu ns node status1 in
307 eval_trans run run.bu_cache tag
313 unsafe_set run.sat node_id status2 status0;
314 if last_run && status2 != StateSet.empty then update_res true status2 node;
318 let _ = loop_td_and_bu (T.root tree) T.nil dummy_set in
319 run.pass <- run.pass + 2;
320 run.stats.pass <- run.stats.pass + 1;
321 run.stats.nodes_per_run <- !num_visited :: run.stats.nodes_per_run
325 let mk_update_result auto =
326 let sel_states = Ata.get_selecting_states auto in
327 let res = L.create () in
328 (fun prepend sat node ->
329 if StateSet.intersect sel_states sat then begin
330 if prepend then L.push_front node res else
336 let mk_update_full_result auto =
337 let dummy = L.create () in
338 let res_mapper = Cache.N1.create dummy in
341 (fun q -> Cache.N1.add res_mapper (q :> int) (L.create()))
342 (Ata.get_selecting_states auto)
344 (fun prepend sat node ->
347 let res = Cache.N1.find res_mapper (q :> int) in
348 if res != dummy then begin
349 if prepend then L.push_front node res
350 else L.push_back node res
355 (fun q acc -> (q, Cache.N1.find res_mapper (q :> int))::acc)
356 (Ata.get_selecting_states auto) [])
358 let prepare_run run list =
359 let tree = run.tree in
360 let auto = run.auto in
361 let sat = IFHTML((List.hd run.sat), run.sat) in
362 let sat0 = Ata.get_starting_states auto in
364 let node_id = T.preorder tree node in
365 sat.(node_id) <- sat0) list
368 let compute_run auto tree nodes update_res =
369 let run = make auto tree in
370 prepare_run run nodes;
371 let rank = Ata.get_max_rank auto in
372 while run.pass <= rank do
373 top_down run update_res;
374 IFHTML((run.sat <- (Array.copy (List.hd run.sat)) :: run.sat), ());
375 run.td_cache <- Cache.N6.create dummy_set;
376 run.bu_cache <- Cache.N6.create dummy_set;
378 IFHTML((run.sat <- List.tl run.sat), ());
379 IFHTML(Html_trace.gen_trace auto run.sat (module T : Tree.S with type t = T.t) tree ,());
383 let last_stats = ref []
385 let full_eval auto tree nodes =
386 let update_full,get_full = mk_update_full_result auto in
387 let run = compute_run auto tree nodes update_full in
388 last_stats := run.stats :: !last_stats;
391 let eval auto tree nodes =
392 let update_res,get_res = mk_update_result auto in
393 let run = compute_run auto tree nodes update_res in
394 last_stats := run.stats :: !last_stats;
397 let stats () = match !last_stats with
398 _ :: _ as l -> List.iter (fun s -> s.nodes_per_run <- List.rev s.nodes_per_run) l; List.rev l
399 | [] -> failwith "Missing stats"