8 type query_tree_desc = Binop of op * query_tree * query_tree
9 | Axis of Xpath.Ast.axis * query_tree
12 | Tag of QNameSet.t * Tree.NodeKind.t
14 and op = Union | Inter | Diff
17 mutable desc : query_tree_desc;
27 (q1.id == q2.id && q1.id != -1) ||
28 match q1.desc, q2.desc with
29 | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)-> op1==op2&& (equal qt1 qt3 && equal qt2 qt4)
31 | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && equal qt1 qt2
32 | Tag(t1,k1),Tag(t2,k2) -> t1==t2&& k1==k2
33 | Dom,Dom | Start,Start -> true
35 and compare_axis a1 a2 =
37 Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
38 | FollowingSibling , FollowingSibling
39 | PrecedingSibling , PrecedingSibling
40 | Preceding , Preceding | Following , Following -> true
41 | Descendant b1, Descendant b2 -> b1==b2
42 | Ancestor b1, Ancestor b2 -> b1==b2
46 if q.hash != -1 then q.hash
47 else match q.desc with
50 | Tag(s,_) -> 5 + 17*QNameSet.hash s
51 | Axis(a,q) -> 7 + 17 * Hashtbl.hash a + 23* hash q
52 | Binop(op,q1,q2) -> 11 + 17* Hashtbl.hash op + 23* hash q1 + 27* hash q2
57 module QTreeHash = Hashtbl.Make(QTree)
59 let compare_node tree a b =
60 compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b )
65 | Noeud of 'a tas * 'a * 'a tas
67 let comp_node tree a b = (Naive_tree.preorder tree a )< (Naive_tree.preorder tree b )
72 | Noeud (t1,racine,t2) -> 1+ size t1 + size t2
77 | Noeud (t1,racine,t2) -> 1 + max (height t1) (height t2)
83 | Noeud (t1,racine,t2) -> 1 + min (aux t1) (aux t2)
85 let max_h = height t in
87 if max_h- min_h >1 then false
91 if not (equilibre t) then false
96 | Noeud (Vide,racine,Vide) -> racine >= n
97 | Noeud (t1,racine, t2) -> (aux racine t1) && (aux racine t2)
103 Vide -> failwith "Tas vide"
104 | Noeud (t1, racine, t2) -> begin
108 | Noeud (t3,r1,t4),Noeud (t5,r2,t6) -> if comp_node tree r1 r2 then Noeud (pop tree t1, r1,t2)
109 else Noeud (pop tree t2, r2, t1)
112 let rec push tree t a =
114 Vide -> Noeud(Vide,a,Vide)
115 | Noeud (t1,r,t2) -> if comp_node tree a r then Noeud (t2,a,push tree t1 r)
116 else Noeud(t2,r, push tree t1 a)
118 let tas_of_list tree l =
119 List.fold_left (push tree) Vide l
121 let is_empty t = (size t )== 0
123 let rec list_of_tas tree t =
126 | Noeud(t1,r,t2) -> r::(list_of_tas tree (pop tree t))
128 let sort_of_list tree l =
129 let t = tas_of_list tree l in
134 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
137 let rec union_list t l1 l2 =
141 | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
142 else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
143 else h1 ::(union_list t ll1 ll2)
145 let rec inter_list t l1 l2 =
149 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
150 else if (comp_node t h2 h1) then inter_list t l1 ll2
151 else h1 :: (inter_list t ll1 ll2)
153 let rec diff_list t l1 l2 =
157 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
158 else if (comp_node t h2 h1) then h2 :: (diff_list t l1 ll2)
159 else diff_list t ll1 ll2
161 let print_node_list tree l =
162 List.iter (fun node ->
163 Naive_tree.print_xml stdout tree node;
167 let rec print_query_tree fmt q =
169 Dom -> Format.fprintf fmt "Dom"
170 | Start -> Format.fprintf fmt "Start"
171 | Tag (t,k) -> Format.fprintf fmt "Tag(%a, %a)" QNameSet.print t Tree.NodeKind.print k
173 Format.fprintf fmt "%a(%a)" Xpath.Ast.print_axis a print_query_tree q
174 | Binop (op,q1,q2) ->
175 Format.fprintf fmt "%a(%a, %a)"
180 and print_binop fmt o =
182 | Union -> Format.fprintf fmt "Union"
183 | Inter -> Format.fprintf fmt "Inter"
184 | Diff -> Format.fprintf fmt "Diff"
186 let rec eval_relation tree m n =
189 | Firstchild -> Naive_tree.first_child tree n
190 | Nextsibling -> Naive_tree.next_sibling tree n
191 | Revfirstchild -> Naive_tree.parent_of_first tree n
192 | Prevsibling -> Naive_tree.prev_sibling tree n
195 parametres : tree l'arbre xml
196 ls l'ensemble de noeuds
198 retour : l'ensemble de noeuds qui correspondent ॆ la relation r
204 let rec eval_move tree ls m =
207 | r -> List.filter (fun n -> n != Naive_tree.nil)
208 (List.map (eval_relation tree r) ls)
212 parametres : tree l'arbre xml
213 ls l'ensemble de noeuds
215 retour : l'ensemble de noeuds qui correspondent ॆ des relations lr
218 and eval_star tree ls lr =
219 let h = Hashtbl.create 17 in
220 let q = Queue.create () in
221 List.iter ( fun e -> Queue.add e q ) ls;
222 while not (Queue.is_empty q ) do
223 let n = Queue.pop q in
224 if not (Hashtbl.mem h n) then begin
226 List.iter ( fun r -> let m = eval_relation tree r n in
227 if m != Naive_tree.nil && not (Hashtbl.mem h m ) then begin
233 let l = Hashtbl.fold (fun k _ acc -> k::acc) h [] in
236 Tas.sort_of_list tree l
237 List.sort (compare_node tree) l*)
239 let rec compare_node_list tree l1 l2 =
244 | n1::ll1,n2::ll2 -> let b = compare_node tree n1 n2 in
245 if b=0 then compare_node_list tree ll1 ll2
248 let get_descendant tree ln =
250 if n == Naive_tree.nil then acc
251 else let n1 = Naive_tree.first_child tree n in
252 let acc1 = aux n1 (n::acc) in
253 let n2 = Naive_tree.next_sibling tree n in
254 let acc2 = aux n2 acc1 in
257 let l = List.fold_left (fun acc n -> if List.mem n acc then acc
258 else let n1 = Naive_tree.first_child tree n in
263 let get_child tree ln =
265 if n == Naive_tree.nil then acc
267 let n1 = Naive_tree.next_sibling tree n in
270 let ll = List.map (fun n->
271 let n1 = Naive_tree.first_child tree n in
272 let res = aux n1 [] in
275 List.fold_left (fun acc l -> union_list tree acc l) [] ll
278 let get_followingSibling tree ln =
280 let n1 = Naive_tree.next_sibling tree n in
281 if n1 == Naive_tree.nil then acc
282 else aux n1 (n1::acc)
284 let ll = List.map (fun n -> let res = aux n [] in
286 List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll
289 let rec get_firstBling tree n pred =
290 if n== Naive_tree.nil then pred
291 else get_firstBling tree (Naive_tree.prev_sibling tree n) n
293 let get_parent tree ln =
294 let l = List.fold_left (fun acc n ->
295 let n1 = get_firstBling tree n Naive_tree.nil in
296 let n2 = Naive_tree.parent_of_first tree n1 in
297 if n2 == Naive_tree.nil or List.mem n2 acc then acc
298 else union_list tree [n2] acc
305 let get_ancestor tree ln =
306 let rec aux tree l1 acc =
309 | _ -> let ll1 = get_parent tree l1 in
310 let acc1 = union_list tree acc ll1 in
313 let l = aux tree ln [] in
316 let get_preSibling tree ln =
318 let n1 = Naive_tree.prev_sibling tree n in
319 if n1 == Naive_tree.nil then acc
320 else aux n1 (n1::acc)
322 let ll = List.map (fun n -> aux n [] ) ln in
323 List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll
327 let rec eval_axis tree ls a =
328 let open Xpath.Ast in
332 | Attribute -> get_child tree ls
334 | Child -> get_child tree ls
336 | Descendant c -> let ls2 = get_descendant tree ls in
339 else union_list tree ls2 ls
343 | FollowingSibling -> get_followingSibling tree ls
345 | Parent -> get_parent tree ls
348 let ls3 = get_ancestor tree ls in
351 else union_list tree ls3 ls
355 | PrecedingSibling -> get_preSibling tree ls
357 | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
358 let ls3 = eval_axis tree ls2 PrecedingSibling in
359 let lp = eval_axis tree ls3 (Descendant true) in
362 | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
363 let ls3 = eval_axis tree ls2 FollowingSibling in
364 let lf = eval_axis tree ls3 (Descendant true) in