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