RDF.ex v0.7.1 RDF.Graph View Source
A set of RDF triples with an optional name.
RDF.Graph
implements:
- Elixir's
Access
behaviour - Elixir's
Enumerable
protocol - Elixir's
Inspect
protocol - the
RDF.Data
protocol
Link to this section Summary
Functions
Adds triples to a RDF.Graph
.
Adds triples to a RDF.Graph
.
Adds prefixes
to the given graph
.
Removes all triples from graph
.
Clears the base IRI of the given graph
.
Clears the base IRI and all prefixes of the given graph
.
Clears all prefixes of the given graph
.
Deletes statements from a RDF.Graph
.
Deletes statements from a RDF.Graph
.
Deletes prefixes
from the given graph
.
Deletes all statements with the given subjects.
Checks if a RDF.Graph
contains statements about the given resource.
The RDF.Description
of the given subject.
All RDF.Description
s within a RDF.Graph
.
Checks if two RDF.Graph
s are equal.
Fetches the description of the given subject.
Gets the description of the given subject.
Gets and updates the description of the given subject, in a single pass.
Checks if the given statement exists within a RDF.Graph
.
Creates an RDF.Graph
.
Creates an RDF.Graph
initialized with data.
Creates an RDF.Graph
with initial triples.
The set of all resources used in the objects within a RDF.Graph
.
Pops an arbitrary triple from a RDF.Graph
.
Pops the description of the given subject.
The set of all properties used in the predicates of the statements within a RDF.Graph
.
Adds statements to a RDF.Graph
and overwrites all existing statements with the same subjects and predicates.
Add statements to a RDF.Graph
, overwriting all statements with the same subject and predicate.
Add statements to a RDF.Graph
, overwriting all statements with the same subject and predicate.
The set of all resources used within a RDF.Graph
.
Sets the base IRI of the given graph
.
The number of subjects within a RDF.Graph
.
The set of all subjects used in the statements within a RDF.Graph
.
Creates a graph from another one by limiting its statements to those using one of the given subjects
.
The number of statements within a RDF.Graph
.
The list of all statements within a RDF.Graph
.
Updates the description of the subject
in graph
with the given function.
Returns a nested map of the native Elixir values of a RDF.Graph
.
Link to this section Types
get_and_update_description_fun()
View Sourceget_and_update_description_fun() :: (RDF.Description.t() -> {RDF.Description.t(), input()} | :pop)
graph_description()
View Sourcegraph_description() :: %{ required(RDF.Statement.subject()) => RDF.Description.t() }
t()
View Sourcet() :: %RDF.Graph{ base_iri: RDF.IRI.t() | nil, descriptions: graph_description(), name: RDF.IRI.t() | nil, prefixes: RDF.PrefixMap.t() | nil }
update_description_fun()
View Sourceupdate_description_fun() :: (RDF.Description.t() -> RDF.Description.t())
Link to this section Functions
Adds triples to a RDF.Graph
.
When the statements to be added are given as another RDF.Graph
,
the graph name must not match graph name of the graph to which the statements
are added. As opposed to that RDF.Data.merge/2
will produce a RDF.Dataset
containing both graphs.
Also when the statements to be added are given as another RDF.Graph
, the
prefixes of this graph will be added. In case of conflicting prefix mappings
the original prefix from graph
will be kept.
add(graph, subject, predicate, objects)
View Sourceadd( t(), RDF.Statement.coercible_subject(), RDF.Statement.coercible_predicate(), RDF.Statement.coercible_object() | [RDF.Statement.coercible_object()] ) :: t()
Adds triples to a RDF.Graph
.
add_prefixes(graph, prefixes, conflict_resolver \\ nil)
View Sourceadd_prefixes( t(), RDF.PrefixMap.t() | map() | keyword() | nil, RDF.PrefixMap.conflict_resolver() | nil ) :: t()
Adds prefixes
to the given graph
.
The prefixes
mappings can be given as any structure convertible to a
RDF.PrefixMap
.
When a prefix with another mapping already exists it will be overwritten with
the new one. This behaviour can be customized by providing a conflict_resolver
function. See RDF.PrefixMap.merge/3
for more on that.
Removes all triples from graph
.
This function is useful for getting an empty graph based on the settings of another graph, as this function keeps graph name name, base IRI and default prefixes as they are and just removes the triples.
Clears the base IRI of the given graph
.
Clears the base IRI and all prefixes of the given graph
.
Clears all prefixes of the given graph
.
Deletes statements from a RDF.Graph
.
Note: When the statements to be deleted are given as another RDF.Graph
,
the graph name must not match graph name of the graph from which the statements
are deleted. If you want to delete only graphs with matching names, you can
use RDF.Data.delete/2
.
delete(graph, subject, predicate, object)
View Sourcedelete( t(), RDF.Statement.coercible_subject(), RDF.Statement.coercible_predicate(), RDF.Statement.coercible_object() | [RDF.Statement.coercible_object()] ) :: t()
Deletes statements from a RDF.Graph
.
delete_prefixes(graph, prefixes)
View Sourcedelete_prefixes(t(), RDF.PrefixMap.t()) :: t()
Deletes prefixes
from the given graph
.
The prefixes
can be a single prefix or a list of prefixes.
Prefixes not in prefixes of the graph are simply ignored.
delete_subjects(graph, subjects)
View Sourcedelete_subjects( t(), RDF.Statement.coercible_subject() | [RDF.Statement.coercible_subject()] ) :: t()
Deletes all statements with the given subjects.
describes?(graph, subject)
View Sourcedescribes?(t(), RDF.Statement.coercible_subject()) :: boolean()
Checks if a RDF.Graph
contains statements about the given resource.
Examples
iex> RDF.Graph.new([{EX.S1, EX.p1, EX.O1}]) |> RDF.Graph.describes?(EX.S1)
true
iex> RDF.Graph.new([{EX.S1, EX.p1, EX.O1}]) |> RDF.Graph.describes?(EX.S2)
false
description(graph, subject)
View Sourcedescription(t(), RDF.Statement.coercible_subject()) :: RDF.Description.t() | nil
The RDF.Description
of the given subject.
All RDF.Description
s within a RDF.Graph
.
Checks if two RDF.Graph
s are equal.
Two RDF.Graph
s are considered to be equal if they contain the same triples
and have the same name. The prefixes of the graph are irrelevant for equality.
fetch(graph, subject)
View Sourcefetch(t(), RDF.Statement.coercible_subject()) :: {:ok, RDF.Description.t()} | :error
Fetches the description of the given subject.
When the subject can not be found :error
is returned.
Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}]) |>
...> RDF.Graph.fetch(EX.S1)
{:ok, RDF.Description.new({EX.S1, EX.P1, EX.O1})}
iex> RDF.Graph.fetch(RDF.Graph.new, EX.foo)
:error
get(graph, subject, default \\ nil)
View Sourceget(t(), RDF.Statement.coercible_subject(), RDF.Description.t() | nil) :: RDF.Description.t() | nil
Gets the description of the given subject.
When the subject can not be found the optionally given default value or nil
is returned.
Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}]) |>
...> RDF.Graph.get(EX.S1)
RDF.Description.new({EX.S1, EX.P1, EX.O1})
iex> RDF.Graph.get(RDF.Graph.new, EX.Foo)
nil
iex> RDF.Graph.get(RDF.Graph.new, EX.Foo, :bar)
:bar
get_and_update(graph, subject, fun)
View Sourceget_and_update( t(), RDF.Statement.coercible_subject(), get_and_update_description_fun() ) :: {RDF.Description.t(), input()}
Gets and updates the description of the given subject, in a single pass.
Invokes the passed function on the RDF.Description
of the given subject;
this function should return either {description_to_return, new_description}
or :pop
.
If the passed function returns {description_to_return, new_description}
, the
return value of get_and_update
is {description_to_return, new_graph}
where
new_graph
is the input Graph
updated with new_description
for
the given subject.
If the passed function returns :pop
the description for the given subject is
removed and a {removed_description, new_graph}
tuple gets returned.
Examples
iex> RDF.Graph.new({EX.S, EX.P, EX.O}) |>
...> RDF.Graph.get_and_update(EX.S, fn current_description ->
...> {current_description, {EX.P, EX.NEW}}
...> end)
{RDF.Description.new(EX.S, EX.P, EX.O), RDF.Graph.new(EX.S, EX.P, EX.NEW)}
include?(graph, triple)
View Sourceinclude?(t(), RDF.Statement.t()) :: boolean()
Checks if the given statement exists within a RDF.Graph
.
Creates an empty unnamed RDF.Graph
.
Creates an RDF.Graph
.
If a keyword list is given an empty graph is created. Otherwise an unnamed graph initialized with the given data is created.
See new/2
for available arguments and the different ways to provide data.
Examples
RDF.Graph.new({EX.S, EX.p, EX.O})
RDF.Graph.new(name: EX.GraphName)
Creates an RDF.Graph
initialized with data.
The initial RDF triples can be provided
- as a single statement tuple
- an
RDF.Description
- an
RDF.Graph
- or a list with any combination of the former
Available options:
name
: the name of the graph to be createdprefixes
: some prefix mappings which should be stored alongside the graph and will be used for example when serializing in a format with prefix supportbase_iri
: a base IRI which should be stored alongside the graph and will be used for example when serializing in a format with base IRI support
Examples
RDF.Graph.new({EX.S, EX.p, EX.O})
RDF.Graph.new({EX.S, EX.p, EX.O}, name: EX.GraphName)
RDF.Graph.new({EX.S, EX.p, [EX.O1, EX.O2]})
RDF.Graph.new([{EX.S1, EX.p1, EX.O1}, {EX.S2, EX.p2, EX.O2}])
RDF.Graph.new(RDF.Description.new(EX.S, EX.P, EX.O))
RDF.Graph.new([graph, description, triple])
RDF.Graph.new({EX.S, EX.p, EX.O}, name: EX.GraphName, base_iri: EX.base)
new(subject, predicate, objects, options \\ [])
View Sourcenew( RDF.Statement.coercible_subject(), RDF.Statement.coercible_predicate(), RDF.Statement.coercible_object() | [RDF.Statement.coercible_object()], keyword() ) :: t()
Creates an RDF.Graph
with initial triples.
See new/2
for available arguments.
The set of all resources used in the objects within a RDF.Graph
.
Note: This function does collect only IRIs and BlankNodes, not Literals.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S3, EX.p1, EX.O2},
...> {EX.S4, EX.p2, RDF.bnode(:bnode)},
...> {EX.S5, EX.p3, "foo"}
...> ]) |> RDF.Graph.objects
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
Pops an arbitrary triple from a RDF.Graph
.
pop(graph, subject)
View Sourcepop(t(), RDF.Statement.coercible_subject()) :: {RDF.Description.t() | nil, t()}
Pops the description of the given subject.
When the subject can not be found the optionally given default value or nil
is returned.
Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}]) |>
...> RDF.Graph.pop(EX.S1)
{RDF.Description.new({EX.S1, EX.P1, EX.O1}), RDF.Graph.new({EX.S2, EX.P2, EX.O2})}
iex> RDF.Graph.pop(RDF.Graph.new({EX.S, EX.P, EX.O}), EX.Missing)
{nil, RDF.Graph.new({EX.S, EX.P, EX.O})}
The set of all properties used in the predicates of the statements within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S1, EX.p2, EX.O3}]) |>
...> RDF.Graph.predicates
MapSet.new([EX.p1, EX.p2])
Adds statements to a RDF.Graph
and overwrites all existing statements with the same subjects and predicates.
When the statements to be added are given as another RDF.Graph
, the prefixes
of this graph will be added. In case of conflicting prefix mappings the
original prefix from graph
will be kept.
Examples
iex> RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S2, EX.P2, EX.O2}]) |>
...> RDF.Graph.put([{EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])
RDF.Graph.new([{EX.S1, EX.P1, EX.O1}, {EX.S1, EX.P2, EX.O3}, {EX.S2, EX.P2, EX.O3}])
put(graph, subject, predications)
View Sourceput( t(), RDF.Statement.coercible_subject(), RDF.Description.statements() | [RDF.Description.statements()] ) :: t()
Add statements to a RDF.Graph
, overwriting all statements with the same subject and predicate.
put(graph, subject, predicate, objects)
View Sourceput( t(), RDF.Statement.coercible_subject(), RDF.Statement.coercible_predicate(), RDF.Statement.coercible_object() | [RDF.Statement.coercible_object()] ) :: t()
Add statements to a RDF.Graph
, overwriting all statements with the same subject and predicate.
Examples
iex> RDF.Graph.new(EX.S, EX.P, EX.O1) |> RDF.Graph.put(EX.S, EX.P, EX.O2)
RDF.Graph.new(EX.S, EX.P, EX.O2)
iex> RDF.Graph.new(EX.S, EX.P1, EX.O1) |> RDF.Graph.put(EX.S, EX.P2, EX.O2)
RDF.Graph.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
The set of all resources used within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p1, EX.O2},
...> {EX.S2, EX.p2, RDF.bnode(:bnode)},
...> {EX.S3, EX.p1, "foo"}
...> ]) |> RDF.Graph.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])
Sets the base IRI of the given graph
.
The base_iri
can be given as anything accepted by RDF.IRI.coerce_base/1
.
See RDF.Graph.triples/1
.
The number of subjects within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S1, EX.p2, EX.O3}]) |>
...> RDF.Graph.subject_count
2
The set of all subjects used in the statements within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S1, EX.p2, EX.O3}]) |>
...> RDF.Graph.subjects
MapSet.new([RDF.iri(EX.S1), RDF.iri(EX.S2)])
take(graph, subjects, properties \\ nil)
View Sourcetake( t(), [RDF.Statement.coercible_subject()] | nil, [RDF.Statement.coercible_predicate()] | nil ) :: t()
Creates a graph from another one by limiting its statements to those using one of the given subjects
.
If subjects
contains IRIs that are not used in the graph
, they're simply ignored.
The optional properties
argument allows to limit also properties of the subject descriptions.
If nil
is passed as the subjects
, the subjects will not be limited.
The number of statements within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S1, EX.p2, EX.O3}]) |>
...> RDF.Graph.triple_count
3
The list of all statements within a RDF.Graph
.
Examples
iex> RDF.Graph.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.S2, EX.p2, EX.O2},
...> {EX.S1, EX.p2, EX.O3}
...> ]) |> RDF.Graph.triples
[{RDF.iri(EX.S1), RDF.iri(EX.p1), RDF.iri(EX.O1)},
{RDF.iri(EX.S1), RDF.iri(EX.p2), RDF.iri(EX.O3)},
{RDF.iri(EX.S2), RDF.iri(EX.p2), RDF.iri(EX.O2)}]
update(graph, subject, initial \\ nil, fun)
View Sourceupdate( t(), RDF.Statement.coercible_subject(), RDF.Description.statements() | [RDF.Description.statements()] | nil, update_description_fun() ) :: t()
Updates the description of the subject
in graph
with the given function.
If subject
is present in graph
with description
as description,
fun
is invoked with argument description
and its result is used as the new
description of subject
. If subject
is not present in graph
,
initial
is inserted as the description of subject
. The initial value will
not be passed through the update function.
The initial value and the returned objects by the update function will be tried
te coerced to proper RDF descriptions before added. If the initial or returned
description is a RDF.Description
with another subject, the respective
statements are added with subject
as subject.
Examples
iex> RDF.Graph.new({EX.S, EX.p, EX.O}) |>
...> RDF.Graph.update(EX.S,
...> fn description -> Description.add(description, EX.p, EX.O2) end)
RDF.Graph.new([{EX.S, EX.p, EX.O}, {EX.S, EX.p, EX.O2}])
iex> RDF.Graph.new({EX.S, EX.p, EX.O}) |>
...> RDF.Graph.update(EX.S,
...> fn _ -> Description.new(EX.S2, EX.p2, EX.O2) end)
RDF.Graph.new([{EX.S, EX.p2, EX.O2}])
iex> RDF.Graph.new() |>
...> RDF.Graph.update(EX.S, Description.new({EX.S, EX.p, EX.O}),
...> fn description -> Description.add(description, EX.p, EX.O2) end)
RDF.Graph.new([{EX.S, EX.p, EX.O}])
values(graph, mapping \\ &RDF.Statement.default_term_mapping/1)
View Sourcevalues(t(), RDF.Statement.term_mapping()) :: map()
Returns a nested map of the native Elixir values of a RDF.Graph
.
The optional second argument allows to specify a custom mapping with a function
which will receive a tuple {statement_position, rdf_term}
where
statement_position
is one of the atoms :subject
, :predicate
or :object
,
while rdf_term
is the RDF term to be mapped.
Examples
iex> [
...> {~I<http://example.com/S1>, ~I<http://example.com/p>, ~L"Foo"},
...> {~I<http://example.com/S2>, ~I<http://example.com/p>, RDF.integer(42)}
...> ]
...> |> RDF.Graph.new()
...> |> RDF.Graph.values()
%{
"http://example.com/S1" => %{"http://example.com/p" => ["Foo"]},
"http://example.com/S2" => %{"http://example.com/p" => [42]}
}
iex> [
...> {~I<http://example.com/S1>, ~I<http://example.com/p>, ~L"Foo"},
...> {~I<http://example.com/S2>, ~I<http://example.com/p>, RDF.integer(42)}
...> ]
...> |> RDF.Graph.new()
...> |> RDF.Graph.values(fn
...> {:predicate, predicate} ->
...> predicate
...> |> to_string()
...> |> String.split("/")
...> |> List.last()
...> |> String.to_atom()
...> {_, term} ->
...> RDF.Term.value(term)
...> end)
%{
"http://example.com/S1" => %{p: ["Foo"]},
"http://example.com/S2" => %{p: [42]}
}