Une version marche bien avec bitvector
[tatoo.git] / src / query_tree.ml
1 open Table
2
3 let query_tree_size = ref 0
4
5 let table_qtree = QTreeHash.create 97
6                         
7
8 let element_by_tag tree tagset kind = let v = Bitvector.create (Naive_tree.size tree) in
9                                       for i=0 to (Bitvector.length v)-1 do
10                                         let c = Naive_tree.by_preorder tree i in
11                                         if (Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
12                                               QNameSet.mem (Naive_tree.tag tree c) tagset )
13                                         then Bitvector.set v i true
14                                       done;
15                                       v
16                          
17
18 let mk_node q = {desc = q; id = -1; hash = -1}
19
20 let rec compile_single_path p =
21   let open Xpath.Ast in
22       match p with
23         | Absolute p | Relative p -> compile_step_list (List.rev p)
24       
25 and compile_step_list p = 
26     match p with
27       | [] -> mk_node Start
28       | (a,(test,kind),el) :: r ->
29         let qtree = compile_step_list r in
30         let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in
31         List.fold_left (fun acc e ->
32           mk_node (Binop(Inter, acc, compile_expr e))) res el  
33
34   and compile_expr  (e : Xpath.Ast.expr )  = match e with
35     | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
36       let qtree = compile_expr e0 in
37       mk_node (Binop (Diff , mk_node (Dom), qtree))
38
39     | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
40                           let qtree2 = compile_expr e2 in 
41                           begin
42                             match op with 
43                               | Or -> mk_node (Binop (Union , qtree1,qtree2))
44                               | And -> mk_node (Binop (Inter ,qtree1,qtree2))
45                               | _ -> failwith "Unknown operator"
46                           end
47     | Path p -> compile_path_rev p
48     | _ -> failwith "Unknown expression"
49       
50   and compile_path_rev p = 
51     match p with
52       | [] -> assert false
53       | [p] -> compile_single_path_rev p  
54       | 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         
56   and compile_single_path_rev p = 
57     match p with
58       | Absolute p | Relative p -> compile_step_list_rev p 
59
60   and compile_step_list_rev p = match p with
61     | [] -> mk_node Dom        
62     | (a,(test,kind),el) :: r -> 
63       let qtree = compile_step_list_rev r in
64       let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in
65       let qtree2 = List.fold_left (fun acc e ->
66         mk_node (Binop(Inter, acc, compile_expr e))) res el in
67       let a_rev = axis_rev a in
68       mk_node (Axis (a_rev , qtree2))
69                                                 
70       
71     and axis_rev a =
72       let open Xpath.Ast in
73           match a with
74               Self -> Self
75             | Attribute -> Parent
76             | Child -> Parent
77             | Descendant b -> 
78               if not b then (Ancestor false)
79               else (Ancestor true)    (* true = descendant-or-self, false = descendant *)
80             | FollowingSibling -> PrecedingSibling
81             | Parent -> Child
82             | Ancestor b -> 
83               if not b then (Descendant false) 
84               else (Descendant true)  (* true = ancestor-or-self, false = ancestor *)
85             | PrecedingSibling -> FollowingSibling
86             | Preceding -> Following
87             | Following -> Preceding
88             
89             
90 let compile_xpath p = match p with
91   | [] -> assert false
92   | [p] -> compile_single_path p
93   | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
94
95
96
97
98 let do_debug = ref false
99
100 let debug tree q l =
101   if !do_debug then begin
102     Format.fprintf Format.std_formatter "Evaluation de: ";
103     print_query_tree Format.std_formatter q;
104     Format.fprintf Format.std_formatter "\nResultat: %i\n"
105     (List.length l);
106     Format.pp_print_flush Format.std_formatter ();
107     print_node_list tree l;
108     (*List.iter
109       (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
110     Format.fprintf Format.std_formatter "\n----------------\n";
111     Format.pp_print_flush Format.std_formatter ();
112   end
113
114 let mini_id = ref 0
115 let mini_table = QTreeHash.create 17 
116
117 let rec minimize_qtree q =
118    if q.id != -1 then q
119    else
120     try
121       QTreeHash.find mini_table q
122     with Not_found ->
123       let mdesc =
124         match q.desc with
125             (Start | Dom | Tag _) as d -> d
126           | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
127                                let mq2 = minimize_qtree q2 in
128                                Binop(op,mq1,mq2)
129           | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
130                           Axis(a,mq1)         
131       in
132       q.desc <- mdesc;
133       q.hash <- QTree.hash q;
134       q.id <- !mini_id;
135       incr mini_id;
136       QTreeHash.add mini_table q q;
137       q
138
139
140           
141 let rec eval_qtree tree start q =
142   let resultat = 
143     begin
144       try
145         QTreeHash.find table_qtree q
146       with Not_found -> 
147         let res =
148         match q.desc with
149           | Start -> start
150           | Dom -> Bitvector.create ~init:true (Naive_tree.size tree)
151           | Tag (t,k) -> element_by_tag tree t k                     
152           | Axis (a,q1) -> let v = eval_qtree tree start q1 in
153                            eval_axis tree v a                      
154           | Binop (op,q1,q2)-> begin
155             let v1 = eval_qtree tree start q1 in
156             let v2 = eval_qtree tree start q2 in
157             Table.node_compteur := !Table.node_compteur + 2*Bitvector.length v1; 
158             match op with         
159               | Union -> Bitvector.union v1 v2      
160               | Inter -> Bitvector.inter v1 v2
161               | Diff -> Bitvector.diff  v1 v2
162           end
163         in
164         QTreeHash.add table_qtree q res;
165         res
166     end
167   in
168  (* debug tree q resultat;*)
169   incr query_tree_size;
170   resultat