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;
32 let dummy_set = StateSet.singleton State.dummy_state
38 type sat_array = StateSet.t array list
39 DEFINE IFHTML(a,b) = (a)
41 type sat_array = StateSet.t array
42 DEFINE IFHTML(a,b) = (b)
46 if i < 0 then StateSet.empty else
47 Array.unsafe_get (IFHTML(List.hd a, a)) i
49 let unsafe_set a i v =
50 (* if v != old_v then *)
51 Array.unsafe_set (IFHTML(List.hd a, a)) i v
53 type 'node td_action = 'node -> int -> 'node -> 'node -> StateSet.t
54 type 'node bu_action = 'node -> int -> StateSet.t
55 let dummy_action = fun _ _ -> assert false
57 type ('tree, 'node) run = {
59 (* The argument of the run *)
61 (* The automaton to be run *)
62 mutable sat: sat_array;
63 (* A mapping from node preorders to states satisfied at that node *)
65 (* Number of run we have performed *)
66 mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
67 (* A cache from states * label to list of transitions *)
68 mutable td_cache : 'node td_action Cache.N6.t;
69 mutable bu_cache : 'node bu_action Cache.N6.t;
70 (* Two 6-way caches used during the top-down and bottom-up phase
71 label * self-set * fc-set * ns-set * parent-set * node-shape -> self-set
76 let dummy_form = Ata.Formula.stay State.dummy_state
78 let get_form run tag (q : State.t) =
79 let auto = run.auto in
80 let fetch_trans_cache = run.fetch_trans_cache in
81 let stats = run.stats in
83 stats.fetch_trans_cache_access <- stats.fetch_trans_cache_access + 1;
84 Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
86 if phi == dummy_form then
87 let phi = Ata.get_form auto tag q in
89 stats.fetch_trans_cache_miss <- stats.fetch_trans_cache_miss + 1;
100 let eval_form phi fcs nss ps ss summary =
103 begin match Formula.expr phi with
104 | Boolean.False -> false
105 | Boolean.True -> true
106 | Boolean.Atom (a, b) ->
108 let open Tree.NodeSummary in
109 match a.Atom.node with
111 b && StateSet.mem q (
114 | `Next_sibling -> nss
115 | `Parent | `Previous_sibling -> ps
118 | Is_first_child -> b == is_left summary
119 | Is_next_sibling -> b == is_right summary
120 | Is k -> b == (k == kind summary)
121 | Has_first_child -> b == has_left summary
122 | Has_next_sibling -> b == has_right summary
124 | Boolean.And(phi1, phi2) -> loop phi1 && loop phi2
125 | Boolean.Or (phi1, phi2) -> loop phi1 || loop phi2
131 let eval_trans_aux run tag summary fcs nss ps sat todo =
132 StateSet.fold (fun q (a_sat) ->
136 if eval_form phi fcs nss ps a_sat summary then
142 let rec eval_trans_fix run tag summary fcs nss ps sat todo =
144 eval_trans_aux run tag summary fcs nss ps sat todo
146 if new_sat == sat then sat else
147 eval_trans_fix run tag summary fcs nss ps new_sat todo
150 let eval_trans run trans_cache tag summary fcs nss ps ss todo action_builder =
151 let stats = run.stats in
152 let fcsid = (fcs.StateSet.id :> int) in
153 let nssid = (nss.StateSet.id :> int) in
154 let psid = (ps.StateSet.id :> int) in
155 let ssid = (ss.StateSet.id :> int) in
156 let tagid = (tag.QName.id :> int) in
158 let res = Cache.N6.find trans_cache tagid summary ssid fcsid nssid psid in
159 stats.eval_trans_cache_access <- 1 + stats.eval_trans_cache_access;
160 if res != dummy_action then
164 eval_trans_fix run tag summary fcs nss ps ss todo
166 stats.eval_trans_cache_miss <- 1 + stats.eval_trans_cache_miss;
167 let new_action = action_builder ps summary tag new_sat in
168 Cache.N6.add trans_cache tagid summary ssid fcsid nssid psid new_action;
171 module NodeSummary = Tree.NodeSummary
172 module Make (T : Tree.S) =
174 module Tree : Tree.S with type node = T.node = T
175 module ResultSet : Deque.S with type elem = Tree.node =
176 Deque.Make (struct type t = Tree.node end)
180 let len = Tree.size tree in
184 sat = (let a = Array.make len StateSet.empty in
187 fetch_trans_cache = Cache.N2.create dummy_form;
188 td_cache = Cache.N6.create dummy_action;
189 bu_cache = Cache.N6.create dummy_action;
193 fetch_trans_cache_access = 0;
194 fetch_trans_cache_miss = 0;
195 eval_trans_cache_access = 0;
196 eval_trans_cache_miss = 0;
202 let top_down run mk_update_res =
203 let num_visited = ref 0 in
205 let tree = run.tree in
206 let auto = run.auto in
207 let states_by_rank = Ata.get_states_by_rank auto in
208 let td_todo = states_by_rank.(i) in
210 if i == Array.length states_by_rank - 1 then StateSet.empty
214 let run_sat = run.sat in
215 let last_run = i >= Array.length states_by_rank - 2 in
217 let rec common node parent parent_sat action_builder =
220 let tag = Tree.tag tree node in
221 let node_id = Tree.preorder tree node in
222 let summary = Tree.summary tree node in
223 let fc = Tree.first_child tree node in
224 let ns = Tree.next_sibling tree node in
225 (* We enter the node from its parent *)
226 let status0 = unsafe_get run_sat node_id in
227 (* get the node_statuses for the first child, next sibling and parent *)
228 (* evaluate the transitions with all this statuses *)
233 (unsafe_get run_sat (Tree.preorder tree fc))
234 (unsafe_get run_sat (Tree.preorder tree ns))
236 status0 td_todo action_builder
238 action node node_id fc ns
241 and td_action_builder parent_sat summary tag status1 =
242 let update_res = mk_update_res false status1 in
243 match NodeSummary.(has_left summary, has_right summary) with
245 (fun node node_id fc ns ->
246 unsafe_set run_sat node_id status1;
250 (fun node node_id fc ns ->
251 unsafe_set run_sat node_id status1;
253 loop_td fc node status1)
255 (fun node node_id fc ns ->
256 unsafe_set run_sat node_id status1;
258 loop_td ns node status1)
260 (fun node node_id fc ns ->
261 unsafe_set run_sat node_id status1; (* write the td_states *)
263 ignore (loop_td fc node status1);
264 loop_td ns node status1 (* tail call *)
266 and td_and_bu_action_builder parent_sat summary tag status1 =
267 match NodeSummary.(has_left summary, has_right summary) with
269 (fun node node_id fc ns ->
271 eval_trans run run.bu_cache tag summary StateSet.empty StateSet.empty
272 parent_sat status1 bu_todo bu_action_builder
276 (fun node node_id fc ns ->
277 let fcs1 = loop_td_and_bu fc node status1 in
279 eval_trans run run.bu_cache tag summary fcs1 StateSet.empty
280 parent_sat status1 bu_todo bu_action_builder
285 (fun node node_id fc ns ->
286 let nss1 = loop_td_and_bu ns node status1 in
288 eval_trans run run.bu_cache tag summary StateSet.empty nss1
289 parent_sat status1 bu_todo bu_action_builder
294 (fun node node_id fc ns ->
295 let fcs1 = loop_td_and_bu fc node status1 in
296 let nss1 = loop_td_and_bu ns node status1 in
298 eval_trans run run.bu_cache tag summary fcs1 nss1 parent_sat status1 bu_todo bu_action_builder
302 and bu_action_builder parent_sat summary tag status2 node node_id =
303 unsafe_set run_sat node_id status2;
305 and td_and_bu_last_action_builder parent_sat summary tag status1 =
306 match NodeSummary.(has_left summary, has_right summary) with
308 (fun node node_id fc ns ->
310 eval_trans run run.bu_cache tag summary StateSet.empty StateSet.empty
311 parent_sat status1 bu_todo bu_last_action_builder
315 (fun node node_id fc ns ->
316 let fcs1 = loop_td_and_bu_last fc node status1 in
318 eval_trans run run.bu_cache tag summary fcs1 StateSet.empty
319 parent_sat status1 bu_todo bu_last_action_builder
324 (fun node node_id fc ns ->
325 let nss1 = loop_td_and_bu_last ns node status1 in
327 eval_trans run run.bu_cache tag summary StateSet.empty nss1
328 parent_sat status1 bu_todo bu_last_action_builder
333 (fun node node_id fc ns ->
334 let nss1 = loop_td_and_bu_last ns node status1 in
335 let fcs1 = loop_td_and_bu_last fc node status1 in
337 eval_trans run run.bu_cache tag summary fcs1 nss1 parent_sat status1 bu_todo bu_last_action_builder
341 and bu_last_action_builder parent_sat summary tag status2 =
342 let update_res = mk_update_res true status2 in
344 unsafe_set run_sat node_id status2;
347 and loop_td node parent parent_sat =
348 common node parent parent_sat td_action_builder
349 and loop_td_and_bu node parent parent_sat =
350 common node parent parent_sat td_and_bu_action_builder
351 and loop_td_and_bu_last node parent parent_sat =
352 common node parent parent_sat td_and_bu_last_action_builder
355 if bu_todo == StateSet.empty then
356 loop_td (Tree.root tree) Tree.nil dummy_set
357 else if last_run then
358 loop_td_and_bu_last (Tree.root tree) Tree.nil dummy_set
360 loop_td_and_bu (Tree.root tree) Tree.nil dummy_set
362 run.pass <- run.pass + 2;
363 run.stats.pass <- run.stats.pass + 1;
364 run.stats.nodes_per_run <- !num_visited :: run.stats.nodes_per_run
368 let mk_update_result auto =
369 let sel_states = Ata.get_selecting_states auto in
370 let res = ResultSet.create () in
372 if StateSet.intersect sat sel_states then
373 if prepend then (fun n -> ResultSet.push_front n res)
374 else (fun n -> ResultSet.push_back n res)
379 let mk_update_full_result auto =
380 let sel_states = Ata.get_selecting_states auto in
382 StateSet.fold_right (fun q acc -> (q, ResultSet.create())::acc) sel_states []
385 StateSet.fold (fun q f_acc ->
386 if StateSet.mem q sel_states then
387 let res = List.assoc q res_mapper in
388 if prepend then (fun n -> ResultSet.push_front n res; f_acc n)
389 else (fun n -> ResultSet.push_back n res; f_acc n)
390 else f_acc) sat (fun _ -> ())),
391 (fun () -> res_mapper)
393 let prepare_run run list =
394 let tree = run.tree in
395 let auto = run.auto in
396 let sat = IFHTML((List.hd run.sat), run.sat) in
397 let sat0 = Ata.get_starting_states auto in
398 ResultSet.iter (fun node ->
399 let node_id = Tree.preorder tree node in
400 sat.(node_id) <- sat0) list
403 let compute_run auto tree nodes mk_update_res =
404 let run = make auto tree in
405 prepare_run run nodes;
406 let rank = Ata.get_max_rank auto in
407 while run.pass <= rank do
408 top_down run mk_update_res;
409 IFHTML((run.sat <- (Array.copy (List.hd run.sat)) :: run.sat), ());
410 run.td_cache <- Cache.N6.create dummy_action;
411 run.bu_cache <- Cache.N6.create dummy_action;
413 IFHTML((run.sat <- List.tl run.sat), ());
414 IFHTML(Html_trace.gen_trace auto run.sat (module T : Tree.S with type t = Tree.t) tree ,());
418 let last_stats = ref None
420 let full_eval auto tree nodes =
421 let mk_update_full,get_full = mk_update_full_result auto in
422 let run = compute_run auto tree nodes mk_update_full in
423 last_stats := Some run.stats;
426 let eval auto tree nodes =
427 let mk_update_res,get_res = mk_update_result auto in
428 let run = compute_run auto tree nodes mk_update_res in
429 last_stats := Some run.stats;
432 let stats () = match !last_stats with
433 Some s -> s.nodes_per_run <- List.rev s.nodes_per_run;s
434 | None -> failwith "Missing stats"