OCaml library to manipulate RDF graphs; implements SPARQL
Library rdf
Module Rdf . Graph


type options = (string * string) list
val get_option : ?def:string -> string -> options -> string

get_options name options returns the value associated to the option with the given name, in option list. If the option name is not found in the list, the function raises the Failure exception with a message about the missing option.

  • parameter def

    can be used to specify a default value; in this case, if the option name was not found in list, the default value is returned instead of raising Failure.

Creating storages

This is useful only to create your own storage.

module type Storage_BGP = sig ... end

Interface to query Basic Graph Patterns (BGP) in a graph. Here the term representation is abstracted, so that it can be for example an id in a database table, which will make triple matching and joining faster when matching a BGP by querying the real terms only for the result of the whole BGP, instead of retrieving terms and joining results of each triple.

module type Storage = sig ... end

A storage is a module with this interface.

exception Storage_error of string * string * exn

This is the exception raised by the module we get when applying Make on a storage.

Each call to a Storage function is embedded so that the Storage_error exception is raised when an error occurs in a storage function. The exception provides the name of the storage, the error message (obtained with Storage.string_of_error) and the original exception.

Refer to the documentation of Storage for information about the functions provided by the resulting module.

module Bid_map : Map.S with type key = Term.blank_id
module type Graph = sig ... end
module Make (S : Storage) : Graph with type g = S.g

Registering storages

val add_storage : (module Storage) -> unit

Add a storage to the list of registered storages.

type graph = {
name : unit -> Iri.t;
size : unit -> int;
add_triple : sub:Term.term -> pred:Iri.t -> obj:Term.term -> unit;
rem_triple : sub:Term.term -> pred:Iri.t -> obj:Term.term -> unit;
add_triple_t : Term.triple -> unit;
rem_triple_t : Term.triple -> unit;
subjects_of : pred:Iri.t -> obj:Term.term -> Term.term list;
predicates_of : sub:Term.term -> obj:Term.term -> Iri.t list;
objects_of : sub:Term.term -> pred:Iri.t -> Term.term list;
find : ?sub:Term.term -> ?pred:Iri.t -> ?obj:Term.term -> unit -> Term.triple list;
exists : ?sub:Term.term -> ?pred:Iri.t -> ?obj:Term.term -> unit -> bool;
exists_t : Term.triple -> bool;
subjects : unit -> Term.term list;
predicates : unit -> Iri.t list;
objects : unit -> Term.term list;
folder : unit -> Term.TSet.t Iri.Map.t Term.TMap.t option;
transaction_start : unit -> unit;
transaction_commit : unit -> unit;
transaction_rollback : unit -> unit;
new_blank_id : unit -> Term.blank_id;
namespaces : unit -> (Iri.t * string) list;
add_namespace : Iri.t -> string -> unit;
rem_namespace : string -> unit;
set_namespaces : (Iri.t * string) list -> unit;
bgp : (module Bgp.S);

This is the structure returned by open_graph. It contains the same functions as in Graph, except the graph data is hidden, like in a class interface. Refer to the documentation of Storage for information about the functions in the fields.

Graph creation

val open_graph : ?options:(string * string) list -> Iri.t -> graph

open_graph ~options iri_name creates a new graph. The storage used is specified by the "storage" option. For example, having ("storage", "mysql") in the options indicates to use the storage "mysql".

If the specified storage is not registered, the function raises Failure. Other options may be used by each storage.

To make sure the storage you want to use is registered, beware of linking the corresponding module in your executable, either by using the -linkall option or by adding a reference to the module in your code.

The "rdf" namespace is automatically added at creation time, associated to http://www.w3.org/1999/02/22-rdf-syntax-ns#.

val merge : ?map:( Term.term -> Term.term option ) -> graph -> graph -> unit

merge g1 g2 add triples from g2 to g1.

  • parameter map

    can be used to explicitely map terms from g2. When merging blank nodes, if map returns a term, then it is kept as is. Else the blank node is replaced by a fresh one. Default map function always returns None.

val only_iris : Term.term list -> Iri.t list
val only_literals : Term.term list -> Term.literal list
val iri_subjects_of : graph -> pred:Iri.t -> obj:Term.term -> Iri.t list
val iri_objects_of : graph -> sub:Term.term -> pred:Iri.t -> Iri.t list
val literal_objects_of : graph -> sub:Term.term -> pred:Iri.t -> Term.literal list
val subgraph_from : ?options:(string * string) list -> graph -> Term.term -> graph