3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
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.
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.
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.
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,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
36 let debug = 2;; (* 0 -> no debug
37 1 -> waiting for an answer / answer received
38 2 -> XML messages dumping
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;;
47 let is_xml_element n = match n#node_type with T_element _ -> true | _ -> false
48 let get_attr node name =
50 (match node#attribute name with
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 ->
58 | _ -> raise (Parse_error ("", "Expected node: " ^ name))
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
66 let buf = Buffer.create 10240 in
67 let node_to_string (node: ('a node extension as 'a) node) =
69 node#write (`Out_buffer buf) `Enc_utf8;
70 let res = Buffer.contents buf in
74 let (goal_node, type_node, body_node) =
76 (find_element "CurrentGoal" root,
77 find_element "ConstantType" root,
78 find_element "CurrentProof" root)
80 raise (Parse_error ("", "Malformed HBugs status XML document"))
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)
92 int_of_string (goal_node#data)
93 with Failure "int_of_string" ->
94 raise (Parse_error (goal_node#data, "can't parse goal"))
96 (type_string, body_string, goal)
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" ->
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? *)
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? *)
123 let parse_hint_type n = n#data (* TODO parsare il possibile tipo di suggerimento *)
124 let parse_tutor_dscs n =
126 (fun n -> (get_attr n "id", n#data))
127 (List.filter is_xml_element n#sub_nodes)
128 let parse_tutor_ids node =
130 (fun n -> get_attr n "id") (List.filter is_xml_element node#sub_nodes)
132 let tutors_sep = Pcre.regexp ",\\s*"
134 let msg_of_string' s =
135 let root = (* xml tree's root *)
136 parse_wfcontent_entity default_config (from_string s) default_spec
138 match root#node_type with
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)
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" ->
155 Some (find_element ~deeply:false "gTopLevelStatus" root)
156 with Not_found -> None
160 match state_node with
161 | Some n -> (try Some (parse_state n) with Empty_node -> None)
163 | T_element "wow" -> Wow (get_attr root "id")
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
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" ->
180 find_element "eureka" root
181 with Not_found -> find_element "sorry" root
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 *)
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" ->
202 (fun n -> get_attr n "id")
203 (List.filter is_xml_element (find_element "stopped" root)#sub_nodes),
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)
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" ->
215 find_element ~deeply:false "gTopLevelStatus" root
216 with Not_found -> raise (No_element_found "gTopLevelStatus")
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")
225 | _ -> raise (No_element_found s)
227 let msg_of_string s =
230 with e -> raise (Parse_error (s, Printexc.to_string e))
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
237 "<gTopLevelStatus>\n" ^
238 (sprintf "<CurrentGoal>%d</CurrentGoal>\n" goal) ^
241 "</gTopLevelStatus>\n"
242 | None -> "<gTopLevelStatus />\n"
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
257 sprintf "<hints>\n%s\n</hints>"
258 (String.concat "\n" (List.map pp_hint hints))
260 let pp_hint_type s = s (* TODO pretty print hint_type *)
264 sprintf "%s<tutor_dsc id=\"%s\">%s</tutor_dsc>" s id dsc)
267 List.fold_left (fun s id -> sprintf "%s<tutor id=\"%s\" />" s id) ""
269 let string_of_msg = function
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>"
284 | Wow id -> sprintf "<wow id=\"%s\" />" id
285 | Register_tutor (id, url, hint_type, dsc) ->
287 "<register_tutor id=\"%s\" url=\"%s\">
288 <hint_type>%s</hint_type>
289 <description>%s</description>
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) ->
299 "<musing_completed id=\"%s\" musing_id=\"%s\">%s</musing_completed>"
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) ->
314 "<state_accepted id=\"%s\">
315 <stopped>%s</stopped>
316 <started>%s</started>
320 (List.map (fun id -> sprintf "<musing id=\"%s\" />" id) stop_ids))
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
337 (* debugging function that dump on stderr the sent messages *)
341 (sprintf "<SENDING_MESSAGE>\n%s\n</SENDING_MESSAGE>"
343 | State_change _ -> "<state_change>omissis ...</state_change>"
344 | msg -> string_of_msg msg))
347 let submit_req ~url msg =
349 if debug >= 1 then (prerr_string "Waiting for an answer ... "; flush stderr);
351 msg_of_string (Hbugs_misc.http_post ~body:(string_of_msg msg) url)
353 if debug >= 1 then (prerr_string "answer received!\n"; flush stderr);
356 let return_xml_msg body outchan =
357 Http_daemon.respond ~headers:["Content-Type", "text/xml"] ~body outchan
359 let respond_msg msg outchan =
361 return_xml_msg (string_of_msg msg) outchan
362 (* close_out outchan *)
364 let respond_exc name value = respond_msg (Exception (name, value));;