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 let acc1 = get_descendant tree [root] in
12 let element_by_tag tree tagset kind = let dom = all_nodes tree in
14 Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
15 QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
17 let mk_node q = {desc = q; id = -1; hash = -1}
19 let rec compile_single_path p =
22 | Absolute p | Relative p -> compile_step_list (List.rev p)
24 and compile_step_list p =
27 | (a,(test,kind),el) :: r ->
28 let qtree = compile_step_list r in
29 let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in
30 List.fold_left (fun acc e ->
31 mk_node (Binop(Inter, acc, compile_expr e))) res el
33 and compile_expr (e : Xpath.Ast.expr ) = match e with
34 | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
35 let qtree = compile_expr e0 in
36 mk_node (Binop (Diff , mk_node (Dom), qtree))
38 | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
39 let qtree2 = compile_expr e2 in
42 | Or -> mk_node (Binop (Union , qtree1,qtree2))
43 | And -> mk_node (Binop (Inter ,qtree1,qtree2))
44 | _ -> failwith "Unknown operator"
46 | Path p -> compile_path_rev p
47 | _ -> failwith "Unknown expression"
49 and compile_path_rev p =
52 | [p] -> compile_single_path_rev p
53 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r
55 and compile_single_path_rev p =
57 | Absolute p | Relative p -> compile_step_list_rev p
59 and compile_step_list_rev p = match p with
61 | (a,(test,kind),el) :: r ->
62 let qtree = compile_step_list_rev r in
63 let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in
64 let qtree2 = List.fold_left (fun acc e ->
65 mk_node (Binop(Inter, acc, compile_expr e))) res el in
66 let a_rev = axis_rev a in
67 mk_node (Axis (a_rev , qtree2))
77 if not b then (Ancestor false)
78 else (Ancestor true) (* true = descendant-or-self, false = descendant *)
79 | FollowingSibling -> PrecedingSibling
82 if not b then (Descendant false)
83 else (Descendant true) (* true = ancestor-or-self, false = ancestor *)
84 | PrecedingSibling -> FollowingSibling
85 | Preceding -> Following
86 | Following -> Preceding
89 let compile_xpath p = match p with
91 | [p] -> compile_single_path p
92 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
97 let do_debug = ref false
100 if !do_debug then begin
101 Format.fprintf Format.std_formatter "Evaluation de: ";
102 print_query_tree Format.std_formatter q;
103 Format.fprintf Format.std_formatter "\nResultat: %i\n"
105 Format.pp_print_flush Format.std_formatter ();
106 print_node_list tree l;
108 (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
109 Format.fprintf Format.std_formatter "\n----------------\n";
110 Format.pp_print_flush Format.std_formatter ();
116 let rec compare_query_tree q1 q2 =
117 q1.id==q2.id|| match q1.desc,q2.desc with
118 | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4)
119 || (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
120 | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
121 | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
122 | Dom,Dom | Start,Start ->true
125 and compare_axis a1 a2 =
127 Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
128 | FollowingSibling , FollowingSibling
129 | PrecedingSibling , PrecedingSibling
130 | Preceding , Preceding | Following , Following -> true
131 | Descendant b1, Descendant b2 -> b1==b2
132 | Ancestor b1, Ancestor b2 -> b1==b2
136 let mini_table = QTreeHash.create 17
138 let rec minimize_qtree q =
142 QTreeHash.find mini_table q
146 (Start | Dom | Tag _) as d -> d
147 | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
148 let mq2 = minimize_qtree q2 in
150 | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
154 q.hash <- QTree.hash q;
157 QTreeHash.add mini_table q q;
162 let rec eval_qtree tree start q =
166 QTreeHash.find table_qtree q
171 | Dom -> all_nodes tree
172 | Tag (t,k) -> element_by_tag tree t k
173 | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
175 | Binop (op,q1,q2)-> begin
176 let ls1 = eval_qtree tree start q1 in
177 let ls2 = eval_qtree tree start q2 in
179 | Union -> union_list tree ls1 ls2
180 | Inter -> inter_list tree ls1 ls2
181 | Diff -> diff_list tree ls1 ls2
184 QTreeHash.add table_qtree q res;
185 compteur := !compteur + (List.length res);
189 debug tree q resultat;