Full implem BU Oracle + eval_form in Formula (impossible in Asta) + transitions_lab...
[tatoo.git] / src / run.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                   Lucca Hirschi, LRI UMR8623                        *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
8 (*  Copyright 2010-2012 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   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 module Node  =
17 struct
18   type t = int
19   let hash n = n
20   let compare n1 n2 = n1 - n2
21   let equal n1 n2 = n1 = n2
22 end
23   
24 module NodeHash = Hashtbl.Make (Node)
25   
26 type t = (StateSet.t*StateSet.t) NodeHash.t
27 (** Map from node to query and recognizing states *)
28 (* Note that we do not consider the nil nodes *)
29
30 exception Oracle_fail
31 exception Over_max_fail
32 exception Max_fail
33
34 (* Build the Oracle *)
35 let rec bu_oracle asta run tree tnode =
36   let init_set node =
37     let set = (StateSet.empty,StateSet.empty) in
38     NodeHash.add run node set
39   and node = Tree.preorder tree tnode in
40   if (Tree.is_leaf tree tnode)
41   then
42     if not (tnode == Tree.nil)
43     then
44       init_set node
45     else ()
46   else
47     let tfnode = Tree.first_child tree tnode
48     and tnnode = Tree.next_sibling tree tnode in
49     begin
50       bu_oracle asta run tree tfnode;
51       bu_oracle asta run tree tnnode;
52       let (fnode,nnode) =
53         (Tree.preorder tree tfnode, Tree.preorder tree tnnode) in
54       let q_rec n =
55         try NodeHash.find run n
56         with Not_found -> (StateSet.empty,StateSet.empty) in
57       let (_,qf),(_,qn) = q_rec fnode,q_rec nnode in
58       let lab = Tree.tag tree tnode in
59       let _,list_tr = Asta.transitions_lab asta lab in (* only take reco. *)
60       let result_set = ref StateSet.empty in
61       let rec result = function
62         | [] -> ()
63         | (q,form) :: tl ->
64           if Formula.eval_form (qf,qn) form
65           then begin
66             result_set := (StateSet.add q (!result_set));
67             result tl; end
68           else result tl in
69       result list_tr;
70       NodeHash.add run node (StateSet.empty, !result_set)
71     (* Do not remove elt in Hahs (the old one would appear) *)
72     end
73
74 (* Build the over-approx. of the maximal run *)
75 let rec bu_over_max asta run tree node =
76   ()
77
78 (* Build the maximal run *)
79 let rec tp_max asta run tree node =
80   ()
81
82 let compute tree asta =
83   let size_tree = 10000 in              (* todo *)
84   let map = NodeHash.create size_tree in
85   bu_oracle asta map tree (Tree.root tree);
86   bu_over_max asta map tree (Tree.root tree);
87   tp_max asta map tree (Tree.root tree);
88   map
89
90 let print fmt run =
91   let print_d_set fmt (s_1,s_2) = 
92     Format.fprintf fmt "@[<hov 0>(%a,@ %a)@]"
93       StateSet.print s_1 StateSet.print s_2 in
94   let print_map fmt run = 
95     let pp = Format.fprintf fmt in
96     if NodeHash.length run = 0
97     then Format.fprintf fmt "ø"
98     else
99       NodeHash.iter (fun cle set -> pp "|  %i-->%a  @ " cle print_d_set set)
100         run in
101   let print_box fmt run =
102     let pp = Format.fprintf fmt in
103     pp "@[<hov 0>@.  # Mapping:@.   @[<hov 0>%a@]@]"
104       print_map run
105   in
106   Format.fprintf fmt "@[<hov 0>##### RUN #####@, %a@]@." print_box run