]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mqint.ml
structurated attribute names added
[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 open Attribute;;
46
47
48 exception BooleExpTrue
49
50 let init connection_param = Dbconn.init connection_param 
51
52 let close () = Dbconn.close ()
53
54 let check () = 
55    let status = Dbconn.pgc () 
56    in ()
57
58 let stat = ref true
59
60 let set_stat b = stat := b
61
62 let get_stat () = ! stat
63
64 let postgres_db = "postgres"
65
66 let galax_db = "galax"
67
68 let dbname = ref galax_db
69
70 let set_database s = 
71     if s = postgres_db || s = galax_db then dbname := s
72     else raise (Invalid_argument s)
73
74 let get_database () = ! dbname
75
76 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
77
78 let rec exec_set_exp c = function
79      MathQL.SVar svar -> List.assoc svar c.svars
80    | MathQL.RVar rvar -> [List.assoc rvar c.rvars]  
81    | MathQL.Ref vexp -> List.map (fun s -> (s,[])) (exec_val_exp c vexp)
82    | MathQL.Intersect (sexp1, sexp2) ->    
83         let before = Sys.time() in
84         let rs1 = exec_set_exp c sexp1 in
85         let rs2 = exec_set_exp c sexp2 in
86         let res = intersect_ex rs1 rs2 in
87         let after = Sys.time() in
88         let ll1 = string_of_int (List.length rs1) in
89         let ll2 = string_of_int (List.length rs2) in
90         let diff = string_of_float (after -. before) in
91         if !stat then
92         (print_endline("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
93          ": " ^ diff ^ "s");
94          flush stdout);
95         res
96    | MathQL.Union (sexp1, sexp2) -> 
97         let before = Sys.time () in
98         let res = union_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2) in
99         let after = Sys.time() in
100         let diff = string_of_float (after -. before) in
101         if !stat then
102         (print_endline ("UNION: " ^ diff ^ "s");
103          flush stdout);
104         res                     
105    | MathQL.LetSVar (svar, sexp1, sexp2) ->
106         let before = Sys.time() in
107         let c1 = upd_svars c ((svar, exec_set_exp c sexp1) :: c.svars) in 
108         let res = exec_set_exp c1 sexp2 in
109         if ! stat then
110         (print_string ("LETIN " ^ svar ^ " = " ^ string_of_int (List.length res) ^ ": ");
111          print_endline (string_of_float (Sys.time() -. before) ^ "s");
112          flush stdout); 
113         res                     
114    | MathQL.LetVVar (vvar, vexp, sexp) ->
115         let before = Sys.time() in
116         let c1 = upd_vvars c ((vvar, exec_val_exp c vexp) :: c.vvars) in
117         let res = exec_set_exp c1 sexp in
118         if ! stat then
119         (print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
120          print_endline (string_of_float (Sys.time() -. before) ^ "s");
121          flush stdout); 
122         res
123    | MathQL.Relation (inv, rop, path, sexp, attl) -> 
124         let before = Sys.time() in
125         if ! dbname = postgres_db then
126         (let res = relation_ex rop path (exec_set_exp c sexp) attl in
127          if ! stat then 
128          (print_string ("RELATION " ^ (fst path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
129           print_endline (string_of_float (Sys.time() -. before) ^ "s");
130           flush stdout);
131          res)
132         else
133         (let res = relation_galax_ex rop path (exec_set_exp c sexp) attl in
134          if !stat then
135          (print_string ("RELATION-GALAX " ^ (fst path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
136           print_endline (string_of_float (Sys.time() -. before) ^ "s");
137           flush stdout);
138          res) 
139         
140         
141    | MathQL.Select (rvar, sexp, bexp) ->
142         let before = Sys.time() in
143         let rset = (exec_set_exp c sexp) in
144         let rec select_ex rset =
145         match rset with 
146           [] -> []
147         | r::tl -> let c1 = upd_rvars c ((rvar,r)::c.rvars) in                      
148                    if (exec_boole_exp c1 bexp) then r::(select_ex tl)
149                    else select_ex tl
150         in 
151         let res = select_ex rset in
152         if ! stat then
153         (print_string ("SELECT " ^ rvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
154          print_endline (string_of_float (Sys.time() -. before) ^ "s");
155          flush stdout); 
156         res
157    | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
158    | _ -> assert false
159    
160 (* valuta una MathQL.boole_exp e ritorna un boole *)
161
162 and exec_boole_exp c = function
163      MathQL.False      -> false
164    | MathQL.True       -> true
165    | MathQL.Not x      -> not (exec_boole_exp c x)
166    | MathQL.And (x, y) -> (exec_boole_exp c x) && (exec_boole_exp c y)
167    | MathQL.Or (x, y)  -> (exec_boole_exp c x) || (exec_boole_exp c y)
168    | MathQL.Sub (vexp1, vexp2) -> sub_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
169    | MathQL.Meet (vexp1, vexp2) -> meet_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
170    | MathQL.Eq (vexp1, vexp2) -> (exec_val_exp c vexp1) = (exec_val_exp c vexp2)
171    | MathQL.Ex l bexp -> 
172         if l = [] then (exec_boole_exp c bexp)
173         else
174          let latt = List.map (fun uri -> 
175                                 let (r,attl) = List.assoc uri c.rvars in (uri,attl)) l (*latt = l + attributi*)
176          in
177          try
178          let rec prod c = function
179               [] -> if (exec_boole_exp c bexp) then raise BooleExpTrue 
180             | (uri,attl)::tail1 -> let rec sub_prod attl =
181                                       match attl with
182 (*per ogni el. di attl  *)              [] -> () 
183 (*devo andare in ric. su tail1*)      | att::tail2 -> let c1 = upd_groups c ((uri,att)::c.groups) in             
184                                                        prod c1 tail1; sub_prod tail2 
185                                      in       
186                                       sub_prod attl 
187          in
188          prod c latt; false
189          with BooleExpTrue -> true
190
191 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
192
193 and exec_val_exp c = function
194      MathQL.Const x -> let
195         ol = List.sort compare x in 
196                         let rec edup = function
197                         
198                            [] -> [] 
199                          | s::tl -> if tl <> [] then  
200                                                  if s = (List.hd tl) then edup tl
201                                                  else s::(edup tl)
202                                     else s::[]
203                         in
204                          edup ol
205    | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups) 
206                                   
207    | MathQL.VVar s -> List.assoc s c.vvars                                
208    | MathQL.RefOf sexp -> List.map (fun (s,_) -> s) (exec_set_exp c sexp)
209    | MathQL.Fun (s, vexp) -> fun_ex s (exec_val_exp c vexp)
210    | MathQL.Attribute (inv, rop, path, vexp) -> attribute_ex rop path inv (exec_val_exp c vexp) 
211
212 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
213 and execute x =
214    exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x