4 let table_qtree = QTreeHash.create 97
6 let all_nodes tree = let root = Naive_tree.root tree in
7 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 ();
113 let mini_table = QTreeHash.create 17
115 let rec minimize_qtree q =
119 QTreeHash.find mini_table q
123 (Start | Dom | Tag _) as d -> d
124 | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
125 let mq2 = minimize_qtree q2 in
127 | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
131 q.hash <- QTree.hash q;
134 QTreeHash.add mini_table q q;
139 let rec eval_qtree tree start q =
143 QTreeHash.find table_qtree q
148 | Dom -> all_nodes tree
149 | Tag (t,k) -> element_by_tag tree t k
150 | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
152 | Binop (op,q1,q2)-> begin
153 let ls1 = eval_qtree tree start q1 in
154 let ls2 = eval_qtree tree start q2 in
156 | Union -> union_list tree ls1 ls2
157 | Inter -> inter_list tree ls1 ls2
158 | Diff -> diff_list tree ls1 ls2
161 QTreeHash.add table_qtree q res;
162 compteur := !compteur + (List.length res);
166 debug tree q resultat;