ima_exe.ml 6.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(*-----------------------------------------------------------------------
 ** Copyright (C) 2001, 2002 - Verimag.
 ** This file may only be copied under the terms of the GNU Library General
 ** Public License 
 **-----------------------------------------------------------------------
 **
 ** File: ima_exe.ml
 ** Main author: jahier@imag.fr
 *)

(*------------------------------------------------------------------------*)

open Util
open Formula
open Env_state
open List
open Command_line_ima_exe

(*------------------------------------------------------------------------*)


let (options:Command_line_ima_exe.optionsT) = {
  user_seed = 0 ;
  boot = false
}


(*------------------------------------------------------------------------*)
(* RIF parsing *)

let lexer = Genlex.make_lexer ["t"; "f"; "#"]

type rif_token = Genlex.token 
type rif_stream = Genlex.token Stream.t

let (get_inputs : vnt list -> rif_stream -> env_in) =
  fun vntl stream -> 
    (** The first token ougth to be a valid token (namely, rif pragmas have
      been filtered out). *)
  
    let tbl = Hashtbl.create (List.length vntl) in
    let _ = 
      List.iter
	(fun  vnt ->  
	   let new_stream =
	     match Stream.peek stream with
		 Some(_) -> stream
	       | None -> 
		   let line = read_line () in
		     lexer(Stream.of_string line) 
	   in
	   let (vn, vt) = vnt in
	     match (vnt, (Stream.next new_stream)) with
54
55
56
57
		 ((vn, BoolT), Genlex.Kwd "t") -> Hashtbl.add tbl vn (B(true))
	       | ((vn, BoolT), Genlex.Kwd "f") -> Hashtbl.add tbl vn (B(false))
	       | ((vn, BoolT), Genlex.Int 0)   -> Hashtbl.add tbl vn (B(false))
	       | ((vn, BoolT), Genlex.Int 1)   -> Hashtbl.add tbl vn (B(true))
58
		   
59
60
	       | ((vn, IntT(_,_)), Genlex.Int i) -> Hashtbl.add tbl vn (N(I(i)))
	       | ((vn, FloatT(_,_)), Genlex.Float f) -> Hashtbl.add tbl vn (N(F(f)))
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

	       | _ -> failwith ("*** Bad input. A " ^ (var_type_to_string2 vt) ^ 
				" was expected for variable " ^ vn ^ ".\n")
	)
	vntl
    in
      tbl
    
let rec (read_rif_input : vnt list -> env_in) =
  fun vntl -> 
    (** Reads the environment inputs on stdin. It should respect the rif format. *)
    let line = read_line () in
    let stream = lexer(Stream.of_string line) in
    let tok_list = Stream.npeek 2 stream in
      match tok_list with
	  [Genlex.Kwd "#"; Genlex.Ident "outs"] ->
	    Stream.junk stream ; 
	    Stream.junk stream ; 
	    get_inputs vntl stream 
	| [Genlex.Kwd "#"; _] -> 
	    (* Ignore everything after a "#"... if it is not followed by "outs". *)
	    read_rif_input vntl
	| [] -> read_rif_input vntl
	| _ -> get_inputs vntl stream 


let (write_rif_output : vnt list -> env_out -> unit) =
  fun vntl output -> 
    let ae_l = List.map (fun (vn, _) -> List.assoc vn output) vntl in
90
      List.iter (print_var_value stdout) ae_l
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213


(*------------------------------------------------------------------------*)

let arg_nb = (Array.length Sys.argv) 

external random_seed: unit -> int = "sys_random_seed";;

