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
11 let element_by_tag tree tagset kind = let dom = all_nodes tree in
13 Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
14 QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
16 let mk_node q = {desc = q; id = -1; hash = -1}
18 let rec compile_single_path p =
21 | Absolute p | Relative p -> compile_step_list (List.rev p)
23 and compile_step_list p =
26 | (a,(test,kind),el) :: r ->
27 let qtree = compile_step_list r in
28 let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in
29 List.fold_left (fun acc e ->
30 mk_node (Binop(Inter, acc, compile_expr e))) res el
32 and compile_expr (e : Xpath.Ast.expr ) = match e with
33 | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
34 let qtree = compile_expr e0 in
35 mk_node (Binop (Diff , mk_node (Dom), qtree))
37 | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
38 let qtree2 = compile_expr e2 in
41 | Or -> mk_node (Binop (Union , qtree1,qtree2))
42 | And -> mk_node (Binop (Inter ,qtree1,qtree2))
43 | _ -> failwith "Unknown operator"
45 | Path p -> compile_path_rev p
46 | _ -> failwith "Unknown expression"
48 and compile_path_rev p =
51 | [p] -> compile_single_path_rev p
52 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r
54 and compile_single_path_rev p =
56 | Absolute p | Relative p -> compile_step_list_rev p
58 and compile_step_list_rev p = match p with
60 | (a,(test,kind),el) :: r ->
61 let qtree = compile_step_list_rev r in
62 let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in
63 let qtree2 = List.fold_left (fun acc e ->
64 mk_node (Binop(Inter, acc, compile_expr e))) res el in
65 let a_rev = axis_rev a in
66 mk_node (Axis (a_rev , qtree2))
76 if not b then (Ancestor false)
77 else (Ancestor true) (* true = descendant-or-self, false = descendant *)
78 | FollowingSibling -> PrecedingSibling
81 if not b then (Descendant false)
82 else (Descendant true) (* true = ancestor-or-self, false = ancestor *)
83 | PrecedingSibling -> FollowingSibling
84 | Preceding -> Following
85 | Following -> Preceding
88 let compile_xpath p = match p with
90 | [p] -> compile_single_path p
91 | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
96 let do_debug = ref false
99 if !do_debug then begin
100 Format.fprintf Format.std_formatter "Evaluation de: ";
101 print_query_tree Format.std_formatter q;
102 Format.fprintf Format.std_formatter "\nResultat: %i\n"
104 Format.pp_print_flush Format.std_formatter ();
105 print_node_list tree l;
107 (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
108 Format.fprintf Format.std_formatter "\n----------------\n";
109 Format.pp_print_flush Format.std_formatter ();
115 let rec compare_query_tree q1 q2 =
116 q1.id==q2.id|| match q1.desc,q2.desc with
117 | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4)
118 || (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
119 | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
120 | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
121 | Dom,Dom | Start,Start ->true
124 and compare_axis a1 a2 =
126 Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
127 | FollowingSibling , FollowingSibling
128 | PrecedingSibling , PrecedingSibling
129 | Preceding , Preceding | Following , Following -> true
130 | Descendant b1, Descendant b2 -> b1==b2
131 | Ancestor b1, Ancestor b2 -> b1==b2
135 let mini_table = QTreeHash.create 17
137 let rec minimize_qtree q =
141 QTreeHash.find mini_table q
145 (Start | Dom | Tag _) as d -> d
146 | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
147 let mq2 = minimize_qtree q2 in
149 | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
153 q.hash <- QTree.hash q;
156 QTreeHash.add mini_table q q;
161 let rec eval_qtree tree start q =
165 QTreeHash.find table_qtree q
170 | Dom -> all_nodes tree
171 | Tag (t,k) -> element_by_tag tree t k
172 | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
174 | Binop (op,q1,q2)-> begin
175 let ls1 = eval_qtree tree start q1 in
176 let ls2 = eval_qtree tree start q2 in
178 | Union -> union_list tree ls1 ls2
179 | Inter -> inter_list tree ls1 ls2
180 | Diff -> diff_list tree ls1 ls2
183 QTreeHash.add table_qtree q res;
184 compteur := !compteur + (List.length res);
188 debug tree q resultat;