]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/tutors/search_pattern_apply_tutor.ml
4790ab88ab7620c1d89045a8873f1ced8e838c85
[helm.git] / helm / hbugs / tutors / search_pattern_apply_tutor.ml
1
2 open Hbugs_types;;
3 open Printf;;
4
5 exception Empty_must;;
6
7 module MQICallbacks =
8    struct
9       let log s = prerr_string s
10    end
11
12 module MQI = MQueryInterpreter.Make(MQICallbacks)
13
14 let broker_id = ref None
15 let my_own_id = Hbugs_tutors_common.init_tutor ()
16 let my_own_addr, my_own_port = "127.0.0.1", 50011
17 let my_own_url = sprintf "%s:%d" my_own_addr my_own_port
18
19 let is_authenticated id =
20   match !broker_id with
21   | None -> false
22   | Some broker_id -> id = broker_id
23
24   (* thread who do the dirty work *)
25 let slave (state, musing_id) =
26  try
27   prerr_endline (sprintf "Hi, I'm the slave for musing %s" musing_id);
28   let (proof, goal) = Hbugs_tutors_common.load_state state in
29   let hint =
30     try
31       let choose_must must only = (* euristic: use 2nd precision level
32                                   1st is more precise but is more slow *)
33         match must with
34         | [] -> raise Empty_must
35         | _::hd::tl -> hd
36         | hd::tl -> hd
37       in
38       let uris =
39         TacticChaser.searchPattern
40          ~output_html:prerr_endline ~choose_must () ~status:(proof, goal)
41       in
42       if uris = [] then
43         Sorry
44       else
45         Eureka (Hints (List.map (fun uri -> Use_apply_Luke uri) uris))
46     with Empty_must -> Sorry
47   in
48   let answer = Musing_completed (my_own_id, musing_id, hint) in
49   ignore (Hbugs_messages.submit_req ~url:Hbugs_tutors_common.broker_url answer);
50   prerr_endline
51     (sprintf "Bye, I've completed my duties (success = %b)" (hint <> Sorry))
52  with
53   (Pxp_types.At _) as e ->
54     let rec unbox_exception =
55      function
56          Pxp_types.At (_,e) -> unbox_exception e
57       | e -> e
58     in
59      prerr_endline ("Uncaught PXP exception: " ^ Pxp_types.string_of_exn e) ;
60      (* e could be the Thread.exit exception; otherwise we will release an  *)
61      (* uncaught exception and the Pxp_types.At was already an uncaught     *)
62      (* exception ==> no additional arm                                     *)
63      raise (unbox_exception e)
64
65 let hbugs_callback =
66   let ids = Hashtbl.create 17 in
67   let forbidden () =
68     prerr_endline "ignoring request from unauthorized broker";
69     Exception ("forbidden", "")
70   in
71   function
72   | Start_musing (broker_id, state) ->
73       if is_authenticated broker_id then begin
74         prerr_endline "received Start_musing";
75         let new_musing_id = Hbugs_id_generator.new_musing_id () in
76         let id = Hbugs_deity.create slave (state, new_musing_id) in
77         prerr_endline
78          (sprintf "starting a new musing (tid = %d, id = %s)" id new_musing_id);
79         Hashtbl.add ids new_musing_id id ;
80         (*ignore (Thread.create slave (state, new_musing_id));*)
81         Musing_started (my_own_id, new_musing_id)
82       end else  (* broker unauthorized *)
83         forbidden ();
84   | Abort_musing (broker_id, musing_id) ->
85       prerr_endline "CSC: Abort_musing received" ;
86       if is_authenticated broker_id then begin
87         (* prerr_endline "Ignoring 'Abort_musing' message ..."; *)
88         (try
89           Hbugs_deity.kill (Hashtbl.find ids musing_id) ;
90           Hashtbl.remove ids musing_id ;
91          with
92             Not_found
93           | Hbugs_deity.Can_t_kill _ ->
94              prerr_endline ("Can not kill slave " ^ musing_id)) ;
95         Musing_aborted (my_own_id, musing_id)
96       end else (* broker unauthorized *)
97         forbidden ();
98   | unexpected_msg ->
99       Exception ("unexpected_msg",
100         Hbugs_messages.string_of_msg unexpected_msg)
101
102 let callback (req: Http_types.request) outchan =
103   try
104     let req_msg = Hbugs_messages.msg_of_string req#body in
105     let answer = hbugs_callback req_msg in
106     Http_daemon.respond ~body:(Hbugs_messages.string_of_msg answer) outchan
107   with Hbugs_messages.Parse_error (subj, reason) ->
108     Http_daemon.respond
109       ~body:(Hbugs_messages.string_of_msg
110         (Exception ("parse_error", reason)))
111       outchan
112
113 let main () =
114   let mqi_options = "" in (* default MathQL interpreter options *)
115   try
116     Sys.catch_break true;
117     at_exit (fun () -> Hbugs_tutors_common.unregister_from_broker my_own_id);
118     broker_id :=
119       Some (Hbugs_tutors_common.register_to_broker
120         my_own_id my_own_url "FOO" "Search_pattern_apply tutor");
121     ignore (MQI.init mqi_options) ;
122     Http_daemon.start'
123       ~addr:my_own_addr ~port:my_own_port ~mode:`Thread callback;
124     MQI.close mqi_options
125   with Sys.Break -> ()  (* exit nicely, invoking at_exit functions *)
126 ;;
127
128 main ()
129