f0e24ee1693a7da8004833aff549ae4a0581f6d0
[tatoo.git] / src / table.ml
1
2 type move = Self
3             | Firstchild
4             | Nextsibling
5             | Revfirstchild
6             | Prevsibling
7
8 type query_tree_desc = Binop of op * query_tree * query_tree
9                        | Axis of Xpath.Ast.axis * query_tree
10                        | Start 
11                        | Dom
12                        | Tag of QNameSet.t * Tree.NodeKind.t
13
14 and op = Union | Inter | Diff
15
16 and query_tree = {
17   mutable desc  : query_tree_desc;
18   mutable id : int;
19   mutable hash : int;
20 }
21
22
23 module QTree = struct
24   type t = query_tree
25   let rec equal q1 q2 =
26     q1 == q2 ||
27       (q1.id == q2.id && q1.id != -1) ||
28       match q1.desc, q2.desc with
29         | Binop(op1,qt1,qt2),Binop(op2,qt3,qt4)-> op1==op2&& (equal qt1 qt3 && equal qt2 qt4) 
30                                                            
31         | Axis(a1,qt1),Axis(a2,qt2) -> compare_axis a1 a2 && equal qt1 qt2
32         | Tag(t1,k1),Tag(t2,k2) -> t1==t2&& k1==k2
33         | Dom,Dom | Start,Start -> true
34         | _,_ ->false
35   and compare_axis a1 a2 =
36     match a1,a2 with
37         Self ,Self | Attribute, Attribute | Child , Child | Parent , Parent
38       | FollowingSibling , FollowingSibling        
39       | PrecedingSibling , PrecedingSibling
40       | Preceding , Preceding | Following , Following -> true
41       | Descendant b1, Descendant b2 -> b1==b2 
42       | Ancestor b1, Ancestor b2 -> b1==b2
43       | _,_ -> false
44
45   let rec hash q = 
46     if q.hash != -1 then q.hash
47     else match q.desc with
48         Dom -> 1
49       | Start -> 3
50       | Tag(s,_) -> 5 + 17*QNameSet.hash s
51       | Axis(a,q) -> 7 + 17 * Hashtbl.hash a + 23* hash q
52       | Binop(op,q1,q2) -> 11 + 17* Hashtbl.hash op + 23* hash q1 + 27* hash q2
53
54 end
55
56
57 module QTreeHash = Hashtbl.Make(QTree)
58
59
60
61 (*28/01/2014  
62   parametres : tree  l'arbre xml
63                n     un noeud
64                m     move   
65   retour :un noeud qui correspond ॆ la relation r
66 *)
67
68 let print_node_list tree l =
69   List.iter (fun node ->
70     Naive_tree.print_xml stdout tree node;
71     print_newline() 
72   ) l
73
74 let rec print_query_tree fmt q =
75   match q.desc with
76       Dom -> Format.fprintf fmt "Dom"
77     | Start -> Format.fprintf fmt "Start"
78     | Tag (t,k) -> Format.fprintf fmt "Tag(%a, %a)" QNameSet.print t Tree.NodeKind.print k
79     | Axis (a,q) ->
80       Format.fprintf fmt "%a(%a)" Xpath.Ast.print_axis a print_query_tree q
81     | Binop (op,q1,q2) -> 
82       Format.fprintf fmt "%a(%a, %a)"
83       print_binop  op
84       print_query_tree  q1 
85       print_query_tree  q2 
86  
87 and print_binop fmt o =
88   match o with
89     | Union -> Format.fprintf fmt "Union"
90     | Inter -> Format.fprintf fmt "Inter"
91     | Diff -> Format.fprintf fmt "Diff"
92
93 let rec eval_relation tree m n =
94   match m with
95       Self -> n
96     | Firstchild ->  Naive_tree.first_child tree n
97     | Nextsibling -> Naive_tree.next_sibling tree n
98     | Revfirstchild -> Naive_tree.parent_of_first tree n
99     | Prevsibling -> Naive_tree.prev_sibling tree n
100
101 (*28/01/2014  
102   parametres : tree  l'arbre xml
103                ls    l'ensemble de noeuds
104                m     move   
105   retour : l'ensemble de noeuds qui correspondent ॆ la relation r
106 *)
107
108
109 let compare_node tree a b =
110   compare (Naive_tree.preorder tree a ) (Naive_tree.preorder tree b ) 
111
112 let rec eval_move tree ls m =
113   match m with
114       Self -> ls
115     | r -> List.filter (fun n -> n != Naive_tree.nil)
116            (List.map (eval_relation tree r) ls) 
117            
118
119 (*28/01/2014  
120   parametres : tree  l'arbre xml
121                ls    l'ensemble de noeuds
122                m     move   
123   retour : l'ensemble de noeuds qui correspondent ॆ des relations lr
124 *)
125
126 and eval_star tree ls lr =
127   let h = Hashtbl.create 17 in
128   let q = Queue.create () in
129   List.iter ( fun e -> Queue.add e q ) ls;
130   while not (Queue.is_empty q ) do
131     let n = Queue.pop q in
132     if not (Hashtbl.mem h n) then begin
133       Hashtbl.add h n ();
134       List.iter ( fun r -> let m = eval_relation tree r n in
135                            if m != Naive_tree.nil && not (Hashtbl.mem h m ) then begin
136                              
137                              Queue.add m q; end
138       ) lr
139     end
140   done;
141   let l = Hashtbl.fold (fun k _ acc -> k::acc) h [] in
142   List.sort (compare_node tree) l
143     
144 (*28/01/2014  
145   parametres : tree  l'arbre xml
146                ls    l'ensemble de noeuds
147                a     axis   
148   retour : l'ensemble de noeuds qui correspondent ॆ l'axe
149 *)
150
151 let keep_elements t l = (*
152    List.filter (fun n -> match Naive_tree.kind t n with
153      | Element | Text | Document | Attribute -> true | _ -> false) l
154                         *) l
155
156 let keep_attributs t l = (*
157   List.filter (fun n -> match Naive_tree.kind t n with
158     | Attribute ->true | _ -> false) *) l
159
160 let rec eval_axis tree ls a =
161   let open Xpath.Ast in
162         match a with
163             Self -> ls
164               
165           | Attribute -> let lfc = eval_move tree ls Firstchild in
166                          let lc = eval_star tree lfc [Nextsibling] in
167                          keep_attributs tree lc
168             
169           | Child -> let lfc = eval_move tree ls Firstchild in
170                      let lc = eval_star tree lfc [Nextsibling] in
171                      keep_elements tree lc
172                        
173           | Descendant c -> let lfc = eval_move tree ls Firstchild in                
174                             let ls2 = eval_star tree lfc [Firstchild;Nextsibling] in
175                             let ldes =
176                             if not c then ls2
177                             else List.merge (compare_node tree) ls2 ls
178                             in
179                             keep_elements tree ldes
180                               
181           | FollowingSibling -> let lnexts = eval_move tree ls Nextsibling in
182                                 let lfs = eval_star tree lnexts [Nextsibling] in
183                                 keep_elements tree lfs
184                                   
185           | Parent -> let lprevs = eval_star tree ls [Prevsibling] in
186                       let lp = eval_move tree lprevs Revfirstchild in
187                       keep_elements tree lp
188                         
189           | Ancestor b -> let ls2 = eval_star tree ls [Revfirstchild;Prevsibling] in
190                           let ls3 = eval_move tree ls2 Revfirstchild in
191                           let lac =
192                           if not b then ls3
193                           else List.merge (compare_node tree ) ls3 ls
194                           in
195                           keep_elements tree lac
196                             
197           | PrecedingSibling -> let ls2 = eval_star tree ls [Prevsibling] in
198                                 let lps = eval_move tree ls2 Prevsibling in
199                                 keep_elements tree lps
200                                   
201           | Preceding -> let ls2 = eval_axis tree ls (Ancestor true) in
202                          let ls3 = eval_axis tree ls2 PrecedingSibling in
203                          let lp = eval_axis tree ls3 (Descendant true) in
204                          keep_elements tree lp
205                          
206           | Following -> let ls2 = eval_axis tree ls (Ancestor true) in
207                          let ls3 = eval_axis tree ls2 FollowingSibling in
208                          let lf = eval_axis tree ls3 (Descendant true) in
209                          keep_elements tree lf
210
211
212
213              
214
215
216
217
218