package rdf

  1. Overview
  2. Docs

Graph abstraction.

The graph provides an abstraction of the storage used (memory, database, ...). The graph is modified in place.

Example of usage:

let options =
  [
    "storage", "mysql" ;
    "database", "mydb";
    "user", "john" ;
  ]
in
let graph = Graph.open_graph ~options (Iri.iri "http://hello.fr") in
graph.add_triple
  ~sub: (Term.term_of_iri_string "http://john.net")
  ~pred: (Iri.iri "http://relations.org/hasMailbox")
  ~obj: (Term.term_of_literal_string "john\@john.net");
...

Options

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 : Stdlib.Map.S with type key = Rdf.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 = {
  1. name : unit -> Iri.t;
  2. size : unit -> int;
  3. add_triple : sub:Term.term -> pred:Iri.t -> obj:Term.term -> unit;
  4. rem_triple : sub:Term.term -> pred:Iri.t -> obj:Term.term -> unit;
  5. add_triple_t : Term.triple -> unit;
  6. rem_triple_t : Term.triple -> unit;
  7. subjects_of : pred:Iri.t -> obj:Term.term -> Term.term list;
  8. predicates_of : sub:Term.term -> obj:Term.term -> Iri.t list;
  9. objects_of : sub:Term.term -> pred:Iri.t -> Term.term list;
  10. find : ?sub:Term.term -> ?pred:Iri.t -> ?obj:Term.term -> unit -> Term.triple list;
  11. exists : ?sub:Term.term -> ?pred:Iri.t -> ?obj:Term.term -> unit -> bool;
  12. exists_t : Term.triple -> bool;
  13. subjects : unit -> Term.term list;
  14. predicates : unit -> Iri.t list;
  15. objects : unit -> Term.term list;
  16. folder : unit -> Rdf.Term.TSet.t Iri.Map.t Rdf.Term.TMap.t option;
  17. transaction_start : unit -> unit;
  18. transaction_commit : unit -> unit;
  19. transaction_rollback : unit -> unit;
  20. copy : unit -> graph;
  21. new_blank_id : unit -> Term.blank_id;
  22. namespaces : unit -> (Iri.t * string) list;
  23. add_namespace : Iri.t -> string -> unit;
  24. rem_namespace : string -> unit;
  25. set_namespaces : (Iri.t * string) list -> unit;
  26. 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
val to_list : graph -> Term.term -> Term.term list

to_list g t builds of list by following Rdf_.first and Rdf_.rest nodes in g, starting from term t.

val add_list : graph -> Term.term list -> Term.term

add_list g l adds blank nodes and triples to insert the given list into the graph, returning the blank node at the head of the list.

val types_of : graph -> Term.term -> Iri.t list

types_of g sub is a shorthand for iri_objects_of g ~sub ~pred:Rdf_.type_.

val root_opt : graph -> Term.term option

root_opt g returns the first root term found in the graph, i.e. a node present in the graph which does not appear as object of a triple. If there are more than one root node, only the first one is returned. If the search falls into a cycle, a node of the cycle is returned. If the graph is empty, None is returned.

val root : graph -> Term.term

Same as root_opt but in case no root is found, return the name of the graph.

type diff =
  1. | Cardinals of int * int
  2. | Missing_triple of Term.triple
  3. | Extra_triple of Term.triple

Differences when checking if two graphs are isomorphic.

val string_of_diff : diff -> string
val pp_diff : Stdlib.Format.formatter -> diff -> unit
val isomorphic_diff : ?ignore_blanks:bool -> graph -> graph -> diff option

Return None when the two given graphs are isomorphic or the first difference.