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 (***********************************************************************)
18 type move = [ `First_child
24 type predicate = Move of move * State.t
27 | Is of Tree.NodeKind.t
31 let is_move = function Move _ -> true | _ -> false
33 module Atom : (Boolean.ATOM with type data = predicate) =
39 let equal n1 n2 = n1 = n2
40 let hash n = Hashtbl.hash n
43 include Hcons.Make(Node)
47 | Move (m, q) -> begin
49 `First_child -> fprintf ppf "%s" Pretty.down_arrow
50 | `Next_sibling -> fprintf ppf "%s" Pretty.right_arrow
51 | `Parent -> fprintf ppf "%s" Pretty.up_arrow
52 | `Previous_sibling -> fprintf ppf "%s" Pretty.left_arrow
53 | `Stay -> fprintf ppf "%s" Pretty.bullet
55 fprintf ppf "%a" State.print q
56 | Is_first_child -> fprintf ppf "%s?" Pretty.up_arrow
57 | Is_next_sibling -> fprintf ppf "%s?" Pretty.left_arrow
58 | Is k -> fprintf ppf "is-%a?" Tree.NodeKind.print k
59 | Has_first_child -> fprintf ppf "%s?" Pretty.down_arrow
60 | 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 mk_kind 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
120 module Transition = Hcons.Make (struct
121 type t = State.t * QNameSet.t * Formula.t
122 let equal (a, b, c) (d, e, f) =
123 a == d && b == e && c == f
125 HASHINT4 (PRIME1, a, ((QNameSet.uid b) :> int), ((Formula.uid c) :> int))
129 module TransList : sig
130 include Hlist.S with type elt = Transition.t
131 val print : Format.formatter -> ?sep:string -> t -> unit
134 include Hlist.Make(Transition)
135 let print ppf ?(sep="\n") l =
137 let q, lab, f = Transition.node t in
138 fprintf ppf "%a, %a -> %a%s" State.print q QNameSet.print lab Formula.print f sep) l
143 type node_summary = int
144 let dummy_summary = -1
154 let has_right (s : node_summary) : bool =
156 let has_left (s : node_summary) : bool =
157 Obj.magic ((s lsr 1) land 1)
159 let is_right (s : node_summary) : bool =
160 Obj.magic ((s lsr 2) land 1)
162 let is_left (s : node_summary) : bool =
163 Obj.magic ((s lsr 3) land 1)
165 let kind (s : node_summary ) : Tree.NodeKind.t =
168 let node_summary is_left is_right has_left has_right kind =
169 ((Obj.magic kind) lsl 4) lor
170 ((Obj.magic is_left) lsl 3) lor
171 ((Obj.magic is_right) lsl 2) lor
172 ((Obj.magic has_left) lsl 1) lor
173 (Obj.magic has_right)
181 summary : node_summary;
184 module Config = Hcons.Make(struct
189 c.unsat == d.unsat &&
191 c.summary == d.summary
194 HASHINT4((c.sat.StateSet.id :> int),
195 (c.unsat.StateSet.id :> int),
196 (c.todo.TransList.id :> int),
203 mutable states : StateSet.t;
204 mutable selection_states: StateSet.t;
205 transitions: (State.t, (QNameSet.t*Formula.t) list) Hashtbl.t;
206 mutable cache2 : TransList.t Cache.N2.t;
207 mutable cache4 : Config.t Cache.N4.t;
210 let next = Uid.make_maker ()
212 let dummy2 = TransList.cons
213 (Transition.make (State.dummy,QNameSet.empty, Formula.false_))
219 Config.make { sat = StateSet.empty;
220 unsat = StateSet.empty;
221 todo = TransList.nil;
222 summary = dummy_summary
227 let auto = { id = next ();
229 selection_states = ss;
230 transitions = Hashtbl.create 17;
231 cache2 = Cache.N2.create dummy2;
232 cache4 = Cache.N4.create dummy_config;
238 Cache.N2.iteri (fun _ _ _ b -> if b then incr n2) auto.cache2;
239 Cache.N4.iteri (fun _ _ _ _ _ b -> if b then incr n4) auto.cache4;
240 Logger.msg `STATS "automaton %i, cache2: %i entries, cache6: %i entries"
241 (auto.id :> int) !n2 !n4;
242 let c2l, c2u = Cache.N2.stats auto.cache2 in
243 let c4l, c4u = Cache.N4.stats auto.cache4 in
245 "cache2: length: %i, used: %i, occupation: %f"
246 c2l c2u (float c2u /. float c2l);
248 "cache4: length: %i, used: %i, occupation: %f"
249 c4l c4u (float c4u /. float c4l)
255 a.cache4 <- Cache.N4.create (Cache.N4.dummy a.cache4)
259 a.cache2 <- Cache.N2.create (Cache.N2.dummy a.cache2)
262 let get_trans_aux a tag states =
263 StateSet.fold (fun q acc0 ->
265 let trs = Hashtbl.find a.transitions q in
266 List.fold_left (fun acc1 (labs, phi) ->
267 if QNameSet.mem tag labs then TransList.cons (Transition.make (q, labs, phi)) acc1 else acc1) acc0 trs
268 with Not_found -> acc0
269 ) states TransList.nil
272 let get_trans a tag states =
274 Cache.N2.find a.cache2
275 (tag.QName.id :> int) (states.StateSet.id :> int)
277 if trs == dummy2 then
278 let trs = get_trans_aux a tag states in
281 (tag.QName.id :> int)
282 (states.StateSet.id :> int) trs; trs)
285 let simplify_atom atom pos q { Config.node=config; _ } =
286 if (pos && StateSet.mem q config.sat)
287 || ((not pos) && StateSet.mem q config.unsat) then Formula.true_
288 else if (pos && StateSet.mem q config.unsat)
289 || ((not pos) && StateSet.mem q config.sat) then Formula.false_
292 let eval_form phi fcs nss ps ss summary =
294 begin match Formula.expr phi with
295 Boolean.True | Boolean.False -> phi
296 | Boolean.Atom (a, b) ->
298 match a.Atom.node with
300 let states = match m with
302 | `Next_sibling -> nss
303 | `Parent | `Previous_sibling -> ps
305 in simplify_atom phi b q states
306 | Is_first_child -> Formula.of_bool (b == (is_left summary))
307 | Is_next_sibling -> Formula.of_bool (b == (is_right summary))
308 | Is k -> Formula.of_bool (b == (k == (kind summary)))
309 | Has_first_child -> Formula.of_bool (b == (has_left summary))
310 | Has_next_sibling -> Formula.of_bool (b == (has_right summary))
312 | Boolean.And(phi1, phi2) -> Formula.and_ (loop phi1) (loop phi2)
313 | Boolean.Or (phi1, phi2) -> Formula.or_ (loop phi1) (loop phi2)
320 let eval_trans auto fcs nss ps ss =
321 let fcsid = (fcs.Config.id :> int) in
322 let nssid = (nss.Config.id :> int) in
323 let psid = (ps.Config.id :> int) in
324 let rec loop old_config =
325 let oid = (old_config.Config.id :> int) in
327 let res = Cache.N4.find auto.cache4 oid fcsid nssid psid in
328 if res != dummy_config then res
333 summary = old_summary } = old_config.Config.node
335 let sat, unsat, removed, kept, todo =
338 let q, lab, phi = Transition.node trs in
339 let a_sat, a_unsat, a_rem, a_kept, a_todo = acc in
340 if StateSet.mem q a_sat || StateSet.mem q a_unsat then acc else
342 eval_form phi fcs nss ps old_config old_summary
344 if Formula.is_true new_phi then
345 StateSet.add q a_sat, a_unsat, StateSet.add q a_rem, a_kept, a_todo
346 else if Formula.is_false new_phi then
347 a_sat, StateSet.add q a_unsat, StateSet.add q a_rem, a_kept, a_todo
349 let new_tr = Transition.make (q, lab, new_phi) in
350 (a_sat, a_unsat, a_rem, StateSet.add q a_kept, (TransList.cons new_tr a_todo))
351 ) old_todo (old_sat, old_unsat, StateSet.empty, StateSet.empty, TransList.nil)
353 (* States that have been removed from the todo list and not kept are now
355 let unsat = StateSet.union unsat (StateSet.diff removed kept) in
356 (* States that were found once to be satisfiable remain so *)
357 let unsat = StateSet.diff unsat sat in
358 let new_config = Config.make { old_config.Config.node with sat; unsat; todo; } in
359 Cache.N4.add auto.cache4 oid fcsid nssid psid new_config;
362 if res == old_config then res else loop res
367 [add_trans a q labels f] adds a transition [(q,labels) -> f] to the
368 automaton [a] but ensures that transitions remains pairwise disjoint
371 let add_trans a q s f =
372 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
374 List.fold_left (fun (acup, atrs) (labs, phi) ->
375 let lab1 = QNameSet.inter labs s in
376 let lab2 = QNameSet.diff labs s in
378 if QNameSet.is_empty lab1 then []
379 else [ (lab1, Formula.or_ phi f) ]
382 if QNameSet.is_empty lab2 then []
383 else [ (lab2, Formula.or_ phi f) ]
385 (QNameSet.union acup labs, tr1@ tr2 @ atrs)
386 ) (QNameSet.empty, []) trs
388 let rem = QNameSet.diff s cup in
389 let ntrs = if QNameSet.is_empty rem then ntrs
390 else (rem, f) :: ntrs
392 Hashtbl.replace a.transitions q ntrs
394 let _pr_buff = Buffer.create 50
395 let _str_fmt = formatter_of_buffer _pr_buff
396 let _flush_str_fmt () = pp_print_flush _str_fmt ();
397 let s = Buffer.contents _pr_buff in
398 Buffer.clear _pr_buff; s
402 "Internal UID: %i@\n\
404 Selection states: %a@\n\
405 Alternating transitions:@\n"
407 StateSet.print a.states
408 StateSet.print a.selection_states;
411 (fun q t acc -> List.fold_left (fun acc (s , f) -> (q,s,f)::acc) acc t)
415 let sorted_trs = List.stable_sort (fun (q1, s1, _) (q2, s2, _) ->
416 let c = State.compare q1 q2 in - (if c == 0 then QNameSet.compare s1 s2 else c))
419 let _ = _flush_str_fmt () in
420 let strs_strings, max_pre, max_all = List.fold_left (fun (accl, accp, acca) (q, s, f) ->
421 let s1 = State.print _str_fmt q; _flush_str_fmt () in
422 let s2 = QNameSet.print _str_fmt s; _flush_str_fmt () in
423 let s3 = Formula.print _str_fmt f; _flush_str_fmt () in
424 let pre = Pretty.length s1 + Pretty.length s2 in
425 let all = Pretty.length s3 in
426 ( (q, s1, s2, s3) :: accl, max accp pre, max acca all)
427 ) ([], 0, 0) sorted_trs
429 let line = Pretty.line (max_all + max_pre + 6) in
430 let prev_q = ref State.dummy in
431 fprintf fmt "%s@\n" line;
432 List.iter (fun (q, s1, s2, s3) ->
433 if !prev_q != q && !prev_q != State.dummy then fprintf fmt "%s@\n" line;
435 fprintf fmt "%s, %s" s1 s2;
436 fprintf fmt "%s" (Pretty.padding (max_pre - Pretty.length s1 - Pretty.length s2));
437 fprintf fmt " %s %s@\n" Pretty.right_arrow s3;
439 fprintf fmt "%s@\n" line
442 [complete transitions a] ensures that for each state q
443 and each symbols s in the alphabet, a transition q, s exists.
444 (adding q, s -> F when necessary).
447 let complete_transitions a =
448 StateSet.iter (fun q ->
449 let qtrans = Hashtbl.find a.transitions q in
451 List.fold_left (fun rem (labels, _) ->
452 QNameSet.diff rem labels) QNameSet.any qtrans
455 if QNameSet.is_empty rem then qtrans
457 (rem, Formula.false_) :: qtrans
459 Hashtbl.replace a.transitions q nqtrans
462 let cleanup_states a =
463 let memo = ref StateSet.empty in
465 if not (StateSet.mem q !memo) then begin
466 memo := StateSet.add q !memo;
467 let trs = try Hashtbl.find a.transitions q with Not_found -> [] in
468 List.iter (fun (_, phi) ->
469 StateSet.iter loop (Formula.get_states phi)) trs
472 StateSet.iter loop a.selection_states;
473 let unused = StateSet.diff a.states !memo in
474 StateSet.iter (fun q -> Hashtbl.remove a.transitions q) unused;
477 (* [normalize_negations a] removes negative atoms in the formula
478 complementing the sub-automaton in the negative states.
479 [TODO check the meaning of negative upward arrows]
482 let normalize_negations auto =
483 let memo_state = Hashtbl.create 17 in
484 let todo = Queue.create () in
486 match Formula.expr f with
487 Boolean.True | Boolean.False -> if b then f else Formula.not_ f
488 | Boolean.Or(f1, f2) -> (if b then Formula.or_ else Formula.and_)(flip b f1) (flip b f2)
489 | Boolean.And(f1, f2) -> (if b then Formula.and_ else Formula.or_)(flip b f1) (flip b f2)
490 | Boolean.Atom(a, b') -> begin
491 match a.Atom.node with
493 if b == b' then begin
494 (* a appears positively, either no negation or double negation *)
495 if not (Hashtbl.mem memo_state (q,b)) then Queue.add (q,true) todo;
496 Formula.mk_atom (Move(m, q))
498 (* need to reverse the atom
499 either we have a positive state deep below a negation
500 or we have a negative state in a positive formula
501 b' = sign of the state
502 b = sign of the enclosing formula
506 (* does the inverted state of q exist ? *)
507 Hashtbl.find memo_state (q, false)
510 (* create a new state and add it to the todo queue *)
511 let nq = State.make () in
512 auto.states <- StateSet.add nq auto.states;
513 Hashtbl.add memo_state (q, false) nq;
514 Queue.add (q, false) todo; nq
516 Formula.mk_atom (Move (m,not_q))
518 | _ -> if b then f else Formula.not_ f
521 (* states that are not reachable from a selection stat are not interesting *)
522 StateSet.iter (fun q -> Queue.add (q, true) todo) auto.selection_states;
524 while not (Queue.is_empty todo) do
525 let (q, b) as key = Queue.pop todo in
528 Hashtbl.find memo_state key
531 let nq = if b then q else
532 let nq = State.make () in
533 auto.states <- StateSet.add nq auto.states;
536 Hashtbl.add memo_state key nq; nq
538 let trans = Hashtbl.find auto.transitions q in
539 let trans' = List.map (fun (lab, f) -> lab, flip b f) trans in
540 Hashtbl.replace auto.transitions q' trans';