]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mqint.ml
the db connection parameters are now parametrized istead of hard-coded
[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  * implementazione del'interprete MathQL
28  *)
29
30 (*
31 (* FG: ROBA VECCHIA DA BUTTARE (tranne apertura e chiusura database *)
32
33 open MathQL;;
34 open Eval;;
35 open Utility;;
36
37 open Pattern;;*)
38 open Dbconn;;
39 open Union;;
40 open Intersect;;
41 open Meet;;
42 open Sub;;
43 open Context;;
44 open Diff;;
45 open Relation;;
46 (*open Sortedby;;
47 open Use;;
48 open Select;;
49 open Letin;;
50 open Mathql_semantics;;
51
52
53
54 let prop_pool = ref None;;
55
56 let fi_to_string fi =
57  match fi with
58     (None, _)   ->
59      ""
60  |  (Some i, y) ->
61      "#xpointer(1/"       ^
62      string_of_int i      ^
63      (
64       match y with
65          None   ->
66           ""
67       |  Some j ->
68           "/" ^ (string_of_int j)
69      )                    ^
70      ")"
71 ;;
72
73 let see_prop_pool () =
74  let _ = print_endline "eccomi" in
75  List.iter
76   (fun elem -> print_endline (fst elem ^ ": " ^ snd elem))
77   (match !prop_pool with Some l -> l | _ -> print_endline "ciao"; assert false)
78 ;;
79
80
81
82 let get_prop_id prop =
83  if prop="refObj" then "F"
84  else if prop="backPointer" then "B"
85  else List.assoc prop (match !prop_pool with Some l -> l | _ -> assert false)
86 ;;
87
88 (* execute_ex env q                                                   *)
89 (*  [env] is the attributed uri environment in which the query [q]    *)
90 (*        must be evaluated                                           *)
91 (*  [q]   is the query to evaluate                                    *)
92 (*  It returns a [Mathql_semantics.result]                            *)
93 let rec execute_ex env =
94  function
95     MQSelect (apvar, alist, abool) ->
96      select_ex env apvar (execute_ex env alist) abool
97  |  MQUsedBy (alist, asvar) ->
98      use_ex (execute_ex env alist) asvar (get_prop_id "refObj")      (* "F" (*"refObj"*) *)
99  |  MQUse (alist, asvar) ->
100      use_ex (execute_ex env alist) asvar (get_prop_id "backPointer") (* "B" (*"backPointer"*) *)
101  |  MQPattern (apreamble, apattern, afragid) ->
102      pattern_ex (apreamble, apattern, afragid)
103  |  MQUnion (l1, l2) ->
104      union_ex (execute_ex env l1) (execute_ex env l2)
105  |  MQDiff (l1, l2) ->
106      diff_ex (execute_ex env l1) (execute_ex env l2)
107  |  MQSortedBy (l, o, f) ->
108      sortedby_ex (execute_ex env l) o f
109  |  MQIntersect (l1, l2) ->
110      intersect_ex (execute_ex env l1) (execute_ex env l2)
111  |  MQListRVar rvar -> [List.assoc rvar env]
112  |  MQLetIn (lvar, l1, l2) ->
113      let t = Sys.time () in
114       let res =
115        (*CSC: The interesting code *)
116        let _ = letin_ex lvar (execute_ex env l1) in
117         execute_ex env l2
118        (*CSC: end of the interesting code *)
119       in
120        letdispose ();
121        print_string ("LETIN = " ^ string_of_int (List.length res) ^ ": ") ;
122        print_endline (string_of_float (Sys.time () -. t) ^ "s") ;
123        flush stdout ;
124        res
125  |  MQListLVar lvar ->
126      letref_ex lvar
127  |  MQReference l ->
128      let rec build_result = function
129        | [] -> []
130        | s :: tail -> 
131          {uri = s ; attributes = [] ; extra = ""} :: build_result tail
132      in build_result (List.sort compare l)
133 ;;
134
135 (* Let's initialize the execute in Select, creating a cyclical recursion *)
136 Select.execute := execute_ex;;
137
138 (*
139  * converte il risultato interno di una query (uri + contesto)
140  * in un risultato di sole uri
141  *
142  * parametri:
143  * l: string list list;
144  *
145  * output: mqresult;
146  *
147  * note:
148  * il tipo del risultato mantenuto internamente e' diverso dal tipo di risultato
149  * restituito in output poiche', mentre chi effettua le query vuole come risultato
150  * solo le eventuali uri che soddisfano le query stesse, internamente ad una uri
151  * sono associati anche i valori delle variabili che ancora non sono state valutate
152  * perche', ad esempio, si trovano in altri rami dell'albero.
153  *
154 * Esempio:
155  * SELECT x IN USE PATTERN "cic:/**.con" POSITION $a WHERE $a IS MainConclusion
156  * L'albero corrispondente a questa query e':
157  *
158  *                  SELECT
159  *                /   |    \
160  *               x   USE    IS
161  *                  /   \    /\
162  *           PATTERN    $a  $a MainConclusion
163  *
164  * Nel momento in cui si esegue il ramo USE non sono noti i vincoli sullla variabile $a
165  * percui e' necessario considerare, oltre alle uri, i valori della variabile per i quali
166  * la uri puo' far parte del risultato.
167  *)
168 let xres_to_res l =
169  MQRefs (List.map (function {Mathql_semantics.uri = uri} -> uri) l)
170 (*
171  let tmp = List.map (function {Mathql_semantics.uri = uri} -> uri) l in
172   MQRefs
173    (List.map
174     (function l ->
175       (*let _ = print_endline ("DEBUG: (mqint.ml: xres_to_res)" ^ l) in*)
176       match Str.split (Str.regexp ":\|#\|/\|(\|)") l with
177          hd::""::tl -> (
178           match List.rev tl with
179              n::"1"::"xpointer"::tail    ->
180               (
181                Some hd,
182                List.fold_left
183                 (fun par t ->
184                  match par with
185                     [] -> [MQBC t] 
186                  |  _  -> (MQBC t) :: MQBD :: par
187                 )
188                 []
189                 tail, 
190                [MQFC (int_of_string n)]
191               )
192           |  n::m::"1"::"xpointer"::tail ->
193               (
194                Some hd,
195                List.fold_left
196                 (fun par t ->
197                  match par with
198                     [] -> [MQBC t] 
199                  |  _  -> (MQBC t) :: MQBD :: par
200                 )
201                 []
202                 tail,
203                [MQFC (int_of_string m); MQFC (int_of_string n)]
204               )
205           |  tail                          ->
206               (
207                Some hd,
208                List.fold_left
209                 (fun par t ->
210                  match par with
211                     [] -> [MQBC t] 
212                  |  _  -> (MQBC t) :: MQBD :: par
213                 )
214                 []
215                 tail, 
216                []
217               )
218       )  
219        | _ -> assert false
220     )
221     tmp
222    )
223 *)
224 ;;
225
226
227 (*
228  * 
229  *)
230 let execute q =
231  match q with
232     MQList qq -> xres_to_res (execute_ex [] qq)
233 ;;
234
235 let prop_pool = ref None;;
236
237 *****************************************************************************)
238
239 let init connection_param = Dbconn.init connection_param 
240 (*
241    let c = pgc () in
242    let res = 
243       c#exec "select name,id from property where ns_id in (select id from namespace where url='http://www.cs.unibo.it/helm/schemas/mattone.rdf#')"
244    in
245    prop_pool := Some
246      (
247       List.map
248        (function
249            a::b::_ -> (a, b)
250          | _       -> print_endline "no"; assert false
251        )
252        res#get_list
253      )
254 *) 
255
256 let close () = Dbconn.close ()
257
258 let check () = Dbconn.pgc ()
259
260 exception BooleExpTrue
261
262 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
263
264 let rec exec_set_exp c = function
265    |MathQL.SVar svar -> List.assoc svar c.svars
266    |MathQL.RVar rvar -> [List.assoc rvar c.rvars]  
267    | MathQL.Ref vexp -> List.map (fun s -> (s,[])) (exec_val_exp c vexp)
268    | MathQL.Intersect (sexp1, sexp2) -> intersect_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)    
269    | MathQL.Union (sexp1, sexp2) -> union_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
270    | MathQL.LetSVar (svar, sexp1, sexp2) -> let _ = (svar, (exec_set_exp c sexp1)):: (List.remove_assoc svar c.svars) 
271                                         in (exec_set_exp c sexp2)
272    | MathQL.LetVVar (vvar, vexp, sexp) ->
273         let before = Sys.time () in
274         let c1 = upd_vvars c ((vvar, exec_val_exp c vexp) :: c.vvars) in
275         let res = exec_set_exp c1 sexp in
276         print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ") ;
277         print_endline (string_of_float (Sys.time () -. before) ^ "s") ;
278         flush stdout ; res
279    | MathQL.Relation (rop, path, sexp, attl) -> relation_ex rop path (exec_set_exp c sexp) attl
280    | MathQL.Select (rvar, sexp, bexp) -> let rset = (exec_set_exp c sexp) in
281                                           let rec select_ex rset =
282                                             match rset with 
283                                                       [] -> []
284                                             | r::tl -> let c1 = upd_rvars c ((rvar,r)::c.rvars) in                      
285                                                if (exec_boole_exp c1 bexp) then r::(select_ex tl)
286                                                else select_ex tl
287                                           in select_ex rset
288                                                        
289                                                                                    
290    
291    | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
292    | _ -> assert false
293    
294 (* valuta una MathQL.boole_exp e ritorna un boole *)
295
296 and exec_boole_exp c = function
297    | MathQL.False      -> false
298    | MathQL.True       -> true
299    | MathQL.Not x      -> not (exec_boole_exp c x)
300    | MathQL.And (x, y) -> (exec_boole_exp c x) && (exec_boole_exp c y)
301    | MathQL.Or (x, y)  -> (exec_boole_exp c x) || (exec_boole_exp c y)
302    | MathQL.Sub (vexp1, vexp2) -> sub_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
303    | MathQL.Meet (vexp1, vexp2) -> meet_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
304    | MathQL.Eq (vexp1, vexp2) -> (exec_val_exp c vexp1) = (exec_val_exp c vexp2)
305    | MathQL.Ex l bexp -> 
306         if l = [] then (exec_boole_exp c bexp)
307         else
308           let latt = List.map (fun uri -> 
309                                        let (r,attl) = List.assoc uri c.rvars 
310                                        in (uri,attl)) l (*latt = l + attributi*)
311           in
312            try
313              let rec prod c = function
314                 [] -> if (exec_boole_exp c bexp) then raise BooleExpTrue 
315               | (uri,attl)::tail1 -> let rec sub_prod attl =
316                                       match attl with
317 (*per ogni el. di attl  *)              [] -> () 
318 (*devo andare in ric. su tail1*)      | att::tail2 -> let c1 = upd_groups c ((uri,att)::c.groups) in             
319                                                        prod c1 tail1; sub_prod tail2 
320                                      in       
321                                       sub_prod attl 
322              in
323               prod c latt; false
324            with BooleExpTrue -> true  
325    | _ -> assert false    
326
327 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
328
329 and exec_val_exp c = function
330    | MathQL.Const x -> let
331    ol = List.sort compare x in 
332                         let rec edup = function
333                         
334                            [] -> [] 
335                          | s::tl -> if tl <> [] then  
336                                                  if s = (List.hd tl) then edup tl
337                                                  else s::(edup tl)
338                                     else s::[]
339                         in
340                          edup ol
341    | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups) 
342                                   
343    | MathQL.VVar s -> List.assoc s c.vvars                                
344    | MathQL.RefOf sexp -> List.map (fun (s,_) -> s) (exec_set_exp c sexp)
345    
346    | _ -> assert false
347
348
349 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
350
351 and execute x =
352    exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x