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 -> let odep_option_list = List.map trd cr_o in
150 let lo_dep_int = List.fold_left to_int_list [] odep_option_list in
151 List.map string_of_int lo_dep_int
157 | Some cr_r -> (List.map fst cr_r) in
162 | Some cr_r -> let rdep_option_list = List.map snd cr_r in
163 let lr_dep_int = List.fold_left to_int_list [] rdep_option_list in
164 List.map string_of_int lr_dep_int
171 | Some cr_s -> (List.map tfst cr_s) in
177 | Some cr_s -> let sdep_option_list = List.map tsnd cr_s in
178 let ls_dep_int = List.fold_left to_int_list [] sdep_option_list in
179 List.map string_of_int ls_dep_int
186 | Some cr_s -> List.map trd cr_s in
189 let q_where_obj = function
196 M.Relation (false, M.RefineExact, in_path "refObj", M.Ref (M.RefOf (M.RVar "uri0")), [assign "pos" "position"]),
198 (M.Meet (M.VVar "obj_positions", M.Record ("uri", in_path "pos"))))),
206 (false, M.RefineExact, in_path "refObj",
207 M.Ref (M.RefOf (M.RVar "uri0")),
208 [assign "p" "position"; assign "d" "depth"]
212 ((M.Meet(M.VVar "obj_positions",M.Record("uri",in_path "p"))),
213 (M.Meet(M.VVar "obj_depths", M.Record("uri",in_path "d")))))
222 let q_where_rel = function
227 (false, M.RefineExact, ("refRel", ["position"]),
228 M.RefOf(M.RVar "uri0")),
229 M.VVar "rel_positions")
237 (false, M.RefineExact, ("refRel", ["depth"]),
238 M.RefOf(M.RVar "uri0")),
239 M.VVar "rel_depths"))
243 let q_where_sort = function
249 (false, M.RefineExact, ("refSort", ["position"]),
250 M.RefOf(M.RVar "uri0")
252 M.VVar "sort_positions"),
255 (false, M.RefineExact, ("refSort", ["sort"]),
256 M.RefOf(M.RVar "uri0")),
265 (M.VVar "sort_depths",
267 (false, M.RefineExact, ("refSort", ["depth"]),
268 M.RefOf(M.RVar "uri0"))))
273 let (cr_o,cr_r,cr_s) = cr in
274 M.And(M.And(q_where_obj cr_o, q_where_rel cr_r), q_where_sort cr_s)
278 (* must restrictions *)
280 let build_select_obj (r, pos, dep) =
284 M.Relation (false, M.RefineExact, ("backPointer", []),
285 M.Ref (M.Const [r]), [assign "pos" "position"]),
287 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "pos")))))
288 | Some dep -> let string_dep = string_of_int dep in
291 M.Relation (false, M.RefineExact, ("backPointer", []),
292 M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
295 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
296 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
299 let build_select_rel (pos, dep) =
303 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
305 (M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))))
306 | Some dep -> let string_dep = string_of_int dep in
309 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
312 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
313 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
316 let build_select_sort (pos, dep, sor) =
320 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
323 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
324 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
326 | Some dep -> let string_dep = string_of_int dep in
329 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
333 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
334 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))),
335 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
338 let rec build_intersect_obj = function
339 [] -> M.Pattern (M.Const ["[.]*"])
340 | [hd] -> build_select_obj hd
341 | hd :: tl -> M.Intersect (build_select_obj hd, build_intersect_obj tl)
344 let rec build_intersect_rel = function
345 [] -> M.Ref(M.Const [])
346 | [hd] -> build_select_rel hd
347 | hd :: tl -> M.Intersect (build_select_rel hd, build_intersect_rel tl)
350 let rec build_intersect_sort = function
351 [] -> M.Ref(M.Const [])
352 | [hd] -> build_select_sort hd
353 | hd :: tl -> M.Intersect (build_select_sort hd, build_intersect_sort tl)
356 let build_intersect = function
357 (* let tostring_sort (a,b,c) =
358 let b1 = string_of_int b in
361 let tostring_rel (a,b) =
362 let b1 = string_of_int b in
366 (* let (l1,l2,l3) = must in
367 match (l1,l2,l3) with *)
368 l1,[],[] -> build_intersect_obj l1
369 | [],l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
370 build_intersect_rel l2
371 | [],[],l3 ->(* let lsort = List.map tostring_sort l3 in*)
372 build_intersect_sort l3
373 | l1,l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
374 M.Intersect (build_intersect_obj l1, build_intersect_rel l2)
375 | l1,[],l3 ->(* let lsort = List.map tostring_sort l3 in *)
376 M.Intersect (build_intersect_obj l1, build_intersect_sort l3)
377 | [],l2,l3 ->(* let lrel = List.map tostring_rel l2 in
378 let lsort = List.map tostring_sort l3 in*)
379 M.Intersect (build_intersect_rel l2, build_intersect_sort l3)
380 | l1,l2,l3 ->(* let lrel = List.map tostring_rel l2 in
381 let lsort = List.map tostring_sort l3 in *)
382 M.Intersect (M.Intersect (build_intersect_obj l1, build_intersect_rel l2), build_intersect_sort l3)
385 let q_in = build_intersect must_use in
386 let q_select = M.Select ("uri0", q_in, q_where can_use) in
388 (* variables for can restrictions *)
390 let q_let_u = M.LetVVar ("universe", M.Const universe, q_select) in
392 let q_let_s = M.LetVVar ("sorts", M.Const sor, q_let_u) in
394 let q_let_ds = M.LetVVar ("sort_depths", M.Const sdep, q_let_s) in
396 let q_let_dr = M.LetVVar ("rel_depths", M.Const rdep, q_let_ds) in
398 let q_let_do = M.LetVVar ("obj_depths", M.Const odep, q_let_dr) in
400 let q_let_ps = M.LetVVar ("sort_positions", M.Const spos, q_let_do) in
402 let q_let_pr = M.LetVVar ("rel_positions", M.Const rpos, q_let_ps) in
404 let q_let_po = M.LetVVar ("obj_positions", M.Const opos, q_let_pr) in
405 print_endline ("### " ^ MQueryUtil.text_of_query q_let_po) ; flush stdout;
406 execute_query q_let_po