Preliminary work for multiple starters evaluation.
[tatoo.git] / src / xpath / compile.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 open Ast
17
18
19 let ( => ) a b = (a, b)
20 let ( ++ ) a b = Ata.Formula.or_ a b
21 let ( %% ) a b = Ata.Formula.and_ a b
22 let ( @: ) a b = StateSet.add a b
23
24 module F = Ata.Formula
25
26
27 let node_set = QNameSet.remove QName.document QNameSet.any
28 let star_set = QNameSet.diff QNameSet.any (
29   List.fold_right (QNameSet.add)
30     [ QName.document; QName.text; QName.comment ]
31     QNameSet.empty)
32 let root_set = QNameSet.singleton QName.document
33
34 (* [compile_axis_test axis test q phi trans states] Takes an xpath
35    [axis] and node [test], a formula [phi], a list of [trans]itions
36    and a set of [states] and returns a formula [phi'], a new set of
37    transitions, and a new set of states such that [phi'] holds iff
38    there exists a node reachable through [axis]::[test] where [phi]
39    holds.
40 *)
41
42 let compile_axis_test axis (test,kind) phi trans states=
43   let q = State.make () in
44   let phi = match kind with
45     Tree.NodeKind.Node -> phi
46   | _ -> phi %% F.is kind
47   in
48   let phi', trans', states' =
49     match axis with
50     | Self ->
51         (F.stay q,
52          (q, [  test => phi ]) :: trans,
53          states)
54
55     | Child ->
56         (F.first_child q,
57          (q, [ test => phi;
58                QNameSet.any => F.next_sibling q ]) :: trans,
59          states)
60
61     | Descendant false ->
62         (F.first_child q,
63          (q, [ test => phi;
64                QNameSet.any => F.first_child q ++ F.next_sibling q;
65              ]) :: trans,
66          states)
67     | Descendant true ->
68         let q' = State.make () in
69         (F.or_ (F.stay q) (F.first_child q'),
70          (q', [ test => phi;
71                QNameSet.any => F.first_child q' ++ F.next_sibling q';
72              ])::
73          (q, [ test => phi]):: trans,
74          states)
75
76     | Parent ->
77         let q' = State.make () in
78         let move = F.parent q ++ F.previous_sibling q' in
79         (move,
80          (q, [ test => phi ])
81          :: (q', [ QNameSet.any => move ]) :: trans,
82          (q' @: states))
83
84     | Ancestor self ->
85         let q' = State.make () in
86         let move = F.parent q ++ F.previous_sibling q' in
87         (if self then F.stay q else move),
88         (q, [ test => phi;
89               QNameSet.any => move ])
90         :: (q', [ QNameSet.any => move ]) :: trans,
91         (q' @: states)
92
93     | FollowingSibling | PrecedingSibling ->
94         let move =
95           if axis = PrecedingSibling then
96             F.previous_sibling q
97           else F.next_sibling q
98         in
99         move,
100         (q, [ test => phi;
101               QNameSet.any => move ]) :: trans,
102         states
103
104     | Attribute ->
105         (F.first_child q,
106          (q, [ test => phi;
107                QNameSet.any => F.next_sibling q]) :: trans,
108          states)
109     | _ -> assert false
110
111   in
112   phi', trans', q @: states'
113
114 let rec compile_expr e trans states =
115   match e with
116   | Binop (e1, (And|Or as op), e2) ->
117       let phi1, trans1, states1 = compile_expr e1 trans states in
118       let phi2, trans2, states2 = compile_expr e2 trans1 states1 in
119       (if op = Or then phi1 ++ phi2 else phi1 %% phi2),
120       trans2,
121       states2
122   | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
123       let phi, trans0, states0 = compile_expr e0 trans states in
124       (F.not_ phi),
125       trans0,
126       states0
127   | Path p -> compile_path p trans states
128
129   | _ -> assert false
130 and compile_path paths trans states =
131   List.fold_left (fun (aphi, atrans, astates) p ->
132     let phi, ntrans, nstates = compile_single_path p atrans astates in
133     (F.or_ phi aphi),
134     ntrans,
135     nstates) (F.false_,trans,states) paths
136
137 and compile_single_path p trans states =
138   let steps =
139     match p with
140     | Absolute steps ->
141         (Ancestor false, (QNameSet.singleton QName.document,
142                           Tree.NodeKind.Node), [])
143         :: steps
144     | Relative steps -> steps
145   in
146   compile_step_list steps trans states
147
148 and compile_step_list l trans states =
149   match l with
150   | [] -> F.true_, trans, states
151   | (axis, test, elist) :: ll ->
152       let phi0, trans0, states0 = compile_step_list ll trans states in
153       let phi1, trans1, states1 =
154         compile_axis_test axis test phi0 trans0 states0
155       in
156       List.fold_left (fun (aphi, atrans, astates) e ->
157         let ephi, etrans, estates = compile_expr e atrans astates in
158         aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
159
160 (**
161    Compile the top-level XPath query in reverse (doing downward
162    to the last top-level state):
163    /a0::t0[p0]/.../an-1::tn-1[pn-1]/an::tn[pn] becomes:
164    self::node()[ pn and
165    self::tn[pn]/inv(an)::(tn-1)[pn-1]/.../inv(a1)::t0[p0]/inv(a0)::document()]
166
167    /child::a/attribute::b
168    self::@b/parent::a/parent::doc()
169 *)
170
171 let compile_top_level_step_list l trans states =
172   let rec loop l trans states phi_above =
173     match l with
174     | [] -> assert false
175     | (axis, (test,kind), elist) :: ll ->
176         let phi0, trans0, states0 =
177           compile_axis_test (invert_axis axis)
178             (QNameSet.any, Tree.NodeKind.Node)
179             phi_above trans states
180         in
181         (* Only select attribute nodes if the previous axis
182            is attribute *)
183         let phi0 =
184           if axis != Attribute then
185             phi0 %% (F.not_ F.is_attribute)
186           else phi0
187         in
188         match ll with
189           [] ->
190             let phi1, trans1, states1 =
191               List.fold_left (fun (aphi, atrans, astates) e ->
192                 let ephi, etrans, estates = compile_expr e atrans astates in
193                 aphi %% ephi, etrans, estates) (phi0, trans0, states0) elist
194             in
195             let _, trans2, states2 =
196               compile_axis_test Self (test,kind) phi1 trans1 states1
197             in
198             let marking_state =
199               StateSet.choose (StateSet.diff states2 states1)
200             in
201             marking_state, trans2, states2
202         | _ ->
203             let phi1, trans1, states1 =
204               compile_axis_test Self (test,kind) phi0 trans0 states0
205             in
206             let phi2, trans2, states2 =
207               List.fold_left (fun (aphi, atrans, astates) e ->
208                 let ephi, etrans, estates = compile_expr e atrans astates in
209                 aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
210             in
211             loop ll trans2 states2  phi2
212   in
213   let starting = State.make () in
214   let phi0, trans0, states0 =
215     compile_axis_test
216       Self
217       (QNameSet.any, Tree.NodeKind.Node)
218       (F.stay starting)
219       trans
220       states
221   in
222   let mstates, trans, states = loop l trans0 states0 phi0 in
223   starting, mstates, trans, states
224 ;;
225
226 let path p =
227   let sstates, mstates, trans, states =
228     List.fold_left (fun (ass, ams, atrs, asts) p ->
229       let ss, ms, natrs, nasts =
230         match p with
231         | Absolute l | Relative l -> compile_top_level_step_list l atrs asts
232       in
233       (StateSet.add ss ass),
234       (StateSet.add ms ams),
235       natrs,
236       nasts) (StateSet.empty, StateSet.empty, [], StateSet.empty) p
237   in
238   let builder = Ata.Builder.make () in
239   StateSet.iter
240     (Ata.Builder.add_state builder ~starting:true) sstates;
241   StateSet.iter
242     (Ata.Builder.add_state builder ~selecting:true) mstates;
243   StateSet.iter
244     (Ata.Builder.add_state builder) states;
245   List.iter (fun (q, l) ->
246     List.iter (fun (lab, phi) ->
247       Ata.Builder.add_trans builder q lab phi
248     ) l) trans;
249   Ata.Builder.finalize builder