]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/mQueryGenerator.ml
7e061eb0be302de929f024498d6d275f8edd13fb
[helm.git] / helm / gTopLevel / mQueryGenerator.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 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                     Ferruccio Guidi <fguidi@cs.unibo.it>                   *)
31 (*                                 30/04/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 (* Query issuing functions **************************************************)
37
38
39
40 type uri = string
41 type position = string
42 type depth = int option
43 type sort = string 
44
45 type r_obj = (uri * position * depth)  
46 type r_rel = (position* depth) 
47 type r_sort = (position* depth * sort) 
48
49 type must_restrictions = (r_obj list * r_rel list * r_sort list)
50 type only_restrictions =
51  (r_obj list option * r_rel list option * r_sort list option)
52
53 exception Discard  
54
55 let nl = " <p>\n"
56
57 let query_num = ref 1
58
59 let log_file = ref ""
60
61 let confirm_query = ref (fun _ -> true)
62
63 let info = ref []
64
65 let set_log_file f = 
66    log_file := f
67
68 let set_confirm_query f =
69    confirm_query := f
70
71 let get_query_info () = ! info
72
73 let execute_query query =
74    let module Util = MQueryUtil in
75    let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
76    let perm = 64 * 6 + 8 * 6 + 4 in
77    let time () =
78       let lt = Unix.localtime (Unix.time ()) in
79       "NEW LOG: " ^
80       string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
81       string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
82       string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
83       string_of_int (lt.Unix.tm_hour) ^ ":" ^
84       string_of_int (lt.Unix.tm_min) ^ ":" ^
85       string_of_int (lt.Unix.tm_sec) 
86    in
87    let log q r = 
88       let och = open_out_gen mode perm ! log_file in
89       if ! query_num = 1 then output_string och (time () ^ nl);
90       let str =
91        "Query: " ^ string_of_int ! query_num ^ nl ^ Util.text_of_query q ^ nl ^ 
92        "Result:" ^ nl ^ Util.text_of_result r nl in
93       output_string och str; 
94       flush och 
95    in
96    let execute q =
97       let r = Mqint.execute q in    
98       if ! log_file <> "" then log q r; 
99       info := string_of_int ! query_num :: ! info;
100       incr query_num;
101       r
102    in 
103    if ! confirm_query query then execute query else raise Discard
104                           
105 (* Query building functions  ************************************************)   
106
107 let locate s =
108    let module M = MathQL in
109    let q =
110     M.Ref (M.Property true M.RefineExact ("objectName", []) (M.Const [s]))
111    in
112     execute_query q
113
114 let searchPattern must_use can_use =    
115    let module M = MathQL in
116    let in_path s = (s, []) in
117    let assign v p = (in_path v, in_path p) in 
118   
119 (* can restrictions *)  
120    
121    let (cr_o,cr_r,cr_s) = can_use in
122    
123    let uri_of_entry (r, p, d) = r in
124    
125    let universe = 
126      match cr_o with
127        None -> []
128      | Some cr_o -> List.map uri_of_entry cr_o 
129    in
130   
131    let tfst (a,b,c) = a in
132    let tsnd (a,b,c) = b in
133    let trd (a,b,c) = c in
134    
135    let to_int_list l d =
136    match d with
137      None -> l
138    | Some d -> l@[d]
139    in
140
141    let opos =
142      match cr_o with
143        None -> []
144      | Some cr_o -> (List.map tsnd cr_o) in
145    
146    let odep = 
147      match cr_o with
148        None -> []
149      | Some cr_o -> List.map trd cr_o
150                   (*  let odep_option_list = List.map trd cr_o in
151                     let lo_dep_int = List.fold_left to_int_list [] odep_option_list in
152                       List.map string_of_int lo_dep_int*)
153    in
154    print_string "#### LUNGHEZZA ODEP: "; print_int (List.length odep); flush stdout;
155    print_endline"";
156    let rpos = 
157      match cr_r with
158        None -> []
159      | Some cr_r -> (List.map fst cr_r) in
160    
161    let rdep = 
162      match cr_r with
163        None -> []
164      | Some cr_r -> List.map snd cr_r 
165                    (* let rdep_option_list = List.map snd cr_r in
166                     let lr_dep_int = List.fold_left to_int_list [] rdep_option_list in
167                       List.map string_of_int lr_dep_int *)
168    in 
169    
170
171    let spos = 
172      match cr_s with
173        None -> []
174      | Some cr_s -> (List.map tfst cr_s) in
175    
176       
177    let sdep = 
178      match cr_s with
179        None -> []
180      | Some cr_s -> List.map tsnd cr_s
181                    (* let sdep_option_list = List.map tsnd cr_s in
182                     let ls_dep_int = List.fold_left to_int_list [] sdep_option_list in
183                       List.map string_of_int ls_dep_int*)
184    in  
185    
186    
187    let sor = 
188      match cr_s with
189        None -> []
190      | Some cr_s -> List.map trd cr_s in 
191
192   (* let q_where_obj = function
193        Some l ->
194          if odep = [] then
195            M.Sub
196            (M.RefOf
197            (M.Select
198               ("uri", 
199               M.Relation (false, M.RefineExact, in_path "refObj", M.Ref (M.RefOf (M.RVar "uri0")), [assign "pos" "position"]),
200               M.Ex ["uri"]
201               (M.Meet (M.VVar "obj_positions", M.Record ("uri", in_path "pos"))))), 
202            M.VVar "universe")
203          else
204            M.Sub
205            (M.RefOf
206             (M.Select
207               ("uri", 
208                 M.Relation
209                  (false, M.RefineExact, in_path "refObj",
210                    M.Ref (M.RefOf (M.RVar "uri0")),
211                    [assign "p" "position"; assign "d" "depth"]
212                  ),
213                 M.Ex ["uri"]
214                  (M.And
215                   ((M.Meet(M.VVar "obj_positions",M.Record("uri",in_path "p"))),
216                    (M.Meet(M.VVar "obj_depths", M.Record("uri",in_path "d")))))
217               )
218             ), 
219             M.VVar "universe"
220            )
221          
222      | None -> M.True    
223    in*) 
224   
225
226
227    let q_where_obj n = function
228        Some l ->
229          let rec q_ex n = function
230              [] -> M.True 
231            | [(u,p,None)] ->
232                              M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p"))
233                              
234            | [(u,p,d)] ->
235                           print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
236                                                           print_endline"";
237                           M.And
238                             (M.Meet(M.VVar ("obj_position" ^ string_of_int n),M.Record("uri",in_path "p")),
239                              M.Meet(M.VVar ("obj_depth" ^ string_of_int n), M.Record("uri",in_path "d")))
240            | (u,p,None)::tl ->
241                                M.Or
242                                 (M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p")),
243                                  q_ex (n+1) tl)
244            | (u,p,d)::tl ->
245                              print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
246                                 print_endline"";
247                             M.Or
248                              ((M.And
249                                ((M.Meet(M.VVar ("obj_position" ^ string_of_int n),M.Record("uri",in_path "p"))),
250                                 (M.Meet(M.VVar ("obj_depth" ^ string_of_int n), M.Record("uri",in_path "d"))))),
251                              q_ex (n+1) tl)
252          in    
253          M.Sub
254            (M.RefOf
255             (M.Select
256               ("uri", 
257                 M.Relation
258                  (false, M.RefineExact, in_path "refObj",
259                    M.Ref (M.RefOf (M.RVar "uri0")),
260                    [assign "p" "position"; assign "d" "depth"]
261                  ),
262                 M.Ex ["uri"]
263                 (q_ex 1 l))),
264             M.VVar "universe")
265        | None -> M.True
266    in
267
268
269
270   
271    let rec q_where_rel n cr_r= (*function*)
272      (*  Some l ->*)
273          let q0 =
274           M.Sub
275            (M.Property
276              (false, M.RefineExact, ("refRel", ["position"]),
277                M.RefOf(M.RVar "uri0")),
278             M.VVar ("rel_position" ^ string_of_int n))
279            in
280            match cr_r with
281              Some [] -> M.True
282            | Some [(p,None)] -> q0
283            | Some [(p,d)] ->
284                         M.And  
285                          (q0,
286                           M.Sub
287                            (M.Property
288                              (false, M.RefineExact, ("refRel", ["depth"]),
289                               M.RefOf(M.RVar "uri0")),
290                             M.VVar ("rel_depth" ^ string_of_int n)))
291            | Some ((p,None)::tl) -> 
292                                M.Or
293                                 (q0,
294                                  q_where_rel (n+1) (Some tl))
295            | Some ((p,d)::tl) -> 
296                             M.Or
297                              (M.And
298                                (q0,
299                                 M.Sub
300                                  (M.Property
301                                    (false, M.RefineExact, ("refRel", ["depth"]),
302                                     M.RefOf(M.RVar "uri0")),
303                                   M.VVar ("rel_depth" ^ string_of_int n))),
304                               q_where_rel (n+1) (Some tl))
305            | None -> M.True            
306    in
307
308    let rec q_where_sort n cr_s = (*function *)
309      (*  Some l ->*)
310         let q0 =
311          M.And
312           (M.Sub
313             (M.Property
314               (false, M.RefineExact, ("refSort", ["position"]),
315                 M.RefOf(M.RVar "uri0")
316                ),
317              M.VVar ("sort_position" ^ string_of_int n)),
318            M.Sub
319             (M.Property
320               (false, M.RefineExact, ("refSort", ["sort"]),
321                 M.RefOf(M.RVar "uri0")),
322              M.VVar ("sort" ^ string_of_int n))) 
323         in
324         match cr_s with
325           Some [] -> M.True
326         | Some [(p,None,s)] -> q0
327               
328         | Some [(p,d,s)] ->
329                        M.And
330                         (q0,
331                          M.Sub
332                           (M.Property
333                             (false, M.RefineExact, ("refSort", ["depth"]),
334                              M.RefOf(M.RVar "uri0")),
335                            M.VVar ("sort_depth" ^ string_of_int n))) 
336               
337         | Some ((p,None,s)::tl) ->
338                             M.Or
339                              (q0,
340                               q_where_sort (n+1) (Some tl))
341               
342         | Some((p,d,s)::tl) ->
343                            M.Or
344                             (M.And
345                               (q0,
346                                M.Sub
347                                 (M.Property
348                                   (false, M.RefineExact, ("refSort", ["depth"]),
349                                    M.RefOf(M.RVar "uri0")),
350                                  M.VVar ("sort_depth" ^ string_of_int n))),
351                              q_where_sort (n+1) (Some tl))
352         | None -> M.True
353    in
354             
355
356
357   
358    let q_where cr =
359      let (cr_o,cr_r,cr_s) = cr in
360      M.And(M.And(q_where_obj 1 cr_o, (q_where_rel 1 cr_r)), (q_where_sort 1 cr_s))     
361    
362        in
363   
364 (* must restrictions *)   
365    
366    let build_select_obj (r, pos, dep) =
367      match dep with
368        None -> M.Select
369                  ("uri", 
370                   M.Relation (false, M.RefineExact, ("backPointer", []),
371                               M.Ref (M.Const [r]), [assign "p" "position"]),
372                   M.Ex ["uri"] 
373                   ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p")))))
374      | Some dep -> let string_dep = string_of_int dep in
375                    M.Select
376                      ("uri", 
377                       M.Relation (false, M.RefineExact, ("backPointer", []),
378                                   M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
379                       M.Ex ["uri"] 
380                       (M.And
381                       ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
382                       (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))  
383    in 
384   
385    let build_select_rel (pos, dep) = 
386      match dep with 
387        None -> M.Select                               
388                  ("uri", 
389                   M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]), 
390                   M.Ex ["uri"]
391                   (M.Sub (M.Const [pos], M.Record ("uri", in_path "p")))) 
392      | Some dep -> let string_dep = string_of_int dep in 
393                    M.Select                               
394                      ("uri", 
395                       M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]), 
396                       M.Ex ["uri"] 
397                       (M.And
398                       ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
399                       (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
400    in 
401
402    let build_select_sort (pos, dep, sor) =  
403      match dep with
404        None -> M.Select                               
405                  ("uri", 
406                   M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]), 
407                   M.Ex ["uri"] 
408                   (M.And
409                   ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
410                   (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
411    
412      | Some dep -> let string_dep = string_of_int dep in
413                    M.Select                               
414                      ("uri", 
415                       M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]), 
416                       M.Ex ["uri"] 
417                       (M.And
418                       ((M.And
419                       ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
420                       (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))),
421                       (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
422    in 
423
424    let rec build_intersect_obj = function
425        []       -> M.Pattern (M.Const ["[.]*"])
426      | [hd]     -> build_select_obj hd
427      | hd :: tl -> M.Intersect (build_select_obj hd, build_intersect_obj tl)
428    in
429    
430    let rec build_intersect_rel = function
431        []       -> M.Ref(M.Const [])                      
432      | [hd]     -> build_select_rel hd
433      | hd :: tl -> M.Intersect (build_select_rel hd, build_intersect_rel tl)
434    in
435
436    let rec build_intersect_sort = function
437        []       -> M.Ref(M.Const [])                         
438      | [hd]     -> build_select_sort hd
439      | hd :: tl -> M.Intersect (build_select_sort hd, build_intersect_sort tl)
440    in
441    
442    let build_intersect = function
443 (*      let tostring_sort (a,b,c) = 
444         let b1 = string_of_int b in 
445           (a,b1,c)
446       in
447       let tostring_rel (a,b) = 
448         let b1 = string_of_int b in 
449           (a,b1)
450       in*)
451
452 (*      let (l1,l2,l3) = must in
453       match (l1,l2,l3) with *)
454         l1,[],[] -> build_intersect_obj l1
455       | [],l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
456                       build_intersect_rel l2
457       | [],[],l3 ->(* let lsort = List.map tostring_sort l3 in*)
458                       build_intersect_sort l3
459       | l1,l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
460                       M.Intersect (build_intersect_obj l1, build_intersect_rel l2)
461       | l1,[],l3 ->(* let lsort = List.map tostring_sort l3 in                *)
462                       M.Intersect (build_intersect_obj l1, build_intersect_sort l3)
463       | [],l2,l3 ->(* let lrel = List.map tostring_rel l2 in
464                     let lsort = List.map tostring_sort l3 in*)
465                       M.Intersect (build_intersect_rel l2, build_intersect_sort l3)
466       | l1,l2,l3 ->(* let lrel = List.map tostring_rel l2 in
467              let lsort = List.map tostring_sort l3 in *)                          
468               M.Intersect (M.Intersect (build_intersect_obj l1, build_intersect_rel l2), build_intersect_sort l3)
469    in  
470
471    let q_in = build_intersect must_use in
472    let q_select = M.Select ("uri0", q_in, q_where can_use) in
473
474 (* variables for can restrictions *)
475
476    let q_let_u = M.LetVVar ("universe", M.Const universe, q_select) in
477    
478    let rec q_let_s sor n =
479      match sor with
480        [] -> q_let_u
481      | [s] -> M.LetVVar ("sort" ^ (string_of_int n), M.Const [s], q_let_u)
482      | s::tl -> M.LetVVar ("sort" ^ (string_of_int n), M.Const [s], q_let_s tl (n+1))
483    in
484
485 (*   let q_let_s = M.LetVVar ("sorts", M.Const sor, q_let_u) in *)
486    
487    let rec q_let_ds sdep n =
488      match sdep with
489        [] 
490      | [None] -> q_let_s sor 1  
491      | (None)::tl -> q_let_ds tl (n+1)
492      | [Some d] -> M.LetVVar ("sort_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_s sor 1)
493      | (Some d)::tl -> M.LetVVar ("sort_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_ds tl (n+1))
494    in  
495    
496 (*   let q_let_ds = M.LetVVar ("sort_depths", M.Const sdep, q_let_s) in   *)
497    
498    let rec q_let_dr rdep n =
499      match rdep with
500        [] 
501      | [None] -> q_let_ds sdep 1 
502      | (None)::tl -> q_let_dr tl (n+1)
503      | [Some d] -> M.LetVVar ("rel_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_ds sdep 1)
504      | (Some d)::tl -> M.LetVVar ("rel_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_dr tl (n+1))
505    in
506   
507    
508    (*let q_let_dr = M.LetVVar ("rel_depths", M.Const rdep, q_let_ds) in*)
509    
510    let rec q_let_do odep n =
511      match odep with
512        [] 
513      | [None] -> q_let_dr rdep 1
514      | (None)::tl -> q_let_do tl (n+1)  
515      |  [Some d] -> M.LetVVar ("obj_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_dr rdep 1)
516      | (Some d)::tl -> M.LetVVar ("obj_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_do tl (n+1))
517    in
518
519
520 (*   let q_let_do = M.LetVVar ("obj_depths", M.Const odep, q_let_dr) in  *)
521    
522    
523    let rec q_let_ps spos n =
524      match spos with
525        [] -> q_let_do odep 1
526      | [p] -> M.LetVVar ("sort_position" ^ (string_of_int n), M.Const [p], q_let_do odep 1)
527      | p::tl -> M.LetVVar ("sort_position" ^ (string_of_int n), M.Const [p], q_let_ps tl (n+1))
528    in
529    
530    
531 (*   let q_let_ps = M.LetVVar ("sort_positions", M.Const spos, q_let_do) in *)
532    
533    
534    let rec q_let_pr rpos n =
535      match rpos with
536        [] -> q_let_ps spos 1
537      | [p] -> M.LetVVar ("rel_position" ^ (string_of_int n), M.Const [p], q_let_ps spos 1)
538      | p::tl -> M.LetVVar ("rel_position" ^ (string_of_int n), M.Const [p], q_let_pr tl (n+1))
539    in
540    
541    
542    
543 (*   let q_let_pr = M.LetVVar ("rel_positions", M.Const rpos, q_let_ps) in *)
544
545    let rec q_let_po opos n =
546      match opos with
547        [] -> q_let_pr rpos 1
548      | [p] -> M.LetVVar ("obj_position" ^ (string_of_int n), M.Const [p], q_let_pr rpos 1)
549      | p::tl -> M.LetVVar ("obj_position" ^ (string_of_int n), M.Const [p], q_let_po tl (n+1))
550    in
551    
552    (*let q_let_po = M.LetVVar ("obj_positions", M.Const opos, q_let_pr) in*)
553
554 print_endline ("### " ^ MQueryUtil.text_of_query (q_let_po opos 1)) ; flush stdout;
555    execute_query (q_let_po opos 1)