remedier a la liste desordonné par l'ajout de sort a la fin de la fonction eval_query...
[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 rec compile_single_path p =
14   let open Xpath.Ast in
15       match p with
16         | Absolute p | Relative p -> compile_step_list (List.rev p)
17       
18 and compile_step_list p = 
19     match p with
20       | [] -> Start
21       | (a,(test,kind),el) :: r ->
22         let qtree = compile_step_list r in
23         let res = Binop ( Inter,Axis (a,qtree), Tag (test,kind) ) in
24         List.fold_left (fun acc e ->
25           Binop(Inter, acc, compile_expr e)) res el  
26
27   and compile_expr  (e : Xpath.Ast.expr )  = match e with
28     | Fun_call (f, [ e0 ]) when (QName.to_string f) = "not" ->
29       let qtree = compile_expr e0 in
30       Binop (Diff , Dom, qtree)
31
32     | Binop (e1,op,e2) -> let qtree1 = compile_expr e1 in
33                           let qtree2 = compile_expr e2 in 
34                           begin
35                             match op with 
36                               | Or -> Binop (Union , qtree1,qtree2)
37                               | And -> Binop (Inter ,qtree1,qtree2)
38                               | _ -> failwith "Unknown operator"
39                           end
40     | Path p -> compile_path_rev p
41     | _ -> failwith "Unknown expression"
42       
43   and compile_path_rev p = 
44     match p with
45       | [] -> assert false
46       | [p] -> compile_single_path_rev p  
47       | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path_rev p) ) (compile_single_path_rev p) r
48         
49   and compile_single_path_rev p = 
50     match p with
51       | Absolute p | Relative p -> compile_step_list_rev p (*(List.rev p)*)
52
53   and compile_step_list_rev p = match p with
54     | [] -> Dom        
55     | (a,(test,kind),el) :: r -> 
56       let qtree = compile_step_list_rev r in
57       let res = Binop (Inter , qtree, Tag(test,kind)) in
58       let qtree2 = List.fold_left (fun acc e ->
59         Binop(Inter, acc, compile_expr e)) res el in
60       let a_rev = axis_rev a in
61       Axis (a_rev , qtree2)
62                                                 
63       
64     and axis_rev a =
65       let open Xpath.Ast in
66           match a with
67               Self -> Self
68             | Attribute -> Parent
69             | Child -> Parent
70             | Descendant b -> 
71               if not b then (Ancestor false)
72               else (Ancestor true)    (* true = descendant-or-self, false = descendant *)
73             | FollowingSibling -> PrecedingSibling
74             | Parent -> Child
75             | Ancestor b -> 
76               if not b then (Descendant false) 
77               else (Descendant true)  (* true = ancestor-or-self, false = ancestor *)
78             | PrecedingSibling -> FollowingSibling
79             | Preceding -> Following
80             | Following -> Preceding
81             
82             
83 let compile_xpath p = match p with
84   | [] -> assert false
85   | [p] -> compile_single_path p
86   | p::r -> List.fold_left (fun acc p -> Binop (Union , acc, compile_single_path p) ) (compile_single_path p) r
87
88 let comp_node t n1 n2 = (Naive_tree.preorder t n1) < (Naive_tree.preorder t n2)
89
90
91 let rec union_list t l1 l2 =
92   match l1,l2 with
93     | [],l2 -> l2
94     | l1, [] -> l1
95     | h1::ll1, h2::ll2 -> if (comp_node t h2 h1) then h2 :: (union_list t l1 ll2)
96       else if (comp_node t h1 h2) then h1::(union_list t ll1 l2)
97       else h1 ::(union_list t ll1 ll2)
98
99 let rec inter_list t l1 l2 =
100   match l1,l2 with
101     | [],l2 -> []
102     | l1, [] -> []
103     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then inter_list t ll1 l2
104       else if (comp_node t h2 h1) then inter_list t l1 ll2
105       else h1 :: (inter_list t ll1 ll2)
106
107 let rec diff_list t l1 l2 =
108   match l1,l2 with
109     | [],l2 -> []
110     | l1, [] -> l1
111     | h1::ll1, h2::ll2 -> if (comp_node t h1 h2) then h1::(diff_list t ll1 l2)
112       else if (comp_node t h2 h1)  then h2 :: (diff_list t l1 ll2)
113       else diff_list t ll1 ll2
114
115
116 let do_debug = ref false
117
118 let debug tree q l =
119   if !do_debug then begin
120     Format.fprintf Format.std_formatter "Evaluation de: ";
121     print_query_tree Format.std_formatter q;
122     Format.fprintf Format.std_formatter "\nResultat: %i\n"
123     (List.length l);
124     Format.pp_print_flush Format.std_formatter ();
125     print_node_list tree l;
126     (*List.iter
127       (fun n -> Format.fprintf Format.std_formatter "%i, " (Naive_tree.preorder tree n)) l;*)
128     Format.fprintf Format.std_formatter "\n----------------\n";
129     Format.pp_print_flush Format.std_formatter ();
130   end
131
132 let table_query_tree = Hashtbl.create 97 
133
134   
135 let rec eval_query_tree tree start q =
136   let resultat = 
137     begin
138       try
139         Hashtbl.find table_query_tree q
140       with Not_found -> 
141         let res =
142         match q with
143           | Start -> start
144           | Dom ->  all_nodes tree          
145           | Tag (t,k) -> element_by_tag tree t k                     
146           | Axis (a,q1) -> let ls = eval_query_tree tree start q1 in
147                            eval_axis tree ls a                     
148           | Binop (op,q1,q2)-> begin
149             let ls1 = eval_query_tree tree start q1 in
150             let ls2 = eval_query_tree tree start q2 in
151             match op with         
152               | Union -> union_list tree ls1 ls2            
153               | Inter -> inter_list tree ls1 ls2
154               | Diff -> diff_list tree ls1 ls2
155           end
156         in
157         let res = List.sort (Table.compare_node tree) res in
158         Hashtbl.add table_query_tree q res;
159         compteur := !compteur + (List.length res);
160         res
161     end
162   in
163   debug tree q resultat;
164   resultat