]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_test/mqgtop.ml
mathql query generator interface patched
[helm.git] / helm / ocaml / mathql_test / mqgtop.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 let query_num = ref 1
30
31 let interp_file = ref "interp.cic" 
32
33 let log_file = ref ""
34
35 let show_queries = ref false
36
37 let int_options = ref ""
38
39 let nl = " <p>\n"
40
41 module MQX  = MQueryMisc 
42 module MQI  = MQueryInterpreter
43 module MQIC = MQIConn
44 module MQG  = MQueryGenerator
45
46 let get_handle () = 
47    MQIC.init (MQIC.flags_of_string ! int_options)
48              (fun s -> print_string s; flush stdout) 
49              
50 let issue handle q =
51    let module U = MQueryUtil in
52    let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
53    let perm = 64 * 6 + 8 * 6 + 4 in
54    let time () =
55       let lt = Unix.localtime (Unix.time ()) in
56       "NEW LOG: " ^
57       string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
58       string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
59       string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
60       string_of_int (lt.Unix.tm_hour) ^ ":" ^
61       string_of_int (lt.Unix.tm_min) ^ ":" ^
62       string_of_int (lt.Unix.tm_sec) 
63    in
64    let log q r = 
65       let och = open_out_gen mode perm ! log_file in
66       let out = output_string och in 
67       if ! query_num = 1 then out (time () ^ nl);
68       out ("Query: " ^ string_of_int ! query_num ^ nl);
69       U.text_of_query out q nl;
70       out ("Result: " ^ nl);
71       U.text_of_result out r nl;
72       close_out och
73    in
74    if ! show_queries then U.text_of_query (output_string stdout) q nl;
75    let r = MQI.execute handle q in    
76    U.text_of_result (output_string stdout) r nl;
77    if ! log_file <> "" then log q r; 
78    incr query_num;
79    flush stdout
80
81 let get_interp () =
82    let module L = CicTextualLexer in
83    let module T = CicTextualParser in
84    let module P = MQGTopParser in
85    let lexer = function
86       | T.ID s                -> P.ID s
87       | T.CONURI u            -> P.CONURI u
88       | T.VARURI u            -> P.VARURI u
89       | T.INDTYURI (u, p)     -> P.INDTYURI (u, p)
90       | T.INDCONURI (u, p, s) -> P.INDCONURI (u, p, s)
91       | T.LETIN               -> P.ALIAS
92       | T.EOF                 -> P.EOF
93       | _                     -> assert false
94    in
95    let ich = open_in ! interp_file in
96    let lexbuf = Lexing.from_channel ich in
97    let f = P.interp (fun x -> lexer (L.token x)) lexbuf in
98    close_in ich; f
99    
100 let get_terms interp = 
101    let interp = get_interp () in
102    let lexbuf = Lexing.from_channel stdin in
103    let rec aux () =
104       try
105          let dom, mk_term =
106             CicTextualParserContext.main [] [] CicTextualLexer.token lexbuf
107          in
108          (snd (mk_term interp)) :: aux ()
109       with
110       CicTextualParser0.Eof -> []
111    in
112    aux ()
113
114 let pp_type_of uri = 
115    let u = UriManager.uri_of_string uri in  
116    let s = match (CicEnvironment.get_obj u) with
117       | Cic.Constant (_, _, ty, _) -> CicPp.ppterm ty
118       | Cic.Variable (_, _, ty, _) -> CicPp.ppterm ty
119       | _                          -> "Current proof or inductive definition."      
120 (*
121       | Cic.CurrentProof (_,conjs,te,ty) ->
122       | C.InductiveDefinition _ ->
123 *)
124    in print_endline s; flush stdout
125
126 let rec display = function
127    | []           -> ()
128    | term :: tail -> 
129       display tail;
130       print_string ("? " ^ CicPp.ppterm term ^ nl);
131       flush stdout
132
133 let execute ich =
134    let module U = MQueryUtil in
135    let lexbuf = Lexing.from_channel ich in
136    let handle = get_handle () in
137    let rec execute_aux () =
138       try 
139          let q = U.query_of_text lexbuf in
140          issue handle q; execute_aux ()
141       with End_of_file -> ()
142    in
143    execute_aux ();
144    MQIC.close handle
145 (*
146 let compose () =
147    let module P = MQGTopParser in
148    let module L = MQGTopLexer in
149    let module G = MQueryGeneratorNew in
150    let cl = P.specs L.spec_token (Lexing.from_channel stdin) in
151    issue (G.compose cl)
152 *)   
153 let locate name =
154    let handle = get_handle () in  
155    issue handle (MQG.locate name); 
156    MQIC.close handle
157
158 let mpattern n m l =
159    let module C = MQueryLevels2 in
160    let queries = ref [] in
161    let handle = get_handle () in
162    let rec pattern level = function
163       | []           -> ()
164       | term :: tail -> 
165          pattern level tail;
166          print_string ("? " ^ CicPp.ppterm term ^ nl);
167          let t = MQX.start_time () in
168          let om,rm,sm = C.get_constraints term in
169          let oml,rml,sml = List.length om, List.length rm, List.length sm in 
170          let oo, ool = if level land 1 = 0 then None, 0 else Some om, oml in
171          let ro, rol = if level land 2 = 0 then None, 0 else Some rm, rml in
172          let so, sol = if level land 4 = 0 then None, 0 else Some sm, sml in
173          let q = MQG.query_of_constraints None (om,rm,sm) (oo,ro,so) in 
174          if not (List.mem q ! queries) then
175          begin
176             issue handle q;
177             Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
178             Printf.printf "%i GEN = %i: %s"
179                (pred ! query_num) (oml + rml + sml + ool + rol + sol) 
180                (MQX.stop_time t ^ nl);
181             flush stdout; queries := q :: ! queries
182          end
183    in 
184    for level = max m n downto min m n do
185       Printf.eprintf "\nmqgtop: pattern: trying level %i\n" level; 
186       flush stderr; pattern level l
187    done;
188    Printf.eprintf "\nmqgtop: pattern: %i queries issued\n" 
189       (List.length ! queries);
190    flush stderr;
191    MQIC.close handle
192 (*   
193 let mbackward n m l =
194    let module C = MQueryLevels in
195    let module G = MQueryGenerator in
196    let queries = ref [] in
197    let torigth_restriction (u, b) =
198       let p = if b 
199          then "http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion" 
200          else "http://www.cs.unibo.it/helm/schemas/schema-helm#InConclusion"
201       in
202       (u, p, None)
203    in
204    let rec backward level = function
205       | []           -> ()
206       | term :: tail -> 
207          backward level tail;
208          print_string ("? " ^ CicPp.ppterm term ^ nl);
209          let t0 = Sys.time () in
210          let list_of_must, only = C.out_restr [] [] term in
211          let max_level = pred (List.length list_of_must) in 
212          let must = List.nth list_of_must (min level max_level) in 
213          let rigth_must = List.map torigth_restriction must in
214          let rigth_only = Some (List.map torigth_restriction only) in
215          let q = G.searchPattern (rigth_must, [], []) (rigth_only , None, None) in 
216          if not (List.mem q ! queries) then
217          begin
218             issue q;
219             let t1 = Sys.time () -. t0 in
220             Printf.eprintf "[%i] " (pred ! query_num); flush stderr;
221             Printf.printf "%i GEN = %i: %.2fs%s"
222                (pred ! query_num) (List.length must) t1 nl;
223             flush stdout; queries := q :: ! queries
224          end
225    in 
226    for level = max m n downto min m n do
227       Printf.eprintf "\nmqgtop: backward: trying level %i\n" level; 
228       flush stderr; backward level l
229    done;
230    Printf.eprintf "\nmqgtop: backward: %i queries issued\n" 
231       (List.length ! queries);
232    flush stderr
233 *)   
234  
235 let check () =
236    let handle = get_handle () in
237    Printf.eprintf 
238       "mqgtop: current options: %s, connection: %s\n"  
239       ! int_options (if MQIC.connected handle then "on" else "off");
240    MQIC.close handle
241
242 let prerr_help () =
243    prerr_endline "\nUSAGE: mqgtop.opt OPTIONS < INPUTFILE\n"; 
244    prerr_endline "The tool provides a textual interface to the HELM Query Generator, used for";
245    prerr_endline "testing purposes. mqgtop reads its input from stdin and produces ith output";
246    prerr_endline "in HTML on stdout. The options can be one ore more of the following.\n";
247    prerr_endline "OPTIONS:\n";
248    prerr_endline "-h  -help               shows this help message";
249    prerr_endline "-q  -show-queries       outputs generated queries";
250    prerr_endline "-l  -log-file FILE      sets the log file";
251    prerr_endline "-o  -options STRING     sets the interpreter options";
252    prerr_endline "-c  -check              checks the database connection";
253    prerr_endline "-t  -typeof URI         outputs the CIC type of the given HELM object";
254    prerr_endline "-x  -execute            issues a query given in the input file";
255    prerr_endline "-i  -interp FILE        sets the CIC short names interpretation file";
256    prerr_endline "-d  -disply             outputs the CIC terms given in the input file";
257    prerr_endline "-L  -locate ALIAS       issues the \"Locate\" query for the given alias";
258 (*   prerr_endline "-C  -compose            issues the \"Compose\" query reading its specifications";
259    prerr_endline "                        from the input file"; 
260    prerr_endline "-B  -backward LEVEL     issues the \"Backward\" query for the given level on all";
261    prerr_endline "                        CIC terms in the input file";
262    prerr_endline "-MB -multi-backward MAX issues the \"Backward\" query for each level from max to 0";
263    prerr_endline "                        on all CIC terms in the input file";
264 *)   prerr_endline "-P  -pattern LEVEL      issues the \"Pattern\" query for the given level on all";
265    prerr_endline "                        CIC terms in the input file";
266    prerr_endline "-MP -multi-pattern      issues the \"Pattern\" query for each level from 7 to 0";
267    prerr_endline "                        on all CIC terms in the input file\n";
268    prerr_endline "NOTES: * current interpreter options are:";
269    prerr_endline "         P (postgres), G (Galax), S (show statistics), Q (quiet)";
270    prerr_endline "       * CIC terms are read with the HELM CIC Textual Parser";
271    prerr_endline "       * -typeof does not work with inductive types and proofs in progress\n"
272
273 let rec parse = function
274    | [] -> ()
275    | ("-h"|"-help") :: rem -> prerr_help (); parse rem
276    | ("-i"|"-interp") :: arg :: rem -> interp_file := arg; parse rem
277    | ("-d"|"-display") :: rem -> display (get_terms ()); parse rem
278    | ("-t"|"-typeof") :: arg :: rem -> pp_type_of arg; parse rem
279    | ("-x"|"-execute") :: rem -> execute stdin; parse rem
280    | ("-q"|"-show-queries") :: rem -> show_queries := true; parse rem
281    | ("-o"|"-options") :: arg :: rem -> int_options := arg; parse rem
282    | ("-c"|"-check") :: rem -> check (); parse rem
283    | ("-l"|"-log-file") :: arg :: rem -> log_file := arg; parse rem
284    | ("-L"|"-Locate") :: arg :: rem -> locate arg; parse rem
285 (*   | ("-C"|"-compose") :: rem -> compose (); parse rem   
286    | ("-M"|"-backward") :: arg :: rem ->
287       let m = (int_of_string arg) in mbackward m m (get_terms ()); parse rem
288    | ("-MB"|"-multi-backward") :: arg :: rem ->
289       let m = (int_of_string arg) in mbackward m 0 (get_terms ()); parse rem
290 *)   | ("-P"|"-pattern") :: arg :: rem ->
291       let m = (int_of_string arg) in mpattern m m (get_terms ()); parse rem
292    | ("-MP"|"-multi-pattern") :: rem -> mpattern 7 0 (get_terms ()); parse rem
293    | _ :: rem -> parse rem
294
295 let _ =
296    let t = MQX.start_time () in
297    Logger.log_callback :=
298       (Logger.log_to_html 
299        ~print_and_flush:(fun s -> print_string s; flush stdout)) ; 
300    parse (List.tl (Array.to_list Sys.argv)); 
301    prerr_endline ("mqgtop: done in " ^ (MQX.stop_time t));
302    exit 0