RDF.ex v0.6.0 RDF.Description View Source
A set of RDF triples about the same subject.
RDF.Description implements:
- Elixir's
Accessbehaviour - Elixir's
Enumerableprotocol - Elixir's
Inspectprotocol - the
RDF.Dataprotocol
Link to this section Summary
Functions
Adds statements to a RDF.Description.
Add objects to a predicate of a RDF.Description.
Returns the number of statements of a RDF.Description.
Deletes statements from a RDF.Description.
Deletes statements from a RDF.Description.
Deletes all statements with the given properties.
Checks if a RDF.Description has the given resource as subject.
Checks if two RDF.Descriptions are equal.
Fetches the objects for the given predicate of a Description.
Gets a single object for the given predicate of a Description.
Gets the objects for the given predicate of a Description.
Gets and updates the objects of the given predicate of a Description, in a single pass.
Checks if the given statement exists within a RDF.Description.
Creates a new RDF.Description about the given subject with optional initial statements.
Creates a new RDF.Description about the given subject with optional initial statements.
Creates a new RDF.Description about the given subject with optional initial statements.
The set of all resources used in the objects within a RDF.Description.
The set of all resources used in the objects within a RDF.Description satisfying the given filter criterion.
Pops an arbitrary triple from a RDF.Description.
Pops the objects of the given predicate of a Description.
The set of all properties used in the predicates within a RDF.Description.
Adds statements to a RDF.Description and overwrites all existing statements with already used predicates.
Puts objects to a predicate of a RDF.Description, overwriting all existing objects.
The set of all resources used within a RDF.Description.
The list of all triples within a RDF.Description.
Returns a map of the native Elixir values of a RDF.Description.
Link to this section Types
t()
View Source
t() :: module()
t() :: module()
Link to this section Functions
add(description, statements) View Source
Adds statements to a RDF.Description.
Note: When the statements to be added are given as another RDF.Description,
the subject must not match subject of the description to which the statements
are added. As opposed to that RDF.Data.merge/2 will produce a RDF.Graph
containing both descriptions.
add(description, predicate, objects) View Source
Add objects to a predicate of a RDF.Description.
Examples
iex> RDF.Description.add(RDF.Description.new({EX.S, EX.P1, EX.O1}), EX.P2, EX.O2)
RDF.Description.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
iex> RDF.Description.add(RDF.Description.new({EX.S, EX.P, EX.O1}), EX.P, [EX.O2, EX.O3])
RDF.Description.new([{EX.S, EX.P, EX.O1}, {EX.S, EX.P, EX.O2}, {EX.S, EX.P, EX.O3}])
count(description) View Source
Returns the number of statements of a RDF.Description.
delete(description, statements) View Source
Deletes statements from a RDF.Description.
Note: When the statements to be deleted are given as another RDF.Description,
the subject must not match subject of the description from which the statements
are deleted. If you want to delete only a matching description subject, you can
use RDF.Data.delete/2.
delete(description, predicate, objects) View Source
Deletes statements from a RDF.Description.
delete_predicates(description, properties) View Source
Deletes all statements with the given properties.
describes?(description, other_subject) View Source
Checks if a RDF.Description has the given resource as subject.
Examples
iex> RDF.Description.new(EX.S1, EX.p1, EX.O1) |> RDF.Description.describes?(EX.S1)
true
iex> RDF.Description.new(EX.S1, EX.p1, EX.O1) |> RDF.Description.describes?(EX.S2)
false
equal?(description1, description2) View Source
Checks if two RDF.Descriptions are equal.
Two RDF.Descriptions are considered to be equal if they contain the same triples.
fetch(description, predicate) View Source
Fetches the objects for the given predicate of a Description.
When the predicate can not be found :error is returned.
Examples
iex> RDF.Description.fetch(RDF.Description.new({EX.S, EX.p, EX.O}), EX.p)
{:ok, [RDF.iri(EX.O)]}
iex> RDF.Description.fetch(RDF.Description.new([{EX.S, EX.P, EX.O1},
...> {EX.S, EX.P, EX.O2}]), EX.P)
{:ok, [RDF.iri(EX.O1), RDF.iri(EX.O2)]}
iex> RDF.Description.fetch(RDF.Description.new(EX.S), EX.foo)
:error
first(description, predicate) View Source
Gets a single object for the given predicate of a Description.
When the predicate can not be found, the optionally given default value or nil is returned.
Examples
iex> RDF.Description.first(RDF.Description.new({EX.S, EX.P, EX.O}), EX.P)
RDF.iri(EX.O)
iex> RDF.Description.first(RDF.Description.new(EX.S), EX.foo)
nil
get(description, predicate, default \\ nil) View Source
Gets the objects for the given predicate of a Description.
When the predicate can not be found, the optionally given default value or nil is returned.
Examples
iex> RDF.Description.get(RDF.Description.new({EX.S, EX.P, EX.O}), EX.P)
[RDF.iri(EX.O)]
iex> RDF.Description.get(RDF.Description.new(EX.S), EX.foo)
nil
iex> RDF.Description.get(RDF.Description.new(EX.S), EX.foo, :bar)
:bar
get_and_update(description, predicate, fun) View Source
Gets and updates the objects of the given predicate of a Description, in a single pass.
Invokes the passed function on the objects of the given predicate; this
function should return either {objects_to_return, new_object} or :pop.
If the passed function returns {objects_to_return, new_objects}, the return
value of get_and_update is {objects_to_return, new_description} where
new_description is the input Description updated with new_objects for
the given predicate.
If the passed function returns :pop the objects for the given predicate are
removed and a {removed_objects, new_description} tuple gets returned.
Examples
iex> RDF.Description.new({EX.S, EX.P, EX.O}) |>
...> RDF.Description.get_and_update(EX.P, fn current_objects ->
...> {current_objects, EX.NEW}
...> end)
{[RDF.iri(EX.O)], RDF.Description.new({EX.S, EX.P, EX.NEW})}
iex> RDF.Description.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}]) |>
...> RDF.Description.get_and_update(EX.P1, fn _ -> :pop end)
{[RDF.iri(EX.O1)], RDF.Description.new({EX.S, EX.P2, EX.O2})}
include?(description, statement) View Source
Checks if the given statement exists within a RDF.Description.
new(subject)
View Source
new(RDF.Statement.coercible_subject()) :: RDF.Description.t()
new(RDF.Statement.coercible_subject()) :: RDF.Description.t()
Creates a new RDF.Description about the given subject with optional initial statements.
When given a list of statements, the first one must contain a subject.
new(subject, statements) View Source
Creates a new RDF.Description about the given subject with optional initial statements.
new(description, predicate, objects) View Source
Creates a new RDF.Description about the given subject with optional initial statements.
objects(description) View Source
The set of all resources used in the objects within a RDF.Description.
Note: This function does collect only IRIs and BlankNodes, not Literals.
Examples
iex> RDF.Description.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.p2, EX.O2},
...> {EX.p3, EX.O2},
...> {EX.p4, RDF.bnode(:bnode)},
...> {EX.p3, "foo"}
...> ]) |> RDF.Description.objects
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode)])
objects(description, filter_fn) View Source
The set of all resources used in the objects within a RDF.Description satisfying the given filter criterion.
pop(description) View Source
Pops an arbitrary triple from a RDF.Description.
pop(description, predicate) View Source
Pops the objects of the given predicate of a Description.
When the predicate can not be found the optionally given default value or nil is returned.
Examples
iex> RDF.Description.pop(RDF.Description.new({EX.S, EX.P, EX.O}), EX.P)
{[RDF.iri(EX.O)], RDF.Description.new(EX.S)}
iex> RDF.Description.pop(RDF.Description.new({EX.S, EX.P, EX.O}), EX.Missing)
{nil, RDF.Description.new({EX.S, EX.P, EX.O})}
predicates(description) View Source
The set of all properties used in the predicates within a RDF.Description.
Examples
iex> RDF.Description.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.p2, EX.O2},
...> {EX.p2, EX.O3}]) |>
...> RDF.Description.predicates
MapSet.new([EX.p1, EX.p2])
put(description, statements) View Source
Adds statements to a RDF.Description and overwrites all existing statements with already used predicates.
Examples
iex> RDF.Description.put(RDF.Description.new({EX.S, EX.P, EX.O1}), {EX.P, EX.O2})
RDF.Description.new([{EX.S, EX.P, EX.O2}])
iex> RDF.Description.new({EX.S, EX.P1, EX.O1}) |>
...> RDF.Description.put([{EX.P2, EX.O2}, {EX.S, EX.P2, EX.O3}, {EX.P1, EX.O4}])
RDF.Description.new([{EX.S, EX.P1, EX.O4}, {EX.S, EX.P2, EX.O2}, {EX.S, EX.P2, EX.O3}])
iex> RDF.Description.new({EX.S, EX.P, EX.O1}) |>
...> RDF.Description.put(RDF.Description.new(EX.S, EX.P, [EX.O1, EX.O2]))
RDF.Description.new([{EX.S, EX.P, EX.O1}, {EX.S, EX.P, EX.O2}])
iex> RDF.Description.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}]) |>
...> RDF.Description.put(%{EX.P2 => [EX.O3, EX.O4]})
RDF.Description.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O3}, {EX.S, EX.P2, EX.O4}])
put(description, predicate, objects) View Source
Puts objects to a predicate of a RDF.Description, overwriting all existing objects.
Examples
iex> RDF.Description.put(RDF.Description.new({EX.S, EX.P, EX.O1}), EX.P, EX.O2)
RDF.Description.new([{EX.S, EX.P, EX.O2}])
iex> RDF.Description.put(RDF.Description.new({EX.S, EX.P1, EX.O1}), EX.P2, EX.O2)
RDF.Description.new([{EX.S, EX.P1, EX.O1}, {EX.S, EX.P2, EX.O2}])
resources(description) View Source
The set of all resources used within a RDF.Description.
Examples
iex> RDF.Description.new([
...> {EX.S1, EX.p1, EX.O1},
...> {EX.p2, EX.O2},
...> {EX.p1, EX.O2},
...> {EX.p2, RDF.bnode(:bnode)},
...> {EX.p3, "foo"}
...> ]) |> RDF.Description.resources
MapSet.new([RDF.iri(EX.O1), RDF.iri(EX.O2), RDF.bnode(:bnode), EX.p1, EX.p2, EX.p3])
statements(description) View Source
triples(description) View Source
The list of all triples within a RDF.Description.
values(description, mapping \\ &RDF.Statement.default_term_mapping/1) View Source
Returns a map of the native Elixir values of a RDF.Description.
The subject is not part of the result. It can be converted separately with
RDF.Term.value/1.
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 :predicate or :object,
while rdf_term is the RDF term to be mapped.
Examples
iex> {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo"}
...> |> RDF.Description.new()
...> |> RDF.Description.values()
%{"http://example.com/p" => ["Foo"]}
iex> {~I<http://example.com/S>, ~I<http://example.com/p>, ~L"Foo"}
...> |> RDF.Description.new()
...> |> RDF.Description.values(fn
...> {:predicate, predicate} ->
...> predicate
...> |> to_string()
...> |> String.split("/")
...> |> List.last()
...> |> String.to_atom()
...> {_, term} ->
...> RDF.Term.value(term)
...> end)
%{p: ["Foo"]}