5f3c113d2464a43cd4c127de14a31f99f093a9ec
[tatoo.git] / src / query_tree.ml
1 open Table
2
3 let compteur = ref 0
4 let table_query_tree = Hashtbl.create 97 
5 let table_qtree = QTreeHash.create 97
6
7 let all_nodes tree = let root = Naive_tree.root tree in
8                      eval_axis tree [root] (Descendant true)
9
10 let element_by_tag tree tagset kind = let dom = all_nodes tree in
11                               List.filter (fun c ->
12                                 Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
13                                 QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
14
15 let mk_node q = {desc = q; id = -1; hash = -1}
16
17 let rec compile_single_path p =
18   let open Xpath.Ast in
19       match p with
20         | Absolute p | Relative p -> compile_step_list (List.rev p)
21       
22 and compile_step_list p = 
23     match p with
24       | [] -> mk_node Start
25       | (a,(test,kind),el) :: r ->
26         let qtree = compile_step_list r in
27         let res = mk_node ( Binop ( Inter,mk_node( Axis (a,qtree)),mk_node (Tag (test,kind) )) ) in
28         List.fold_left (fun acc e ->
29           mk_node (Binop(Inter, acc, compile_expr e))) res el  
30
31   and compile_expr  (e : Xpath.Ast.expr )  = match e with
32     | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
33       let qtree = compile_expr e0 in
34       mk_node (Binop (Diff , mk_node (Dom), qtree))
35
36     | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
37                           let qtree2 = compile_expr e2 in 
38                           begin
39                             match op with 
40                               | Or -> mk_node (Binop (Union , qtree1,qtree2))
41                               | And -> mk_node (Binop (Inter ,qtree1,qtree2))
42                               | _ -> failwith "Unknown operator"
43                           end
44     | Path p -> compile_path_rev p
45     | _ -> failwith "Unknown expression"
46       
47   and compile_path_rev p = 
48     match p with
49       | [] -> assert false
50       | [p] -> compile_single_path_rev p  
51       | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path_rev p)) ) (compile_single_path_rev p) r
52         
53   and compile_single_path_rev p = 
54     match p with
55       | Absolute p | Relative p -> compile_step_list_rev p 
56
57   and compile_step_list_rev p = match p with
58     | [] -> mk_node Dom        
59     | (a,(test,kind),el) :: r -> 
60       let qtree = compile_step_list_rev r in
61       let res = mk_node (Binop (Inter , qtree,mk_node (Tag(test,kind)))) in
62       let qtree2 = List.fold_left (fun acc e ->
63         mk_node (Binop(Inter, acc, compile_expr e))) res el in
64       let a_rev = axis_rev a in
65       mk_node (Axis (a_rev , qtree2))
66                                                 
67       
68     and axis_rev a =
69       let open Xpath.Ast in
70           match a with
71               Self -> Self
72             | Attribute -> Parent
73             | Child -> Parent
74             | Descendant b -> 
75               if not b then (Ancestor false)
76               else (Ancestor true)    (* true = descendant-or-self, false = descendant *)
77             | FollowingSibling -> PrecedingSibling
78             | Parent -> Child
79             | Ancestor b -> 
80               if not b then (Descendant false) 
81               else (Descendant true)  (* true = ancestor-or-self, false = ancestor *)
82             | PrecedingSibling -> FollowingSibling
83             | Preceding -> Following
84             | Following -> Preceding
85             
86             
87 let compile_xpath p = match p with
88   | [] -> assert false
89   | [p] -> compile_single_path p
90   | p::r -> List.fold_left (fun acc p -> mk_node (Binop (Union , acc, compile_single_path p) )) (compile_single_path p) r
91
92 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
93
94
95 let rec union_list t l1 l2 =
96   match l1,l2 with
97     | [],l2 -> l2
98     | l1, [] -> l1
99     | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
100       else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
101       else h1 ::(union_list t ll1 ll2)
102
103 let rec inter_list t l1 l2 =
104   match l1,l2 with
105     | [],l2 -> []
106     | l1, [] -> []
107     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
108       else if (comp_node t h2 h1) then inter_list t l1 ll2
109       else h1 :: (inter_list t ll1 ll2)
110
111 let rec diff_list t l1 l2 =
112   match l1,l2 with
113     | [],l2 -> []
114     | l1, [] -> l1
115     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
116       else if (comp_node t h2 h1)  then h2 :: (diff_list t l1 ll2)
117       else diff_list t ll1 ll2
118
119
120 let do_debug = ref false
121
122 let debug tree q l =
123   if !do_debug then begin
124     Format.fprintf Format.std_formatter "Evaluation de: ";
125     print_query_tree Format.std_formatter q;
126     Format.fprintf Format.std_formatter "\nResultat: %i\n"
127     (List.length l);
128     Format.pp_print_flush Format.std_formatter ();
129     print_node_list tree l;
130     (*List.iter
131       (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
132     Format.fprintf Format.std_formatter "\n----------------\n";
133     Format.pp_print_flush Format.std_formatter ();
134   end
135
136
137
138
139 let rec compare_query_tree q1 q2 =
140      q1.id==q2.id|| match q1.desc,q2.desc with
141      | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4) 
142                                                           ||  (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
143      | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
144      | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
145      | Dom,Dom | Start,Start ->true
146      | _,_ ->false
147
148 and compare_axis a1 a2 =
149   match a1,a2 with
150       Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
151     | FollowingSibling , FollowingSibling          
152     | PrecedingSibling , PrecedingSibling
153     | Preceding , Preceding | Following , Following -> true
154     | Descendant b1, Descendant b2 -> b1==b2 
155     | Ancestor b1, Ancestor b2 -> b1==b2
156     | _,_ -> false
157
158
159 let rec eval_query_tree tree start q =
160   let resultat = 
161     begin
162       try
163         Hashtbl.find table_query_tree q
164       with Not_found -> 
165         let res =
166         match q.desc with
167           | Start -> start
168           | Dom ->  all_nodes tree          
169           | Tag (t,k) -> element_by_tag tree t k                     
170           | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
171                            eval_axis tree ls a                     
172           | Binop (op,q1,q2)-> begin
173             let ls1 = eval_query_tree tree start q1 in
174             let ls2 = eval_query_tree tree start q2 in
175             match op with         
176               | Union -> union_list tree ls1 ls2            
177               | Inter -> inter_list tree ls1 ls2
178               | Diff -> diff_list tree ls1 ls2
179           end
180         in
181         let res = List.sort (Table.compare_node tree) res in
182         Hashtbl.add table_query_tree q res;
183         compteur := !compteur + (List.length res);
184         res
185     end
186   in
187   debug tree q resultat;
188   resultat
189
190 let mini_id = ref 0
191 let mini_table = QTreeHash.create 17 
192
193 let rec minimize_qtree q =
194    if q.id != -1 then q
195    else
196     try
197       QTreeHash.find mini_table q
198     with Not_found ->
199       let mdesc =
200         match q.desc with
201             (Start | Dom | Tag _) as d -> d
202           | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
203                                let mq2 = minimize_qtree q2 in
204                                Binop(op,mq1,mq2)
205           | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
206                           Axis(a,mq1)         
207       in
208       q.desc <- mdesc;
209       q.hash <- QTree.hash q;
210       q.id <- !mini_id;
211       incr mini_id;
212       QTreeHash.add mini_table q q;
213       q
214
215
216           
217 let rec eval_qtree tree start q =
218   let resultat = 
219     begin
220       try
221         QTreeHash.find table_qtree q
222       with Not_found -> 
223         let res =
224         match q.desc with
225           | Start -> start
226           | Dom ->  all_nodes tree          
227           | Tag (t,k) -> element_by_tag tree t k                     
228           | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
229                            eval_axis tree ls a                     
230           | Binop (op,q1,q2)-> begin
231             let ls1 = eval_qtree tree start q1 in
232             let ls2 = eval_qtree tree start q2 in
233             match op with         
234               | Union -> union_list tree ls1 ls2            
235               | Inter -> inter_list tree ls1 ls2
236               | Diff -> diff_list tree ls1 ls2
237           end
238         in
239         let res = Tas.sort_of_list tree res in
240         QTreeHash.add table_qtree q res;
241         compteur := !compteur + (List.length res);
242         res
243     end
244   in
245   debug tree q resultat;
246   resultat