]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/mQueryUtil.ml
structurated attribute names added
[helm.git] / helm / ocaml / mathql / mQueryUtil.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 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                     Ferruccio Guidi <fguidi@cs.unibo.it>                   *)
31 (*                                 30/04/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36
37 (* text linearization and parsing *******************************************)
38
39 let rec txt_list f s = function
40    | []        -> ""
41    | [a]       -> f a
42    | a :: tail -> f a ^ s ^ txt_list f s tail
43    
44 let txt_str s = "\"" ^ s ^ "\""
45
46 let txt_path (p0, p1) =
47    txt_str p0 ^ (if p1 <> [] then "/" ^ txt_list txt_str "/" p1 else "")
48
49 let text_of_query x =
50    let module M = MathQL in
51    let txt_svar sv = "%" ^ sv in
52    let txt_rvar rv = "@" ^ rv in
53    let txt_vvar vv = "$" ^ vv in
54    let txt_inv i = if i then "inverse " else "" in
55    let txt_ref = function
56       | M.RefineExact -> ""
57       | M.RefineSub   -> "sub "
58       | M.RefineSuper -> "super "
59    in
60    let txt_refpath i r p = txt_inv i ^ txt_ref r ^ txt_path p ^ " " in
61    let rec txt_val = function
62       | M.Const [s]              -> txt_str s
63       | M.Const l                -> "{" ^ txt_list txt_str ", " l ^ "}"
64       | M.VVar vv                -> txt_vvar vv
65       | M.Record (rv, p)         -> txt_rvar rv ^ "." ^ txt_path p
66       | M.Fun (s, x)             -> "fun " ^ txt_str s ^ " " ^ txt_val x
67       | M.Attribute (i, r, p, x) -> "attribute " ^ txt_refpath i r p ^ txt_val x
68       | M.RefOf x                -> "refof " ^ txt_set x
69    and txt_boole = function
70       | M.False       -> "false"
71       | M.True        -> "true"
72       | M.Ex b x      -> "ex " ^ txt_boole x
73 (*    | M.Ex b x      -> "ex [" ^ txt_list txt_rvar "," b ^ "] " ^ txt_boole x
74 *)    | M.Not x       -> "not " ^ txt_boole x
75       | M.And (x, y)  -> "(" ^ txt_boole x ^ " and " ^ txt_boole y ^ ")"
76       | M.Or (x, y)   -> "(" ^ txt_boole x ^ " or " ^ txt_boole y ^ ")"
77       | M.Sub (x, y)  -> "(" ^ txt_val x ^ " sub " ^ txt_val y ^ ")"
78       | M.Meet (x, y) -> "(" ^ txt_val x ^ " meet " ^ txt_val y ^ ")"
79       | M.Eq (x, y)   -> "(" ^ txt_val x ^ " eq " ^ txt_val y ^ ")"
80    and txt_set = function
81       | M.SVar sv                   -> txt_svar sv
82       | M.RVar rv                   -> txt_rvar rv
83       | M.Relation (i, r, p, x, []) -> "relation " ^ txt_refpath i r p ^ txt_set x
84       | M.Relation (i, r, p, x, l)  -> "relation " ^ txt_refpath i r p ^ txt_set x ^ " attr " ^ txt_list txt_str ", " l
85       | M.Union (x, y)              -> "(" ^ txt_set x ^ " union " ^ txt_set y ^ ")"
86       | M.Intersect (x, y)          -> "(" ^ txt_set x ^ " intersect " ^ txt_set y ^ ")"
87       | M.Diff (x, y)               -> "(" ^ txt_set x ^ " diff " ^ txt_set y ^ ")"
88       | M.LetSVar (sv, x, y)        -> "let " ^ txt_svar sv ^ " be " ^ txt_set x ^ " in " ^ txt_set y
89       | M.LetVVar (vv, x, y)        -> "let " ^ txt_vvar vv ^ " be " ^ txt_val x ^ " in " ^ txt_set y
90       | M.Select (rv, x, y)         -> "select " ^ txt_rvar rv ^ " in " ^ txt_set x ^ " where " ^ txt_boole y
91       | M.Pattern x                 -> "pattern " ^ txt_val x
92       | M.Ref x                     -> "ref " ^ txt_val x
93    in 
94    txt_set x
95
96 let text_of_result x sep =
97    let txt_attr = function
98       | (p, []) -> txt_path p
99       | (p, l)  -> txt_path p ^ " = " ^ txt_list txt_str ", " l
100    in
101    let txt_group l = "{" ^ txt_list txt_attr "; " l ^ "}" in
102    let txt_res = function
103       | (s, []) -> txt_str s 
104       | (s, l)  -> txt_str s ^ " attr " ^ txt_list txt_group ", " l
105    in   
106    let txt_set l = txt_list txt_res ("; " ^ sep) l ^ sep in
107    txt_set x
108
109 let query_of_text lexbuf =
110    MQueryTParser.query MQueryTLexer.query_token lexbuf 
111
112 let result_of_text lexbuf =
113    MQueryTParser.result MQueryTLexer.result_token lexbuf 
114
115 (* conversion functions *****************************************************)
116
117 type uriref = UriManager.uri * (int list)
118
119 let string_of_uriref (uri, fi) =
120    let module UM = UriManager in
121    let str = UM.string_of_uri uri in
122    let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
123    match fi with
124       | []          -> str 
125       | [t]         -> str ^ xp t ^ ")" 
126       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"