une version marché et correcte avec bitvector
[tatoo.git] / src / query_tree.ml
1 open Table
2
3 let compteur = ref 0
4 let table_qtree = QTreeHash.create 97
5                         
6
7 let element_by_tag tree tagset kind = let v = Bitvector.create (Naive_tree.size tree) in
8                                       for i=0 to (Bitvector.length v)-1 do
9                                         let c = Naive_tree.by_preorder tree i in
10                                         if (Tree.NodeKind.is_a (Naive_tree.kind tree c) kind &&
11                                               QNameSet.mem (Naive_tree.tag tree c) tagset )
12                                         then Bitvector.set v i true
13                                       done;
14                                       v
15                          
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 let mini_id = ref 0
114 let mini_table = QTreeHash.create 17 
115
116 let rec minimize_qtree q =
117    if q.id != -1 then q
118    else
119     try
120       QTreeHash.find mini_table q
121     with Not_found ->
122       let mdesc =
123         match q.desc with
124             (Start | Dom | Tag _) as d -> d
125           | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
126                                let mq2 = minimize_qtree q2 in
127                                Binop(op,mq1,mq2)
128           | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
129                           Axis(a,mq1)         
130       in
131       q.desc <- mdesc;
132       q.hash <- QTree.hash q;
133       q.id <- !mini_id;
134       incr mini_id;
135       QTreeHash.add mini_table q q;
136       q
137
138
139           
140 let rec eval_qtree tree start q =
141   let resultat = 
142     begin
143       try
144         QTreeHash.find table_qtree q
145       with Not_found -> 
146         let res =
147         match q.desc with
148           | Start -> start
149           | Dom -> Bitvector.create ~init:true (Naive_tree.size tree)
150             (*let v = Bitvector.create (Naive_tree.size tree) in
151             for i=0 to (Bitvector.length v)-1 do
152               Bitvector.set v i true
153             done;
154             v*)
155           | Tag (t,k) -> element_by_tag tree t k                     
156           | Axis (a,q1) -> let v = eval_qtree tree start q1 in
157                            eval_axis tree v a                      
158           | Binop (op,q1,q2)-> begin
159             let v1 = eval_qtree tree start q1 in
160             let v2 = eval_qtree tree start q2 in
161             match op with         
162               | Union -> Bitvector.union v1 v2      
163               | Inter -> Bitvector.inter v1 v2
164               | Diff -> Bitvector.diff  v1 v2
165           end
166         in
167         QTreeHash.add table_qtree q res;
168         compteur := !compteur + Bitvector.length res; (*????8*)
169         res
170     end
171   in
172  (* debug tree q resultat;*)
173   resultat