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 )
62 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
65 let rec union_list t l1 l2 =
69 | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
70 else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
71 else h1 ::(union_list t ll1 ll2)
73 let rec inter_list t l1 l2 =
77 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
78 else if (comp_node t h2 h1) then inter_list t l1 ll2
79 else h1 :: (inter_list t ll1 ll2)
81 let rec diff_list t l1 l2 =
85 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
86 else if (comp_node t h2 h1) then h2 :: (diff_list t l1 ll2)
87 else diff_list t ll1 ll2
89 let print_node_list tree l =
90 List.iter (fun node ->
91 Naive_tree.print_xml stdout tree node;
95 let rec print_query_tree fmt q =
97 Dom -> Format.fprintf fmt "Dom"
98 | Start -> Format.fprintf fmt "Start"
99 | Tag (t,k) -> Format.fprintf fmt "Tag(%a, %a)" QNameSet.print t Tree.NodeKind.print k
101 Format.fprintf fmt "%a(%a)" Xpath.Ast.print_axis a print_query_tree q
102 | Binop (op,q1,q2) ->
103 Format.fprintf fmt "%a(%a, %a)"
108 and print_binop fmt o =
110 | Union -> Format.fprintf fmt "Union"
111 | Inter -> Format.fprintf fmt "Inter"
112 | Diff -> Format.fprintf fmt "Diff"
114 let rec eval_relation tree m n =
117 | Firstchild -> Naive_tree.first_child tree n
118 | Nextsibling -> Naive_tree.next_sibling tree n
119 | Revfirstchild -> Naive_tree.parent_of_first tree n
120 | Prevsibling -> Naive_tree.prev_sibling tree n
123 parametres : tree l'arbre xml
124 ls l'ensemble de noeuds
126 retour : l'ensemble de noeuds qui correspondent ॆ la relation r
132 let rec eval_move tree ls m =
135 | r -> List.filter (fun n -> n != Naive_tree.nil)
136 (List.map (eval_relation tree r) ls)
140 parametres : tree l'arbre xml
141 ls l'ensemble de noeuds
143 retour : l'ensemble de noeuds qui correspondent ॆ des relations lr
146 and eval_star tree ls lr =
147 let h = Hashtbl.create 17 in
148 let q = Queue.create () in
149 List.iter ( fun e -> Queue.add e q ) ls;
150 while not (Queue.is_empty q ) do
151 let n = Queue.pop q in
152 if not (Hashtbl.mem h n) then begin
154 List.iter ( fun r -> let m = eval_relation tree r n in
155 if m != Naive_tree.nil && not (Hashtbl.mem h m ) then begin
161 let l = Hashtbl.fold (fun k _ acc -> k::acc) h [] in
164 Tas.sort_of_list tree l
165 List.sort (compare_node tree) l*)
167 let rec compare_node_list tree l1 l2 =
172 | n1::ll1,n2::ll2 -> let b = compare_node tree n1 n2 in
173 if b=0 then compare_node_list tree ll1 ll2
176 let get_descendant tree ln =
178 if n == Naive_tree.nil then acc
179 else let n1 = Naive_tree.first_child tree n in
180 let acc1 = aux n1 (n::acc) in
181 let n2 = Naive_tree.next_sibling tree n in
182 let acc2 = aux n2 acc1 in
185 let l = List.fold_left (fun acc n -> if List.mem n acc then acc
186 else let n1 = Naive_tree.first_child tree n in
191 let get_child tree ln =
193 if n == Naive_tree.nil then acc
195 let n1 = Naive_tree.next_sibling tree n in
198 let ll = List.map (fun n->
199 let n1 = Naive_tree.first_child tree n in
200 let res = aux n1 [] in
203 List.fold_left (fun acc l -> union_list tree acc l) [] ll
206 let get_followingSibling tree ln =
208 let n1 = Naive_tree.next_sibling tree n in
209 if n1 == Naive_tree.nil then acc
210 else aux n1 (n1::acc)
212 let ll = List.map (fun n -> let res = aux n [] in
214 List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll
217 let rec get_firstBling tree n pred =
218 if n== Naive_tree.nil then pred
219 else get_firstBling tree (Naive_tree.prev_sibling tree n) n
221 let get_parent tree ln =
222 let l = List.fold_left (fun acc n ->
223 let n1 = get_firstBling tree n Naive_tree.nil in
224 let n2 = Naive_tree.parent_of_first tree n1 in
225 if n2 == Naive_tree.nil or List.mem n2 acc then acc
226 else union_list tree [n2] acc
233 let get_ancestor tree ln =
234 let rec aux tree l1 acc =
237 | _ -> let ll1 = get_parent tree l1 in
238 let acc1 = union_list tree acc ll1 in
241 let l = aux tree ln [] in
244 let get_preSibling tree ln =
246 let n1 = Naive_tree.prev_sibling tree n in
247 if n1 == Naive_tree.nil then acc
248 else aux n1 (n1::acc)
250 let ll = List.map (fun n -> aux n [] ) ln in
251 List.fold_left (fun acc l1 -> union_list tree acc l1) [] ll
255 let rec eval_axis tree ls a =
256 let open Xpath.Ast in
260 | Attribute -> get_child tree ls
262 | Child -> get_child tree ls
264 | Descendant c -> let ls2 = get_descendant tree ls in
267 else union_list tree ls2 ls
271 | FollowingSibling -> get_followingSibling tree ls
273 | Parent -> get_parent tree ls
276 let ls3 = get_ancestor tree ls in
279 else union_list tree ls3 ls
283 | PrecedingSibling -> get_preSibling tree ls
285 | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
286 let ls3 = eval_axis tree ls2 PrecedingSibling in
287 let lp = eval_axis tree ls3 (Descendant true) in
290 | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
291 let ls3 = eval_axis tree ls2 FollowingSibling in
292 let lf = eval_axis tree ls3 (Descendant true) in