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;
30 let cache2_hit = ref 0
31 let cache2_access = ref 0
32 let cache5_hit = ref 0
33 let cache5_access = ref 0
34 let reset_stat_counters () =
41 module Make (T : Tree.S) =
46 (* Pack into an integer the result of the is_* and has_ predicates
59 let has_right (s : t) : bool =
62 let has_left (s : t) : bool =
63 Obj.magic ((s lsr 1) land 1)
65 let is_right (s : t) : bool =
66 Obj.magic ((s lsr 2) land 1)
68 let is_left (s : t) : bool =
69 Obj.magic ((s lsr 3) land 1)
71 let kind (s : t) : Tree.NodeKind.t =
74 let make is_left is_right has_left has_right kind =
75 ((Obj.magic kind) lsl 4) lor
76 ((int_of_bool is_left) lsl 3) lor
77 ((int_of_bool is_right) lsl 2) lor
78 ((int_of_bool has_left) lsl 1) lor
79 (int_of_bool has_right)
85 sat : StateSet.t; (* States that are satisfied at the current node *)
86 todo : StateSet.t; (* States that remain to be proven *)
87 (* For every node_status and automaton a,
88 a.states - (sat U todo) = unsat *)
89 summary : NodeSummary.t; (* Summary of the shape of the node *)
91 (* Describe what is kept at each node for a run *)
95 include Hcons.Make(struct
102 c.summary == d.summary
106 (c.sat.StateSet.id :> int),
107 (c.todo.StateSet.id :> int),
113 "{ rank: %i; sat: %a; todo: %a; summary: _ }"
115 StateSet.print s.node.sat
116 StateSet.print s.node.todo
122 sat = StateSet.empty;
123 todo = StateSet.empty;
124 summary = NodeSummary.dummy;
130 (* The argument of the run *)
132 (* The automaton to be run *)
133 status : NodeStatus.t array;
134 (* A mapping from node preorders to NodeStatus *)
136 (* A boolean indicating whether the run is incomplete *)
138 (* The number of times this run was updated *)
139 mutable cache2 : Ata.Formula.t Cache.N2.t;
140 (* A cache from states * label to list of transitions *)
141 mutable cache5 : NodeStatus.t Cache.N5.t;
145 let stable r = not r.redo
150 let dummy_form = Ata.Formula.stay State.dummy
153 let len = T.size tree in
157 status = Array.create len dummy_status;
160 cache2 = Cache.N2.create dummy_form;
161 cache5 = Cache.N5.create dummy_status;
165 if i < 0 then dummy_status else Array.get a i
167 let unsafe_get_status a i =
168 if i < 0 then dummy_status else Array.unsafe_get a i
172 DEFINE IFTRACE(e) = (e)
174 DEFINE IFTRACE(e) = ()
177 let html tree node i config msg =
178 let config = config.NodeStatus.node in
180 (T.preorder tree node) i
186 let debug msg tree node i config =
187 let config = config.NodeStatus.node in
189 "DEBUG:%s node: %i\nsat: %a\ntodo: %a\nround: %i\n"
191 (T.preorder tree node)
192 StateSet.print config.sat
193 StateSet.print config.todo
196 let get_form cache2 auto tag q =
199 Cache.N2.find cache2 (tag.QName.id :> int) (q :> int)
201 if phi == dummy_form then
202 let phi = Ata.get_form auto tag q in
206 (tag.QName.id :> int)
214 type trivalent = False | True | Unknown
215 let of_bool = function false -> False | true -> True
220 | Unknown -> if t2 == True then True else Unknown
226 | Unknown -> if t2 == False then False else Unknown
228 (* Define as macros to get lazyness *)
233 | Unknown -> if (t2) == True then True else Unknown
239 | Unknown -> if (t2) == False then False else Unknown
242 let eval_form phi fcs nss ps ss summary =
245 begin match Formula.expr phi with
246 | Boolean.False -> False
247 | Boolean.True -> True
248 | Boolean.Atom (a, b) ->
250 let open NodeSummary in
251 match a.Atom.node with
253 let down, ({ NodeStatus.node = n_sum; _ } as sum) =
255 `First_child -> true, fcs
256 | `Next_sibling -> true, nss
257 | `Parent | `Previous_sibling -> false, ps
260 if sum == dummy_status
261 || (down && n_sum.rank < ss.NodeStatus.node.rank)
262 || StateSet.mem q n_sum.todo then
265 of_bool (b == StateSet.mem q n_sum.sat)
266 | Is_first_child -> of_bool (b == is_left summary)
267 | Is_next_sibling -> of_bool (b == is_right summary)
268 | Is k -> of_bool (b == (k == kind summary))
269 | Has_first_child -> of_bool (b == has_left summary)
270 | Has_next_sibling -> of_bool (b == has_right summary)
272 | Boolean.And(phi1, phi2) -> AND_ (loop phi1, loop phi2)
273 | Boolean.Or (phi1, phi2) -> OR_ (loop phi1, loop phi2)
279 let eval_trans_aux auto cache2 tag fcs nss ps old_status =
282 summary = old_summary } as os_node = old_status.NodeStatus.node
285 StateSet.fold (fun q ((a_sat, a_todo) as acc) ->
287 get_form cache2 auto tag q
290 let v = eval_form phi fcs nss ps old_status old_summary in
292 True -> StateSet.add q a_sat, a_todo
294 | Unknown -> a_sat, StateSet.add q a_todo
295 ) old_todo (old_sat, StateSet.empty)
297 if old_sat != sat || old_todo != todo then
298 NodeStatus.make { os_node with sat; todo }
302 let eval_trans auto cache2 cache5 tag fcs nss ps ss =
303 let rec loop old_status =
305 eval_trans_aux auto cache2 tag fcs nss ps old_status
307 if new_status == old_status then old_status else loop new_status
309 let fcsid = (fcs.NodeStatus.id :> int) in
310 let nssid = (nss.NodeStatus.id :> int) in
311 let psid = (ps.NodeStatus.id :> int) in
312 let ssid = (ss.NodeStatus.id :> int) in
313 let tagid = (tag.QName.id :> int) in
314 let res = Cache.N5.find cache5 tagid ssid fcsid nssid psid in
316 if res != dummy_status then begin incr cache5_hit; res end
317 else let new_status = loop ss in
318 Cache.N5.add cache5 tagid ssid fcsid nssid psid new_status;
325 let tree = run.tree in
326 let auto = run.auto in
327 let status = run.status in
328 let cache2 = run.cache2 in
329 let cache5 = run.cache5 in
330 let states_by_rank = Ata.get_states_by_rank auto in
331 let td_todo = states_by_rank.(i) in
332 let bu_todo = if i + 1 = Array.length states_by_rank then StateSet.empty
337 let node_id = T.preorder tree node in
338 if node != T.nil then begin
339 let parent = T.parent tree node in
340 let fc = T.first_child tree node in
341 let fc_id = T.preorder tree fc in
342 let ns = T.next_sibling tree node in
343 let ns_id = T.preorder tree ns in
344 let tag = T.tag tree node in
345 (* We enter the node from its parent *)
347 let c = unsafe_get_status status node_id in
348 if c.NodeStatus.node.rank < i then
349 (* first time we visit the node during this run *)
352 sat = c.NodeStatus.node.sat;
354 summary = let summary = c.NodeStatus.node.summary
356 if summary != NodeSummary.dummy then summary
359 (node == T.first_child tree parent) (* is_left *)
360 (node == T.next_sibling tree parent) (* is_right *)
361 (fc != T.nil) (* has_left *)
362 (ns != T.nil) (* has_right *)
363 (T.kind tree node) (* kind *)
367 (* get the node_statuses for the first child, next sibling and parent *)
368 let ps = unsafe_get_status status (T.preorder tree parent) in
369 let fcs = unsafe_get_status status fc_id in
370 let nss = unsafe_get_status status ns_id in
371 (* evaluate the transitions with all this statuses *)
373 if status0.NodeStatus.node.todo == StateSet.empty then status0
375 let status1 = eval_trans auto cache2 cache5 tag fcs nss ps status0 in
376 (* update the cache if the status of the node changed *)
377 if status1 != status0 then status.(node_id) <- status1;
381 (* recursively traverse the first child *)
383 (* here we re-enter the node from its first child,
384 get the new status of the first child *)
385 let fcs1 = unsafe_get_status status fc_id in
386 (* update the status *)
387 let status1 = if status1.NodeStatus.node.rank < i then
388 NodeStatus.make { status1.NodeStatus.node with
395 if status1.NodeStatus.node.todo == StateSet.empty then status1
397 let status2 = eval_trans auto cache2 cache5 tag fcs1 nss ps status1 in
398 if status2 != status1 then status.(node_id) <- status2;
403 let nss1 = unsafe_get_status status ns_id in
404 if status2.NodeStatus.node.todo != StateSet.empty then
405 let status3 = eval_trans auto cache2 cache5 tag fcs1 nss1 ps status2 in
406 if status3 != status2 then status.(node_id) <- status3
412 let get_results run =
413 let cache = run.status in
414 let auto = run.auto in
415 let tree = run.tree in
416 let sel_states = Ata.get_selecting_states auto in
417 let rec loop node acc =
418 if node == T.nil then acc
420 let acc0 = loop (T.next_sibling tree node) acc in
421 let acc1 = loop (T.first_child tree node) acc0 in
423 if StateSet.intersect
424 cache.(T.preorder tree node).NodeStatus.node.sat
425 sel_states then node::acc1
428 loop (T.root tree) []
431 let get_full_results run =
432 let cache = run.status in
433 let auto = run.auto in
434 let tree = run.tree in
435 let res_mapper = Hashtbl.create MED_H_SIZE in
438 (fun q -> Hashtbl.add res_mapper q [])
439 (Ata.get_selecting_states auto)
441 let dummy = [ T.nil ] in
442 let res_mapper = Cache.N1.create dummy in
445 (fun q -> Cache.N1.add res_mapper (q :> int) [])
446 (Ata.get_selecting_states auto)
449 if node != T.nil then
450 let () = loop (T.next_sibling tree node) in
451 let () = loop (T.first_child tree node) in
454 let res = Cache.N1.find res_mapper (q :> int) in
456 Cache.N1.add res_mapper (q :> int) (node::res)
458 cache.(T.preorder tree node).NodeStatus.node.sat
462 (fun q acc -> (q, Cache.N1.find res_mapper (q :> int))::acc)
463 (Ata.get_selecting_states auto) [])
466 let prepare_run run list =
467 let tree = run.tree in
468 let auto = run.auto in
469 let status = run.status in
470 List.iter (fun node ->
471 let parent = T.parent tree node in
472 let fc = T.first_child tree node in
473 let ns = T.next_sibling tree node in
477 sat = Ata.get_starting_states auto;
479 StateSet.diff (Ata.get_states auto) (Ata.get_starting_states auto);
480 summary = NodeSummary.make
481 (node == T.first_child tree parent) (* is_left *)
482 (node == T.next_sibling tree parent) (* is_right *)
483 (fc != T.nil) (* has_left *)
484 (ns != T.nil) (* has_right *)
485 (T.kind tree node) (* kind *)
488 let node_id = T.preorder tree node in
489 status.(node_id) <- status0) list
491 let tree_size = ref 0
493 let compute_run auto tree nodes =
495 tree_size := T.size tree;
496 let run = make auto tree in
497 prepare_run run nodes;
498 for i = 0 to Ata.get_max_rank auto do
500 run.pass <- run.pass + 1
502 pass := Ata.get_max_rank auto + 1;
503 IFTRACE(Html.gen_trace auto (module T : Tree.S with type t = T.t) tree);
507 let full_eval auto tree nodes =
508 let r = compute_run auto tree nodes in
511 let eval auto tree nodes =
512 let r = compute_run auto tree nodes in
516 tree_size = !tree_size;
518 cache2_access = !cache2_access;
519 cache2_hit = !cache2_hit;
520 cache5_access = !cache5_access;
521 cache5_hit = !cache5_hit;