9dcd0d51ae10e50d821956ebe23b28f369b35cf4
[tatoo.git] / src / query_tree.ml
1 open Table
2
3 let compteur = ref 0
4 let table_qtree = QTreeHash.create 97
5
6 let all_nodes tree = let root = Naive_tree.root tree in
7                      let acc1 = get_descendant tree [root] in
8                      root::acc1
9         
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 let mini_id = ref 0
113 let mini_table = QTreeHash.create 17 
114
115 let rec minimize_qtree q =
116    if q.id != -1 then q
117    else
118     try
119       QTreeHash.find mini_table q
120     with Not_found ->
121       let mdesc =
122         match q.desc with
123             (Start | Dom | Tag _) as d -> d
124           | Binop(op,q1,q2) -> let mq1 = minimize_qtree q1 in
125                                let mq2 = minimize_qtree q2 in
126                                Binop(op,mq1,mq2)
127           | Axis(a,q1) -> let mq1 = minimize_qtree q1 in
128                           Axis(a,mq1)         
129       in
130       q.desc <- mdesc;
131       q.hash <- QTree.hash q;
132       q.id <- !mini_id;
133       incr mini_id;
134       QTreeHash.add mini_table q q;
135       q
136
137
138           
139 let rec eval_qtree tree start q =
140   let resultat = 
141     begin
142       try
143         QTreeHash.find table_qtree q
144       with Not_found -> 
145         let res =
146         match q.desc with
147           | Start -> start
148           | Dom ->  all_nodes tree          
149           | Tag (t,k) -> element_by_tag tree t k                     
150           | Axis (a,q1) -> let ls = eval_qtree tree start q1 in
151                            eval_axis tree ls a                     
152           | Binop (op,q1,q2)-> begin
153             let ls1 = eval_qtree tree start q1 in
154             let ls2 = eval_qtree tree start q2 in
155             match op with         
156               | Union -> union_list tree ls1 ls2            
157               | Inter -> inter_list tree ls1 ls2
158               | Diff -> diff_list tree ls1 ls2
159           end
160         in
161         QTreeHash.add table_qtree q res;
162         compteur := !compteur + (List.length res);
163         res
164     end
165   in
166   debug tree q resultat;
167   resultat