]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/relation.ml
Mathql_interpreter now using db helm_mowgli_new_schema.
[helm.git] / helm / ocaml / mathql_interpreter / relation.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://www.cs.unibo.it/helm/.
24  *)
25
26
27 (*
28  * implementazione del comando Relation 
29  *)
30
31
32
33
34 open Union;;
35 open Dbconn;;
36 open Utility;;
37
38
39
40
41 let get_prop_id propl =
42   let prop = fst propl in
43   if prop="refObj" then "F"
44   else if prop="backPointer" then "B"
45        else assert false
46 ;;
47
48
49 let relation_ex inv rop path rset attl =
50   let usek = get_prop_id path in
51   let vvar = if attl = [] then "position"
52              else List.hd attl
53   in       
54   let c = pgc () in
55   let rset_list =  (* lista di singoletti:resource_set di un elemento *)
56   (List.fold_left (fun acc (uri,l) ->
57     let tv = pgresult_to_string (c#exec ("select id from registry where uri='" ^ uri ^ "'")) in
58     let qq = "select uri, position from t" ^ tv ^ " where prop_id='" ^ usek ^ "' order by uri asc" in
59     print_endline ("@@@@LA QUERY:" ^ qq); flush stdout;
60     let res = c#exec qq in
61     (List.map
62     (function 
63          [uri;context] -> [(uri,[[((vvar, []),[context])]])]
64        | _ -> assert false ) 
65         res#get_list) @ acc
66                   )                   
67         [] rset                       
68   )
69   in                
70   let rec edup = function
71       [] -> []
72     | rs1::tl -> union_ex rs1 (edup tl) 
73   in 
74   edup rset_list 
75 ;;
76
77
78
79 (**** IMPLEMENTAZIONE DELLA RELATION PER GALAX ****)
80
81
82 (* trasforma un uri in un filename *)
83 let tofname uri =
84     if String.contains uri ':' then
85       (let len = String.length uri in
86        let scuri = String.sub uri 4 (len-4) in (*tolgo cic:*)
87        if String.contains scuri '#' then
88          (let pos = String.index scuri '#' in
89           let s1 = Str.string_before scuri pos in
90           let xp = Str.string_after scuri pos in
91           let xp = Str.global_replace (Str.regexp "#xpointer(1") "" xp in
92           let xp = Str.global_replace (Str.regexp "\/") "," xp in
93           let xp = Str.global_replace (Str.regexp ")") "" xp in
94           let fname = (s1 ^ xp) in
95           fname)
96        else
97          scuri)
98     else uri
99
100
101 (* prende una lista di uri (contenente alternativamente uri e pos) e costruisce una lista di resource *)
102 let rec rsetl uril vvar = 
103     match uril with                   
104     | uri::tl -> let scuri = (*tofname*) uri in
105                    [(scuri, [[((vvar, []), [(List.hd tl)])]])]::(rsetl (List.tl tl) vvar)
106     | [] -> [] 
107
108
109 (* prende una resource e una vvar e  restituisce la lista delle resource in relazione (refObj o backPointer in base al parametro "path") con tale resource e associa alla proprieta' il nome della vvar contenuto in "attl" *)
110 let muse path attl r =
111         let vvar = if attl = [] then "position"
112                    else List.hd attl
113         in          
114         let uri = fst r in
115         let furi = tofname uri in
116         let dtag = fst path in
117         let dir =
118           match dtag with
119               "refObj" -> "/projects/helm/metadata/create4/forward"
120             | _ -> "/projects/helm/metadata/create4/backward"
121         in 
122         let xq ="namespace h=\"http://www.cs.unibo.it/helm/schemas/mattone.rdf#\"
123                 namespace rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
124                 for $i in document(" ^ "\"" ^ dir ^ furi ^ ".xml" ^ "\"" ^
125                 ")/rdf:RDF/h:Object/h:" ^ dtag ^ "/h:Occurrence
126                 return ($i/h:occurrence, $i/h:position)"
127         
128         in
129         let uril = Toputils.eval_query_string xq in (* e' una lista di liste di stringhe*)
130         let  hd_uril = List.hd uril in(*prendo la testa che contiene altern. lista di uri e pos. *)
131         let rset_list = rsetl hd_uril vvar in (* da hd_uril costruisco una lista di resource_set(singoletti)*)
132         let rec edup = function
133             [] -> []
134           | rs1::tl -> union_ex rs1 (edup tl)
135         in
136         edup rset_list
137                       
138             
139
140
141 (* prende un resource_set, una vvar (primo el. di attl) a cui associare la posizione, e la relazione (refObj o backPointer) e per ogni resource chiama la muse 
142 NOTA: "rop" per ora non viene usato perche' vale sempre "ExactOp" *)
143 let relation_galax_ex inv rop path rset attl =
144     List.stable_sort (fun (uri1,l1) (uri2,l2) -> compare uri1 uri2) (List.concat (List.map (muse path attl) rset))
145
146