e4def93b6e5ba4cfdd5f6e13e3a6a0a7d0fc11ec
[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-15 23:46:44 CET by Kim Nguyen>
18 *)
19
20 INCLUDE "utils.ml"
21 open Format
22 open Utils
23
24 module Make (T : Tree.Sig.S) :
25   sig
26     val eval : Ata.t -> T.t -> T.node -> T.node list
27   end
28  = struct
29
30
31 IFDEF HTMLTRACE
32   THEN
33 DEFINE TRACE(e) = (e)
34   ELSE
35 DEFINE TRACE(e) = ()
36 END
37
38
39
40   type cache = StateSet.t Cache.N1.t
41   let get c t n = Cache.N1.find c (T.preorder t n)
42
43   let set c t n v = Cache.N1.add c (T.preorder t n) v
44
45
46   let top_down_run auto tree node cache _i =
47     let redo = ref false in
48     let rec loop node =
49       if node != T.nil then begin
50         let parent = T.parent tree node in
51         let fc = T.first_child tree node in
52         let ns = T.next_sibling tree node in
53         let tag = T.tag tree node in
54         let states0 = get cache tree node in
55         let trans0 = Ata.get_trans auto tag auto.Ata.states in
56         let () =
57           TRACE(Html.trace (T.preorder tree node) _i "Pre States: %a<br/>Pre Trans: %a<br/>"
58                   StateSet.print states0 (Ata.TransList.print ~sep:"<br/>") trans0)
59         in
60         let ps = get cache tree parent in
61         let fcs = get cache tree fc in
62         let nss = get cache tree ns in
63         let is_left = node == T.first_child tree parent
64         and is_right = node == T.next_sibling tree parent
65         and has_left = fc != T.nil
66         and has_right = ns != T.nil
67         and kind = T.kind tree node
68         in
69         let trans1, states1 =
70           Ata.eval_trans auto trans0
71             fcs nss ps states0
72             is_left is_right has_left has_right kind
73         in
74         let () =
75           TRACE(Html.trace (T.preorder tree node) _i "TD States: %a<br/>TD Trans: %a<br/>" StateSet.print states1 (Ata.TransList.print ~sep:"<br/>") trans1)
76         in
77         if states1 != states0 then set cache tree node states1;
78         let () = loop fc in
79         let fcs1 = get cache tree fc in
80         let trans2, states2 =
81           Ata.eval_trans auto trans1
82             fcs1 nss ps states1
83             is_left is_right has_left has_right kind
84         in
85         let () =
86           TRACE(Html.trace (T.preorder tree node) _i "Left BU States: %a<br/>Left BU Trans: %a<br/>" StateSet.print states2 (Ata.TransList.print ~sep:"<br/>") trans2)
87         in
88         if states2 != states1 then set cache tree node states2;
89         let () = loop ns in
90         let _trans3, states3 =
91           Ata.eval_trans auto trans2
92             fcs1 (get cache tree ns) ps states2
93             is_left is_right has_left has_right kind
94         in
95         let () =
96           TRACE(Html.trace (T.preorder tree node) _i "Right BU States: %a<br/>Right BU Trans: %a<br/>" StateSet.print states3 (Ata.TransList.print ~sep:"<br/>") _trans3)
97         in
98         if states3 != states2 then set cache tree node states3;
99         if states0 != states3 && (not !redo) then redo := true
100       end
101     in
102     loop node;
103     !redo
104
105   let get_results auto tree node cache =
106     let rec loop node acc =
107       if node == T.nil then acc
108       else
109         let acc0 = loop (T.next_sibling tree node) acc in
110         let acc1 = loop (T.first_child tree node) acc0 in
111
112         if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
113           node::acc1
114         else
115           acc1
116     in
117     loop node []
118
119   let eval auto tree node =
120     let cache = Cache.N1.create StateSet.empty in
121     let redo = ref true in
122     let iter = ref 0 in
123     Ata.reset auto;
124     while !redo do
125       redo := top_down_run auto tree node cache !iter;
126       incr iter;
127     done;
128     at_exit (fun () -> eprintf "INFO: %i iterations\n" !iter);
129     let r = get_results auto tree node cache in
130     TRACE(Html.gen_trace (module T : Tree.Sig.S with type t = T.t) (tree));
131     r
132
133 end