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 = { mutable pass : int;
24 mutable fetch_trans_cache_access : int;
25 mutable fetch_trans_cache_miss : int;
26 mutable eval_trans_cache_access : int;
27 mutable eval_trans_cache_miss : int;
28 mutable nodes_per_run : int list;
33 (* Pack into an integer the result of the is_* and has_ predicates
45 let is_left (s : t) : bool =
48 let is_right (s : t) : bool =
51 let has_left (s : t) : bool =
54 let has_right (s : t) : bool =
57 let kind (s : t) : Tree.NodeKind.t =
60 let make is_left is_right has_left has_right kind =
61 (int_of_bool is_left) lor
62 ((int_of_bool is_right) lsl 1) lor
63 ((int_of_bool has_left) lsl 2) lor
64 ((int_of_bool has_right) lsl 3) lor
65 ((Obj.magic kind) lsl 4)
68 let dummy_set = StateSet.singleton State.dummy_state
74 type sat_array = StateSet.t array list
75 DEFINE IFHTML(a,b) = (a)
77 type sat_array = StateSet.t array
78 DEFINE IFHTML(a,b) = (b)
82 if i < 0 then StateSet.empty else
83 Array.unsafe_get (IFHTML(List.hd a, a)) i
85 let unsafe_set a i v =
86 (* if v != old_v then *)
87 Array.unsafe_set (IFHTML(List.hd a, a)) i v
89 type 'node td_action = 'node -> int -> 'node -> 'node -> StateSet.t
90 type 'node bu_action = 'node -> int -> StateSet.t
91 let dummy_action = fun _ _ -> assert false
93 type ('tree, 'node) run = {
95 (* The argument of the run *)
97 (* The automaton to be run *)
98 mutable sat: sat_array;
99 (* A mapping from node preorders to states satisfied at that node *)
101 (* Number of run we have performed *)
102 mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
103 (* A cache from states * label to list of transitions *)
104 mutable td_cache : 'node td_action Cache.N6.t;
105 mutable bu_cache : 'node bu_action Cache.N6.t;
106 (* Two 6-way caches used during the top-down and bottom-up phase
107 label * self-set * fc-set * ns-set * parent-set * node-shape -> self-set
109 node_summaries: Bytes.t;
113 let dummy_form = Ata.Formula.stay State.dummy_state
115 let get_form run tag (q : State.t) =
116 let auto = run.auto in
117 let fetch_trans_cache = run.fetch_trans_cache in
118 let stats = run.stats in
120 stats.fetch_trans_cache_access <- stats.fetch_trans_cache_access + 1;
121 Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
123 if phi == dummy_form then
124 let phi = Ata.get_form auto tag q in
126 stats.fetch_trans_cache_miss <- stats.fetch_trans_cache_miss + 1;
129 (tag.QName.id :> int)
137 let eval_form phi fcs nss ps ss summary =
140 begin match Formula.expr phi with
141 | Boolean.False -> false
142 | Boolean.True -> true
143 | Boolean.Atom (a, b) ->
145 let open NodeSummary in
146 match a.Atom.node with
148 b && StateSet.mem q (
151 | `Next_sibling -> nss
152 | `Parent | `Previous_sibling -> ps
155 | Is_first_child -> b == is_left summary
156 | Is_next_sibling -> b == is_right summary
157 | Is k -> b == (k == kind summary)
158 | Has_first_child -> b == has_left summary
159 | Has_next_sibling -> b == has_right summary
161 | Boolean.And(phi1, phi2) -> loop phi1 && loop phi2
162 | Boolean.Or (phi1, phi2) -> loop phi1 || loop phi2
168 let eval_trans_aux run tag summary fcs nss ps sat todo =
169 StateSet.fold (fun q (a_sat) ->
173 if eval_form phi fcs nss ps a_sat summary then
179 let rec eval_trans_fix run tag summary fcs nss ps sat todo =
181 eval_trans_aux run tag summary fcs nss ps sat todo
183 if new_sat == sat then sat else
184 eval_trans_fix run tag summary fcs nss ps new_sat todo
187 let eval_trans run trans_cache tag summary fcs nss ps ss todo action_builder =
188 let stats = run.stats in
189 let fcsid = (fcs.StateSet.id :> int) in
190 let nssid = (nss.StateSet.id :> int) in
191 let psid = (ps.StateSet.id :> int) in
192 let ssid = (ss.StateSet.id :> int) in
193 let tagid = (tag.QName.id :> int) in
195 let res = Cache.N6.find trans_cache tagid summary ssid fcsid nssid psid in
196 stats.eval_trans_cache_access <- 1 + stats.eval_trans_cache_access;
197 if res != dummy_action then
201 eval_trans_fix run tag summary fcs nss ps ss todo
203 stats.eval_trans_cache_miss <- 1 + stats.eval_trans_cache_miss;
204 let new_action = action_builder ps summary tag new_sat in
205 Cache.N6.add trans_cache tagid summary ssid fcsid nssid psid new_action;
209 module Make (T : Tree.S) =
211 module Tree : Tree.S with type node = T.node = T
212 module ResultSet : Deque.S with type elem = Tree.node =
213 Deque.Make (struct type t = Tree.node end)
217 let len = Tree.size tree in
218 let ba = Bytes.make len '\000' in
222 sat = (let a = Array.make len StateSet.empty in
225 fetch_trans_cache = Cache.N2.create dummy_form;
226 td_cache = Cache.N6.create dummy_action;
227 bu_cache = Cache.N6.create dummy_action;
232 fetch_trans_cache_access = 0;
233 fetch_trans_cache_miss = 0;
234 eval_trans_cache_access = 0;
235 eval_trans_cache_miss = 0;
241 let top_down run mk_update_res =
242 let num_visited = ref 0 in
244 let tree = run.tree in
245 let auto = run.auto in
246 let states_by_rank = Ata.get_states_by_rank auto in
247 let td_todo = states_by_rank.(i) in
249 if i == Array.length states_by_rank - 1 then StateSet.empty
253 let run_sat = run.sat in
254 let last_run = i >= Array.length states_by_rank - 2 in
256 let rec common node parent parent_sat action_builder =
257 if node == Tree.nil then StateSet.empty
260 let tag = Tree.tag tree node in
261 let node_id = Tree.preorder tree node in
262 let fc = Tree.first_child tree node in
263 let ns = Tree.next_sibling tree node in
264 (* We enter the node from its parent *)
266 let s = Char.code (Bytes.unsafe_get run.node_summaries node_id) in
267 if s != 0 then s else
270 (node == (Tree.first_child tree parent)) (*is_left *)
271 (node == (Tree.next_sibling tree parent)) (*is_right *)
272 (fc != Tree.nil) (* has_left *)
273 (ns != Tree.nil) (* has_right *)
274 (Tree.kind tree node) (* kind *)
276 Bytes.unsafe_set run.node_summaries node_id (Char.chr s); s
278 let status0 = unsafe_get run_sat node_id in
279 (* get the node_statuses for the first child, next sibling and parent *)
280 (* evaluate the transitions with all this statuses *)
285 (unsafe_get run_sat (Tree.preorder tree fc))
286 (unsafe_get run_sat (Tree.preorder tree ns))
288 status0 td_todo action_builder
290 action node node_id fc ns
293 and td_action_builder parent_sat summary tag status1 =
294 let update_res = mk_update_res false status1 in
295 match NodeSummary.(has_left summary, has_right summary) with
297 (fun node node_id fc ns ->
298 unsafe_set run_sat node_id status1;
302 (fun node node_id fc ns ->
303 unsafe_set run_sat node_id status1;
305 loop_td fc node status1)
307 (fun node node_id fc ns ->
308 unsafe_set run_sat node_id status1;
310 loop_td ns node status1)
312 (fun node node_id fc ns ->
313 unsafe_set run_sat node_id status1; (* write the td_states *)
315 ignore (loop_td fc node status1);
316 loop_td ns node status1 (* tail call *)
318 and td_and_bu_action_builder parent_sat summary tag status1 node node_id fc ns =
319 let fcs1 = loop_td_and_bu fc node status1 in
320 let nss1 = loop_td_and_bu ns node status1 in
322 eval_trans run run.bu_cache tag summary fcs1 nss1 parent_sat status1 bu_todo bu_action_builder
325 and bu_action_builder parent_sat summary tag status2 node node_id =
326 unsafe_set run_sat node_id status2;
328 and td_and_bu_last_action_builder parent_sat summary tag status1 node node_id fc ns =
329 let nss1 = loop_td_and_bu_last ns node status1 in
330 let fcs1 = loop_td_and_bu_last fc node status1 in
332 eval_trans run run.bu_cache tag summary fcs1 nss1 parent_sat status1 bu_todo bu_last_action_builder
335 and bu_last_action_builder parent_sat summary tag status2 =
336 let update_res = mk_update_res true status2 in
338 unsafe_set run_sat node_id status2;
341 and loop_td node parent parent_sat =
342 common node parent parent_sat td_action_builder
343 and loop_td_and_bu node parent parent_sat =
344 common node parent parent_sat td_and_bu_action_builder
345 and loop_td_and_bu_last node parent parent_sat =
346 common node parent parent_sat td_and_bu_last_action_builder
349 if bu_todo == StateSet.empty then
350 loop_td (Tree.root tree) Tree.nil dummy_set
351 else if last_run then
352 loop_td_and_bu_last (Tree.root tree) Tree.nil dummy_set
354 loop_td_and_bu (Tree.root tree) Tree.nil dummy_set
356 run.pass <- run.pass + 2;
357 run.stats.pass <- run.stats.pass + 1;
358 run.stats.nodes_per_run <- !num_visited :: run.stats.nodes_per_run
362 let mk_update_result auto =
363 let sel_states = Ata.get_selecting_states auto in
364 let res = ResultSet.create () in
366 if StateSet.intersect sat sel_states then
367 if prepend then (fun n -> ResultSet.push_front n res)
368 else (fun n -> ResultSet.push_back n res)
373 let mk_update_full_result auto =
374 let sel_states = Ata.get_selecting_states auto in
376 StateSet.fold_right (fun q acc -> (q, ResultSet.create())::acc) sel_states []
379 StateSet.fold (fun q f_acc ->
380 if StateSet.mem q sel_states then
381 let res = List.assoc q res_mapper in
382 if prepend then (fun n -> ResultSet.push_front n res; f_acc n)
383 else (fun n -> ResultSet.push_front n res; f_acc n)
384 else f_acc) sat (fun _ -> ())),
385 (fun () -> res_mapper)
387 let prepare_run run list =
388 let tree = run.tree in
389 let auto = run.auto in
390 let sat = IFHTML((List.hd run.sat), run.sat) in
391 let sat0 = Ata.get_starting_states auto in
392 ResultSet.iter (fun node ->
393 let node_id = Tree.preorder tree node in
394 sat.(node_id) <- sat0) list
397 let compute_run auto tree nodes mk_update_res =
398 let run = make auto tree in
399 prepare_run run nodes;
400 let rank = Ata.get_max_rank auto in
401 while run.pass <= rank do
402 top_down run mk_update_res;
403 IFHTML((run.sat <- (Array.copy (List.hd run.sat)) :: run.sat), ());
404 run.td_cache <- Cache.N6.create dummy_action;
405 run.bu_cache <- Cache.N6.create dummy_action;
407 IFHTML((run.sat <- List.tl run.sat), ());
408 IFHTML(Html_trace.gen_trace auto run.sat (module T : Tree.S with type t = Tree.t) tree ,());
412 let last_stats = ref None
414 let full_eval auto tree nodes =
415 let mk_update_full,get_full = mk_update_full_result auto in
416 let run = compute_run auto tree nodes mk_update_full in
417 last_stats := Some run.stats;
420 let eval auto tree nodes =
421 let mk_update_res,get_res = mk_update_result auto in
422 let run = compute_run auto tree nodes mk_update_res in
423 last_stats := Some run.stats;
426 let stats () = match !last_stats with
427 Some s -> s.nodes_per_run <- List.rev s.nodes_per_run;s
428 | None -> failwith "Missing stats"