]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/common/hbugs_messages.ml
b320501ba70eb4b570a9ffae0f74d517c3425fdf
[helm.git] / helm / hbugs / common / hbugs_messages.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Hbugs_types;;
30 open Printf;;
31 open Pxp_document;;
32 open Pxp_dtd;;
33 open Pxp_types;;
34 open Pxp_yacc;;
35
36 let debug = 2;; (*  0 -> no debug
37                     1 -> waiting for an answer / answer received
38                     2 -> XML messages dumping
39                 *)
40
41 exception Attribute_not_found of string;;
42 exception Empty_node;;  (** found a node with no _element_ children *)
43 exception No_element_found of string;;
44 exception Parse_error of string * string;;  (* parsing subject, reason *)
45 exception Unexpected_message of message;;
46
47 let is_xml_element n = match n#node_type with T_element _ -> true | _ -> false
48 let get_attr node name =
49   try
50     (match node#attribute name with
51     | Value s -> s
52     | _ -> raise Not_found)
53   with Not_found -> raise (Attribute_not_found name)
54 let assert_element n name =
55   match n#node_type with
56   | T_element n when n = name ->
57       ()
58   | _ -> raise (Parse_error ("", "Expected node: " ^ name))
59
60   (** given a string representation of a proof asistant state (e.g. the first
61   child of the XML root of a State_change or Start_musing message), build from
62   it an HBugs view of a proof assistant state *)
63 let parse_state (root: ('a node extension as 'a) node) =
64   if (List.filter is_xml_element root#sub_nodes) = [] then
65     raise Empty_node;
66   let buf = Buffer.create 10240 in
67   let node_to_string (node: ('a node extension as 'a) node) =
68     Buffer.clear buf;
69     node#write (`Out_buffer buf) `Enc_utf8;
70     let res = Buffer.contents buf in
71     Buffer.clear buf;
72     res
73   in
74   let (goal_node, type_node, body_node) =
75     try
76       (find_element "CurrentGoal" root,
77        find_element "ConstantType" root,
78        find_element "CurrentProof" root)
79     with Not_found ->
80       raise (Parse_error ("", "Malformed HBugs status XML document"))
81   in
82   assert_element root "gTopLevelStatus";
83   assert_element goal_node "CurrentGoal";
84   assert_element type_node "ConstantType";
85   assert_element body_node "CurrentProof";
86   goal_node#write (`Out_buffer buf) `Enc_utf8;
87   let (type_string, body_string) =
88     (node_to_string type_node, node_to_string body_node)
89   in
90   let goal =
91     try
92       int_of_string (goal_node#data)
93     with Failure "int_of_string" ->
94       raise (Parse_error (goal_node#data, "can't parse goal"))
95   in
96   (type_string, body_string, goal)
97
98   (** parse an hint from an XML node, XML node should have type 'T_element _'
99   (the name is ignored), attributes on it are ignored *)
100 let parse_hint node =
101  let rec parse_hint_node node =
102    match node#node_type with
103    | T_element "ring" -> Use_ring_Luke
104    | T_element "fourier" -> Use_fourier_Luke
105    | T_element "reflexivity" -> Use_reflexivity_Luke
106    | T_element "symmetry" -> Use_symmetry_Luke
107    | T_element "assumption" -> Use_assumption_Luke
108    | T_element "contradiction" -> Use_contradiction_Luke
109    | T_element "exists" -> Use_exists_Luke
110    | T_element "split" -> Use_split_Luke
111    | T_element "left" -> Use_left_Luke
112    | T_element "right" -> Use_right_Luke
113    | T_element "apply" -> Use_apply_Luke node#data
114    | T_element "hints" ->
115        Hints
116         (List.map parse_hint_node (List.filter is_xml_element node#sub_nodes))
117    | _ -> assert false (* CSC: should this assert false be a raise something? *)
118  in
119   match List.filter is_xml_element node#sub_nodes with
120      [node] -> parse_hint_node node
121    | _ -> assert false (* CSC: should this assert false be a raise something? *)
122
123 let parse_hint_type n = n#data (* TODO parsare il possibile tipo di suggerimento *)
124 let parse_tutor_dscs n =
125   List.map
126     (fun n -> (get_attr n "id", n#data))
127     (List.filter is_xml_element n#sub_nodes)
128 let parse_tutor_ids node =
129   List.map
130     (fun n -> get_attr n "id") (List.filter is_xml_element node#sub_nodes)
131
132 let tutors_sep = Pcre.regexp ",\\s*"
133
134 let msg_of_string' s =
135   let root =  (* xml tree's root *)
136     parse_wfcontent_entity default_config (from_string s) default_spec
137   in
138   match root#node_type with
139
140     (* general purpose *)
141   | T_element "help" -> Help
142   | T_element "usage" -> Usage root#data
143   | T_element "exception" -> Exception (get_attr root "name", root#data)
144
145     (* client -> broker *)
146   | T_element "register_client" ->
147       Register_client (get_attr root "id", get_attr root "url")
148   | T_element "unregister_client" -> Unregister_client (get_attr root "id")
149   | T_element "list_tutors" -> List_tutors (get_attr root "id")
150   | T_element "subscribe" ->
151       Subscribe (get_attr root "id", parse_tutor_ids root)
152   | T_element "state_change" ->
153       let state_node =
154         try
155           Some (find_element ~deeply:false "gTopLevelStatus" root)
156         with Not_found -> None
157       in
158       State_change
159         (get_attr root "id",
160         match state_node with
161         | Some n -> (try Some (parse_state n) with Empty_node -> None)
162         | None -> None)
163   | T_element "wow" -> Wow (get_attr root "id")
164
165     (* tutor -> broker *)
166   | T_element "register_tutor" ->
167       let hint_node = find_element "hint_type" root in
168       let dsc_node = find_element "description" root in
169       Register_tutor
170         (get_attr root "id", get_attr root "url",
171         parse_hint_type hint_node, dsc_node#data)
172   | T_element "unregister_tutor" -> Unregister_tutor (get_attr root "id")
173   | T_element "musing_started" ->
174       Musing_started (get_attr root "id", get_attr root "musing_id")
175   | T_element "musing_aborted" ->
176       Musing_started (get_attr root "id", get_attr root "musing_id")
177   | T_element "musing_completed" ->
178       let main_node =
179         try
180           find_element "eureka" root
181         with Not_found -> find_element "sorry" root
182       in
183       Musing_completed
184         (get_attr root "id", get_attr root "musing_id",
185         (match main_node#node_type with
186         | T_element "eureka" ->
187             Eureka (parse_hint main_node)
188         | T_element "sorry" -> Sorry
189         | _ -> assert false)) (* can't be there, see 'find_element' above *)
190
191     (* broker -> client *)
192   | T_element "client_registered" -> Client_registered (get_attr root "id")
193   | T_element "client_unregistered" -> Client_unregistered (get_attr root "id")
194   | T_element "tutor_list" ->
195       Tutor_list (get_attr root "id", parse_tutor_dscs root)
196   | T_element "subscribed" ->
197       Subscribed (get_attr root "id", parse_tutor_ids root)
198   | T_element "state_accepted" ->
199       State_accepted
200         (get_attr root "id",
201         List.map
202           (fun n -> get_attr n "id")
203           (List.filter is_xml_element (find_element "stopped" root)#sub_nodes),
204         List.map
205           (fun n -> get_attr n "id")
206           (List.filter is_xml_element (find_element "started" root)#sub_nodes))
207   | T_element "hint" -> Hint (get_attr root "id", parse_hint root)
208
209     (* broker -> tutor *)
210   | T_element "tutor_registered" -> Tutor_registered (get_attr root "id")
211   | T_element "tutor_unregistered" -> Tutor_unregistered (get_attr root "id")
212   | T_element "start_musing" ->
213       let state_node =
214         try
215           find_element ~deeply:false "gTopLevelStatus" root
216         with Not_found -> raise (No_element_found "gTopLevelStatus")
217       in
218       Start_musing (get_attr root "id", parse_state state_node)
219   | T_element "abort_musing" ->
220       Abort_musing (get_attr root "id", get_attr root "musing_id")
221   | T_element "thanks" -> Thanks (get_attr root "id", get_attr root "musing_id")
222   | T_element "too_late" ->
223       Too_late (get_attr root "id", get_attr root "musing_id")
224
225   | _ -> raise (No_element_found s)
226
227 let msg_of_string s =
228   try
229     msg_of_string' s
230   with e -> raise (Parse_error (s, Printexc.to_string e))
231
232 let pp_state = function
233   | Some (type_string, body_string, goal) ->
234     (* ASSUMPTION: type_string and body_string are well formed XML document
235     contents (i.e. they don't contain heading <?xml ... ?> declaration nor
236     DOCTYPE one *)
237     "<gTopLevelStatus>\n" ^
238     (sprintf "<CurrentGoal>%d</CurrentGoal>\n" goal) ^
239     type_string ^ "\n" ^
240     body_string ^ "\n" ^
241     "</gTopLevelStatus>\n"
242   | None -> "<gTopLevelStatus />\n"
243
244 let rec pp_hint = function
245   | Use_ring_Luke -> sprintf "<ring />"
246   | Use_fourier_Luke -> sprintf "<fourier />"
247   | Use_reflexivity_Luke -> sprintf "<reflexivity />"
248   | Use_symmetry_Luke -> sprintf "<symmetry />"
249   | Use_assumption_Luke -> sprintf "<assumption />"
250   | Use_contradiction_Luke -> sprintf "<contradiction />"
251   | Use_exists_Luke -> sprintf "<exists />"
252   | Use_split_Luke -> sprintf "<split />"
253   | Use_left_Luke -> sprintf "<left />"
254   | Use_right_Luke -> sprintf "<right />"
255   | Use_apply_Luke term -> sprintf "<apply>%s</apply>" term
256   | Hints hints ->
257       sprintf "<hints>\n%s\n</hints>"
258         (String.concat "\n" (List.map pp_hint hints))
259
260 let pp_hint_type s = s   (* TODO pretty print hint_type *)
261 let pp_tutor_dscs =
262   List.fold_left
263     (fun s (id, dsc) ->
264       sprintf "%s<tutor_dsc id=\"%s\">%s</tutor_dsc>" s id dsc)
265     ""
266 let pp_tutor_ids =
267   List.fold_left (fun s id -> sprintf "%s<tutor id=\"%s\" />" s id) ""
268
269 let string_of_msg = function
270   | Help -> "<help />"
271   | Usage usage_string -> sprintf "<usage>%s</usage>" usage_string
272   | Exception (name, value) ->
273       sprintf "<exception name=\"%s\">%s</exception>" name value
274   | Register_client (id, url) ->
275       sprintf "<register_client id=\"%s\" url=\"%s\" />" id url
276   | Unregister_client id -> sprintf "<unregister_client id=\"%s\" />" id
277   | List_tutors id -> sprintf "<list_tutors id=\"%s\" />" id
278   | Subscribe (id, tutor_ids) ->
279       sprintf "<subscribe id=\"%s\">%s</subscribe>"
280         id (pp_tutor_ids tutor_ids)
281   | State_change (id, state) ->
282       sprintf "<state_change id=\"%s\">%s</state_change>"
283         id (pp_state state)
284   | Wow id -> sprintf "<wow id=\"%s\" />" id
285   | Register_tutor (id, url, hint_type, dsc) ->
286       sprintf
287 "<register_tutor id=\"%s\" url=\"%s\">
288 <hint_type>%s</hint_type>
289 <description>%s</description>
290 </register_tutor>"
291         id url (pp_hint_type hint_type) dsc
292   | Unregister_tutor id -> sprintf "<unregister_tutor id=\"%s\" />" id
293   | Musing_started (id, musing_id) ->
294       sprintf "<musing_started id=\"%s\" musing_id=\"%s\" />" id musing_id
295   | Musing_aborted (id, musing_id) ->
296       sprintf "<musing_aborted id=\"%s\" musing_id=\"%s\" />" id musing_id
297   | Musing_completed (id, musing_id, result) ->
298       sprintf
299         "<musing_completed id=\"%s\" musing_id=\"%s\">%s</musing_completed>"
300         id musing_id
301         (match result with
302         | Sorry -> "<sorry />"
303         | Eureka hint -> sprintf "<eureka>%s</eureka>" (pp_hint hint))
304   | Client_registered id -> sprintf "<client_registered id=\"%s\" />" id
305   | Client_unregistered id -> sprintf "<client_unregistered id=\"%s\" />" id
306   | Tutor_list (id, tutor_dscs) ->
307       sprintf "<tutor_list id=\"%s\">%s</tutor_list>"
308         id (pp_tutor_dscs tutor_dscs)
309   | Subscribed (id, tutor_ids) ->
310       sprintf "<subscribed id=\"%s\">%s</subscribed>"
311         id (pp_tutor_ids tutor_ids)
312   | State_accepted (id, stop_ids, start_ids) ->
313       sprintf
314 "<state_accepted id=\"%s\">
315 <stopped>%s</stopped>
316 <started>%s</started>
317 </state_accepted>"
318         id
319         (String.concat ""
320           (List.map (fun id -> sprintf "<musing id=\"%s\" />" id) stop_ids))
321         (String.concat ""
322           (List.map (fun id -> sprintf "<musing id=\"%s\" />" id) start_ids))
323   | Hint (id, hint) -> sprintf "<hint id=\"%s\">%s</hint>" id (pp_hint hint)
324   | Tutor_registered id -> sprintf "<tutor_registered id=\"%s\" />" id
325   | Tutor_unregistered id -> sprintf "<tutor_unregistered id=\"%s\" />" id
326   | Start_musing (id, state) ->
327       sprintf "<start_musing id=\"%s\">%s</start_musing>"
328         id (pp_state (Some state))
329   | Abort_musing (id, musing_id) ->
330       sprintf "<abort_musing id=\"%s\" musing_id=\"%s\" />" id musing_id
331   | Thanks (id, musing_id) ->
332       sprintf "<thanks id=\"%s\" musing_id=\"%s\" />" id musing_id
333   | Too_late (id, musing_id) ->
334       sprintf "<too_late id=\"%s\" musing_id=\"%s\" />" id musing_id
335 ;;
336
337   (* debugging function that dump on stderr the sent messages *)
338 let dump_msg msg =
339   if debug >= 2 then
340     prerr_endline
341       (sprintf "<SENDING_MESSAGE>\n%s\n</SENDING_MESSAGE>"
342         (match msg with
343         | State_change _ -> "<state_change>omissis ...</state_change>"
344         | msg -> string_of_msg msg))
345 ;;
346
347 let submit_req ~url msg =
348   dump_msg msg;
349   if debug >= 1 then (prerr_string "Waiting for an answer ... "; flush stderr);
350   let res =
351     msg_of_string (Hbugs_misc.http_post ~body:(string_of_msg msg) url)
352   in
353   if debug >= 1 then (prerr_string "answer received!\n"; flush stderr);
354   res
355 ;;
356 let return_xml_msg body outchan =
357   Http_daemon.respond ~headers:["Content-Type", "text/xml"] ~body outchan
358 ;;
359 let respond_msg msg outchan =
360   dump_msg msg;
361   return_xml_msg (string_of_msg msg) outchan
362 (*   close_out outchan *)
363 ;;
364 let respond_exc name value = respond_msg (Exception (name, value));;
365