RDF.Dataset (RDF.ex v0.9.4) View Source

A set of RDF.Graphs.

It may have multiple named graphs and at most one unnamed ("default") graph.

RDF.Dataset implements:

Link to this section Summary

Functions

Adds triples and quads to a RDF.Dataset.

Changes the dataset name of dataset.

The default graph of a RDF.Dataset.

Deletes statements from a RDF.Dataset.

Deletes the default graph.

Deletes the given graph.

Checks if a graph of a RDF.Dataset contains statements about the given resource.

Checks if two RDF.Datasets are equal.

Fetches the RDF.Graph with the given name.

Fetches the RDF.Graph with the given name.

Gets and updates the graph with the given name, in a single pass.

The graph with given name.

The set of all graphs.

Checks if the given input statements exist within dataset.

Returns a nested map of a RDF.Dataset where each element from its quads is mapped with the given function.

Returns the dataset name IRI of dataset.

Creates an empty unnamed RDF.Dataset.

Creates an RDF.Dataset initialized with data.

The set of all resources used in the objects within a RDF.Dataset.

Pops an arbitrary statement from a RDF.Dataset.

Pops the graph with the given name.

The set of all properties used in the predicates within all graphs of a RDF.Dataset.

Returns the aggregated prefixes of all graphs of dataset as a RDF.PrefixMap.

Adds statements to a RDF.Dataset overwriting existing statements with the subjects given in the input data.

Adds statements to a RDF.Dataset and overwrites all existing statements with the same subject-predicate combinations given in the input data.

The set of all resources used within a RDF.Dataset.

The number of statements within a RDF.Dataset.

All statements within all graphs of a RDF.Dataset.

The set of all subjects used in the statement within all graphs of a RDF.Dataset.

Returns a nested map of the native Elixir values of a RDF.Dataset.

Returns the names of all graphs of a RDF.Dataset containing statements about the given subject.

Link to this section Types

Specs

graph_name() :: RDF.IRI.t() | nil

Specs

input() :: RDF.Graph.input() | t()

Specs

t() :: %RDF.Dataset{
  graphs: %{required(graph_name()) => RDF.Graph.t()},
  name: graph_name()
}

Specs

update_graph_fun() :: (RDF.Graph.t() -> {RDF.Graph.t(), input()} | :pop)

Link to this section Functions

Link to this function

add(dataset, input, opts \\ [])

View Source

Specs

add(t(), input(), keyword()) :: t()

Adds triples and quads to a RDF.Dataset.

The triples can be provided in any form accepted by add/2.

The graph option allows to set a different destination graph to which the statements should be added, ignoring the graph context of given quads or the name of given graphs in input.

Note: When the statements to be added are given as another RDF.Dataset and a destination graph is set with the graph option, the descriptions of the subjects in the different graphs are aggregated.

Link to this function

change_name(dataset, new_name)

View Source

Specs

change_name(t(), RDF.Statement.coercible_graph_name()) :: t()

Changes the dataset name of dataset.

Specs

default_graph(t()) :: RDF.Graph.t()

The default graph of a RDF.Dataset.

Link to this function

delete(dataset, input, opts \\ [])

View Source

Specs

delete(t(), input(), keyword()) :: t()

Deletes statements from a RDF.Dataset.

The graph option allows to set a different destination graph from which the statements should be deleted, ignoring the graph context of given quads or the name of given graphs.

Note: When the statements to be deleted are given as another RDF.Dataset, the dataset name must not match dataset name of the dataset from which the statements are deleted. If you want to delete only datasets with matching names, you can use RDF.Data.delete/2.

Link to this function

delete_default_graph(graph)

View Source

Specs

delete_default_graph(t()) :: t()

Deletes the default graph.

Link to this function

delete_graph(graph, graph_names)

View Source

Specs

delete_graph(
  t(),
  RDF.Statement.graph_name() | [RDF.Statement.graph_name()] | nil
) :: t()

Deletes the given graph.

Link to this function

describes?(dataset, subject, graph_context \\ nil)

View Source

Specs

Checks if a graph of a RDF.Dataset contains statements about the given resource.

Examples

  iex> RDF.Dataset.new([{EX.S1, EX.p1, EX.O1}]) |> RDF.Dataset.describes?(EX.S1)
  true
  iex> RDF.Dataset.new([{EX.S1, EX.p1, EX.O1}]) |> RDF.Dataset.describes?(EX.S2)
  false
Link to this function

equal?(dataset1, dataset2)

