]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/mathql_interpreter/mQIProperty.ml
test branch
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.ml
diff --git a/helm/ocaml/mathql_interpreter/mQIProperty.ml b/helm/ocaml/mathql_interpreter/mQIProperty.ml
new file mode 100644 (file)
index 0000000..0e3e2be
--- /dev/null
@@ -0,0 +1,103 @@
+(* Copyright (C) 2000, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://cs.unibo.it/helm/.
+ *)
+
+(*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
+ *)
+
+(* $Id$ *)
+
+module M = MathQL
+module C = MQIConn
+module U = MQIUtil
+module A = MQIMap
+
+let not_supported s = 
+   raise (Failure ("MQIProperty: feature not supported: " ^ s)) 
+
+(* debugging  ***************************************************************)
+
+let pg_print l =
+   let rec pg_record = function 
+      | []           -> prerr_newline ()
+      | head :: tail -> prerr_string (head ^ " "); pg_record tail
+   in
+   List.iter pg_record l
+
+let cl_print l =  
+   let c_print (b, p, v) =
+      prerr_string (if b then "match " else "in ");
+      List.iter (fun x -> prerr_string ("/" ^ x)) p;
+      prerr_newline ();
+      List.iter prerr_endline v
+   in
+   List.iter c_print l
+
+(* Common functions  ********************************************************)
+
+let pg_result distinct subj el res =
+  let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
+  let get_name = function (p, None) -> p | (_, Some p) -> p in
+  let names = List.map get_name el in
+  let mk_grp l = 
+     let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in 
+     if grp = [] then [] else [grp]
+  in
+  let mk_avs l =
+     if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
+  in
+  compose mk_avs res
+
+let get_table h mc ct cfl el =
+   let aux_c ts (_, p, _) = A.refine_tables ts (C.tables h p) in
+   let aux_e ts (p, _) = A.refine_tables ts (C.tables h p) in
+   let fst = C.tables h mc in
+   let snd = List.fold_left aux_c fst (ct @ (List.concat cfl)) in
+   let trd = List.fold_left aux_e snd el in
+   A.default_table trd
+
+let exec_single h mc ct cfl el table =
+   let conv p = C.field h p table in
+   let first = conv mc in
+   let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
+   let cons_true = mk_con ct in 
+   let cons_false = List.map mk_con cfl in
+   let other_cols = List.map (fun (p, _) -> conv p) el in 
+   let cols = if first = "" then other_cols else first :: other_cols in
+   let out q = if C.set h C.Queries then C.log h (q ^ "\n") in
+   let r = C.exec h out (C.resolve h table) cols cons_true cons_false in
+   pg_result false first el r
+   
+let deadline = 100
+
+let exec h refine mc ct cfl el =
+   if refine <> M.RefineExact then not_supported "exec";   
+   let table = get_table h mc ct cfl el in
+   let rec exec_aux ct = match ct with 
+      | (pat, p, v) :: tail when List.length v > deadline ->
+         let single s = exec_aux ((pat, p, [s]) :: tail) in
+        U.mql_iter single v
+      | _                                                 ->
+         exec_single h mc ct cfl el table
+   in exec_aux ct