4 let table_query_tree = Hashtbl.create 97
5 let table_qtree = QTreeHash.create 97
7 let all_nodes tree = let root = Naive_tree.root tree in
8 eval_axis tree [root] (Descendant true)
10 let element_by_tag tree tagset kind = let dom = all_nodes tree in
12 Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
13 QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
15 let mk_node q = {desc = q; id = -1; hash = -1}
17 let rec compile_single_path p =
20 | Absolute p | Relative p -> compile_step_list (List.rev p)
22 and compile_step_list p =
25 | (a,(test,kind),el) :: r ->
26 let qtree = compile_step_list r in
27 let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in
28 List.fold_left (fun acc e ->
29 mk_node (Binop(Inter, acc, compile_expr e))) res el
31 and compile_expr (e : Xpath.Ast.expr ) = match e with
32 | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
33 let qtree = compile_expr e0 in
34 mk_node (Binop (Diff , mk_node (Dom), qtree))
36 | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
37 let qtree2 = compile_expr e2 in
40 | Or -> mk_node (Binop (Union , qtree1,qtree2))
41 | And -> mk_node (Binop (Inter ,qtree1,qtree2))
42 | _ -> failwith "Unknown operator"
44 | Path p -> compile_path_rev p
45 | _ -> failwith "Unknown expression"
47 and compile_path_rev p =
50 | [p] -> compile_single_path_rev p
51 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r
53 and compile_single_path_rev p =
55 | Absolute p | Relative p -> compile_step_list_rev p
57 and compile_step_list_rev p = match p with
59 | (a,(test,kind),el) :: r ->
60 let qtree = compile_step_list_rev r in
61 let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in
62 let qtree2 = List.fold_left (fun acc e ->
63 mk_node (Binop(Inter, acc, compile_expr e))) res el in
64 let a_rev = axis_rev a in
65 mk_node (Axis (a_rev , qtree2))
75 if not b then (Ancestor false)
76 else (Ancestor true) (* true = descendant-or-self, false = descendant *)
77 | FollowingSibling -> PrecedingSibling
80 if not b then (Descendant false)
81 else (Descendant true) (* true = ancestor-or-self, false = ancestor *)
82 | PrecedingSibling -> FollowingSibling
83 | Preceding -> Following
84 | Following -> Preceding
87 let compile_xpath p = match p with
89 | [p] -> compile_single_path p
90 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
92 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
95 let rec union_list t l1 l2 =
99 | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
100 else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
101 else h1 ::(union_list t ll1 ll2)
103 let rec inter_list t l1 l2 =
107 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
108 else if (comp_node t h2 h1) then inter_list t l1 ll2
109 else h1 :: (inter_list t ll1 ll2)
111 let rec diff_list t l1 l2 =
115 | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
116 else if (comp_node t h2 h1) then h2 :: (diff_list t l1 ll2)
117 else diff_list t ll1 ll2
120 let do_debug = ref false
123 if !do_debug then begin
124 Format.fprintf Format.std_formatter "Evaluation de: ";
125 print_query_tree Format.std_formatter q;
126 Format.fprintf Format.std_formatter "\nResultat: %i\n"
128 Format.pp_print_flush Format.std_formatter ();
129 print_node_list tree l;
131 (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
132 Format.fprintf Format.std_formatter "\n----------------\n";
133 Format.pp_print_flush Format.std_formatter ();
139 let rec compare_query_tree q1 q2 =
140 q1.id==q2.id|| match q1.desc,q2.desc with
141 | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4)
142 || (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
143 | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
144 | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
145 | Dom,Dom | Start,Start ->true
148 and compare_axis a1 a2 =
150 Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
151 | FollowingSibling , FollowingSibling
152 | PrecedingSibling , PrecedingSibling
153 | Preceding , Preceding | Following , Following -> true
154 | Descendant b1, Descendant b2 -> b1==b2
155 | Ancestor b1, Ancestor b2 -> b1==b2
159 let rec eval_query_tree tree start q =
163 Hashtbl.find table_query_tree q
168 | Dom -> all_nodes tree
169 | Tag (t,k) -> element_by_tag tree t k
170 | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
172 | Binop (op,q1,q2)-> begin
173 let ls1 = eval_query_tree tree start q1 in
174 let ls2 = eval_query_tree tree start q2 in
176 | Union -> union_list tree ls1 ls2
177 | Inter -> inter_list tree ls1 ls2
178 | Diff -> diff_list tree ls1 ls2
181 let res = List.sort (Table.compare_node tree) res in
182 Hashtbl.add table_query_tree q res;
183 compteur := !compteur + (List.length res);
187 debug tree q resultat;
191 let mini_table = QTreeHash.create 17
193 let rec minimize_qtree q =
197 QTreeHash.find mini_table q
201 (Start | Dom | Tag _) as d -> d
202 | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
203 let mq2 = minimize_qtree q2 in
205 | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
209 q.hash <- QTree.hash q;
212 QTreeHash.add mini_table q q;
217 let rec eval_qtree tree start q =
221 QTreeHash.find table_qtree q
226 | Dom -> all_nodes tree
227 | Tag (t,k) -> element_by_tag tree t k
228 | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
230 | Binop (op,q1,q2)-> begin
231 let ls1 = eval_qtree tree start q1 in
232 let ls2 = eval_qtree tree start q2 in
234 | Union -> union_list tree ls1 ls2
235 | Inter -> inter_list tree ls1 ls2
236 | Diff -> diff_list tree ls1 ls2
239 let res = Tas.sort_of_list tree res in
240 (* let res = List.sort (Table.compare_node tree) res in*)
241 QTreeHash.add table_qtree q res;
242 compteur := !compteur + (List.length res);
246 debug tree q resultat;