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