]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIProperty.ml
MathQL 1.3 ready for use
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.ml
1
2 (* Copyright (C) 2000, HELM Team.
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 module M = MathQL
28 module P = MQIPostgres
29 module C = MQIConn
30 module U = MQIUtil
31
32 let not_supported s = 
33    raise (Failure ("MQIProperty: feature not supported: " ^ s)) 
34
35 (* debugging  ***************************************************************)
36
37 let pg_print l =
38    let rec pg_record = function 
39       | []           -> prerr_newline ()
40       | head :: tail -> prerr_string (head ^ " "); pg_record tail
41    in
42    List.iter pg_record l
43
44 let cl_print l =  
45    let c_print (b, p, v) =
46       prerr_string (if b then "match " else "in ");
47       List.iter (fun x -> prerr_string ("/" ^ x)) p;
48       prerr_newline ();
49       List.iter prerr_endline v
50    in
51    List.iter c_print l
52
53 (* PostgreSQL backend  ******************************************************)
54
55 let pg_query h table cols ct cfl =
56    let exec q = 
57       if C.set h C.Log then C.log h (q ^ "\n");
58       P.exec (C.pgc h) q 
59    in
60    let rec iter f sep = function
61       | []           -> ""
62       | [head]       -> f head 
63       | head :: tail -> f head ^ sep ^ iter f sep tail
64    in
65    let pg_cols = iter (fun x -> x) ", " cols in
66    let pg_msval v = iter P.quote ", " v in
67    let pg_con (pat, col, v) = 
68       if col <> "" then 
69          let f s = col ^ " ~ " ^ P.quote ("^" ^ s ^ "$") in
70          if pat then "(" ^ iter f " or " v ^ ")"
71          else match v with 
72             | [s] -> col ^ " = " ^ (P.quote s)     
73             | v   -> col ^ " in (" ^ pg_msval v ^ ")"
74       else "true"
75    in
76    let pg_cons l = iter pg_con " and " l in
77    let pg_cons_not l = "not (" ^ pg_cons l ^ ")" in
78    let pg_cons_not_l ll = iter pg_cons_not " and " ll in
79    let pg_where = match ct, cfl with
80       | [], []  -> ""
81       | lt, []  -> " where " ^ pg_cons lt
82       | [], llf -> " where " ^ pg_cons_not_l llf
83       | lt, llf -> " where " ^ pg_cons lt ^ " and " ^ pg_cons_not_l llf
84    in
85    if cols = [] then
86       let r = exec ("select count (source) from " ^ table ^ pg_where) in
87       match r with
88          | [[s]] when int_of_string s > 0 -> [[]]
89          | _                              -> []
90    else
91       exec ("select " ^ pg_cols ^ " from " ^ table ^ pg_where ^ 
92             " order by " ^ List.hd cols ^ " asc")
93
94 (* Galax backend  ***********************************************************)
95
96 let gx_query h table cols ct cfl = not_supported "Galax"
97
98 (* Common functions  ********************************************************)
99
100 let pg_result distinct subj el res =
101   let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
102   let get_name = function (p, None) -> p | (_, Some p) -> p in
103   let names = List.map get_name el in
104   let mk_grp l = 
105      let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in 
106      if grp = [] then [] else [grp]
107   in
108   let mk_avs l =
109      if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
110   in
111   compose mk_avs res
112
113 let get_table mc ct cfl el =
114    let fst = function
115       | []           -> None
116       | head :: tail -> Some head
117    in
118    let comp n1 n2 = match n1, n2 with
119       | None, _                   -> n2 
120       | Some h, Some k when h = k -> n2
121       | _, None                   -> n1
122       | _                         -> not_supported "comp"
123    in   
124    let rec get_c prev = function
125       | []                -> prev
126       | (_, p, _) :: tail -> get_c (comp prev (fst p)) tail
127    in
128    let rec get_e prev = function
129       | []             -> prev
130       | (p, _) :: tail -> get_e (comp prev (fst p)) tail
131    in
132    get_e (get_c (get_c (fst mc) ct) (List.concat cfl)) el  
133
134 let rec decolon s =
135    let l = String.length s in
136    try 
137       let i = String.index s ':' in
138       String.sub s 0 i ^ "_" ^ decolon (String.sub s (succ i) (l - succ i))
139    with Not_found -> s
140
141 let conv = function
142    | []             -> "source"
143    | ["objectName"] -> "value" 
144    | [t]            -> ""
145    | [_; t]         -> decolon t
146    | _              -> not_supported "conv" 
147
148 let exec_single h mc ct cfl el t =
149    let table = match t with Some t -> decolon t | None -> "objectName" in
150    let first = conv mc in
151    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
152    let cons_true = mk_con ct in 
153    let cons_false = List.map mk_con cfl in
154    let other_cols = List.map (fun (p, _) -> conv p) el in 
155    let cols = if first = "" then other_cols else first :: other_cols in
156    let low_level = if C.set h C.Galax then gx_query else pg_query in
157    pg_result false first el (low_level h table cols cons_true cons_false) 
158    
159 let deadline = 100
160
161 let exec h refine mc ct cfl el =
162    if refine <> M.RefineExact then not_supported "exec";   
163    let table = get_table mc ct cfl el in
164    let rec exec_aux ct = match ct with 
165       | (pat, p, v) :: tail when List.length v > deadline ->
166          let single s = exec_aux ((pat, p, [s]) :: tail) in
167          U.mql_iter single v
168       | _                                                 ->
169          exec_single h mc ct cfl el table
170    in exec_aux ct        
171
172 (* funzioni vecchie  ********************************************************)
173
174 let pg_name h s = 
175    let q = "select id from registry where uri = " ^ P.quote s in
176    match P.exec h q with
177       | [[id]] -> "t" ^ id
178       | _      -> ""
179
180 let get_id b = if b then ["B"] else ["F"]