]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIProperty.ml
test branch
[helm.git] / helm / ocaml / mathql_interpreter / mQIProperty.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 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 (* $Id$ *)
30
31 module M = MathQL
32 module C = MQIConn
33 module U = MQIUtil
34 module A = MQIMap
35
36 let not_supported s = 
37    raise (Failure ("MQIProperty: feature not supported: " ^ s)) 
38
39 (* debugging  ***************************************************************)
40
41 let pg_print l =
42    let rec pg_record = function 
43       | []           -> prerr_newline ()
44       | head :: tail -> prerr_string (head ^ " "); pg_record tail
45    in
46    List.iter pg_record l
47
48 let cl_print l =  
49    let c_print (b, p, v) =
50       prerr_string (if b then "match " else "in ");
51       List.iter (fun x -> prerr_string ("/" ^ x)) p;
52       prerr_newline ();
53       List.iter prerr_endline v
54    in
55    List.iter c_print l
56
57 (* Common functions  ********************************************************)
58
59 let pg_result distinct subj el res =
60   let compose = if distinct then List.map else fun f -> U.mql_iter (fun x -> [f x]) in 
61   let get_name = function (p, None) -> p | (_, Some p) -> p in
62   let names = List.map get_name el in
63   let mk_grp l = 
64      let grp = U.mql_iter2 (fun p s -> [(p, [s])]) names l in 
65      if grp = [] then [] else [grp]
66   in
67   let mk_avs l =
68      if subj = "" then ("", mk_grp l) else (List.hd l, mk_grp (List.tl l))
69   in
70   compose mk_avs res
71
72 let get_table h mc ct cfl el =
73    let aux_c ts (_, p, _) = A.refine_tables ts (C.tables h p) in
74    let aux_e ts (p, _) = A.refine_tables ts (C.tables h p) in
75    let fst = C.tables h mc in
76    let snd = List.fold_left aux_c fst (ct @ (List.concat cfl)) in
77    let trd = List.fold_left aux_e snd el in
78    A.default_table trd
79
80 let exec_single h mc ct cfl el table =
81    let conv p = C.field h p table in
82    let first = conv mc in
83    let mk_con l = List.map (fun (pat, p, v) -> (pat, conv p, v)) l in
84    let cons_true = mk_con ct in 
85    let cons_false = List.map mk_con cfl in
86    let other_cols = List.map (fun (p, _) -> conv p) el in 
87    let cols = if first = "" then other_cols else first :: other_cols in
88    let out q = if C.set h C.Queries then C.log h (q ^ "\n") in
89    let r = C.exec h out (C.resolve h table) cols cons_true cons_false in
90    pg_result false first el r
91    
92 let deadline = 100
93
94 let exec h refine mc ct cfl el =
95    if refine <> M.RefineExact then not_supported "exec";   
96    let table = get_table h mc ct cfl el in
97    let rec exec_aux ct = match ct with 
98       | (pat, p, v) :: tail when List.length v > deadline ->
99          let single s = exec_aux ((pat, p, [s]) :: tail) in
100          U.mql_iter single v
101       | _                                                 ->
102          exec_single h mc ct cfl el table
103    in exec_aux ct