Package org.eclipse.rdf4j.model.util
Class RDFCollections
java.lang.Object
org.eclipse.rdf4j.model.util.RDFCollections
Utilities for working with RDF Collections and converting to/from Java
Collection
classes.
RDF Collections are represented using a Lisp-like structure: the list starts with a head resource (typically a blank
node), which is connected to the first collection member via the RDF.FIRST
relation. The head resource is
then connected to the rest of the list via an RDF.REST
relation. The last resource in the list is marked
using the RDF.NIL
node.
As an example, a list containing three literal values "A", "B", and "C" looks like this as an RDF Collection:
_:n1 -rdf:type--> rdf:List | +---rdf:first--> "A" | +---rdf:rest --> _:n2 -rdf:first--> "B" | +---rdf:rest--> _:n3 -rdf:first--> "C" | +---rdf:rest--> rdf:nilHere,
_:n1
is the head resource of the list. Note that in this example it is declared an instance of
RDF.LIST
, however this is not required for the collection to be considered well-formed.- Author:
- Jeen Broekstra
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Collection<Statement>>
CConverts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection.static <C extends Collection<Statement>>
CasRDF
(Iterable<?> values, Resource head, C sink, ValueFactory valueFactory, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection.static <C extends Collection<Value>>
CConverts an RDF Collection to a JavaCollection
ofValue
objects.static void
consumeCollection
(Iterable<?> values, Resource head, Consumer<Statement> consumer, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection.static void
consumeCollection
(Iterable<?> values, Resource head, Consumer<Statement> consumer, ValueFactory vf, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection.static void
static void
Extracts the RDF Collection starting with suppliedhead
resource from the supplied sourceModel
and sends the statements that make up the collection to the suppliedConsumer
.static <E extends RDF4JException>
voidextract
(GetStatementOptional statementSupplier, Resource head, Consumer<Statement> collectionConsumer, Function<String, Supplier<E>> exceptionSupplier, Resource... contexts) Extracts an RDF Collection starting with the supplied list head from the statement supplier and sends all statements that make up the collection to the suppliedConsumer
function.static <C extends Collection<Statement>>
CgetCollection
(Model sourceModel, Resource head, C sink, Resource... contexts) Extracts the RDF Collection starting with the suppliedhead
resource from the supplied sourceModel
.
-
Constructor Details
-
RDFCollections
public RDFCollections()
-
-
Method Details
-
asRDF
public static <C extends Collection<Statement>> C asRDF(Iterable<?> values, Resource head, C sink, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection. The statements making up the new RDF Collection will be added to the supplied statement collection.- Parameters:
values
- anIterable
of objects (such as a JavaCollection
), which will be converted to an RDF Collection. May not benull
. The method attempts to convert each value that is not already an instance ofValue
to aLiteral
. This conversion will fail with aLiteralUtilException
if the value's object type is not supported. SeeLiterals.createLiteralOrFail(ValueFactory, Object)
for an overview of supported types.head
- aResource
which will be used as the head of the list, that is, the starting point of the created RDF Collection. May benull
, in which case a new resource is generated to represent the list head.sink
- aCollection
ofStatement
objects (for example aModel
) to which the RDF Collection statements will be added. May not benull
.contexts
- the context(s) in which to add the RDF Collection. This argument is an optional vararg and can be left out.- Returns:
- the supplied sink
Collection
ofStatement
s, with the new Statements forming the RDF Collection added. - Throws:
LiteralUtilException
- if one of the supplied values can not be converted to a Literal.- See Also:
-
asRDF
public static <C extends Collection<Statement>> C asRDF(Iterable<?> values, Resource head, C sink, ValueFactory valueFactory, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection. The statements making up the new RDF Collection will be added to the supplied statement collection.- Parameters:
values
- anIterable
of objects (such as a JavaCollection
), which will be converted to an RDF Collection. May not benull
. The method attempts to convert each value that is not already an instance ofValue
to aLiteral
. This conversion will fail with aLiteralUtilException
if the value's object type is not supported. SeeLiterals.createLiteralOrFail(ValueFactory, Object)
for an overview of supported types.head
- aResource
which will be used as the head of the list, that is, the starting point of the created RDF Collection. May benull
, in which case a new resource is generated to represent the list head.sink
- aCollection
ofStatement
objects (for example aModel
) to which the RDF Collection statements will be added. May not benull
.valueFactory
- theValueFactory
to be used for creation of RDF model objects. May not benull
.contexts
- the context(s) in which to add the RDF Collection. This argument is an optional vararg and can be left out.- Returns:
- the supplied sink
Collection
ofStatement
s, with the new Statements forming the RDF Collection added. - Throws:
LiteralUtilException
- if one of the supplied values can not be converted to a Literal.- Since:
- 3.0
- See Also:
-
asValues
public static <C extends Collection<Value>> C asValues(Model m, Resource head, C collection, Resource... contexts) throws ModelException Converts an RDF Collection to a JavaCollection
ofValue
objects. The RDF Collection is given by the suppliedModel
andhead
. This method expects the RDF Collection to be well-formed. If the collection is not well-formed the method may return part of the collection, or may throw aModelException
.- Parameters:
m
- the Model containing the collection to read.head
- theResource
that represents the list head, that is the start resource of the RDF Collection to be read. May not benull
.collection
- the JavaCollection
to add the collection items to.contexts
- the context(s) from which to read the RDF Collection. This argument is an optional vararg and can be left out.- Returns:
- the supplied Java
Collection
, filled with the items from the RDF Collection (if any). - Throws:
ModelException
- if a problem occurs reading the RDF Collection, for example if the Collection is not well-formed.- See Also:
-
consumeCollection
public static void consumeCollection(Iterable<?> values, Resource head, Consumer<Statement> consumer, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection. The statements making up the new RDF Collection will be reported to the suppliedConsumer
function.- Parameters:
values
- anIterable
of objects (such as a JavaCollection
), which will be converted to an RDF Collection. May not benull
. The method attempts to convert each value that is not already an instance ofValue
to aLiteral
. This conversion will fail with aLiteralUtilException
if the value's object type is not supported. SeeLiterals.createLiteralOrFail(ValueFactory, Object)
for an overview of supported types.head
- aResource
which will be used as the head of the list, that is, the starting point of the created RDF Collection. May benull
, in which case a new resource is generated to represent the list head.consumer
- theConsumer
function for the Statements of the RDF Collection. May not benull
.contexts
- the context(s) in which to add the RDF Collection. This argument is an optional vararg and can be left out.- Throws:
LiteralUtilException
- if one of the supplied values can not be converted to a Literal.- See Also:
-
consumeCollection
public static void consumeCollection(Iterable<?> values, Resource head, Consumer<Statement> consumer, ValueFactory vf, Resource... contexts) Converts the suppliedIterable
to an RDF Collection, using the suppliedhead
resource as the starting resource of the RDF Collection. The statements making up the new RDF Collection will be reported to the suppliedConsumer
function.- Parameters:
values
- anIterable
of objects (such as a JavaCollection
), which will be converted to an RDF Collection. May not benull
. The method attempts to convert each value that is not already an instance ofValue
to aLiteral
. This conversion will fail with aLiteralUtilException
if the value's object type is not supported. SeeLiterals.createLiteralOrFail(ValueFactory, Object)
for an overview of supported types.head
- aResource
which will be used as the head of the list, that is, the starting point of the created RDF Collection. May benull
, in which case a new resource is generated to represent the list head.consumer
- theConsumer
function for the Statements of the RDF Collection. May not benull
.vf
- theValueFactory
to use for creation of new model objects. May not benull
contexts
- the context(s) in which to add the RDF Collection. This argument is an optional vararg and can be left out.- Throws:
LiteralUtilException
- if one of the supplied values can not be converted to a Literal.- Since:
- 3.0
- See Also:
-
consumeValues
public static void consumeValues(Model m, Resource head, Consumer<Value> consumer, Resource... contexts) throws ModelException Reads an RDF Collection starting with the supplied list head from the suppliedModel
and sends each collection memberValue
to the suppliedConsumer
function. This method expects the RDF Collection to be well-formed. If the collection is not well-formed the method may report only part of the collection, or may throw aModelException
.- Parameters:
m
- the Model containing the collection to read.head
- theResource
that represents the list head, that is the start resource of the RDF Collection to be read. May not benull
.consumer
- the JavaConsumer
function to which the collection items are reported.contexts
- the context(s) from which to read the RDF Collection. This argument is an optional vararg and can be left out.- Throws:
ModelException
- if a problem occurs reading the RDF Collection, for example if the Collection is not well-formed.- See Also:
-
getCollection
public static <C extends Collection<Statement>> C getCollection(Model sourceModel, Resource head, C sink, Resource... contexts) Extracts the RDF Collection starting with the suppliedhead
resource from the supplied sourceModel
. The statements making up the RDF Collection will be added to the supplied statement collection, which will also be returned.- Parameters:
sourceModel
- the source model, containing the RDF Collection to be read.head
- theResource
that represents the list head, that is the start resource of the RDF Collection to be read. May not benull
. aCollection
ofStatement
objects (for example aModel
) to which the RDF Collection statements will be added. May not benull
.sink
- aCollection
ofStatement
objects (for example aModel
) to which the RDF Collection statements will be added. May not benull
.contexts
- the context(s) from which to read the RDF Collection. This argument is an optional vararg and can be left out.- Returns:
- the supplied sink
Collection
ofStatement
s, with the Statements of the RDF Collection added.
-
extract
public static void extract(Model sourceModel, Resource head, Consumer<Statement> consumer, Resource... contexts) Extracts the RDF Collection starting with suppliedhead
resource from the supplied sourceModel
and sends the statements that make up the collection to the suppliedConsumer
.- Parameters:
sourceModel
- the source model, containing the RDF Collection to be read.head
- theResource
that represents the list head, that is the start resource of the RDF Collection to be read. May not benull
. aCollection
ofStatement
objects (for example aModel
) to which the RDF Collection statements will be added. May not benull
.consumer
- theConsumer
function for the Statements of the RDF Collection. May not benull
.contexts
- the context(s) from which to read the RDF Collection. This argument is an optional vararg and can be left out.
-
extract
public static <E extends RDF4JException> void extract(GetStatementOptional statementSupplier, Resource head, Consumer<Statement> collectionConsumer, Function<String, Supplier<E>> exceptionSupplier, Resource... contexts) throws EExtracts an RDF Collection starting with the supplied list head from the statement supplier and sends all statements that make up the collection to the suppliedConsumer
function. This method expects the RDF Collection to be well-formed. If the collection is not well-formed the method may report only part of the collection, or may throw an exception.- Parameters:
statementSupplier
- the source of the statements from which the RDF collection is to be read, specified as a functional interface.head
- theResource
that represents the list head, that is the start resource of the RDF Collection to be read. May not benull
.collectionConsumer
- the JavaConsumer
function to which the collection statements are reported.exceptionSupplier
- a functional interface that produces the exception type this method will throw when an error occurs.contexts
- the context(s) from which to read the RDF Collection. This argument is an optional vararg and can be left out.- Throws:
E
- if a problem occurs reading the RDF Collection, for example if it is not well-formed.
-