View Source

Specs

equal?(t() | any(), t() | any()) :: boolean()

Checks if two RDF.Datasets are equal.

Two RDF.Datasets are considered to be equal if they contain the same triples and have the same name.

Link to this function

fetch(dataset, graph_name)

View Source

Specs

fetch(t(), RDF.Statement.graph_name() | nil) :: {:ok, RDF.Graph.t()} | :error

Fetches the RDF.Graph with the given name.

When a graph with the given name can not be found can not be found :error is returned.

Examples

iex> dataset = RDF.Dataset.new([{EX.S1, EX.P1, EX.O1, EX.Graph}, {EX.S2, EX.P2, EX.O2}])
...> RDF.Dataset.fetch(dataset, EX.Graph)
{:ok, RDF.Graph.new({EX.S1, EX.P1, EX.O1}, name: EX.Graph)}
iex> RDF.Dataset.fetch(dataset, nil)
{:ok, RDF.Graph.new({EX.S2, EX.P2, EX.O2})}
iex> RDF.Dataset.fetch(dataset, EX.Foo)
:error
Link to this function

get(dataset, graph_name, default \\ nil)

View Source

Specs

get(t(), RDF.Statement.graph_name() | nil, RDF.Graph.t() | nil) ::
  RDF.Graph.t() | nil

Fetches the RDF.Graph with the given name.

When a graph with the given name can not be found can not be found the optionally given default value or nil is returned

Examples

iex> dataset = RDF.Dataset.new([{EX.S1, EX.P1, EX.O1, EX.Graph}, {EX.S2, EX.P2, EX.O2}])
...> RDF.Dataset.get(dataset, EX.Graph)
RDF.Graph.new({EX.S1, EX.P1, EX.O1}, name: EX.Graph)
iex> RDF.Dataset.get(dataset, nil)
RDF.Graph.new({EX.S2, EX.P2, EX.O2})
iex> RDF.Dataset.get(dataset, EX.Foo)
nil
iex> RDF.Dataset.get(dataset, EX.Foo, :bar)
:bar
Link to this function

get_and_update(dataset, graph_name, fun)

View Source

Specs

get_and_update(t(), RDF.Statement.graph_name() | nil, update_graph_fun()) ::
  {RDF.Graph.t(), input()}

Gets and updates the graph with the given name, in a single pass.

Invokes the passed function on the RDF.Graph with the given name; this function should return either {graph_to_return, new_graph} or :pop.

If the passed function returns {graph_to_return, new_graph}, the return value of get_and_update is {graph_to_return, new_dataset} where new_dataset is the input Dataset updated with new_graph for the given name.

If the passed function returns :pop the graph with the given name is removed and a {removed_graph, new_dataset} tuple gets returned.

Examples

iex> dataset = RDF.Dataset.new({EX.S, EX.P, EX.O, EX.Graph})
...> RDF.Dataset.get_and_update(dataset, EX.Graph, fn current_graph ->
...>     {current_graph, {EX.S, EX.P, EX.NEW}}
...>   end)
{RDF.Graph.new({EX.S, EX.P, EX.O}, name: EX.Graph), RDF.Dataset.new({EX.S, EX.P, EX.NEW, EX.Graph})}
Link to this function

graph(dataset, graph_name)

View Source

Specs

graph(t(), RDF.Statement.graph_name() | nil) :: RDF.Graph.t()

The graph with given name.

Specs

graphs(t()) :: [RDF.Graph.t()]

The set of all graphs.

Link to this function

include?(dataset, input, opts \\ [])

View Source

Specs

include?(t(), input(), keyword()) :: boolean()

Checks if the given input statements exist within dataset.

The graph option allows to set a different destination graph in which the statements should be checked, ignoring the graph context of given quads or the name of given graphs.

Examples

  iex> dataset = RDF.Dataset.new([
  ...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
  ...>   {EX.S2, EX.p2, EX.O2},
  ...>   {EX.S1, EX.p2, EX.O3}])
  ...> RDF.Dataset.include?(dataset, {EX.S1, EX.p1, EX.O1, EX.Graph})
  true

Specs

Returns a nested map of a RDF.Dataset where each element from its quads is mapped with the given function.

The function fun will receive a tuple {statement_position, rdf_term} where statement_position is one of the atoms :subject, :predicate, :object or :graph_name while rdf_term is the RDF term to be mapped. When the given function returns nil this will be interpreted as an error and will become the overhaul result of the map/2 call.