let rec (main : unit -> 'a) =
  fun _ ->
    try
      if (arg_nb < 2) 
      then print_string usage 
      else
	main2 ()
    with 
	Failure(errmsg) -> print_string errmsg
      | e -> raise e	

and 
  (get_ima_exe_options: int -> int) =
  fun n -> 
    try 
      begin
	let opt = List.assoc Sys.argv.(n) Command_line_ima_exe.string_to_option in
	  match opt with
	      Boot -> options.boot <- true ; (n+1)
	    | Seed ->
		let str = (Sys.argv.(n+1)) in
		  options.user_seed <- cmd_line_string_to_int str 
		    ("*** Error when calling ima_exe: an " ^
		     "integer is expected after the " ^
		     "option --with-seed\n") ;
		  n+2
      end
    with Not_found -> n
and
  (get_env_from_args: int -> string list list -> string list list) =
  fun n file_ll -> 
    (** Returns the environment file names given at top-level into a
      list of list.  E.g., if the call [ima_exe env1 x env2 x env3
      env4] is done, then [get_env_from_args 10 []] (where 10 is the
      arg nb) returns [[["env1"; "env2"; "env3"]; ["env4"]]].

      Also set the command line options if any.  *)
    if (n = arg_nb) then file_ll
    else
      let m = get_ima_exe_options n in
	(* m > n iff Sys.argv.(n) is an option *)
	if 
	  (m > n) 
	then 
	  get_env_from_args m file_ll
	else
	  let arg = Sys.argv.(m) in
	    if 
	      (arg = "x") 
	    then
	      match file_ll with
		  sl::sll -> 
		    let env = Sys.argv.(n+1) in
                      get_env_from_args (n+2) ((env::sl)::sll)
		| []  -> assert false
	    else 
	      get_env_from_args (n+1) ([arg]::file_ll)
and
  (main2 : unit -> 'a) =
  fun _ -> 
    (* Initialisation of `Env_state.env_state' *)
    let env_llist = (get_env_from_args 1 []) in
    let () = Env.read_env_state env_llist in
     
    let local_var_name_and_type_list_unsorted0 = Env_state.loc_env_unsorted () in
    let local_var_name_and_type_list_unsorted = 
      List.map (fun (vn, vt) -> (vn, (var_type_to_string vt)))
	local_var_name_and_type_list_unsorted0
    in
    let local_var_name_and_type_list = 
      Util.sort_list_string_pair local_var_name_and_type_list_unsorted 
    in
      
    (* Initialisation of the random engine *)
    let seed = 
      if (options.user_seed = 0)
      then (random_seed ())
      else  options.user_seed
    in
      Random.init seed ;
      print_string "#The random engine was initialized with the seed ";
      print_int seed;
      print_string "\n ";
      flush stdout ;      


      env_state.output_var_names <- Env_state.out_env_unsorted () ;

      (* Initializing Dd's libs. *)
      Manager.disable_autodyn env_state.bdd_manager;

      (* Initializing the solution number table *)
      Hashtbl.add env_state.snt 
	(Bdd.dtrue env_state.bdd_manager) (Util.one_sol, Util.zero_sol);
      Hashtbl.add env_state.snt 
	(Bdd.dfalse env_state.bdd_manager) (Util.zero_sol, Util.one_sol);

      print_string "#inputs ";
      List.iter 
	(fun (vn,vt) -> 
	   print_string (vn ^ ":" ^ (var_type_to_string2 vt) ^ " ")
	) 
	(Env_state.out_env_unsorted ());
      print_string "\n";
      flush stdout;

      if options.boot 
      then
	let nll_out_loc_list = Env.env_try 1 1 (Hashtbl.create 0) in
	let (nll, out, loc) = 
	  match nll_out_loc_list with
	      [(nll, out, loc)] -> (nll, out, loc)
	    | _ -> assert false
	in
	  Env.env_step nll out loc ;
214
	  Env.update_pre env_state.input env_state.output env_state.local ;
215
216
217
218
219
220
221
222
223
224
	  write_rif_output (Env_state.out_env_unsorted ()) out ;
      else ();  

      main_loop 1 ;
      flush stdout

and
  main_loop t = 

  let _ = print_string ("\n# step " ^ (string_of_int t) ^ ":\n"); flush stdout in
225
  let previous_nodes = env_state.current_nodes in
226
227

  let input = read_rif_input (Env_state.in_env_unsorted ()) in
228

229
230
231
232
233
234
235
236
237
238

  let nll_out_loc_list = Env.env_try 1 1 input in

  let (nll, out, loc) = 
    match nll_out_loc_list with
	[(nll, out, loc)] -> (nll, out, loc)
      | _ -> assert false
  in

    Env.env_step nll out loc ;
239
240
241
    Env.update_pre env_state.input env_state.output env_state.local ;
    
    Hashtbl.clear env_state.bdd_tbl ;
242
243
244
245
246
247
248
249
250
251
252

    write_rif_output (Env_state.out_env_unsorted ()) out ;

    main_loop (t+1) 
;;
      


main ();;