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 09:22:47 CET by Kim Nguyen>
24 module Make (T : Tree.Sig.S) = struct
26 type cache = (int, StateSet.t) Hashtbl.t
29 try Hashtbl.find c (T.preorder t n)
30 with Not_found -> StateSet.empty
32 let set c t n v = Hashtbl.replace c (T.preorder t n) v
33 let eval_form phi tree node fcs nss ps ss =
35 match Ata.SFormula.expr phi with
37 | Formula.False -> false
39 let p, b, q = Ata.Atom.node a in
43 | First_child -> StateSet.mem q fcs
44 | Next_sibling -> StateSet.mem q nss
45 | Parent | Previous_sibling -> StateSet.mem q ps
46 | Stay -> StateSet.mem q ss
48 node == (T.first_child tree (T.parent tree node))
50 node == (T.next_sibling tree (T.parent tree node))
52 QName.has_attribute_prefix (T.tag tree node)
54 T.nil != T.first_child tree node
56 T.nil != T.next_sibling tree node
58 if Ata.is_move p && (not b) then
59 eprintf "Warning: Invalid negative atom %a" Ata.Atom.print a;
61 | Formula.And(phi1, phi2) -> loop phi1 && loop phi2
62 | Formula.Or (phi1, phi2) -> loop phi1 || loop phi2
66 let eval_trans l tree node fcs nss ps ss acc =
67 List.fold_left (fun (acct, accs) ((q, phi) as trs) ->
68 if StateSet.mem q accs then (acct, accs) else
69 if eval_form phi tree node fcs nss ps ss then
70 (acct, StateSet.add q accs)
75 let top_down_run auto tree node cache i =
76 let redo = ref false in
78 if node != T.nil then begin
79 let parent = T.parent tree node in
80 let fc = T.first_child tree node in
81 let ns = T.next_sibling tree node in
82 let states0 = get cache tree node in
83 let tag = T.tag tree node in
84 let trans0 = Ata.get_trans auto auto.Ata.states tag in
85 let ps = get cache tree parent in
86 let fcs = get cache tree fc in
87 let nss = get cache tree ns in
88 eprintf "-- [Iteration % 4d] --\n node: %a\n%!" i T.print_node node;
89 List.iter (fun (q, phi) -> eprintf " %a -> %a\n"
90 State.print q Ata.SFormula.print phi) trans0;
91 eprintf "----------------------\n%!";
93 eval_trans trans0 tree node fcs nss ps states0 states0
95 if states1 != states0 then set cache tree node states1;
97 let fcs1 = get cache tree fc in
99 eval_trans trans1 tree node fcs1 nss ps states1 states1
101 if states2 != states1 then set cache tree node states2;
104 eval_trans trans2 tree node fcs1 (get cache tree ns) ps states2 states2
106 if states3 != states2 then set cache tree node states3;
107 if states0 != states3 && (not !redo) then redo := true
113 let get_results auto tree node cache =
114 let rec loop node acc =
115 if node == T.nil then acc
117 let acc0 = loop (T.next_sibling tree node) acc in
118 let acc1 = loop (T.first_child tree node) acc0 in
120 if StateSet.intersect (get cache tree node) auto.Ata.selection_states then
127 let eval auto tree node =
128 let cache = Hashtbl.create 511 in
129 let redo = ref true in
132 redo := top_down_run auto tree node cache !iter;
135 get_results auto tree node cache