1) Supprimer le module Tas car il sert a rien
[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                      let acc1 = get_descendant tree [root] in
9                      root::acc1
10         
11
12 let element_by_tag tree tagset kind = let dom = all_nodes tree in
13                               List.filter (fun c ->
14                                 Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
15                                 QNameSet.mem (Naive_tree.tag tree c) tagset ) dom
16
17 let mk_node q = {desc = q; id = -1; hash = -1}
18
19 let rec compile_single_path p =
20   let open Xpath.Ast in
21       match p with
22         | Absolute p | Relative p -> compile_step_list (List.rev p)
23       
24 and compile_step_list p = 
25     match p with
26       | [] -> mk_node Start
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  
32
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))
37
38     | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
39                           let qtree2 = compile_expr e2 in 
40                           begin
41                             match op with 
42                               | Or -> mk_node (Binop (Union , qtree1,qtree2))
43                               | And -> mk_node (Binop (Inter ,qtree1,qtree2))
44                               | _ -> failwith "Unknown operator"
45                           end
46     | Path p -> compile_path_rev p
47     | _ -> failwith "Unknown expression"
48       
49   and compile_path_rev p = 
50     match p with
51       | [] -> assert false
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
54         
55   and compile_single_path_rev p = 
56     match p with
57       | Absolute p | Relative p -> compile_step_list_rev p 
58
59   and compile_step_list_rev p = match p with
60     | [] -> mk_node Dom        
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))
68                                                 
69       
70     and axis_rev a =
71       let open Xpath.Ast in
72           match a with
73               Self -> Self
74             | Attribute -> Parent
75             | Child -> Parent
76             | Descendant b -> 
77               if not b then (Ancestor false)
78               else (Ancestor true)    (* true = descendant-or-self, false = descendant *)
79             | FollowingSibling -> PrecedingSibling
80             | Parent -> Child
81             | Ancestor b -> 
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
87             
88             
89 let compile_xpath p = match p with
90   | [] -> assert false
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
93
94
95
96
97 let do_debug = ref false
98
99 let debug tree q l =
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"
104     (List.length l);
105     Format.pp_print_flush Format.std_formatter ();
106     print_node_list tree l;
107     (*List.iter
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 ();
111   end
112
113
114
115
116 let rec compare_query_tree q1 q2 =
117      q1.id==q2.id|| match q1.desc,q2.desc with
118      | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)->op1==op2&& ((compare_query_tree qt1 qt3 && compare_query_tree qt2 qt4) 
119                                                           ||  (compare_query_tree qt1 qt4 && compare_query_tree qt2 qt3))
120      | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && compare_query_tree qt1 qt2
121      | Tag(t1,k1),Tag(t2,k2) ->t1==t2&& k1==k2
122      | Dom,Dom | Start,Start ->true
123      | _,_ ->false
124
125 and compare_axis a1 a2 =
126   match a1,a2 with
127       Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
128     | FollowingSibling , FollowingSibling          
129     | PrecedingSibling , PrecedingSibling
130     | Preceding , Preceding | Following , Following -> true
131     | Descendant b1, Descendant b2 -> b1==b2 
132     | Ancestor b1, Ancestor b2 -> b1==b2
133     | _,_ -> false
134
135 let mini_id = ref 0
136 let mini_table = QTreeHash.create 17 
137
138 let rec minimize_qtree q =
139    if q.id != -1 then q
140    else
141     try
142       QTreeHash.find mini_table q
143     with Not_found ->
144       let mdesc =
145         match q.desc with
146             (Start | Dom | Tag _) as d -> d
147           | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
148                                let mq2 = minimize_qtree q2 in
149                                Binop(op,mq1,mq2)
150           | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
151                           Axis(a,mq1)         
152       in
153       q.desc <- mdesc;
154       q.hash <- QTree.hash q;
155       q.id <- !mini_id;
156       incr mini_id;
157       QTreeHash.add mini_table q q;
158       q
159
160
161           
162 let rec eval_qtree tree start q =
163   let resultat = 
164     begin
165       try
166         QTreeHash.find table_qtree q
167       with Not_found -> 
168         let res =
169         match q.desc with
170           | Start -> start
171           | Dom ->  all_nodes tree          
172           | Tag (t,k) -> element_by_tag tree t k                     
173           | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
174                            eval_axis tree ls a                     
175           | Binop (op,q1,q2)-> begin
176             let ls1 = eval_qtree tree start q1 in
177             let ls2 = eval_qtree tree start q2 in
178             match op with         
179               | Union -> union_list tree ls1 ls2            
180               | Inter -> inter_list tree ls1 ls2
181               | Diff -> diff_list tree ls1 ls2
182           end
183         in
184         QTreeHash.add table_qtree q res;
185         compteur := !compteur + (List.length res);
186         res
187     end
188   in
189   debug tree q resultat;
190   resultat