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