1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Ferruccio Guidi <fguidi@cs.unibo.it> *)
34 (******************************************************************************)
36 (* Query issuing functions **************************************************)
41 type position = string
42 type depth = int option
45 type r_obj = (uri * position * depth)
46 type r_rel = (position* depth)
47 type r_sort = (position* depth * sort)
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)
61 let confirm_query = ref (fun _ -> true)
68 let set_confirm_query f =
71 let get_query_info () = ! info
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
78 let lt = Unix.localtime (Unix.time ()) in
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)
88 let och = open_out_gen mode perm ! log_file in
89 if ! query_num = 1 then output_string och (time () ^ nl);
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;
97 let r = Mqint.execute q in
98 if ! log_file <> "" then log q r;
99 info := string_of_int ! query_num :: ! info;
103 if ! confirm_query query then execute query else raise Discard
105 (* Query building functions ************************************************)
108 let module M = MathQL in
110 M.Ref (M.Property true M.RefineExact ("objectName", []) (M.Const [s]))
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
119 (* can restrictions *)
121 let (cr_o,cr_r,cr_s) = can_use in
123 let uri_of_entry (r, p, d) = r in
128 | Some cr_o -> List.map uri_of_entry cr_o
131 let tfst (a,b,c) = a in
132 let tsnd (a,b,c) = b in
133 let trd (a,b,c) = c in
135 let to_int_list l d =
144 | Some cr_o -> (List.map tsnd cr_o) in
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*)
154 print_string "#### LUNGHEZZA ODEP: "; print_int (List.length odep); flush stdout;
159 | Some cr_r -> (List.map fst cr_r) in
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 *)
174 | Some cr_s -> (List.map tfst cr_s) in
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*)
190 | Some cr_s -> List.map trd cr_s in
192 (* let q_where_obj = function
199 M.Relation (false, M.RefineExact, in_path "refObj", M.Ref (M.RefOf (M.RVar "uri0")), [assign "pos" "position"]),
201 (M.Meet (M.VVar "obj_positions", M.Record ("uri", in_path "pos"))))),
209 (false, M.RefineExact, in_path "refObj",
210 M.Ref (M.RefOf (M.RVar "uri0")),
211 [assign "p" "position"; assign "d" "depth"]
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")))))
227 let q_where_obj n = function
229 let rec q_ex n = function
232 M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p"))
235 print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
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")))
242 (M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p")),
245 print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
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"))))),
258 (false, M.RefineExact, in_path "refObj",
259 M.Ref (M.RefOf (M.RVar "uri0")),
260 [assign "p" "position"; assign "d" "depth"]
271 let rec q_where_rel n cr_r= (*function*)
276 (false, M.RefineExact, ("refRel", ["position"]),
277 M.RefOf(M.RVar "uri0")),
278 M.VVar ("rel_position" ^ string_of_int n))
282 | Some [(p,None)] -> q0
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) ->
294 q_where_rel (n+1) (Some tl))
295 | Some ((p,d)::tl) ->
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))
308 let rec q_where_sort n cr_s = (*function *)
314 (false, M.RefineExact, ("refSort", ["position"]),
315 M.RefOf(M.RVar "uri0")
317 M.VVar ("sort_position" ^ string_of_int n)),
320 (false, M.RefineExact, ("refSort", ["sort"]),
321 M.RefOf(M.RVar "uri0")),
322 M.VVar ("sort" ^ string_of_int n)))
326 | Some [(p,None,s)] -> q0
333 (false, M.RefineExact, ("refSort", ["depth"]),
334 M.RefOf(M.RVar "uri0")),
335 M.VVar ("sort_depth" ^ string_of_int n)))
337 | Some ((p,None,s)::tl) ->
340 q_where_sort (n+1) (Some tl))
342 | Some((p,d,s)::tl) ->
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))
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))
364 (* must restrictions *)
366 let build_select_obj (r, pos, dep) =
370 M.Relation (false, M.RefineExact, ("backPointer", []),
371 M.Ref (M.Const [r]), [assign "p" "position"]),
373 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p")))))
374 | Some dep -> let string_dep = string_of_int dep in
377 M.Relation (false, M.RefineExact, ("backPointer", []),
378 M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
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"))))))
385 let build_select_rel (pos, dep) =
389 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
391 (M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))))
392 | Some dep -> let string_dep = string_of_int dep in
395 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
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"))))))
402 let build_select_sort (pos, dep, sor) =
406 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
409 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
410 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
412 | Some dep -> let string_dep = string_of_int dep in
415 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
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"))))))
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)
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)
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)
442 let build_intersect = function
443 (* let tostring_sort (a,b,c) =
444 let b1 = string_of_int b in
447 let tostring_rel (a,b) =
448 let b1 = string_of_int b in
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)
471 let q_in = build_intersect must_use in
472 let q_select = M.Select ("uri0", q_in, q_where can_use) in
474 (* variables for can restrictions *)
476 let q_let_u = M.LetVVar ("universe", M.Const universe, q_select) in
478 let rec q_let_s sor n =
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))
485 (* let q_let_s = M.LetVVar ("sorts", M.Const sor, q_let_u) in *)
487 let rec q_let_ds sdep n =
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))
496 (* let q_let_ds = M.LetVVar ("sort_depths", M.Const sdep, q_let_s) in *)
498 let rec q_let_dr rdep n =
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))
508 (*let q_let_dr = M.LetVVar ("rel_depths", M.Const rdep, q_let_ds) in*)
510 let rec q_let_do odep n =
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))
520 (* let q_let_do = M.LetVVar ("obj_depths", M.Const odep, q_let_dr) in *)
523 let rec q_let_ps spos n =
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))
531 (* let q_let_ps = M.LetVVar ("sort_positions", M.Const spos, q_let_do) in *)
534 let rec q_let_pr rpos n =
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))
543 (* let q_let_pr = M.LetVVar ("rel_positions", M.Const rpos, q_let_ps) in *)
545 let rec q_let_po opos n =
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))
552 (*let q_let_po = M.LetVVar ("obj_positions", M.Const opos, q_let_pr) in*)
554 print_endline ("### " ^ MQueryUtil.text_of_query (q_let_po opos 1)) ; flush stdout;
555 execute_query (q_let_po opos 1)