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
57 let is_left (s : t) : bool =
60 let is_right (s : t) : bool =
63 let has_left (s : t) : bool =
66 let has_right (s : t) : bool =
69 let kind (s : t) : Tree.NodeKind.t =
72 let make is_left has_left has_right kind =
73 (int_of_bool is_left) lor
74 ((int_of_bool has_left) lsl 1) lor
75 ((int_of_bool has_right) lsl 2) lor
76 ((Obj.magic kind) lsl 3)
81 sat : StateSet.t; (* States that are satisfied at the current node *)
82 todo : StateSet.t; (* States that remain to be proven *)
83 (* For every node_status and automaton a,
84 a.states - (sat U todo) = unsat *)
85 summary : NodeSummary.t; (* Summary of the shape of the node *)
87 (* Describe what is kept at each node for a run *)
91 include Hcons.Make(struct
98 c.summary == d.summary
102 (c.sat.StateSet.id :> int),
103 (c.todo.StateSet.id :> int),
109 "{ rank: %i; sat: %a; todo: %a; summary: _ }"
111 StateSet.print s.node.sat
112 StateSet.print s.node.todo
118 sat = StateSet.empty;
119 todo = StateSet.empty;
120 summary = NodeSummary.dummy;
126 (* The argument of the run *)
128 (* The automaton to be run *)
129 status : NodeStatus.t array;
130 (* A mapping from node preorders to NodeStatus *)
132 mutable fetch_trans_cache : Ata.Formula.t Cache.N2.t;
133 (* A cache from states * label to list of transitions *)
134 mutable td_cache : NodeStatus.t Cache.N5.t;
135 mutable bu_cache : NodeStatus.t Cache.N5.t;
140 let dummy_form = Ata.Formula.stay State.dummy
143 let len = T.size tree in
147 status = Array.create len dummy_status;
149 fetch_trans_cache = Cache.N2.create dummy_form;
150 td_cache = Cache.N5.create dummy_status;
151 bu_cache = Cache.N5.create dummy_status;
155 if i < 0 then dummy_status else Array.get a i
157 let unsafe_get_status a i =
158 if i < 0 then dummy_status else Array.unsafe_get a i
162 DEFINE IFTRACE(e) = (e)
164 DEFINE IFTRACE(e) = ()
167 let html tree node i config msg =
168 let config = config.NodeStatus.node in
170 (T.preorder tree node) i
176 let debug msg tree node i config =
177 let config = config.NodeStatus.node in
179 "DEBUG:%s node: %i\nsat: %a\ntodo: %a\nround: %i\n"
181 (T.preorder tree node)
182 StateSet.print config.sat
183 StateSet.print config.todo
186 let get_form fetch_trans_cache auto tag q =
188 incr fetch_trans_cache_access;
189 Cache.N2.find fetch_trans_cache (tag.QName.id :> int) (q :> int)
191 if phi == dummy_form then
192 let phi = Ata.get_form auto tag q in
196 (tag.QName.id :> int)
200 incr fetch_trans_cache_hit;
204 type trivalent = False | True | Unknown
205 let of_bool = function false -> False | true -> True
210 | Unknown -> if t2 == True then True else Unknown
216 | Unknown -> if t2 == False then False else Unknown
218 (* Define as macros to get lazyness *)
223 | Unknown -> if (t2) == True then True else Unknown
229 | Unknown -> if (t2) == False then False else Unknown
232 let eval_form phi fcs nss ps ss summary =
235 begin match Formula.expr phi with
236 | Boolean.False -> False
237 | Boolean.True -> True
238 | Boolean.Atom (a, b) ->
240 let open NodeSummary in
241 match a.Atom.node with
243 let down, ({ NodeStatus.node = n_sum; _ } as sum) =
245 `First_child -> true, fcs
246 | `Next_sibling -> true, nss
247 | `Parent | `Previous_sibling -> false, ps
250 if sum == dummy_status
251 (*|| (down && n_sum.rank < ss.NodeStatus.node.rank) *)
252 || StateSet.mem q n_sum.todo then
255 of_bool (b == StateSet.mem q n_sum.sat)
256 | Is_first_child -> of_bool (b == is_left summary)
257 | Is_next_sibling -> of_bool (b == is_right summary)
258 | Is k -> of_bool (b == (k == kind summary))
259 | Has_first_child -> of_bool (b == has_left summary)
260 | Has_next_sibling -> of_bool (b == has_right summary)
262 | Boolean.And(phi1, phi2) -> AND_ (loop phi1, loop phi2)
263 | Boolean.Or (phi1, phi2) -> OR_ (loop phi1, loop phi2)
269 let eval_trans_aux auto fetch_trans_cache tag fcs nss ps old_status =
272 summary = old_summary } as os_node = old_status.NodeStatus.node
275 StateSet.fold (fun q ((a_sat, a_todo) as acc) ->
277 get_form fetch_trans_cache auto tag q
280 let v = eval_form phi fcs nss ps old_status old_summary in
282 True -> StateSet.add q a_sat, a_todo
284 | Unknown -> a_sat, StateSet.add q a_todo
285 ) old_todo (old_sat, StateSet.empty)
287 if old_sat != sat || old_todo != todo then
288 NodeStatus.make { os_node with sat; todo }
292 let rec eval_trans_fix auto fetch_trans_cache tag fcs nss ps old_status =
294 eval_trans_aux auto fetch_trans_cache tag fcs nss ps old_status
296 if new_status == old_status then old_status else
297 eval_trans_fix auto fetch_trans_cache tag fcs nss ps new_status
300 let eval_trans auto fetch_trans_cache td_cache tag fcs nss ps ss =
301 let fcsid = (fcs.NodeStatus.id :> int) in
302 let nssid = (nss.NodeStatus.id :> int) in
303 let psid = (ps.NodeStatus.id :> int) in
304 let ssid = (ss.NodeStatus.id :> int) in
305 let tagid = (tag.QName.id :> int) in
306 let res = Cache.N5.find td_cache tagid ssid fcsid nssid psid in
307 incr eval_trans_cache_access;
308 if res != dummy_status then begin incr eval_trans_cache_hit; res end
309 else let new_status = eval_trans_fix auto fetch_trans_cache tag fcs nss ps ss in
310 Cache.N5.add td_cache tagid ssid fcsid nssid psid new_status;
315 let tree = run.tree in
316 let auto = run.auto in
317 let status = run.status in
318 let fetch_trans_cache = run.fetch_trans_cache in
319 let td_cache = run.td_cache in
320 let bu_cache = run.bu_cache in
321 let states_by_rank = Ata.get_states_by_rank auto in
322 let td_todo = states_by_rank.(i) in
323 let bu_todo = if i + 1 = Array.length states_by_rank then StateSet.empty
327 let rec loop_td_and_bu node =
328 if node == T.nil then () else begin
329 let node_id = T.preorder tree node in
330 let parent = T.parent tree node in
331 let fc = T.first_child tree node in
332 let fc_id = T.preorder tree fc in
333 let ns = T.next_sibling tree node in
334 let ns_id = T.preorder tree ns in
335 let tag = T.tag tree node in
336 (* We enter the node from its parent *)
338 let c = unsafe_get_status status node_id in
339 if c.NodeStatus.node.rank < i then
340 (* first time we visit the node during this run *)
343 sat = c.NodeStatus.node.sat;
346 let summary = c.NodeStatus.node.summary in
347 if summary != NodeSummary.dummy then summary
350 (node != T.next_sibling tree parent)
351 (fc != T.nil) (* has_left *)
352 (ns != T.nil) (* has_right *)
353 (T.kind tree node) (* kind *)
357 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
358 i QName.print tag NodeStatus.print status0
360 (* get the node_statuses for the first child, next sibling and parent *)
361 let ps = unsafe_get_status status (T.preorder tree parent) in
362 let fcs = unsafe_get_status status fc_id in
363 let nss = unsafe_get_status status ns_id in
364 (* evaluate the transitions with all this statuses *)
366 if status0.NodeStatus.node.todo == StateSet.empty then status0
368 let status1 = eval_trans auto fetch_trans_cache td_cache tag fcs nss ps status0 in
369 (* update the cache if the status of the node changed *)
370 if status1 != status0 then status.(node_id) <- status1;
374 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
375 i QName.print tag NodeStatus.print status1
377 (* recursively traverse the first child *)
378 let () = loop_td_and_bu fc in
379 (* here we re-enter the node from its first child,
380 get the new status of the first child *)
381 let fcs1 = unsafe_get_status status fc_id in
382 (* update the status *)
383 let status1 = if status1.NodeStatus.node.rank < i+1 then
384 NodeStatus.make { status1.NodeStatus.node with
391 if status1.NodeStatus.node.todo == StateSet.empty then status1
393 let status2 = eval_trans auto fetch_trans_cache bu_cache tag fcs1 nss ps status1 in
394 if status2 != status1 then status.(node_id) <- status2;
398 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
399 (i+1) QName.print tag NodeStatus.print status2
401 let () = loop_td_and_bu ns in
402 let nss1 = unsafe_get_status status ns_id in
403 if status2.NodeStatus.node.todo != StateSet.empty then
404 let status3 = eval_trans auto fetch_trans_cache bu_cache tag fcs1 nss1 ps status2 in
405 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
406 (i+1) QName.print tag NodeStatus.print status3
409 if status3 != status2 then status.(node_id) <- status3
411 and loop_td_only node =
412 if node == T.nil then () else begin
413 let node_id = T.preorder tree node in
414 let parent = T.parent tree node in
415 let fc = T.first_child tree node in
416 let fc_id = T.preorder tree fc in
417 let ns = T.next_sibling tree node in
418 let ns_id = T.preorder tree ns in
419 let tag = T.tag tree node in
420 (* We enter the node from its parent *)
422 let c = unsafe_get_status status node_id in
423 if c.NodeStatus.node.rank < i then
424 (* first time we visit the node during this run *)
427 sat = c.NodeStatus.node.sat;
430 let summary = c.NodeStatus.node.summary in
431 if summary != NodeSummary.dummy then summary
434 (node != T.next_sibling tree parent)
435 (fc != T.nil) (* has_left *)
436 (ns != T.nil) (* has_right *)
437 (T.kind tree node) (* kind *)
441 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
442 (i) QName.print tag NodeStatus.print status0
445 (* get the node_statuses for the first child, next sibling and parent *)
446 let ps = unsafe_get_status status (T.preorder tree parent) in
447 let fcs = unsafe_get_status status fc_id in
448 let nss = unsafe_get_status status ns_id in
449 (* evaluate the transitions with all this statuses *)
450 if status0.NodeStatus.node.todo != StateSet.empty then begin
451 let status1 = eval_trans auto fetch_trans_cache td_cache tag fcs nss ps status0 in
452 (* update the cache if the status of the node changed *)
453 let () = Logger.msg `STATS "Run %i, Node %a, %a@\n"
454 (i) QName.print tag NodeStatus.print status1
457 if status1 != status0 then status.(node_id) <- status1;
459 (* recursively traverse the first child *)
464 if bu_todo == StateSet.empty then
465 let () = loop_td_only (T.root tree) in
466 run.pass <- run.pass + 1
468 let () = loop_td_and_bu (T.root tree) in
469 run.pass <- run.pass + 2
472 let get_results run =
473 let cache = run.status in
474 let auto = run.auto in
475 let tree = run.tree in
476 let sel_states = Ata.get_selecting_states auto in
477 let rec loop node acc =
478 if node == T.nil then acc
480 let acc0 = loop (T.next_sibling tree node) acc in
481 let acc1 = loop (T.first_child tree node) acc0 in
483 if StateSet.intersect
484 cache.(T.preorder tree node).NodeStatus.node.sat
485 sel_states then node::acc1
488 loop (T.root tree) []
491 let get_full_results run =
492 let cache = run.status in
493 let auto = run.auto in
494 let tree = run.tree in
495 let res_mapper = Hashtbl.create MED_H_SIZE in
498 (fun q -> Hashtbl.add res_mapper q [])
499 (Ata.get_selecting_states auto)
501 let dummy = [ T.nil ] in
502 let res_mapper = Cache.N1.create dummy in
505 (fun q -> Cache.N1.add res_mapper (q :> int) [])
506 (Ata.get_selecting_states auto)
509 if node != T.nil then
510 let () = loop (T.next_sibling tree node) in
511 let () = loop (T.first_child tree node) in
514 let res = Cache.N1.find res_mapper (q :> int) in
516 Cache.N1.add res_mapper (q :> int) (node::res)
518 cache.(T.preorder tree node).NodeStatus.node.sat
522 (fun q acc -> (q, Cache.N1.find res_mapper (q :> int))::acc)
523 (Ata.get_selecting_states auto) [])
526 let prepare_run run list =
527 let tree = run.tree in
528 let auto = run.auto in
529 let status = run.status in
530 List.iter (fun node ->
531 let parent = T.parent tree node in
532 let fc = T.first_child tree node in
533 let ns = T.next_sibling tree node in
537 sat = Ata.get_starting_states auto;
539 StateSet.diff (Ata.get_states auto) (Ata.get_starting_states auto);
540 summary = NodeSummary.make
541 (node != T.next_sibling tree parent) (* is_left *)
542 (fc != T.nil) (* has_left *)
543 (ns != T.nil) (* has_right *)
544 (T.kind tree node) (* kind *)
547 let node_id = T.preorder tree node in
548 status.(node_id) <- status0) list
550 let tree_size = ref 0
552 let compute_run auto tree nodes =
554 tree_size := T.size tree;
555 let run = make auto tree in
556 prepare_run run nodes;
557 let rank = Ata.get_max_rank auto in
558 while run.pass <= rank do
560 run.td_cache <- Cache.N5.create dummy_status;
561 run.bu_cache <- Cache.N5.create dummy_status;
563 pass := Ata.get_max_rank auto + 1;
564 IFTRACE(Html.gen_trace auto (module T : Tree.S with type t = T.t) tree);
568 let full_eval auto tree nodes =
569 let r = compute_run auto tree nodes in
572 let eval auto tree nodes =
573 let r = compute_run auto tree nodes in
577 tree_size = !tree_size;
579 fetch_trans_cache_access = !fetch_trans_cache_access;
580 fetch_trans_cache_hit = !fetch_trans_cache_hit;
581 eval_trans_cache_access = !eval_trans_cache_access;
582 eval_trans_cache_hit = !eval_trans_cache_hit;