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
45 type r_obj = (uri * position * depth option)
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 can_restrictions = (r_obj list option * r_rel list option * r_sort list option)
68 let confirm_query = ref (fun _ -> true)
75 let set_confirm_query f =
78 let get_query_info () = ! info
80 let execute_query query =
81 let module Util = MQueryUtil in
82 let mode = [Open_wronly; Open_append; Open_creat; Open_text] in
83 let perm = 64 * 6 + 8 * 6 + 4 in
85 let lt = Unix.localtime (Unix.time ()) in
87 string_of_int (lt.Unix.tm_mon + 1) ^ "-" ^
88 string_of_int (lt.Unix.tm_mday + 1) ^ "-" ^
89 string_of_int (lt.Unix.tm_year + 1900) ^ " " ^
90 string_of_int (lt.Unix.tm_hour) ^ ":" ^
91 string_of_int (lt.Unix.tm_min) ^ ":" ^
92 string_of_int (lt.Unix.tm_sec)
95 let och = open_out_gen mode perm ! log_file in
96 if ! query_num = 1 then output_string och (time () ^ nl);
98 "Query: " ^ string_of_int ! query_num ^ nl ^ Util.text_of_query q ^ nl ^
99 "Result:" ^ nl ^ Util.text_of_result r nl in
100 output_string och str;
104 let r = Mqint.execute q in
105 if ! log_file <> "" then log q r;
106 info := string_of_int ! query_num :: ! info;
110 if ! confirm_query query then execute query else raise Discard
112 (* Query building functions ************************************************)
115 let module M = MathQL in
117 M.Ref (M.Property true M.RefineExact ("objectName", []) (M.Const [s]))
121 let searchPattern must_use can_use =
122 let module M = MathQL in
123 let in_path s = (s, []) in
124 let assign v p = (in_path v, in_path p) in
126 let q_where_obj = function
133 (false, M.RefineExact, in_path "refObj",
134 M.Ref (M.RefOf (M.RVar "uri0")),
135 [assign "pos" "position"]),
137 (M.Meet (M.VVar "obj_positions", M.Record ("uri", in_path "pos")))
145 let q_where_rel = function
148 (M.Sub(M.VVar "rel_positions", M.Property(false, M.RefineExact, ("refRel", ["position"]), M.RefOf(M.RVar "uri0"))),
149 M.Sub(M.VVar "rel_depths", M.Property(false, M.RefineExact, ("refRel", ["depth"]), M.RefOf(M.RVar "uri0"))))
153 let q_where_sort = function
157 (M.Sub(M.VVar "sort_positions", M.Property(false, M.RefineExact, ("refSort", ["position"]), M.RefOf(M.RVar "uri0"))),
158 M.Sub(M.VVar "sort_depths", M.Property(false, M.RefineExact, ("refSort", ["depth"]), M.RefOf(M.RVar "uri0")))),
159 M.Sub(M.VVar "sorts", M.Property(false, M.RefineExact, ("refSort", ["sort"]), M.RefOf(M.RVar "uri0"))))
164 let (cr_o,cr_r,cr_s) = cr in
165 M.And(M.And(q_where_obj cr_o, q_where_rel cr_r), q_where_sort cr_s)
169 let uri_of_entry (r, p, d) = r in
171 let build_select_obj (r, pos, dep) =
175 M.Relation (false, M.RefineExact, ("backPointer", []),
176 M.Ref (M.Const [r]), [assign "pos" "position"]),
178 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "pos")))))
179 | Some dep -> let string_dep = string_of_int dep in
182 M.Relation (false, M.RefineExact, ("backPointer", []),
183 M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
186 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
187 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
190 let build_select_rel (pos, dep) =
193 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
196 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
197 (M.Sub (M.Const [dep], M.Record ("uri", in_path "d"))))))
200 let build_select_sort (pos, dep, sor) =
203 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
207 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
208 (M.Sub (M.Const [dep], M.Record ("uri", in_path "d"))))),
209 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
212 let rec build_intersect_obj = function
213 [] -> M.Pattern (M.Const ["[.]*"])
214 | [hd] -> build_select_obj hd
215 | hd :: tl -> M.Intersect (build_select_obj hd, build_intersect_obj tl)
218 let rec build_intersect_rel = function
219 [] -> M.Ref(M.Const [])
220 | [hd] -> build_select_rel hd
221 | hd :: tl -> M.Intersect (build_select_rel hd, build_intersect_rel tl)
224 let rec build_intersect_sort = function
225 [] -> M.Ref(M.Const [])
226 | [hd] -> build_select_sort hd
227 | hd :: tl -> M.Intersect (build_select_sort hd, build_intersect_sort tl)
230 let build_intersect must =
231 let tostring_sort (a,b,c) =
232 let b1 = string_of_int b in
235 let tostring_rel (a,b) =
236 let b1 = string_of_int b in
240 let (l1,l2,l3) = must in
241 match (l1,l2,l3) with
242 l1,[],[] -> build_intersect_obj l1
243 | [],l2,[] -> let lrel = List.map tostring_rel l2 in
244 build_intersect_rel lrel
245 | [],[],l3 -> let lsort = List.map tostring_sort l3 in
246 build_intersect_sort lsort
247 | l1,l2,[] -> let lrel = List.map tostring_rel l2 in
248 M.Intersect (build_intersect_obj l1, build_intersect_rel lrel)
249 | l1,[],l3 -> let lsort = List.map tostring_sort l3 in
250 M.Intersect (build_intersect_obj l1, build_intersect_sort lsort)
251 | [],l2,l3 -> let lrel = List.map tostring_rel l2 in
252 let lsort = List.map tostring_sort l3 in
253 M.Intersect (build_intersect_rel lrel, build_intersect_sort lsort)
254 | _ -> let lrel = List.map tostring_rel l2 in
255 let lsort = List.map tostring_sort l3 in
256 M.Intersect (M.Intersect (build_intersect_obj l1, build_intersect_rel lrel), build_intersect_sort lsort)
259 let q_in = build_intersect must_use in
260 let q_select = M.Select ("uri0", q_in, q_where can_use) in
262 let (cr_o,cr_r,cr_s) = can_use in
267 | Some cr_o -> M.Const (List.map uri_of_entry cr_o)
270 let tfst (a,b,c) = a in
271 let tsnd (a,b,c) = b in
272 let trd (a,b,c) = c in
277 | Some cr_o -> M.Const (List.map tsnd cr_o) in
282 | Some cr_o -> let lofc = List.map trd cr_o in
288 let lof_dep_int = List.fold_left f [] lofc in
289 M.Const(List.map string_of_int lof_dep_int)
295 | Some cr_r -> M.Const (List.map fst cr_r) in
300 | Some cr_r -> let rdep_int = List.map snd cr_r in
301 M.Const (List.map string_of_int rdep_int)
308 | Some cr_s -> M.Const (List.map tfst cr_s) in
314 | Some cr_s -> let sdep_int = List.map tsnd cr_s in
315 M.Const (List.map string_of_int sdep_int)
322 | Some cr_s -> M.Const (List.map trd cr_s) in
324 let q_let_u = M.LetVVar ("universe", universe, q_select) in
326 let q_let_s = M.LetVVar ("sorts", sor, q_let_u) in
328 let q_let_ds = M.LetVVar ("sort_depths", sdep, q_let_s) in
330 let q_let_dr = M.LetVVar ("rel_depths", rdep, q_let_ds) in
332 let q_let_do = M.LetVVar ("obj_depths", odep, q_let_dr) in
334 let q_let_ps = M.LetVVar ("sort_positions", spos, q_let_do) in
336 let q_let_pr = M.LetVVar ("rel_positions", rpos, q_let_ps) in
338 let q_let_po = M.LetVVar ("obj_positions", opos, q_let_pr) in
339 print_endline ("### " ^ MQueryUtil.text_of_query q_let_po) ; flush stdout;
340 execute_query q_let_po