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/.
30 * implementazione del'interprete MathQL
47 exception BooleExpTrue
49 let init connection_param = Dbconn.init connection_param
51 let close () = Dbconn.close ()
54 let status = Dbconn.pgc ()
59 let set_stat b = stat := b
61 let get_stat () = ! stat
63 let postgres_db = "postgres"
65 let galax_db = "galax"
67 let dbname = ref galax_db
70 if s = postgres_db || s = galax_db then dbname := s
71 else raise (Invalid_argument s)
73 let get_database () = ! dbname
75 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
77 let rec exec_set_exp c = function
78 MathQL.SVar svar -> List.assoc svar c.svars
79 | MathQL.RVar rvar -> [List.assoc rvar c.rvars]
80 | MathQL.Ref vexp -> List.map (fun s -> (s,[])) (exec_val_exp c vexp)
81 | MathQL.Intersect (sexp1, sexp2) ->
82 let before = Sys.time() in
83 let rs1 = exec_set_exp c sexp1 in
84 let rs2 = exec_set_exp c sexp2 in
85 let res = intersect_ex rs1 rs2 in
86 let after = Sys.time() in
87 let ll1 = string_of_int (List.length rs1) in
88 let ll2 = string_of_int (List.length rs2) in
89 let diff = string_of_float (after -. before) in
91 (print_endline("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
95 | MathQL.Union (sexp1, sexp2) ->
96 let before = Sys.time () in
97 let res = union_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2) in
98 let after = Sys.time() in
99 let diff = string_of_float (after -. before) in
101 (print_endline ("UNION: " ^ diff ^ "s");
104 | MathQL.LetSVar (svar, sexp1, sexp2) ->
105 let before = Sys.time() in
106 let c1 = upd_svars c ((svar, exec_set_exp c sexp1) :: c.svars) in
107 let res = exec_set_exp c1 sexp2 in
109 (print_string ("LETIN " ^ svar ^ " = " ^ string_of_int (List.length res) ^ ": ");
110 print_endline (string_of_float (Sys.time() -. before) ^ "s");
113 | MathQL.LetVVar (vvar, vexp, sexp) ->
114 let before = Sys.time() in
115 let c1 = upd_vvars c ((vvar, exec_val_exp c vexp) :: c.vvars) in
116 let res = exec_set_exp c1 sexp in
118 (print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
119 print_endline (string_of_float (Sys.time() -. before) ^ "s");
122 | MathQL.Relation (rop, path, sexp, attl) ->
123 let before = Sys.time() in
124 if ! dbname = postgres_db then
125 (let res = relation_ex rop path (exec_set_exp c sexp) attl in
127 (print_string ("RELATION " ^ (List.hd path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
128 print_endline (string_of_float (Sys.time() -. before) ^ "s");
132 (let res = relation_galax_ex rop path (exec_set_exp c sexp) attl in
134 (print_string ("RELATION-GALAX " ^ (List.hd path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
135 print_endline (string_of_float (Sys.time() -. before) ^ "s");
140 | MathQL.Select (rvar, sexp, bexp) ->
141 let before = Sys.time() in
142 let rset = (exec_set_exp c sexp) in
143 let rec select_ex rset =
146 | r::tl -> let c1 = upd_rvars c ((rvar,r)::c.rvars) in
147 if (exec_boole_exp c1 bexp) then r::(select_ex tl)
150 let res = select_ex rset in
152 (print_string ("SELECT " ^ rvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
153 print_endline (string_of_float (Sys.time() -. before) ^ "s");
156 | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
159 (* valuta una MathQL.boole_exp e ritorna un boole *)
161 and exec_boole_exp c = function
162 MathQL.False -> false
163 | MathQL.True -> true
164 | MathQL.Not x -> not (exec_boole_exp c x)
165 | MathQL.And (x, y) -> (exec_boole_exp c x) && (exec_boole_exp c y)
166 | MathQL.Or (x, y) -> (exec_boole_exp c x) || (exec_boole_exp c y)
167 | MathQL.Sub (vexp1, vexp2) -> sub_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
168 | MathQL.Meet (vexp1, vexp2) -> meet_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
169 | MathQL.Eq (vexp1, vexp2) -> (exec_val_exp c vexp1) = (exec_val_exp c vexp2)
170 | MathQL.Ex l bexp ->
171 if l = [] then (exec_boole_exp c bexp)
173 let latt = List.map (fun uri ->
174 let (r,attl) = List.assoc uri c.rvars in (uri,attl)) l (*latt = l + attributi*)
177 let rec prod c = function
178 [] -> if (exec_boole_exp c bexp) then raise BooleExpTrue
179 | (uri,attl)::tail1 -> let rec sub_prod attl =
181 (*per ogni el. di attl *) [] -> ()
182 (*devo andare in ric. su tail1*) | att::tail2 -> let c1 = upd_groups c ((uri,att)::c.groups) in
183 prod c1 tail1; sub_prod tail2
188 with BooleExpTrue -> true
190 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
192 and exec_val_exp c = function
193 MathQL.Const x -> let
194 ol = List.sort compare x in
195 let rec edup = function
198 | s::tl -> if tl <> [] then
199 if s = (List.hd tl) then edup tl
204 | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups)
206 | MathQL.VVar s -> List.assoc s c.vvars
207 | MathQL.RefOf sexp -> List.map (fun (s,_) -> s) (exec_set_exp c sexp)
208 | MathQL.Fun (s, vexp) -> fun_ex s (exec_val_exp c vexp)
209 | MathQL.Attribute (rop, path, vexp) -> []
211 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
213 exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x