]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIMap.ml
test branch
[helm.git] / helm / ocaml / mathql_interpreter / mQIMap.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 U = MQueryUtil
32
33 type pg_map = (MathQL.path * (bool * string * string option)) list
34
35 type pg_tables = (bool * string) list
36
37 type pg_alias = (string * string) list
38
39 let empty_map () = [], []
40
41 let read_map () =
42    let map = Helm_registry.get "mathql_interpreter.db_map" in
43    let ich = open_in map in 
44    let rec aux r s =
45       let d = input_line ich in 
46       match Str.split (Str.regexp "[ \t]+") d with
47          | []                  -> aux r s
48          | "#" :: _            -> aux r s
49          | t ::      "<-" :: p -> aux ((p, (false, t, None)) :: r) s 
50          | t :: c :: "<-" :: p -> aux ((p, (false, t, Some c)) :: r) s
51          | t ::      "<+" :: p -> aux ((p, (true, t, None)) :: r) s 
52          | t :: c :: "<+" :: p -> aux ((p, (true, t, Some c)) :: r) s
53          | [a; "->"; t]        -> aux r ((a, t) :: s) 
54          | ["->"]              -> r, s
55          | _                   -> raise (Failure "MQIMap.read_map")
56    in
57    let pgm, pga = aux [] [] in
58    close_in ich;
59    pgm, pga
60
61 let comp c1 c2 = match c1, c2 with
62    | (_, t1), (_, t2) when t1 < t2 -> U.Lt
63    | (_, t1), (_, t2) when t1 > t2 -> U.Gt
64    | (b1, t), (b2, _)              -> U.Eq (b1 || b2, t)
65
66 let get_tables pgm p =
67    let aux l = function
68       | q, (b, t, _) when q = p -> U.list_join comp l [(b, t)]
69       | _, _                    -> l
70     in
71     List.fold_left aux [] pgm  
72
73 let rec refine_tables l1 l2 = 
74    U.list_meet comp l1 l2
75       
76 let default_table = function
77    | [(_, a)] -> a
78    | l        -> 
79       try List.assoc true l 
80       with Not_found -> raise (Failure "MQIMap.default_table")
81
82 let get_field pgm p t =
83    let aux = function
84       | q, (_, u, _) when q = p && u = t -> true
85       | _                                -> false
86    in 
87    match List.filter aux pgm with
88       | [_, (_, _, None)]   -> "" 
89       | [_, (_, _, Some c)] -> c
90       | _                   -> raise (Failure "MQIMap.get_field")
91
92 let resolve pga a =
93    try List.assoc a pga with Not_found -> a