Examples

iex> [
...>   {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo", ~I<http://example.com/Graph>},
...>   {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.XSD.integer(42), }
...> ]
...> |> RDF.Dataset.new()
...> |> RDF.Dataset.map(fn
...>      {:graph_name, graph_name} ->
...>        graph_name
...>      {:predicate, predicate} ->
...>        predicate
...>        |> to_string()
...>        |> String.split("/")
...>        |> List.last()
...>        |> String.to_atom()
...>    {_, term} ->
...>      RDF.Term.value(term)
...>    end)
%{
  ~I<http://example.com/Graph> => %{
    "http://example.com/S" => %{p: ["Foo"]}
  },
  nil => %{
    "http://example.com/S" => %{p: [42]}
  }
}

Specs

Returns the dataset name IRI of dataset.

Specs

new() :: t()

Creates an empty unnamed RDF.Dataset.

Specs

new(input() | keyword()) :: t()

Creates an RDF.Dataset.

If a keyword list is given an empty dataset is created. Otherwise an unnamed dataset initialized with the given data is created.

See new/2 for available arguments and the different ways to provide data.

Examples

RDF.Dataset.new(name: EX.GraphName)

RDF.Dataset.new(init: {EX.S, EX.p, EX.O})

RDF.Dataset.new({EX.S, EX.p, EX.O})

Specs

new(input(), keyword()) :: t()

Creates an RDF.Dataset initialized with data.

The initial RDF triples can be provided in any form accepted by add/3.

Available options:

  • name: the name of the dataset to be created
  • init: some data with which the dataset should be initialized; the data can be provided in any form accepted by add/3 and above that also with a function returning the initialization data in any of these forms

The set of all resources used in the objects within a RDF.Dataset.

Note: This function does collect only IRIs and BlankNodes, not Literals.

Examples

iex> RDF.Dataset.new([
...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
...>   {EX.S2, EX.p2, EX.O2, EX.Graph},
...>   {EX.S3, EX.p1, EX.O2},
...>   {EX.S4, EX.p2, RDF.bnode(:bnode)},
...>   {EX.S5, EX.p3, "foo"}
...> ]) |> RDF.Dataset.objects
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])

Specs

pop(t()) :: {RDF.Statement.t() | nil, t()}

Pops an arbitrary statement from a RDF.Dataset.

Link to this function

pop(dataset, graph_name)

View Source

Specs

Pops the graph with the given name.

When a graph with given name can not be found the optionally given default value or nil is returned.

Examples

iex> dataset = RDF.Dataset.new([
...>   {EX.S1, EX.P1, EX.O1, EX.Graph},
...>   {EX.S2, EX.P2, EX.O2}])
...> RDF.Dataset.pop(dataset, EX.Graph)
{RDF.Graph.new({EX.S1, EX.P1, EX.O1}, name: EX.Graph), RDF.Dataset.new({EX.S2, EX.P2, EX.O2})}
iex> RDF.Dataset.pop(dataset, EX.Foo)
{nil, dataset}

The set of all properties used in the predicates within all graphs of a RDF.Dataset.

Examples

iex> RDF.Dataset.new([
...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
...>   {EX.S2, EX.p2, EX.O2},
...>   {EX.S1, EX.p2, EX.O3}]) |>
...>   RDF.Dataset.predicates
MapSet.new([EX.p1, EX.p2])

Specs

prefixes(t()) :: RDF.PrefixMap.t() | nil

Returns the aggregated prefixes of all graphs of dataset as a RDF.PrefixMap.

Link to this function

put(dataset, input, opts \\ [])

View Source

Specs

put(t(), input(), keyword()) :: t()

Adds statements to a RDF.Dataset overwriting existing statements with the subjects given in the input data.

The graph option allows to set a different destination graph to which the statements should be added, ignoring the graph context of given quads or the name of given graphs in input.

Note: When the statements to be added are given as another RDF.Dataset and a destination graph is set with the graph option, the descriptions of the subjects in the different graphs are aggregated.

Examples

iex> dataset = RDF.Dataset.new({EX.S, EX.P1, EX.O1})
...> RDF.Dataset.put(dataset, {EX.S, EX.P2, EX.O2})
RDF.Dataset.new({EX.S, EX.P2, EX.O2})
iex> RDF.Dataset.put(dataset, {EX.S2, EX.P2, EX.O2})
RDF.Dataset.new([{EX.S, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}])
Link to this function

put_properties(dataset, input, opts \\ [])

