1 (***********************************************************************)
5 (* Lucca Hirschi, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2012 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 (***********************************************************************)
19 exception Not_core_XPath
20 (** Raised whenever the XPath query contains not implemented structures *)
22 let pr_er = Format.err_formatter
27 (* Buidling of the ASTA step by step with a special case for the last
28 step. Then add a top most state. Each function modifies asta. *)
29 let asta = Asta.empty in
30 (* builds asta from the bottom of the query *)
31 let rec trans = function
33 | s :: tl -> trans tl; trans_step s (ax_(List.hd tl))
36 (* Add THE top most state for top-level query (done in the end) *)
38 let top_st = Asta.new_state () in
40 List.fold_left (fun acc x -> ((`Left *+ x) +| acc))
41 (Formula.false_) (Asta.top_states asta) in
42 Asta.add_quer asta top_st;
44 Asta.add_top asta top_st;
45 Asta.add_bot asta top_st; (* for trees which are leaves *)
46 Asta.add_tr asta (top_st, Asta.any_label, or_top) true
48 (* A selecting state is needed *)
49 and trans_last (ax,test,pred) =
50 let fo_p = trans_pr pred in
51 let q,q' = Asta.new_state(), Asta.new_state() in
52 Asta.add_selec asta q';
54 Asta.add_quer asta q';
57 Asta.add_bot asta q; (* q' \notin B !! *)
58 let Simple lab = test in
59 let tr_selec = (q', lab, fo_p)
60 and tr_q = (q, Asta.any_label, form_propa_selec q q' ax) in
61 Asta.add_tr asta tr_selec true;
62 Asta.add_tr asta tr_q true
64 and form_next_step ax_next top_states_next form_pred =
66 | Self -> (form_pred) *& (* (\/ top_next) /\ predicate *)
67 (List.fold_left (fun acc x -> (`Self *+ x ) +| acc)
68 Formula.false_ top_states_next)
69 | FollowingSibling -> (form_pred) *& (* (\/ top_next) /\ predicate *)
70 (List.fold_left (fun acc x -> (`Right *+ x ) +| acc)
71 Formula.false_ top_states_next)
72 | _ -> (form_pred) *& (* (\/ top_next) /\ predicate *)
73 (List.fold_left (fun acc x -> (`Left *+ x ) +| acc)
74 Formula.false_ top_states_next)
76 (* Add a new state and its transitions for the step *)
77 and trans_step (ax,test,pred) ax_next =
78 let fo_p = trans_pr pred
79 and q = Asta.new_state() in
80 let Simple label = test
81 and form_next = form_next_step ax_next (Asta.top_states asta) fo_p in
82 let tr_next = (q, label, form_next)
83 and tr_propa = (q, Asta.any_label, form_propa q ax) in
87 Asta.add_tr asta tr_next true;
88 Asta.add_tr asta tr_propa true;
92 (* Translating of predicates. Either we apply De Morgan rules
93 in xPath.parse or here *)
94 and trans_pr = function
95 | Expr True -> Formula.true_
96 | Expr False -> Formula.false_
97 | Or (p_1,p_2) -> trans_pr(p_1) +| trans_pr(p_2)
98 | And (p_1,p_2) -> trans_pr(p_1) *& trans_pr(p_2)
99 | Not (Expr Path q) -> (trans_pr_path false q)
100 | Expr Path q -> (trans_pr_path true q)
101 | x -> print_predicate pr_er x; raise Not_core_XPath
103 (* Builds asta for predicate and gives the formula which must be satsified *)
104 and trans_pr_path posi = function
105 | Relative [] -> if posi then Formula.true_ else Formula.false_
106 | Relative steps -> List.fold_left
107 (fun acc x -> if posi then (`Left *+ x) +| acc else (`Left *- x) +| acc)
108 Formula.false_ (trans_pr_step_l steps)
109 | AbsoluteDoS steps as x -> print pr_er x; raise Not_core_XPath
110 | Absolute steps as x -> print pr_er x; raise Not_core_XPath
112 (* Builds asta for a predicate query and give the formula *)
113 and trans_pr_step_l = function
114 | [step] -> trans_pr_step [] step (Self) (* Self doesn't matter since [] *)
115 | step :: tl -> let list_top = trans_pr_step_l tl in
116 trans_pr_step list_top step (ax_ (List.hd tl))
117 | [] -> failwith "Can not happened! 1"
119 (* Add a step on the top of a list of states in a predicate *)
120 and trans_pr_step list (ax,test,pred) ax_next =
124 else form_next_step ax_next list (trans_pr pred)
125 and q = Asta.new_state()
126 and Simple label = test in
127 let tr_next = (q,label, form_next)
128 and tr_propa = (q, Asta.any_label, form_propa q ax) in
129 Asta.add_reco asta q;
130 Asta.add_tr asta tr_next false;
131 Asta.add_tr asta tr_propa false;
132 [q] (* always one element here, but more with self
134 (* Gives the propagation formula *)
135 and form_propa q = function
136 | Child -> `Right *+ q
137 | Descendant -> (`Left *+ q +| `Right *+ q)
139 | FollowingSibling -> `Right *+ q
140 | x -> print_axis pr_er x; raise Not_core_XPath
142 (* The same with a selecting state *)
143 and form_propa_selec q q' = function
144 | Child -> `Right *+ q +| `Right *+ q'
145 | Descendant -> (`Left *+ q +| `Right *+ q) +| (`Left *+ q' +| `Right *+ q')
146 | Self -> `Self *+ q'
147 | FollowingSibling -> `Right *+ q +| `Self *+ q'
148 | x -> print_axis pr_er x; raise Not_core_XPath
151 (* Match the top-level query *)
153 | Absolute steps -> trans steps; trans_init(); asta
154 | AbsoluteDoS steps as x -> print pr_er x; raise Not_core_XPath
155 | Relative steps as x -> print pr_er x; raise Not_core_XPath