1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-03-09 11:35:17 CET by Kim Nguyen>
24 type predicate = | First_child
35 let is_move p = match p with
36 | First_child | Next_sibling
37 | Parent | Previous_sibling | Stay -> true
41 type atom = predicate * bool * State.t
43 module Atom : (Formula.ATOM with type data = atom) =
49 let equal n1 n2 = n1 = n2
50 let hash n = Hashtbl.hash n
53 include Hcons.Make(Node)
56 let p, b, q = a.node in
57 if not b then fprintf ppf "%s" Pretty.lnot;
59 | First_child -> fprintf ppf "FC(%a)" State.print q
60 | Next_sibling -> fprintf ppf "NS(%a)" State.print q
61 | Parent -> fprintf ppf "FC%s(%a)" Pretty.inverse State.print q
62 | Previous_sibling -> fprintf ppf "NS%s(%a)" Pretty.inverse State.print q
63 | Stay -> fprintf ppf "%s(%a)" Pretty.epsilon State.print q
64 | Is_first_child -> fprintf ppf "FC%s?" Pretty.inverse
65 | Is_next_sibling -> fprintf ppf "NS%s?" Pretty.inverse
66 | Is_attribute -> fprintf ppf "@?"
67 | Has_first_child -> fprintf ppf "FC?"
68 | Has_next_sibling -> fprintf ppf "NS?"
71 let p, b, q = a.node in
79 include Formula.Make(Atom)
80 let mk_atom a b c = atom_ (Atom.make (a,b,c))
82 (mk_atom Has_first_child true State.dummy)
84 let has_next_sibling =
85 (mk_atom Has_next_sibling true State.dummy)
88 (mk_atom Is_first_child true State.dummy)
91 (mk_atom Is_next_sibling true State.dummy)
94 (mk_atom Is_attribute true State.dummy)
98 (mk_atom First_child true q)
103 (mk_atom Next_sibling true q)
108 (mk_atom Parent true q)
111 let previous_sibling q =
113 (mk_atom Previous_sibling true q)
116 (mk_atom Stay true q)
121 | Formula.Atom a -> let _, _, q = Atom.node a in
122 if q != State.dummy then StateSet.add q acc else acc
130 mutable states : StateSet.t;
131 mutable selection_states: StateSet.t;
132 transitions: (State.t, (QNameSet.t*SFormula.t) list) Hashtbl.t;
135 let next = Uid.make_maker ()
137 let create () = { id = next ();
138 states = StateSet.empty;
139 selection_states = StateSet.empty;
140 transitions = Hashtbl.create 17;
144 let get_trans a states tag =
145 StateSet.fold (fun q acc0 ->
147 let trs = Hashtbl.find a.transitions q in
148 List.fold_left (fun acc1 (labs, phi) ->
149 if QNameSet.mem tag labs then (q,phi)::acc1 else acc1) acc0 trs
150 with Not_found -> acc0
154 [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
155 automaton [a] but ensures that transitions remains pairwise disjoint
158 let add_trans a q s f =
159 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
161 List.fold_left (fun (acup, atrs) (labs, phi) ->
162 let lab1 = QNameSet.inter labs s in
163 let lab2 = QNameSet.diff labs s in
165 if QNameSet.is_empty lab1 then []
166 else [ (lab1, SFormula.or_ phi f) ]
169 if QNameSet.is_empty lab2 then []
170 else [ (lab2, SFormula.or_ phi f) ]
172 (QNameSet.union acup labs, tr1@ tr2 @ atrs)
173 ) (QNameSet.empty, []) trs
175 let rem = QNameSet.diff s cup in
176 let ntrs = if QNameSet.is_empty rem then ntrs
177 else (rem, f) :: ntrs
179 Hashtbl.replace a.transitions q ntrs
181 let _pr_buff = Buffer.create 50
182 let _str_fmt = formatter_of_buffer _pr_buff
183 let _flush_str_fmt () = pp_print_flush _str_fmt ();
184 let s = Buffer.contents _pr_buff in
185 Buffer.clear _pr_buff; s
189 "\nInternal UID: %i@\n\
191 Selection states: %a@\n\
192 Alternating transitions:@\n"
194 StateSet.print a.states
195 StateSet.print a.selection_states;
198 (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
202 let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
203 let c = State.compare q1 q2 in - (if c == 0 then QNameSet.compare s1 s2 else c))
206 let _ = _flush_str_fmt () in
207 let strs_strings, max_pre, max_all = List.fold_left (fun (accl, accp, acca) (q, s, f) ->
208 let s1 = State.print _str_fmt q; _flush_str_fmt () in
209 let s2 = QNameSet.print _str_fmt s; _flush_str_fmt () in
210 let s3 = SFormula.print _str_fmt f; _flush_str_fmt () in
211 let pre = Pretty.length s1 + Pretty.length s2 in
212 let all = Pretty.length s3 in
213 ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
214 ) ([], 0, 0) sorted_trs
216 let line = Pretty.line (max_all + max_pre + 6) in
217 let prev_q = ref State.dummy in
218 List.iter (fun (q, s1, s2, s3) ->
219 if !prev_q != q && !prev_q != State.dummy then fprintf fmt " %s\n%!" line;
221 fprintf fmt " %s, %s" s1 s2;
222 fprintf fmt "%s" (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
223 fprintf fmt " %s %s@\n%!" Pretty.right_arrow s3;
225 fprintf fmt " %s\n%!" line
228 [complete transitions a] ensures that for each state q
229 and each symbols s in the alphabet, a transition q, s exists.
230 (adding q, s -> F when necessary).
233 let complete_transitions a =
234 StateSet.iter (fun q ->
235 let qtrans = Hashtbl.find a.transitions q in
237 List.fold_left (fun rem (labels, _) ->
238 QNameSet.diff rem labels) QNameSet.any qtrans
241 if QNameSet.is_empty rem then qtrans
243 (rem, SFormula.false_) :: qtrans
245 Hashtbl.replace a.transitions q nqtrans
248 let cleanup_states a =
249 let memo = ref StateSet.empty in
251 if not (StateSet.mem q !memo) then begin
252 memo := StateSet.add q !memo;
253 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
254 List.iter (fun (_, phi) ->
255 StateSet.iter loop (SFormula.get_states phi)) trs
258 StateSet.iter loop a.selection_states;
259 let unused = StateSet.diff a.states !memo in
260 eprintf "Unused states %a\n%!" StateSet.print unused;
261 StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
264 (* [normalize_negations a] removes negative atoms in the formula
265 complementing the sub-automaton in the negative states.
266 [TODO check the meaning of negative upward arrows]
269 let normalize_negations auto =
270 eprintf "Automaton before normalize_trans:\n";
271 print err_formatter auto;
272 eprintf "--------------------\n%!";
274 let memo_state = Hashtbl.create 17 in
275 let todo = Queue.create () in
277 match SFormula.expr f with
278 Formula.True | Formula.False -> if b then f else SFormula.not_ f
279 | Formula.Or(f1, f2) -> (if b then SFormula.or_ else SFormula.and_)(flip b f1) (flip b f2)
280 | Formula.And(f1, f2) -> (if b then SFormula.and_ else SFormula.or_)(flip b f1) (flip b f2)
281 | Formula.Atom(a) -> begin
282 let l, b', q = Atom.node a in
283 if q == State.dummy then if b then f else SFormula.not_ f
285 if b == b' then begin
286 (* a appears positively, either no negation or double negation *)
287 if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
288 SFormula.atom_ (Atom.make (l, true, q))
290 (* need to reverse the atom
291 either we have a positive state deep below a negation
292 or we have a negative state in a positive formula
293 b' = sign of the state
294 b = sign of the enclosing formula
298 (* does the inverted state of q exist ? *)
299 Hashtbl.find memo_state (q, false)
302 (* create a new state and add it to the todo queue *)
303 let nq = State.make () in
304 auto.states <- StateSet.add nq auto.states;
305 Hashtbl.add memo_state (q, false) nq;
306 Queue.add (q, false) todo; nq
308 SFormula.atom_ (Atom.make (l, true, not_q))
312 (* states that are not reachable from a selection stat are not interesting *)
313 StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selection_states;
315 while not (Queue.is_empty todo) do
316 let (q, b) as key = Queue.pop todo in
319 Hashtbl.find memo_state key
322 let nq = if b then q else
323 let nq = State.make () in
324 auto.states <- StateSet.add nq auto.states;
327 Hashtbl.add memo_state key nq; nq
329 let trans = Hashtbl.find auto.transitions q in
330 let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
331 Hashtbl.replace auto.transitions q' trans';