]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mqint.ml
053e20c74bb009fa5ecbef498a7a3f12cb4bfcfb
[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 open Dbconn;;
37 open Pattern;;
38 open Union;;
39 open Intersect;;
40 open Diff;;
41 open Sortedby;;
42 open Use;;
43 open Select;;
44 open Letin;;
45 open Mathql_semantics;;
46
47
48
49 let prop_pool = ref None;;
50
51 let fi_to_string fi =
52  match fi with
53     (None, _)   ->
54      ""
55  |  (Some i, y) ->
56      "#xpointer(1/"       ^
57      string_of_int i      ^
58      (
59       match y with
60          None   ->
61           ""
62       |  Some j ->
63           "/" ^ (string_of_int j)
64      )                    ^
65      ")"
66 ;;
67
68 let see_prop_pool () =
69  let _ = print_endline "eccomi" in
70  List.iter
71   (fun elem -> print_endline (fst elem ^ ": " ^ snd elem))
72   (match !prop_pool with Some l -> l | _ -> print_endline "ciao"; assert false)
73 ;;
74
75 (*
76  * inizializzazione della connessione al database
77  *)
78 let init () =
79  let _ = Dbconn.init () in
80   let c = pgc () in
81    let res = 
82     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#')"
83    in
84     prop_pool := Some
85      (
86       List.map
87        (function
88            a::b::_ -> (a, b)
89          | _       -> print_endline "no"; assert false
90        )
91        res#get_list
92      )
93 ;;
94
95 let get_prop_id prop =
96  if prop="refObj" then "F"
97  else if prop="backPointer" then "B"
98  else List.assoc prop (match !prop_pool with Some l -> l | _ -> assert false)
99 ;;
100
101 (* execute_ex env q                                                   *)
102 (*  [env] is the attributed uri environment in which the query [q]    *)
103 (*        must be evaluated                                           *)
104 (*  [q]   is the query to evaluate                                    *)
105 (*  It returns a [Mathql_semantics.result]                            *)
106 let rec execute_ex env =
107  function
108     MQSelect (apvar, alist, abool) ->
109      select_ex env apvar (execute_ex env alist) abool
110  |  MQUsedBy (alist, asvar) ->
111      use_ex (execute_ex env alist) asvar (get_prop_id "refObj")      (* "F" (*"refObj"*) *)
112  |  MQUse (alist, asvar) ->
113      use_ex (execute_ex env alist) asvar (get_prop_id "backPointer") (* "B" (*"backPointer"*) *)
114  |  MQPattern (apreamble, apattern, afragid) ->
115      pattern_ex (apreamble, apattern, afragid)
116  |  MQUnion (l1, l2) ->
117      union_ex (execute_ex env l1) (execute_ex env l2)
118  |  MQDiff (l1, l2) ->
119      diff_ex (execute_ex env l1) (execute_ex env l2)
120  |  MQSortedBy (l, o, f) ->
121      sortedby_ex (execute_ex env l) o f
122  |  MQIntersect (l1, l2) ->
123      intersect_ex (execute_ex env l1) (execute_ex env l2)
124  |  MQListRVar rvar -> [List.assoc rvar env]
125  |  MQLetIn (lvar, l1, l2) ->
126      let t = Unix.time () in
127       let res =
128        (*CSC: The interesting code *)
129        let _ = letin_ex lvar (execute_ex env l1) in
130         execute_ex env l2
131        (*CSC: end of the interesting code *)
132       in
133        letdispose ();
134        print_string ("LETIN = " ^ string_of_int (List.length res) ^ ": ") ;
135        print_endline (string_of_float (Unix.time () -. t) ^ "s") ;
136        flush stdout ;
137        res
138  |  MQListLVar lvar ->
139      letref_ex lvar
140  |  MQReference l ->
141      let rec build_result = function
142        | [] -> []
143        | s :: tail -> 
144          {uri = s ; attributes = [] ; extra = ""} :: build_result tail
145      in build_result (List.sort compare l)
146 ;;
147
148 (* Let's initialize the execute in Select, creating a cyclical recursion *)
149 Select.execute := execute_ex;;
150
151 (*
152  * converte il risultato interno di una query (uri + contesto)
153  * in un risultato di sole uri
154  *
155  * parametri:
156  * l: string list list;
157  *
158  * output: mqresult;
159  *
160  * note:
161  * il tipo del risultato mantenuto internamente e' diverso dal tipo di risultato
162  * restituito in output poiche', mentre chi effettua le query vuole come risultato
163  * solo le eventuali uri che soddisfano le query stesse, internamente ad una uri
164  * sono associati anche i valori delle variabili che ancora non sono state valutate
165  * perche', ad esempio, si trovano in altri rami dell'albero.
166  *
167  * Esempio:
168  * SELECT x IN USE PATTERN "cic:/**.con" POSITION $a WHERE $a IS MainConclusion
169  * L'albero corrispondente a questa query e':
170  *
171  *                  SELECT
172  *                /   |    \
173  *               x   USE    IS
174  *                  /   \    /\
175  *           PATTERN    $a  $a MainConclusion
176  *
177  * Nel momento in cui si esegue il ramo USE non sono noti i vincoli sullla variabile $a
178  * percui e' necessario considerare, oltre alle uri, i valori della variabile per i quali
179  * la uri puo' far parte del risultato.
180  *)
181 let xres_to_res l =
182  MQRefs (List.map (function {Mathql_semantics.uri = uri} -> uri) l)
183 (*
184  let tmp = List.map (function {Mathql_semantics.uri = uri} -> uri) l in
185   MQRefs
186    (List.map
187     (function l ->
188       (*let _ = print_endline ("DEBUG: (mqint.ml: xres_to_res)" ^ l) in*)
189       match Str.split (Str.regexp ":\|#\|/\|(\|)") l with
190          hd::""::tl -> (
191           match List.rev tl with
192              n::"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 n)]
204               )
205           |  n::m::"1"::"xpointer"::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                [MQFC (int_of_string m); MQFC (int_of_string n)]
217               )
218           |  tail                          ->
219               (
220                Some hd,
221                List.fold_left
222                 (fun par t ->
223                  match par with
224                     [] -> [MQBC t] 
225                  |  _  -> (MQBC t) :: MQBD :: par
226                 )
227                 []
228                 tail, 
229                []
230               )
231       )  
232        | _ -> assert false
233     )
234     tmp
235    )
236 *)
237 ;;
238
239
240 (*
241  * 
242  *)
243 let execute q =
244  match q with
245     MQList qq -> xres_to_res (execute_ex [] qq)
246 ;;
247
248 (*
249  * chiusura della connessione al database
250  *)
251 let close () = Dbconn.close ();;
252
253 *****************************************************************************)
254
255 let init () = () (* FG: implementare l'apertura del database *)
256
257 let close () = () (* FG: implementare la chiusura del database *)
258
259
260 (* contexts *****************************************************************)
261
262 type svar_context = (MathQL.svar * MathQL.resource_set) list
263
264 type rvar_context = (MathQL.rvar * MathQL.resource) list
265
266 type group_context = (MathQL.rvar * MathQL.attribute_group) list
267
268
269 let svars = ref [] (* contesto delle svar *)
270
271 let rvars = ref [] (* contesto delle rvar *)
272
273 let groups = ref [] (* contesto dei gruppi *)
274
275
276 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
277
278 let rec exec_set_exp = function 
279    | MathQL.Ref x -> []
280
281
282 (* valuta una MathQL.boole_exp e ritorna un boole *)
283
284 and exec_boole_exp = function
285    | MathQL.False      -> false
286    | MathQL.True       -> true
287    | MathQL.Not x      -> not (exec_boole_exp x)
288    | MathQL.And (x, y) -> (exec_boole_exp x) && (exec_boole_exp y)
289    | MathQL.Or (x, y)  -> (exec_boole_exp x) || (exec_boole_exp y)
290
291
292 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
293
294 and exec_val_exp = function
295    | MathQL.Const l -> []
296
297
298 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
299
300 let execute x =
301    svars := []; rvars := []; groups := [];
302    exec_set_exp x