1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
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 *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-04-04 18:43:48 CEST by Kim Nguyen>
23 let ( => ) a b = (a, b)
24 let ( ++ ) a b = Ata.SFormula.or_ a b
25 let ( %% ) a b = Ata.SFormula.and_ a b
26 let ( @: ) a b = StateSet.add a b
28 module F = Ata.SFormula
31 let node_set = QNameSet.remove QName.document QNameSet.any
32 let star_set = QNameSet.diff QNameSet.any (
33 List.fold_right (QNameSet.add)
34 [ QName.document; QName.text; QName.comment ]
36 let root_set = QNameSet.singleton QName.document
38 (* [compile_axis_test axis test q phi trans states] Takes an xpath
39 [axis] and node [test], a formula [phi], a list of [trans]itions
40 and a set of [states] and returns a formula [phi'], a new set of
41 transitions, and a new set of states such that [phi'] holds iff
42 there exists a node reachable through [axis]::[test] where [phi]
46 let compile_axis_test axis (test,kind) phi trans states =
47 let q = State.make () in
48 let phi = match kind with
49 Tree.NodeKind.Node -> phi
50 | _ -> phi %% F.mk_kind kind
52 let phi', trans', states' =
56 (q, [ test => phi ]) :: trans,
62 QNameSet.any => F.next_sibling q ]) :: trans,
68 QNameSet.any => F.first_child q ++ F.next_sibling q;
72 let q' = State.make () in
73 (F.or_ (F.stay q) (F.first_child q'),
75 QNameSet.any => F.first_child q' ++ F.next_sibling q';
77 (q, [ test => phi]):: trans,
81 let q' = State.make () in
82 let move = F.parent q ++ F.previous_sibling q' in
85 :: (q', [ QNameSet.any => move ]) :: trans,
89 let q' = State.make () in
90 let move = F.parent q ++ F.previous_sibling q' in
91 (if self then F.stay q else move),
93 QNameSet.any => move ])
94 :: (q', [ QNameSet.any => move ]) :: trans,
97 | FollowingSibling | PrecedingSibling ->
99 if axis = PrecedingSibling then
101 else F.next_sibling q
105 QNameSet.any => move ]) :: trans,
111 QNameSet.any => F.next_sibling q]) :: trans,
116 phi', trans', q @: states'
118 let rec compile_expr e trans states =
120 | Binop (e1, (And|Or as op), e2) ->
121 let phi1, trans1, states1 = compile_expr e1 trans states in
122 let phi2, trans2, states2 = compile_expr e2 trans1 states1 in
123 (if op = Or then phi1 ++ phi2 else phi1 %% phi2),
126 | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
127 let phi, trans0, states0 = compile_expr e0 trans states in
128 (Ata.SFormula.not_ phi),
131 | Path p -> compile_path p trans states
134 and compile_path paths trans states =
135 List.fold_left (fun (aphi, atrans, astates) p ->
136 let phi, ntrans, nstates = compile_single_path p atrans astates in
137 (Ata.SFormula.or_ phi aphi),
139 nstates) (Ata.SFormula.false_,trans,states) paths
141 and compile_single_path p trans states =
145 (Ancestor false, (QNameSet.singleton QName.document,
146 Tree.NodeKind.Node), [])
148 | Relative steps -> steps
150 compile_step_list steps trans states
152 and compile_step_list l trans states =
154 | [] -> Ata.SFormula.true_, trans, states
155 | (axis, test, elist) :: ll ->
156 let phi0, trans0, states0 = compile_step_list ll trans states in
157 let phi1, trans1, states1 =
158 compile_axis_test axis test phi0 trans0 states0
160 List.fold_left (fun (aphi, atrans, astates) e ->
161 let ephi, etrans, estates = compile_expr e atrans astates in
162 aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
165 Compile the top-level XPath query in reverse (doing downward
166 to the last top-level state):
167 /a0::t0[p0]/.../an-1::tn-1[pn-1]/an::tn[pn] becomes:
169 self::tn[pn]/inv(an)::(tn-1)[pn-1]/.../inv(a1)::t0[p0]/inv(a0)::document()]
171 /child::a/attribute::b
172 self::@b/parent::a/parent::doc()
175 let compile_top_level_step_list l trans states =
176 let rec loop l trans states phi_above =
179 | (axis, (test,kind), elist) :: ll ->
180 let phi0, trans0, states0 =
181 compile_axis_test (invert_axis axis)
182 (QNameSet.any, Tree.NodeKind.Node)
183 phi_above trans states
185 (* Only select attribute nodes if the previous axis
188 if axis != Attribute then
189 phi0 %% (Ata.SFormula.not_ Ata.SFormula.is_attribute)
194 let phi1, trans1, states1 =
195 List.fold_left (fun (aphi, atrans, astates) e ->
196 let ephi, etrans, estates = compile_expr e atrans astates in
197 aphi %% ephi, etrans, estates) (phi0, trans0, states0) elist
199 let _, trans2, states2 =
200 compile_axis_test Self (test,kind) phi1 trans1 states1
203 StateSet.choose (StateSet.diff states2 states1)
205 marking_state, trans2, states2
207 let phi1, trans1, states1 =
208 compile_axis_test Self (test,kind) phi0 trans0 states0
210 let phi2, trans2, states2 =
211 List.fold_left (fun (aphi, atrans, astates) e ->
212 let ephi, etrans, estates = compile_expr e atrans astates in
213 aphi %% ephi, etrans, estates) (phi1, trans1, states1) elist
215 loop ll trans2 states2 phi2
217 let phi0, trans0, states0 =
220 (QNameSet.singleton QName.document, Tree.NodeKind.Node)
225 loop l trans0 states0 phi0
229 let mstates, trans, states = List.fold_left (fun (ams, atrs, asts) p ->
230 let ms, natrs, nasts =
232 | Absolute l | Relative l -> compile_top_level_step_list l atrs asts
234 (StateSet.add ms ams), natrs, nasts) (StateSet.empty, [], StateSet.empty) p
236 let a = Ata.create states mstates in
237 List.iter (fun (q, l) ->
238 List.iter (fun (lab, phi) ->
239 Ata.add_trans a q lab phi
241 Ata.complete_transitions a;
242 Ata.normalize_negations a;