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