]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter_galax/mqint.ml
No more useful
[helm.git] / helm / ocaml / mathql_interpreter_galax / 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 open MathQL;;
30 open Eval;;
31 (*open Utility;;*)
32 open Union;;
33 open Intersect;;
34 open Diff;;
35 open Sortedby;;
36 open Select;;
37 open Letin;;
38 open Mathql_semantics;;
39 open Pattern;;
40 open Use;;
41
42
43
44 let prop_pool = ref None;;
45
46 let fi_to_string fi =
47  match fi with
48     (None, _)   ->
49      ""
50  |  (Some i, y) ->
51      "#xpointer(1/"       ^
52      string_of_int i      ^
53      (
54       match y with
55          None   ->
56           ""
57       |  Some j ->
58           "/" ^ (string_of_int j)
59      )                    ^
60      ")"
61 ;;
62
63 let see_prop_pool () =
64  let _ = print_endline "eccomi" in
65  List.iter
66   (fun elem -> print_endline (fst elem ^ ": " ^ snd elem))
67   (match !prop_pool with Some l -> l | _ -> print_endline "ciao"; assert false)
68 ;;
69
70
71 let get_prop_id prop =
72  if prop="refObj" then "F"
73  else if prop="backPointer" then "B"
74  else List.assoc prop (match !prop_pool with Some l -> l | _ -> assert false)
75 ;;
76
77 (* execute_ex env q                                                   *)
78 (*  [env] is the attributed uri environment in which the query [q]    *)
79 (*        must be evaluated                                           *)
80 (*  [q]   is the query to evaluate                                    *)
81 (*  It returns a [Mathql_semantics.result]                            *)
82 let rec execute_ex env =
83  function
84     MQSelect (apvar, alist, abool) -> select_ex env apvar (execute_ex env alist) abool
85  |  MQUsedBy (alist, asvar) -> use_ex (execute_ex env alist) asvar (get_prop_id "refObj")      (* "F"
86      (*"refObj"*) *) 
87  |  MQUse (alist, asvar) -> use_ex (execute_ex env alist) asvar (get_prop_id "backPointer") (* "B"
88      (*"backPointer"*) *) 
89  |  MQPattern (apreamble, apattern, afragid) ->
90      pattern_ex (apreamble, apattern, afragid)
91  |  MQUnion (l1, l2) -> union_ex (execute_ex env l1) (execute_ex env l2)
92  |  MQDiff (l1, l2) -> diff_ex (execute_ex env l1) (execute_ex env l2)
93  |  MQSortedBy (l, o, f) -> sortedby_ex (execute_ex env l) o f
94  |  MQIntersect (l1, l2) -> intersect_ex (execute_ex env l1) (execute_ex env l2)
95  |  MQListRVar rvar ->[List.assoc rvar env]
96  |  MQLetIn (lvar, l1, l2) -> 
97     let t = Unix.time () in
98       let res =
99        (*CSC: The interesting code *)
100        let _ = letin_ex lvar (execute_ex env l1) in
101         execute_ex env l2
102        (*CSC: end of the interesting code *)
103       in
104        letdispose ();
105        print_string ("LETIN = " ^ string_of_int (List.length res) ^ ": ") ;
106        print_endline (string_of_float (Unix.time () -. t) ^ "s") ;
107        flush stdout ;
108        res 
109  |  MQListLVar lvar -> letref_ex lvar
110  |  MQReference l -> 
111      let rec build_result = function
112        | [] -> []
113        | s :: tail ->
114        
115         let len = String.length s in
116          let s = (String.sub s 4 (len-4))(*^".xml"*) in
117           if String.contains s '#' then
118             let pos = String.index s '#' in
119              let s1 = Str.string_before s pos in
120               let xp = Str.string_after s pos in
121                let xp = Str.global_replace (Str.regexp "#xpointer(1") "" xp in
122                 let xp = Str.global_replace (Str.regexp "\/") "," xp in
123                  let xp = Str.global_replace (Str.regexp ")") "" xp in
124                   let s = (s1 ^ xp) in
125                     {uri = s ; attributes = [] ; extra = ""} :: build_result tail
126           else
127             {uri = s ; attributes = [] ; extra = ""} :: build_result tail
128      in build_result (List.sort compare l) 
129 ;;
130
131 (* Let's initialize the execute in Select, creating a cyclical recursion *)
132 Select.execute := execute_ex;;
133
134
135
136 (*exception ExecuteFunctionNotInitialized;;
137 let execute =
138  ref
139   (function _ -> raise ExecuteFunctionNotInitialized)
140 ;;
141
142
143
144 execute := execute_ex;;*)
145
146
147
148 (*
149  * converte il risultato interno di una query (uri + contesto)
150  * in un risultato di sole uri
151  *
152  * parametri:
153  * l: string list list;
154  *
155  * output: mqresult;
156  *
157  * note:
158  * il tipo del risultato mantenuto internamente e' diverso dal tipo di risultato
159  * restituito in output poiche', mentre chi effettua le query vuole come risultato
160  * solo le eventuali uri che soddisfano le query stesse, internamente ad una uri
161  * sono associati anche i valori delle variabili che ancora non sono state valutate
162  * perche', ad esempio, si trovano in altri rami dell'albero.
163  *
164  * Esempio:
165  * SELECT x IN USE PATTERN "cic:/**.con" POSITION $a WHERE $a IS MainConclusion
166  * L'albero corrispondente a questa query e':
167  *
168  *                  SELECT
169  *                /   |    \
170  *               x   USE    IS
171  *                  /   \    /\
172  *           PATTERN    $a  $a MainConclusion
173  *
174  * Nel momento in cui si esegue il ramo USE non sono noti i vincoli sullla variabile $a
175  * percui e' necessario considerare, oltre alle uri, i valori della variabile per i quali
176  * la uri puo' far parte del risultato.
177  *)
178
179  (* L.N.: prende una lista di attributed_uri e la trasforma in lista di
180   uri(stringhe) costruendola con il costruttore MQRefs *)
181 let xres_to_res l =
182  MQRefs (List.map (function {Mathql_semantics.uri = uri} -> uri) l)
183
184  
185  (*
186  let tmp = List.map (function {Mathql_semantics.uri = uri} -> uri) l in
187   MQRefs
188    (List.map
189     (function l ->
190       (*let _ = print_endline ("DEBUG: (mqint.ml: xres_to_res)" ^ l) in*)
191       match Str.split (Str.regexp ":\|#\|/\|(\|)") l with
192          hd::""::tl -> (
193           match List.rev tl with
194              n::"1"::"xpointer"::tail    ->
195               (
196                Some hd,
197                List.fold_left
198                 (fun par t ->
199                  match par with
200                     [] -> [MQBC t] 
201                  |  _  -> (MQBC t) :: MQBD :: par
202                 )
203                 []
204                 tail, 
205                [MQFC (int_of_string n)]
206               )
207           |  n::m::"1"::"xpointer"::tail ->
208               (
209                Some hd,
210                List.fold_left
211                 (fun par t ->
212                  match par with
213                     [] -> [MQBC t] 
214                  |  _  -> (MQBC t) :: MQBD :: par
215                 )
216                 []
217                 tail,
218                [MQFC (int_of_string m); MQFC (int_of_string n)]
219               )
220           |  tail                          ->
221               (
222                Some hd,
223                List.fold_left
224                 (fun par t ->
225                  match par with
226                     [] -> [MQBC t] 
227                  |  _  -> (MQBC t) :: MQBD :: par
228                 )
229                 []
230                 tail, 
231                []
232               )
233       )  
234        | _ -> assert false
235     )
236     tmp
237    )
238 *)
239 ;;
240
241
242 (*
243  * 
244  *)
245 let execute q =
246      
247  match q with
248    MQList qq -> try xres_to_res (execute_ex [] qq) with e -> Error.print_exn e; raise e
249 ;;
250
251 let init () = ();;
252 let close () = ();;