Flatten the sources, only leave the XPath module packed.
[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-04 18:45:19 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       end
100     in
101     loop node;
102     !redo
103
104   let get_results auto tree node cache =
105     let rec loop node acc =
106       if node == T.nil then acc
107       else
108         let acc0 = loop (T.next_sibling tree node) acc in
109         let acc1 = loop (T.first_child tree node) acc0 in
110
111         if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
112           node::acc1
113         else
114           acc1
115     in
116     loop node []
117
118   let eval auto tree node =
119     let cache = Cache.N1.create StateSet.empty in
120     let redo = ref true in
121     let iter = ref 0 in
122     Ata.reset auto;
123     while !redo do
124       redo := top_down_run auto tree node cache !iter;
125       incr iter;
126     done;
127     let r = get_results auto tree node cache in
128     TRACE(Html.gen_trace (module T : Tree.Sig.S with type t = T.t) (tree));
129     r
130
131 end