Source file ds.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(*********************************************************************************)
(*                OCaml-RDF                                                      *)
(*                                                                               *)
(*    Copyright (C) 2012-2021 Institut National de Recherche en Informatique     *)
(*    et en Automatique. All rights reserved.                                    *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU General Public License          *)
(*    along with this program; if not, write to the Free Software                *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** *)

module Irimap = Iri.Map
module Iriset = Iri.Set

exception Could_not_retrieve_graph of Iri.t * string

let () = Printexc.register_printer
  (function
   | Could_not_retrieve_graph (iri,s) ->
       Some (Printf.sprintf "Could not retrieve graph %s: %s" (Iri.to_string iri) s)
   | _ -> None)

let could_not_retrieve_graph iri msg =
  raise (Could_not_retrieve_graph (iri, msg))
;;

type dataset =
  { default : Graph.graph ;
    named : Iriset.t ;
    get_named : Iri.t -> Graph.graph ;
  }

let simple_dataset ?(named=[]) default =
  let named_set = List.fold_left (fun set (iri,_) -> Iriset.add iri set) Iriset.empty named in
  let named = List.fold_left (fun map (iri,g) -> Irimap.add iri g map) Irimap.empty named in
  let get_named iri =
    try Irimap.find iri named
    with Not_found ->
        could_not_retrieve_graph iri
          ("Unknown graph "^(Iri.to_string iri))
  in
  { default ; named = named_set ; get_named }
;;

let dataset ?get_named ?(named=Iriset.empty) default =
  match get_named with
    None -> simple_dataset default
  | Some get_named -> { default ; named ; get_named }
;;