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 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-03-05 16:24:35 CET by Kim Nguyen>
24 module Make (T : Tree.Sig.S) = struct
26 type cache = (int, StateSet.t) Hashtbl.t
29 try Hashtbl.find c (T.preorder t n)
30 with Not_found -> StateSet.empty
32 let set c t n v = Hashtbl.replace c (T.preorder t n) v
34 let eval_trans l ctx acc =
35 List.fold_left (fun (acct, accs) ((q, phi) as trs) ->
36 if Ata.SFormula.eval ctx phi then
37 (acct, StateSet.add q accs)
42 let top_down_run auto tree node cache i =
43 let redo = ref false in
44 let rec loop node is_left =
45 if node != T.nil then begin
46 let parent = T.parent tree node in
47 let fc = T.first_child tree node in
48 let ns = T.next_sibling tree node in
49 let states0 = get cache tree node in
50 let tag = T.tag tree node in
51 let trans0 = Ata.get_trans auto auto.Ata.states tag in
52 let parent_states = if parent == T.nil then auto.Ata.top_states else get cache tree parent in
53 let fc_states = if fc == T.nil then auto.Ata.bottom_states else get cache tree fc in
54 let ns_states = if ns == T.nil then auto.Ata.bottom_states else get cache tree ns in
57 Ata.make_ctx fc_states ns_states parent_states StateSet.empty states0
59 Ata.make_ctx fc_states ns_states StateSet.empty parent_states states0
61 eprintf "[Iteration % 4d] node: %a, context: %a\n%!"
62 i T.print_node node Ata.print_ctx ctx0;
63 List.iter (fun (q, phi) -> eprintf "%a -> %a\n" State.print q Ata.SFormula.print phi) trans0;
64 eprintf "----------------------\n%!";
65 let trans1, states1 = eval_trans trans0 ctx0 StateSet.empty in
66 if states1 != states0 then set cache tree node states1;
67 let () = loop fc true in
68 let ctx1 = {ctx0 with Ata.left = (get cache tree fc) ; Ata.epsilon = states1 } in
69 let trans2, states2 = eval_trans trans1 ctx1 states1 in
70 if states2 != states1 then set cache tree node states2;
71 let () = loop ns false in
72 let ctx2 = { ctx1 with Ata.right = (get cache tree ns); Ata.epsilon = states2 } in
73 let _, states3 = eval_trans trans2 ctx2 states2 in
74 if states3 != states2 then set cache tree node states3;
75 if states0 != states3 && (not !redo) then redo := true
81 let get_results auto tree node cache =
82 let rec loop node acc =
83 if node == T.nil then acc
85 let acc0 = loop (T.next_sibling tree node) acc in
86 let acc1 = loop (T.first_child tree node) acc0 in
88 if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
95 let eval auto tree node =
96 let cache = Hashtbl.create 511 in
97 let redo = ref true in
100 redo := top_down_run auto tree node cache !iter;
103 get_results auto tree node cache