Implement runtime optimisation via Hashing of transitions.
[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-13 18:27:13 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   type cache = (int, StateSet.t) Hashtbl.t
31   let get c t n =
32     try Hashtbl.find c (T.preorder t n)
33     with Not_found -> StateSet.empty
34
35   let set c t n v = Hashtbl.replace c (T.preorder t n) v
36
37   module Info = struct
38     type t = { is_left : bool;
39                is_right : bool;
40                has_left : bool;
41                has_right : bool;
42                kind : Tree.Common.NodeKind.t;
43              }
44     let equal a b = a = b
45     let hash  a = Hashtbl.hash a
46   end
47
48   module NodeInfo = Hcons.Make(Info)
49
50   let eval_form phi node_info fcs nss ps ss =
51     let open NodeInfo in
52     let open Info in
53     let rec loop phi =
54       begin match Ata.SFormula.expr phi with
55         Formula.True -> true
56       | Formula.False -> false
57       | Formula.Atom a ->
58           let p, b, q = Ata.Atom.node a in
59           let pos =
60             let open Ata in
61                 match p with
62                 | First_child -> StateSet.mem q fcs
63                 | Next_sibling ->  StateSet.mem q nss
64                 | Parent | Previous_sibling -> StateSet.mem q ps
65                 | Stay -> StateSet.mem q ss
66                 | Is_first_child -> node_info.node.is_left
67                 | Is_next_sibling -> node_info.node.is_right
68                 | Is k -> k == node_info.node.kind
69                 | Has_first_child -> node_info.node.has_left
70                 | Has_next_sibling -> node_info.node.has_right
71             in
72             if Ata.is_move p && (not b) then
73               eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
74             b == pos
75       | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
76       | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
77       end
78     in
79     loop phi
80
81   let eval_trans cache ltrs node_info fcs nss ps ss =
82     let i = (ltrs.Ata.TransList.id :> int)
83     and j = (node_info.NodeInfo.id :> int)
84     and k = (fcs.StateSet.id :> int)
85     and l = (nss.StateSet.id :> int)
86     and m = (ps.StateSet.id :> int)
87     and n = (ss.StateSet.id :> int) in
88     let res = Cache.N6.find cache i j k l m n in
89     if res == Cache.N6.dummy cache then
90       let res =
91         Ata.TransList.fold (fun trs (acct, accs) ->
92           let q, _, phi = Ata.Transition.node trs in
93           if StateSet.mem q accs then (acct, accs) else
94             if eval_form phi node_info fcs nss ps accs then
95               (acct, StateSet.add q accs)
96             else
97               (Ata.TransList.cons trs acct, accs)
98         ) ltrs (Ata.TransList.nil, ss) 
99       in
100       Cache.N6.add cache i j k l m n res; res
101     else
102       res
103
104   let top_down_run auto tree node cache _i =
105     let redo = ref false in
106     let dummy2 = Ata.TransList.cons
107       (Ata.Transition.make (State.dummy,QNameSet.empty, Ata.SFormula.false_))
108       Ata.TransList.nil
109     in
110     let dummy6 = (dummy2, StateSet.empty) in
111     let trans_cache6 = Cache.N6.create 17 dummy6 in
112     let trans_cache2 = Cache.N2.create 17 dummy2 in
113     let rec loop node =
114       if node != T.nil then begin
115         let parent = T.parent tree node in
116         let fc = T.first_child tree node in
117         let ns = T.next_sibling tree node in
118         let tag = T.tag tree node in
119         let states0 = get cache tree node in
120         let trans0 =
121           let trs =
122             Cache.N2.find trans_cache2
123               (tag.QName.id :> int) (auto.Ata.states.StateSet.id :> int)
124           in
125           if trs == dummy2 then
126             let trs = Ata.get_trans auto auto.Ata.states tag in
127             (Cache.N2.add
128               trans_cache2
129               (tag.QName.id :> int)
130               (auto.Ata.states.StateSet.id :> int) trs; trs)
131           else trs
132         in
133         let ps = get cache tree parent in
134         let fcs = get cache tree fc in
135         let nss = get cache tree ns in
136         let node_info = NodeInfo.make
137           (Info.({ is_left = node == T.first_child tree parent;
138                    is_right = node == T.next_sibling tree parent;
139                    has_left = fc != T.nil;
140                    has_right = ns != T.nil;
141                    kind = T.kind tree node }))
142         in
143         let trans1, states1 =
144           eval_trans trans_cache6 trans0 node_info fcs nss ps states0
145         in
146         if states1 != states0 then set cache tree node states1;
147         let () = loop fc in
148         let fcs1 = get cache tree fc in
149         let trans2, states2 =
150           eval_trans trans_cache6 trans1 node_info fcs1 nss ps states1
151         in
152         if states2 != states1 then set cache tree node states2;
153         let () = loop ns in
154         let _, states3 =
155           eval_trans trans_cache6 trans2 node_info fcs1 (get cache tree ns) ps states2
156         in
157         if states3 != states2 then set cache tree node states3;
158         if states0 != states3 && (not !redo) then redo := true
159       end
160     in
161     loop node;
162     !redo
163
164   let get_results auto tree node cache =
165     let rec loop node acc =
166       if node == T.nil then acc
167       else
168         let acc0 = loop (T.next_sibling tree node) acc in
169         let acc1 = loop (T.first_child tree node) acc0 in
170
171         if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
172           node::acc1
173         else
174           acc1
175     in
176     loop node []
177
178   let eval auto tree node =
179     let cache = Hashtbl.create 511 in
180     let redo = ref true in
181     let iter = ref 0 in
182     while !redo do
183       redo := top_down_run auto tree node cache !iter;
184       incr iter;
185     done;
186     get_results auto tree node cache
187
188 end