Add a first runtime function. Positive fragment seems to work.
[tatoo.git] / src / auto / eval.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
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   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 (*
17   Time-stamp: <Last modified on 2013-03-05 16:24:35 CET by Kim Nguyen>
18 *)
19
20 INCLUDE "utils.ml"
21 open Format
22 open Utils
23
24 module Make (T : Tree.Sig.S) = struct
25
26   type cache = (int, StateSet.t) Hashtbl.t
27
28   let get c t n =
29     try Hashtbl.find c (T.preorder t n)
30     with Not_found -> StateSet.empty
31
32   let set c t n v = Hashtbl.replace c (T.preorder t n) v
33
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)
38       else
39         (trs::acct, accs)
40     ) ([], acc) l
41
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
55         let ctx0 =
56           if is_left then
57             Ata.make_ctx fc_states ns_states parent_states  StateSet.empty states0
58           else
59             Ata.make_ctx fc_states ns_states StateSet.empty parent_states states0
60         in
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
76       end
77     in
78     loop node true;
79     !redo
80
81   let get_results auto tree node cache =
82     let rec loop node acc =
83       if node == T.nil then acc
84       else
85         let acc0 = loop (T.next_sibling tree node) acc in
86         let acc1 = loop (T.first_child tree node) acc0 in
87
88         if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
89           node::acc1
90         else
91           acc1
92     in
93     loop node []
94
95   let eval auto tree node =
96     let cache = Hashtbl.create 511 in
97     let redo = ref true in
98     let iter = ref 0 in
99     while !redo do
100       redo := top_down_run auto tree node cache !iter;
101       incr iter;
102     done;
103     get_results auto tree node cache
104
105 end