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 (***********************************************************************)
19 type move = [ `First_child
25 type predicate = Move of move * State.t
28 | Is of Tree.NodeKind.t
38 let equal n1 n2 = n1 = n2
39 let hash n = Hashtbl.hash n
42 include Hcons.Make(Node)
46 | Move (m, q) -> begin
48 `First_child -> fprintf ppf "%s" Pretty.down_arrow
49 | `Next_sibling -> fprintf ppf "%s" Pretty.right_arrow
50 | `Parent -> fprintf ppf "%s" Pretty.up_arrow
51 | `Previous_sibling -> fprintf ppf "%s" Pretty.left_arrow
52 | `Stay -> fprintf ppf "%s" Pretty.bullet
54 fprintf ppf "%a" State.print q
55 | Is_first_child -> fprintf ppf "%s?" Pretty.up_arrow
56 | Is_next_sibling -> fprintf ppf "%s?" Pretty.left_arrow
57 | Is k -> fprintf ppf "is-%a?" Tree.NodeKind.print k
58 | Has_first_child -> fprintf ppf "%s?" Pretty.down_arrow
59 | Has_next_sibling -> fprintf ppf "%s?" Pretty.right_arrow
66 include Boolean.Make(Atom)
68 let mk_atom a = atom_ (Atom.make a)
69 let is k = mk_atom (Is k)
71 let has_first_child = mk_atom Has_first_child
73 let has_next_sibling = mk_atom Has_next_sibling
75 let is_first_child = mk_atom Is_first_child
77 let is_next_sibling = mk_atom Is_next_sibling
79 let is_attribute = mk_atom (Is Attribute)
81 let is_element = mk_atom (Is Element)
83 let is_processing_instruction = mk_atom (Is ProcessingInstruction)
85 let is_comment = mk_atom (Is Comment)
87 let mk_move m q = mk_atom (Move(m,q))
90 (mk_move `First_child q)
95 (mk_move `Next_sibling q)
103 let previous_sibling q =
105 (mk_move `Previous_sibling q)
108 let stay q = mk_move `Stay q
113 | Boolean.Atom ({ Atom.node = Move(_,q) ; _ }, _) -> StateSet.add q acc
121 include Hcons.Make (struct
122 type t = State.t * QNameSet.t * Formula.t
123 let equal (a, b, c) (d, e, f) =
124 a == d && b == e && c == f
126 HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((Formula.uid c) :> int))
129 let q, l, f = t.node in
130 fprintf ppf "%a, %a %s %a"
133 Pretty.double_right_arrow
138 module TransList : sig
139 include Hlist.S with type elt = Transition.t
140 val print : Format.formatter -> ?sep:string -> t -> unit
143 include Hlist.Make(Transition)
144 let print ppf ?(sep="\n") l =
146 let q, lab, f = Transition.node t in
147 fprintf ppf "%a, %a -> %a%s" State.print q QNameSet.print lab Formula.print f sep) l
154 mutable states : StateSet.t;
155 mutable starting_states : StateSet.t;
156 mutable selecting_states: StateSet.t;
157 transitions: (State.t, (QNameSet.t*Formula.t) list) Hashtbl.t;
162 let get_states a = a.states
163 let get_starting_states a = a.starting_states
164 let get_selecting_states a = a.selecting_states
167 let _pr_buff = Buffer.create 50
168 let _str_fmt = formatter_of_buffer _pr_buff
169 let _flush_str_fmt () = pp_print_flush _str_fmt ();
170 let s = Buffer.contents _pr_buff in
171 Buffer.clear _pr_buff; s
174 let _ = _flush_str_fmt() in
176 "Internal UID: %i@\n\
178 Starting states: %a@\n\
179 Selection states: %a@\n\
180 Alternating transitions:@\n"
182 StateSet.print a.states
183 StateSet.print a.starting_states
184 StateSet.print a.selecting_states;
187 (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
191 let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
192 let c = State.compare q1 q2 in - (if c == 0 then QNameSet.compare s1 s2 else c))
195 let _ = _flush_str_fmt () in
196 let strs_strings, max_pre, max_all = List.fold_left (fun (accl, accp, acca) (q, s, f) ->
197 let s1 = State.print _str_fmt q; _flush_str_fmt () in
198 let s2 = QNameSet.print _str_fmt s; _flush_str_fmt () in
199 let s3 = Formula.print _str_fmt f; _flush_str_fmt () in
200 let pre = Pretty.length s1 + Pretty.length s2 in
201 let all = Pretty.length s3 in
202 ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
203 ) ([], 0, 0) sorted_trs
205 let line = Pretty.line (max_all + max_pre + 6) in
206 let prev_q = ref State.dummy in
207 fprintf fmt "%s@\n" line;
208 List.iter (fun (q, s1, s2, s3) ->
209 if !prev_q != q && !prev_q != State.dummy then fprintf fmt "%s@\n" line;
211 fprintf fmt "%s, %s" s1 s2;
212 fprintf fmt "%s" (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
213 fprintf fmt " %s %s@\n" Pretty.right_arrow s3;
215 fprintf fmt "%s@\n" line
218 let get_trans a tag states =
219 StateSet.fold (fun q acc0 ->
221 let trs = Hashtbl.find a.transitions q in
222 List.fold_left (fun acc1 (labs, phi) ->
223 if QNameSet.mem tag labs then
224 TransList.cons (Transition.make (q, labs, phi)) acc1
226 with Not_found -> acc0
227 ) states TransList.nil
233 [complete transitions a] ensures that for each state q
234 and each symbols s in the alphabet, a transition q, s exists.
235 (adding q, s -> F when necessary).
238 let complete_transitions a =
239 StateSet.iter (fun q ->
240 if StateSet.mem q a.starting_states then ()
242 let qtrans = Hashtbl.find a.transitions q in
244 List.fold_left (fun rem (labels, _) ->
245 QNameSet.diff rem labels) QNameSet.any qtrans
248 if QNameSet.is_empty rem then qtrans
250 (rem, Formula.false_) :: qtrans
252 Hashtbl.replace a.transitions q nqtrans
255 (* [cleanup_states] remove states that do not lead to a
258 let cleanup_states a =
259 let memo = ref StateSet.empty in
261 if not (StateSet.mem q !memo) then begin
262 memo := StateSet.add q !memo;
263 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
264 List.iter (fun (_, phi) ->
265 StateSet.iter loop (Formula.get_states phi)) trs
268 StateSet.iter loop a.selecting_states;
269 let unused = StateSet.diff a.states !memo in
270 StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
273 (* [normalize_negations a] removes negative atoms in the formula
274 complementing the sub-automaton in the negative states.
275 [TODO check the meaning of negative upward arrows]
278 let normalize_negations auto =
279 let memo_state = Hashtbl.create 17 in
280 let todo = Queue.create () in
282 match Formula.expr f with
283 Boolean.True | Boolean.False -> if b then f else Formula.not_ f
284 | Boolean.Or(f1, f2) -> (if b then Formula.or_ else Formula.and_)(flip b f1) (flip b f2)
285 | Boolean.And(f1, f2) -> (if b then Formula.and_ else Formula.or_)(flip b f1) (flip b f2)
286 | Boolean.Atom(a, b') -> begin
287 match a.Atom.node with
289 if b == b' then begin
290 (* a appears positively, either no negation or double negation *)
291 if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
292 Formula.mk_atom (Move(m, q))
294 (* need to reverse the atom
295 either we have a positive state deep below a negation
296 or we have a negative state in a positive formula
297 b' = sign of the state
298 b = sign of the enclosing formula
302 (* does the inverted state of q exist ? *)
303 Hashtbl.find memo_state (q, false)
306 (* create a new state and add it to the todo queue *)
307 let nq = State.make () in
308 auto.states <- StateSet.add nq auto.states;
309 Hashtbl.add memo_state (q, false) nq;
310 Queue.add (q, false) todo; nq
312 Formula.mk_atom (Move (m,not_q))
314 | _ -> if b then f else Formula.not_ f
317 (* states that are not reachable from a selection stat are not interesting *)
318 StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selecting_states;
320 while not (Queue.is_empty todo) do
321 let (q, b) as key = Queue.pop todo in
324 Hashtbl.find memo_state key
327 let nq = if b then q else
328 let nq = State.make () in
329 auto.states <- StateSet.add nq auto.states;
332 Hashtbl.add memo_state key nq; nq
334 let trans = try Hashtbl.find auto.transitions q with Not_found -> [] in
335 let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
336 Hashtbl.replace auto.transitions q' trans';
348 let next = Uid.make_maker ()
354 states = StateSet.empty;
355 starting_states = StateSet.empty;
356 selecting_states = StateSet.empty;
357 transitions = Hashtbl.create MED_H_SIZE;
364 Cache.N2.iteri (fun _ _ _ b -> if b then incr n2) auto.cache2;
365 Cache.N4.iteri (fun _ _ _ _ _ b -> if b then incr n4) auto.cache4;
366 Logger.msg `STATS "automaton %i, cache2: %i entries, cache6: %i entries"
367 (auto.id :> int) !n2 !n4;
368 let c2l, c2u = Cache.N2.stats auto.cache2 in
369 let c4l, c4u = Cache.N4.stats auto.cache4 in
371 "cache2: length: %i, used: %i, occupation: %f"
372 c2l c2u (float c2u /. float c2l);
374 "cache4: length: %i, used: %i, occupation: %f"
375 c4l c4u (float c4u /. float c4l)
380 let add_state a ?(starting=false) ?(selecting=false) q =
381 a.states <- StateSet.add q a.states;
382 if starting then a.starting_states <- StateSet.add q a.starting_states;
383 if selecting then a.selecting_states <- StateSet.add q a.selecting_states
385 let add_trans a q s f =
386 if not (StateSet.mem q a.states) then add_state a q;
387 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
389 List.fold_left (fun (acup, atrs) (labs, phi) ->
390 let lab1 = QNameSet.inter labs s in
391 let lab2 = QNameSet.diff labs s in
393 if QNameSet.is_empty lab1 then []
394 else [ (lab1, Formula.or_ phi f) ]
397 if QNameSet.is_empty lab2 then []
398 else [ (lab2, Formula.or_ phi f) ]
400 (QNameSet.union acup labs, tr1@ tr2 @ atrs)
401 ) (QNameSet.empty, []) trs
403 let rem = QNameSet.diff s cup in
404 let ntrs = if QNameSet.is_empty rem then ntrs
405 else (rem, f) :: ntrs
407 Hashtbl.replace a.transitions q ntrs
410 complete_transitions a;
411 normalize_negations a;
417 StateSet.fold (fun q a -> StateSet.add (f q) a) s StateSet.empty
419 let map_hash fk fv h =
420 let h' = Hashtbl.create (Hashtbl.length h) in
421 let () = Hashtbl.iter (fun k v -> Hashtbl.add h' (fk k) (fv v)) h in
424 let rec map_form f phi =
425 match Formula.expr phi with
426 | Boolean.Or(phi1, phi2) -> Formula.or_ (map_form f phi1) (map_form f phi2)
427 | Boolean.And(phi1, phi2) -> Formula.and_ (map_form f phi1) (map_form f phi2)
428 | Boolean.Atom({ Atom.node = Move(m,q); _}, b) ->
429 let a = Formula.mk_atom (Move (m,f q)) in
430 if b then a else Formula.not_ a
433 let rename_states mapper a =
434 let rename q = try Hashtbl.find mapper q with Not_found -> q in
435 { Builder.make () with
436 states = map_set rename a.states;
437 starting_states = map_set rename a.starting_states;
438 selecting_states = map_set rename a.selecting_states;
443 (List.map (fun (labels, form) -> (labels, map_form rename form)) l))
448 let mapper = Hashtbl.create MED_H_SIZE in
450 StateSet.iter (fun q -> Hashtbl.add mapper q (State.make())) a.states
452 rename_states mapper a
460 (fun q phi -> Formula.(or_ (stay q) phi))
461 a1.selecting_states Formula.false_
463 Hashtbl.iter (fun q trs -> Hashtbl.add a1.transitions q trs)
467 Hashtbl.replace a1.transitions q [(QNameSet.any, link_phi)])
470 states = StateSet.union a1.states a2.states;
471 selecting_states = a2.selecting_states;
472 transitions = a1.transitions;
479 states = StateSet.union a1.states a2.states;
480 selecting_states = StateSet.union a1.selecting_states a2.selecting_states;
481 starting_states = StateSet.union a1.starting_states a2.starting_states;
484 Hashtbl.iter (fun k v -> Hashtbl.add a1.transitions k v) a2.transitions