]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mqint.ml
mqint.ml patched
[helm.git] / helm / ocaml / mathql_interpreter / mqint.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
29 (*
30  * implementazione del'interprete MathQL
31  *)
32
33
34
35
36 open Dbconn;;
37 open Union;;
38 open Intersect;;
39 open Meet;;
40 open Sub;;
41 open Context;;
42 open Diff;;
43 open Relation;;
44 open Func;;
45
46
47 exception BooleExpTrue
48
49 let init connection_param = Dbconn.init connection_param 
50
51 let close () = Dbconn.close ()
52
53 let check () = 
54    let status = Dbconn.pgc () 
55    in ()
56
57 let stat = ref false
58
59 let set_stat b = stat := b
60
61 let get_stat () = ! stat
62
63 let postgres_db = "postgres"
64
65 let galax_db = "galax"
66
67 let dbname = ref postgres_db
68
69 let set_database s = 
70     if s = postgres_db || s = galax_db then dbname := s
71     else raise (Invalid_argument s)
72
73 let get_database () = ! dbname
74
75 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
76
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
90         if ! stat then
91         (print_endline("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
92          ": " ^ diff ^ "s");
93          flush stdout);
94         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
100         if ! stat then
101         (print_endline ("UNION: " ^ diff ^ "s");
102          flush stdout);
103         res                     
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
108         if ! stat then
109         (print_string ("LETIN " ^ svar ^ " = " ^ string_of_int (List.length res) ^ ": ");
110          print_endline (string_of_float (Sys.time() -. before) ^ "s");
111          flush stdout); 
112         res                     
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
117         if ! stat then
118         (print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
119          print_endline (string_of_float (Sys.time() -. before) ^ "s");
120          flush stdout); 
121         res
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
126          if ! stat then 
127          (print_string ("RELATION " ^ (List.hd path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
128           print_endline (string_of_float (Sys.time() -. before) ^ "s");
129           flush stdout);
130          res)
131         else
132         (let res = relation_galax_ex rop path (exec_set_exp c sexp) attl in
133          if ! stat then
134          (print_string ("RELATION-GALAX " ^ (List.hd path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
135           print_endline (string_of_float (Sys.time() -. before) ^ "s");
136           flush stdout);
137          res) 
138         
139         
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 =
144         match rset with 
145           [] -> []
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)
148                    else select_ex tl
149         in 
150         let res = select_ex rset in
151         if ! stat then
152         (print_string ("SELECT " ^ rvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
153          print_endline (string_of_float (Sys.time() -. before) ^ "s");
154          flush stdout); 
155         res
156    | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
157    | _ -> assert false
158    
159 (* valuta una MathQL.boole_exp e ritorna un boole *)
160
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)
172         else
173          let latt = List.map (fun uri -> 
174                                 let (r,attl) = List.assoc uri c.rvars in (uri,attl)) l (*latt = l + attributi*)
175          in
176          try
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 =
180                                       match attl with
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 
184                                      in       
185                                       sub_prod attl 
186          in
187          prod c latt; false
188          with BooleExpTrue -> true
189
190 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
191
192 and exec_val_exp c = function
193      MathQL.Const x -> let
194         ol = List.sort compare x in 
195                         let rec edup = function
196                         
197                            [] -> [] 
198                          | s::tl -> if tl <> [] then  
199                                                  if s = (List.hd tl) then edup tl
200                                                  else s::(edup tl)
201                                     else s::[]
202                         in
203                          edup ol
204    | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups) 
205                                   
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) -> [] 
210
211 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
212 and execute x =
213    exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x