View Source

Specs

put_properties(t(), input(), keyword()) :: t()

Adds statements to a RDF.Dataset and overwrites all existing statements with the same subject-predicate combinations given in the input data.

The graph option allows to set a different destination graph to which the statements should be added, ignoring the graph context of given quads or the name of given graphs in input.

Note: When the statements to be added are given as another RDF.Dataset and a destination graph is set with the graph option, the descriptions of the subjects in the different graphs are aggregated.

Examples

iex> dataset = RDF.Dataset.new({EX.S, EX.P1, EX.O1})
...> RDF.Dataset.put_properties(dataset, {EX.S, EX.P1, EX.O2})
RDF.Dataset.new({EX.S, EX.P1, EX.O2})
iex> RDF.Dataset.put_properties(dataset, {EX.S, EX.P2, EX.O2})
RDF.Dataset.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
iex> RDF.Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}])
...> |> RDF.Dataset.put_properties([{EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])
RDF.Dataset.new([{EX.S1, EX.P1, EX.O1}, {EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])

The set of all resources used within a RDF.Dataset.

Examples

iex> RDF.Dataset.new([ ...> {EX.S1, EX.p1, EX.O1, EX.Graph}, ...> {EX.S2, EX.p1, EX.O2, EX.Graph}, ...> {EX.S2, EX.p2, RDF.bnode(:bnode)}, ...> {EX.S3, EX.p1, "foo"} ...> ]) |> RDF.Dataset.resources MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2), RDF.iri(EX.S3),

RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode), EX.p1, EX.p2])
Link to this function

statement_count(dataset)

View Source

Specs

statement_count(t()) :: non_neg_integer()

The number of statements within a RDF.Dataset.

Examples

iex> RDF.Dataset.new([
...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
...>   {EX.S2, EX.p2, EX.O2},
...>   {EX.S1, EX.p2, EX.O3}]) |>
...>   RDF.Dataset.statement_count
3

Specs

statements(t()) :: [RDF.Statement.t()]

All statements within all graphs of a RDF.Dataset.

Examples

  iex> RDF.Dataset.new([
  ...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
  ...>   {EX.S2, EX.p2, EX.O2},
  ...>   {EX.S1, EX.p2, EX.O3}]) |>
  ...>   RDF.Dataset.statements
  [{RDF.iri(EX.S1), RDF.iri(EX.p2), RDF.iri(EX.O3)},
   {RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)},
   {RDF.iri(EX.S1), RDF.iri(EX.p1), RDF.iri(EX.O1), RDF.iri(EX.Graph)}]

The set of all subjects used in the statement within all graphs of a RDF.Dataset.

Examples

iex> RDF.Dataset.new([
...>   {EX.S1, EX.p1, EX.O1, EX.Graph},
...>   {EX.S2, EX.p2, EX.O2},
...>   {EX.S1, EX.p2, EX.O3}]) |>
...>   RDF.Dataset.subjects
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2)])
Link to this function

values(dataset, opts \\ [])

View Source

Specs

values(t(), keyword()) :: map()

Returns a nested map of the native Elixir values of a RDF.Dataset.

When a :context option is given with a RDF.PropertyMap, predicates will be mapped to the terms defined in the RDF.PropertyMap, if present.

Examples

iex> [
...>   {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo", ~I<http://example.com/Graph>},
...>   {~I<http://example.com/S>, ~I<http://example.com/p>, RDF.XSD.integer(42), }
...> ]
...> |> RDF.Dataset.new()
...> |> RDF.Dataset.values()
%{
  "http://example.com/Graph" => %{
    "http://example.com/S" => %{"http://example.com/p" => ["Foo"]}
  },
  nil => %{
    "http://example.com/S" => %{"http://example.com/p" => [42]}
  }
}
Link to this function

who_describes(dataset, subject)

View Source

Specs

who_describes(t(), RDF.Statement.coercible_subject()) :: [RDF.Graph.t()]

Returns the names of all graphs of a RDF.Dataset containing statements about the given subject.

Examples

  iex> dataset = RDF.Dataset.new([
  ...>   {EX.S1, EX.p, EX.O},
  ...>   {EX.S2, EX.p, EX.O},
  ...>   {EX.S1, EX.p, EX.O, EX.Graph1},
  ...>   {EX.S2, EX.p, EX.O, EX.Graph2}])
  ...> RDF.Dataset.who_describes(dataset, EX.S1)
  [nil, RDF.iri(EX.Graph1)]