4 let table_qtree = QTreeHash.create 97
7 let element_by_tag tree tagset kind = let v = Bitvector.create (Naive_tree.size tree) in
8 for i=0 to (Bitvector.length v)-1 do
9 let c = Naive_tree.by_preorder tree i in
10 if (Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
11 QNameSet.mem (Naive_tree.tag tree c) tagset )
12 then Bitvector.set v i true
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 ();
114 let mini_table = QTreeHash.create 17
116 let rec minimize_qtree q =
120 QTreeHash.find mini_table q
124 (Start | Dom | Tag _) as d -> d
125 | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
126 let mq2 = minimize_qtree q2 in
128 | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
132 q.hash <- QTree.hash q;
135 QTreeHash.add mini_table q q;
140 let rec eval_qtree tree start q =
144 QTreeHash.find table_qtree q
149 | Dom -> Bitvector.create ~init:true (Naive_tree.size tree)
150 (*let v = Bitvector.create (Naive_tree.size tree) in
151 for i=0 to (Bitvector.length v)-1 do
152 Bitvector.set v i true
155 | Tag (t,k) -> element_by_tag tree t k
156 | Axis (a,q1) -> let v = eval_qtree tree start q1 in
158 | Binop (op,q1,q2)-> begin
159 let v1 = eval_qtree tree start q1 in
160 let v2 = eval_qtree tree start q2 in
162 | Union -> Bitvector.union v1 v2
163 | Inter -> Bitvector.inter v1 v2
164 | Diff -> Bitvector.diff v1 v2
167 QTreeHash.add table_qtree q res;
168 compteur := !compteur + Bitvector.length res; (*????8*)
172 (* debug tree q resultat;*)