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-17 20:26:59 CET by Kim Nguyen>
24 type predicate = | First_child
31 | Is of (Tree.Common.NodeKind.t)
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 k -> fprintf ppf "is-%a?" Tree.Common.NodeKind.print k
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 open Tree.Common.NodeKind
81 let mk_atom a b c = atom_ (Atom.make (a,b,c))
82 let mk_kind k = mk_atom (Is k) true State.dummy
84 (mk_atom Has_first_child true State.dummy)
86 let has_next_sibling =
87 (mk_atom Has_next_sibling true State.dummy)
90 (mk_atom Is_first_child true State.dummy)
93 (mk_atom Is_next_sibling true State.dummy)
96 (mk_atom (Is Attribute) true State.dummy)
99 (mk_atom (Is Element) true State.dummy)
101 let is_processing_instruction =
102 (mk_atom (Is ProcessingInstruction) true State.dummy)
105 (mk_atom (Is Comment) true State.dummy)
109 (mk_atom First_child true q)
114 (mk_atom Next_sibling true q)
119 (mk_atom Parent true q)
122 let previous_sibling q =
124 (mk_atom Previous_sibling true q)
128 (mk_atom Stay true q)
133 | Formula.Atom a -> let _, _, q = Atom.node a in
134 if q != State.dummy then StateSet.add q acc else acc
141 module Transition = Hcons.Make (struct
142 type t = State.t * QNameSet.t * SFormula.t
143 let equal (a, b, c) (d, e, f) =
144 a == d && b == e && c == f
146 HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((SFormula.uid c) :> int))
150 module TransList : sig
151 include Hlist.S with type elt = Transition.t
152 val print : Format.formatter -> ?sep:string -> t -> unit
155 include Hlist.Make(Transition)
156 let print ppf ?(sep="\n") l =
158 let q, lab, f = Transition.node t in
159 fprintf ppf "%a, %a -> %a%s" State.print q QNameSet.print lab SFormula.print f sep) l
165 mutable states : StateSet.t;
166 mutable selection_states: StateSet.t;
167 transitions: (State.t, (QNameSet.t*SFormula.t) list) Hashtbl.t;
168 mutable cache2 : TransList.t Cache.N2.t;
169 mutable cache6 : (TransList.t*StateSet.t) Cache.N6.t;
172 let next = Uid.make_maker ()
174 let dummy2 = TransList.cons
175 (Transition.make (State.dummy,QNameSet.empty, SFormula.false_))
178 let dummy6 = (dummy2, StateSet.empty)
182 let auto = { id = next ();
184 selection_states = ss;
185 transitions = Hashtbl.create 17;
186 cache2 = Cache.N2.create dummy2;
187 cache6 = Cache.N6.create dummy6;
193 Cache.N2.iteri (fun _ _ _ b -> if b then incr n2) auto.cache2;
194 Cache.N6.iteri (fun _ _ _ _ _ _ _ b -> if b then incr n6) auto.cache6;
195 Format.eprintf "INFO: automaton %i, cache2: %i entries, cache6: %i entries\n%!"
196 (auto.id :> int) !n2 !n6;
197 let c2l, c2u = Cache.N2.stats auto.cache2 in
198 let c6l, c6u = Cache.N6.stats auto.cache6 in
199 Format.eprintf "INFO: cache2: length: %i, used: %i, occupation: %f\n%!" c2l c2u (float c2u /. float c2l);
200 Format.eprintf "INFO: cache6: length: %i, used: %i, occupation: %f\n%!" c6l c6u (float c6u /. float c6l)
206 a.cache2 <- Cache.N2.create dummy2;
207 a.cache6 <- Cache.N6.create dummy6
210 let get_trans_aux a tag states =
211 StateSet.fold (fun q acc0 ->
213 let trs = Hashtbl.find a.transitions q in
214 List.fold_left (fun acc1 (labs, phi) ->
215 if QNameSet.mem tag labs then TransList.cons (Transition.make (q, labs, phi)) acc1 else acc1) acc0 trs
216 with Not_found -> acc0
217 ) states TransList.nil
220 let get_trans a tag states =
222 Cache.N2.find a.cache2
223 (tag.QName.id :> int) (states.StateSet.id :> int)
225 if trs == dummy2 then
226 let trs = get_trans_aux a tag states in
229 (tag.QName.id :> int)
230 (states.StateSet.id :> int) trs; trs)
235 let eval_form phi fcs nss ps ss is_left is_right has_left has_right kind =
237 begin match SFormula.expr phi with
238 Formula.True | Formula.False -> phi
240 let p, b, q = Atom.node a in begin
243 if b == StateSet.mem q fcs then SFormula.true_ else phi
245 if b == StateSet.mem q nss then SFormula.true_ else phi
246 | Parent | Previous_sibling ->
247 if b == StateSet.mem q ps then SFormula.true_ else phi
249 if b == StateSet.mem q ss then SFormula.true_ else phi
250 | Is_first_child -> SFormula.of_bool (b == is_left)
251 | Is_next_sibling -> SFormula.of_bool (b == is_right)
252 | Is k -> SFormula.of_bool (b == (k == kind))
253 | Has_first_child -> SFormula.of_bool (b == has_left)
254 | Has_next_sibling -> SFormula.of_bool (b == has_right)
256 | Formula.And(phi1, phi2) -> SFormula.and_ (loop phi1) (loop phi2)
257 | Formula.Or (phi1, phi2) -> SFormula.or_ (loop phi1) (loop phi2)
262 let int_of_conf is_left is_right has_left has_right kind =
263 ((Obj.magic kind) lsl 4) lor
264 ((Obj.magic is_left) lsl 3) lor
265 ((Obj.magic is_right) lsl 2) lor
266 ((Obj.magic has_left) lsl 1) lor
267 (Obj.magic has_right)
269 let eval_trans auto ltrs fcs nss ps ss is_left is_right has_left has_right kind =
270 let n = int_of_conf is_left is_right has_left has_right kind
271 and k = (fcs.StateSet.id :> int)
272 and l = (nss.StateSet.id :> int)
273 and m = (ps.StateSet.id :> int) in
274 let rec loop ltrs ss =
275 let i = (ltrs.TransList.id :> int)
276 and j = (ss.StateSet.id :> int) in
277 let (new_ltrs, new_ss) as res =
278 let res = Cache.N6.find auto.cache6 i j k l m n in
279 if res == dummy6 then
281 TransList.fold (fun trs (acct, accs) ->
282 let q, lab, phi = Transition.node trs in
283 if StateSet.mem q accs then (acct, accs) else
287 is_left is_right has_left has_right kind
289 if SFormula.is_true new_phi then
290 (acct, StateSet.add q accs)
291 else if SFormula.is_false new_phi then
294 let new_tr = Transition.make (q, lab, new_phi) in
295 (TransList.cons new_tr acct, accs)
296 ) ltrs (TransList.nil, ss)
298 Cache.N6.add auto.cache6 i j k l m n res; res
302 if new_ss == ss then res else
312 [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
313 automaton [a] but ensures that transitions remains pairwise disjoint
316 let add_trans a q s f =
317 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
319 List.fold_left (fun (acup, atrs) (labs, phi) ->
320 let lab1 = QNameSet.inter labs s in
321 let lab2 = QNameSet.diff labs s in
323 if QNameSet.is_empty lab1 then []
324 else [ (lab1, SFormula.or_ phi f) ]
327 if QNameSet.is_empty lab2 then []
328 else [ (lab2, SFormula.or_ phi f) ]
330 (QNameSet.union acup labs, tr1@ tr2 @ atrs)
331 ) (QNameSet.empty, []) trs
333 let rem = QNameSet.diff s cup in
334 let ntrs = if QNameSet.is_empty rem then ntrs
335 else (rem, f) :: ntrs
337 Hashtbl.replace a.transitions q ntrs
339 let _pr_buff = Buffer.create 50
340 let _str_fmt = formatter_of_buffer _pr_buff
341 let _flush_str_fmt () = pp_print_flush _str_fmt ();
342 let s = Buffer.contents _pr_buff in
343 Buffer.clear _pr_buff; s
347 "\nInternal UID: %i@\n\
349 Selection states: %a@\n\
350 Alternating transitions:@\n"
352 StateSet.print a.states
353 StateSet.print a.selection_states;
356 (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
360 let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
361 let c = State.compare q1 q2 in - (if c == 0 then QNameSet.compare s1 s2 else c))
364 let _ = _flush_str_fmt () in
365 let strs_strings, max_pre, max_all = List.fold_left (fun (accl, accp, acca) (q, s, f) ->
366 let s1 = State.print _str_fmt q; _flush_str_fmt () in
367 let s2 = QNameSet.print _str_fmt s; _flush_str_fmt () in
368 let s3 = SFormula.print _str_fmt f; _flush_str_fmt () in
369 let pre = Pretty.length s1 + Pretty.length s2 in
370 let all = Pretty.length s3 in
371 ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
372 ) ([], 0, 0) sorted_trs
374 let line = Pretty.line (max_all + max_pre + 6) in
375 let prev_q = ref State.dummy in
376 List.iter (fun (q, s1, s2, s3) ->
377 if !prev_q != q && !prev_q != State.dummy then fprintf fmt " %s\n%!" line;
379 fprintf fmt " %s, %s" s1 s2;
380 fprintf fmt "%s" (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
381 fprintf fmt " %s %s@\n%!" Pretty.right_arrow s3;
383 fprintf fmt " %s\n%!" line
386 [complete transitions a] ensures that for each state q
387 and each symbols s in the alphabet, a transition q, s exists.
388 (adding q, s -> F when necessary).
391 let complete_transitions a =
392 StateSet.iter (fun q ->
393 let qtrans = Hashtbl.find a.transitions q in
395 List.fold_left (fun rem (labels, _) ->
396 QNameSet.diff rem labels) QNameSet.any qtrans
399 if QNameSet.is_empty rem then qtrans
401 (rem, SFormula.false_) :: qtrans
403 Hashtbl.replace a.transitions q nqtrans
406 let cleanup_states a =
407 let memo = ref StateSet.empty in
409 if not (StateSet.mem q !memo) then begin
410 memo := StateSet.add q !memo;
411 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
412 List.iter (fun (_, phi) ->
413 StateSet.iter loop (SFormula.get_states phi)) trs
416 StateSet.iter loop a.selection_states;
417 let unused = StateSet.diff a.states !memo in
418 eprintf "Unused states %a\n%!" StateSet.print unused;
419 StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
422 (* [normalize_negations a] removes negative atoms in the formula
423 complementing the sub-automaton in the negative states.
424 [TODO check the meaning of negative upward arrows]
427 let normalize_negations auto =
428 eprintf "Automaton before normalize_trans:\n";
429 print err_formatter auto;
430 eprintf "--------------------\n%!";
432 let memo_state = Hashtbl.create 17 in
433 let todo = Queue.create () in
435 match SFormula.expr f with
436 Formula.True | Formula.False -> if b then f else SFormula.not_ f
437 | Formula.Or(f1, f2) -> (if b then SFormula.or_ else SFormula.and_)(flip b f1) (flip b f2)
438 | Formula.And(f1, f2) -> (if b then SFormula.and_ else SFormula.or_)(flip b f1) (flip b f2)
439 | Formula.Atom(a) -> begin
440 let l, b', q = Atom.node a in
441 if q == State.dummy then if b then f else SFormula.not_ f
443 if b == b' then begin
444 (* a appears positively, either no negation or double negation *)
445 if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
446 SFormula.atom_ (Atom.make (l, true, q))
448 (* need to reverse the atom
449 either we have a positive state deep below a negation
450 or we have a negative state in a positive formula
451 b' = sign of the state
452 b = sign of the enclosing formula
456 (* does the inverted state of q exist ? *)
457 Hashtbl.find memo_state (q, false)
460 (* create a new state and add it to the todo queue *)
461 let nq = State.make () in
462 auto.states <- StateSet.add nq auto.states;
463 Hashtbl.add memo_state (q, false) nq;
464 Queue.add (q, false) todo; nq
466 SFormula.atom_ (Atom.make (l, true, not_q))
470 (* states that are not reachable from a selection stat are not interesting *)
471 StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selection_states;
473 while not (Queue.is_empty todo) do
474 let (q, b) as key = Queue.pop todo in
477 Hashtbl.find memo_state key
480 let nq = if b then q else
481 let nq = State.make () in
482 auto.states <- StateSet.add nq auto.states;
485 Hashtbl.add memo_state key nq; nq
487 let trans = Hashtbl.find auto.transitions q in
488 let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
489 Hashtbl.replace auto.transitions q' trans';