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 (***********************************************************************)
20 module Make (T : Tree.S) =
25 (* Pack into an integer the result of the is_* and has_ predicates
38 let has_right (s : t) : bool =
41 let has_left (s : t) : bool =
42 Obj.magic ((s lsr 1) land 1)
44 let is_right (s : t) : bool =
45 Obj.magic ((s lsr 2) land 1)
47 let is_left (s : t) : bool =
48 Obj.magic ((s lsr 3) land 1)
50 let kind (s : t) : Tree.NodeKind.t =
53 let make is_left is_right has_left has_right kind =
54 ((Obj.magic kind) lsl 4) lor
55 ((int_of_bool is_left) lsl 3) lor
56 ((int_of_bool is_right) lsl 2) lor
57 ((int_of_bool has_left) lsl 1) lor
58 (int_of_bool has_right)
65 todo : Ata.TransList.t;
66 summary : NodeSummary.t;
68 (* Describe what is kept at each node for a run *)
70 module NodeStatus = Hcons.Make(struct
77 c.summary == d.summary
80 HASHINT4((c.sat.StateSet.id :> int),
81 (c.unsat.StateSet.id :> int),
82 (c.todo.Ata.TransList.id :> int),
88 NodeStatus.make { sat = StateSet.empty;
89 unsat = StateSet.empty;
90 todo = Ata.TransList.nil;
91 summary = NodeSummary.dummy;
97 (* The argument of the run *)
99 (* The automaton to be run *)
100 status : NodeStatus.t array;
101 (* A mapping from node preorders to NodeStatus *)
102 unstable : Bitvector.t;
103 (* A bitvector remembering whether a subtree is stable *)
105 (* A boolean indicating whether the run is incomplete *)
107 (* The number of times this run was updated *)
108 mutable cache2 : Ata.TransList.t Cache.N2.t;
109 (* A cache from states * label to list of transitions *)
110 mutable cache4 : NodeStatus.t Cache.N4.t;
114 let stable r = not r.redo
122 (State.dummy,QNameSet.empty, Formula.false_))
126 let len = T.size tree in
130 status = Array.create len dummy_status;
131 unstable = Bitvector.create ~init:true len;
134 cache2 = Cache.N2.create dummy_trl;
135 cache4 = Cache.N4.create dummy_status;
139 if i < 0 then dummy_status else Array.get a i
141 let unsafe_get_status a i =
142 if i < 0 then dummy_status else Array.unsafe_get a i
146 DEFINE TRACE(e) = (e)
151 let html tree node i config msg =
152 let config = config.NodeStatus.node in
153 Html.trace (T.preorder tree node) i
154 "node: %i<br/>%s<br/>sat: %a<br/>unsat: %a<br/>todo: %around: %i<br/>"
155 (T.preorder tree node)
157 StateSet.print config.sat
158 StateSet.print config.unsat
159 (Ata.TransList.print ~sep:"<br/>") config.todo i
162 let debug msg tree node i config =
163 let config = config.NodeStatus.node in
165 "DEBUG:%s node: %i\nsat: %a\nunsat: %a\ntodo: %around: %i\n"
167 (T.preorder tree node)
168 StateSet.print config.sat
169 StateSet.print config.unsat
170 (Ata.TransList.print ~sep:"\n") config.todo i
173 let get_trans cache2 auto tag states =
176 (tag.QName.id :> int) (states.StateSet.id :> int)
178 if trs == dummy_trl then
179 let trs = Ata.get_trans auto tag states in
182 (tag.QName.id :> int)
183 (states.StateSet.id :> int) trs; trs)
188 let simplify_atom atom pos q { NodeStatus.node = status; _ } =
189 if (pos && StateSet.mem q status.sat)
190 || ((not pos) && StateSet.mem q status.unsat) then Ata.Formula.true_
191 else if (pos && StateSet.mem q status.unsat)
192 || ((not pos) && StateSet.mem q status.sat) then Ata.Formula.false_
196 let eval_form phi fcs nss ps ss summary =
199 begin match Formula.expr phi with
200 Boolean.True | Boolean.False -> phi
201 | Boolean.Atom (a, b) ->
203 let open NodeSummary in
204 match a.Atom.node with
206 let states = match m with
208 | `Next_sibling -> nss
209 | `Parent | `Previous_sibling -> ps
211 in simplify_atom phi b q states
212 | Is_first_child -> Formula.of_bool (b == is_left summary)
213 | Is_next_sibling -> Formula.of_bool (b == is_right summary)
214 | Is k -> Formula.of_bool (b == (k == kind summary))
215 | Has_first_child -> Formula.of_bool (b == has_left summary)
216 | Has_next_sibling -> Formula.of_bool (b == has_right summary)
218 | Boolean.And(phi1, phi2) -> Formula.and_ (loop phi1) (loop phi2)
219 | Boolean.Or (phi1, phi2) -> Formula.or_ (loop phi1) (loop phi2)
225 let eval_trans_aux cache4 fcs nss ps ss old_config =
229 summary = old_summary } = old_config.NodeStatus.node
231 let sat, unsat, removed, kept, todo =
234 let q, lab, phi = Ata.Transition.node trs in
235 let a_sat, a_unsat, a_rem, a_kept, a_todo = acc in
236 if StateSet.mem q a_sat || StateSet.mem q a_unsat then acc else
238 eval_form phi fcs nss ps old_config old_summary
240 if Ata.Formula.is_true new_phi then
241 StateSet.add q a_sat, a_unsat, StateSet.add q a_rem, a_kept, a_todo
242 else if Ata.Formula.is_false new_phi then
243 a_sat, StateSet.add q a_unsat, StateSet.add q a_rem, a_kept, a_todo
245 let new_tr = Ata.Transition.make (q, lab, new_phi) in
246 (a_sat, a_unsat, a_rem, StateSet.add q a_kept, (Ata.TransList.cons new_tr a_todo))
247 ) old_todo (old_sat, old_unsat, StateSet.empty, StateSet.empty, Ata.TransList.nil)
249 (* States that have been removed from the todo list and not kept are now
251 let unsat = StateSet.union unsat (StateSet.diff removed kept) in
252 (* States that were found once to be satisfiable remain so *)
253 let unsat = StateSet.diff unsat sat in
254 let new_config = NodeStatus.make { old_config.NodeStatus.node with sat; unsat; todo; } in
258 let eval_trans cache4 fcs nss ps ss =
259 let fcsid = (fcs.NodeStatus.id :> int) in
260 let nssid = (nss.NodeStatus.id :> int) in
261 let psid = (ps.NodeStatus.id :> int) in
262 let rec loop old_config =
263 let oid = (old_config.NodeStatus.id :> int) in
265 let res = Cache.N4.find cache4 oid fcsid nssid psid in
266 if res != dummy_status then res
269 eval_trans_aux cache4 fcs nss ps ss old_config
271 Cache.N4.add cache4 oid fcsid nssid psid new_config;
274 if res == old_config then res else loop res
282 let tree = run.tree in
283 let auto = run.auto in
284 let status = run.status in
285 let cache2 = run.cache2 in
286 let cache4 = run.cache4 in
287 let unstable = run.unstable in
289 let node_id = T.preorder tree node in
290 if node == T.nil || not (Bitvector.get unstable node_id) then false else begin
291 let parent = T.parent tree node in
292 let fc = T.first_child tree node in
293 let fc_id = T.preorder tree fc in
294 let ns = T.next_sibling tree node in
295 let ns_id = T.preorder tree ns in
296 let tag = T.tag tree node in
297 (* We enter the node from its parent *)
300 let c = unsafe_get_status status node_id in
301 if c == dummy_status then
302 (* first time we visit the node *)
303 let ltrs = get_trans cache2 auto tag (Ata.get_states auto) in
305 { sat = StateSet.empty;
306 unsat = Ata.get_starting_states auto;
308 summary = NodeSummary.make
309 (node == T.first_child tree parent) (* is_left *)
310 (node == T.next_sibling tree parent) (* is_right *)
311 (fc != T.nil) (* has_left *)
312 (ns != T.nil) (* has_right *)
313 (T.kind tree node) (* kind *)
318 TRACE(html tree node _i config0 "Entering node");
320 (* get the node_statuses for the first child, next sibling and parent *)
321 let ps = unsafe_get_status status (T.preorder tree parent) in
322 let fcs = unsafe_get_status status fc_id in
323 let nss = unsafe_get_status status ns_id in
324 (* evaluate the transitions with all this statuses *)
325 let status1 = eval_trans cache4 fcs nss ps status0 in
327 TRACE(html tree node _i config1 "Updating transitions");
329 (* update the cache if the status of the node changed *)
331 if status1 != status0 then status.(node_id) <- status1;
332 (* recursively traverse the first child *)
333 let unstable_left = loop fc in
334 (* here we re-enter the node from its first child,
335 get the new status of the first child *)
336 let fcs1 = unsafe_get_status status fc_id in
337 (* update the status *)
338 let status2 = eval_trans cache4 fcs1 nss ps status1 in
340 TRACE(html tree node _i config2 "Updating transitions (after first-child)");
342 if status2 != status1 then status.(node_id) <- status2;
343 let unstable_right = loop ns in
344 let nss1 = unsafe_get_status status ns_id in
345 let status3 = eval_trans cache4 fcs1 nss1 ps status2 in
347 TRACE(html tree node _i config3 "Updating transitions (after next-sibling)");
349 if status3 != status2 then status.(node_id) <- status3;
352 (* if either our left or right child is unstable or if we still have transitions
353 pending, the current node is unstable *)
356 || Ata.TransList.nil != status3.NodeStatus.node.todo
358 Bitvector.unsafe_set unstable node_id unstable_self;
359 TRACE((if not unstable_self then
363 Ata.(StateSet.intersect config3.Config.node.sat auto.selection_states)));
367 run.redo <- loop (T.root tree);
368 run.pass <- run.pass + 1
373 let len = Bitvector.length run.unstable in
374 for i = 0 to len - 1 do
375 if not (Bitvector.unsafe_get run.unstable i) then
379 "%i nodes over %i were skipped in iteration %i (%.2f %%), redo is: %b"
380 !count len run.pass (100. *. (float !count /. float len))
384 let eval auto tree node =
385 let len = T.size tree in
386 let run = { config = Array.create len Ata.dummy_config;
387 unstable = Bitvector.create ~init:true len;
394 Ata.reset auto; (* prevents the .cache2 and .cache4 memoization tables from growing too much *)
395 run.redo <- top_down_run auto tree node run;
397 run.pass <- run.pass + 1;
399 at_exit (fun () -> Logger.msg `STATS "%i iterations" run.pass);
400 at_exit (fun () -> stats run);
401 let r = get_results auto tree node run.config in
403 TRACE(Html.gen_trace (module T : Tree.S with type t = T.t) (tree));
408 let get_results run =
409 let cache = run.status in
410 let auto = run.auto in
411 let tree = run.tree in
412 let rec loop node acc =
413 if node == T.nil then acc
415 let acc0 = loop (T.next_sibling tree node) acc in
416 let acc1 = loop (T.first_child tree node) acc0 in
420 cache.(T.preorder tree node).NodeStatus.node.sat
421 (get_selecting_states auto)) then node::acc1
424 loop (T.root tree) []
427 let get_full_results run =
428 let cache = run.status in
429 let auto = run.auto in
430 let tree = run.tree in
431 let res_mapper = Hashtbl.create MED_H_SIZE in
434 (fun q -> Hashtbl.add res_mapper q [])
435 (Ata.get_selecting_states auto)
438 if node != T.nil then
439 let () = loop (T.next_sibling tree node) in
440 let () = loop (T.first_child tree node) in
444 let acc = Hashtbl.find res_mapper q in
445 Hashtbl.replace res_mapper q (node::acc)
448 cache.(T.preorder tree node).NodeStatus.node.sat
452 (fun q acc -> (q, Hashtbl.find res_mapper q)::acc)
453 (Ata.get_selecting_states auto) []
455 let prepare_run run list =
456 let tree = run.tree in
457 let auto = run.auto in
458 let status = run.status in
459 let cache2 = run.cache2 in
460 List.iter (fun node ->
461 let parent = T.parent tree node in
462 let fc = T.first_child tree node in
463 let ns = T.next_sibling tree node in
464 let tag = T.tag tree node in
468 { sat = Ata.get_starting_states auto;
469 unsat = StateSet.empty;
470 todo = get_trans cache2 auto tag (Ata.get_states auto);
471 summary = NodeSummary.make
472 (node == T.first_child tree parent) (* is_left *)
473 (node == T.next_sibling tree parent) (* is_right *)
474 (fc != T.nil) (* has_left *)
475 (ns != T.nil) (* has_right *)
476 (T.kind tree node) (* kind *)
479 let node_id = T.preorder tree node in
480 status.(node_id) <- status0) list
483 let eval full auto tree nodes =
484 let run = make auto tree in
485 prepare_run run nodes;
489 if full then `Full (get_full_results run)
490 else `Normal (get_results run)
493 let full_eval auto tree nodes =
494 match eval true auto tree nodes with
498 let eval auto tree nodes =
499 match eval false auto tree nodes with