Class RDF4J
- java.lang.Object
-
- org.apache.commons.rdf.rdf4j.RDF4J
-
- All Implemented Interfaces:
RDF
public final class RDF4J extends java.lang.Object implements RDF
RDF4J implementation of RDF.The
RDF4J()
constructor uses aSimpleValueFactory
to create corresponding RDF4JValue
instances. Alternatively, this factory can be constructed with a differentValueFactory
usingRDF4J(ValueFactory)
.asRDFTerm(Value)
can be used to convert any RDF4JValue
to an RDFTerm. Note that adaptedBNode
s are considered equal if they are converted with the sameRDF4J
instance and have the sameBNode.getID()
.createGraph()
creates a new Graph backed byLinkedHashModel
. To use other models, seeasGraph(Model)
.To adapt a RDF4J
Repository
as aDataset
orGraph
, useasDataset(Repository, Option...)
orasGraph(Repository, Option...)
.asTriple(Statement)
can be used to convert a RDF4JStatement
to a Commons RDFTriple
, and equivalentasQuad(Statement)
to convert aQuad
.To convert any
Triple
orQuad
to to RDF4JStatement
, useasStatement(TripleLike)
. This recognises previously convertedRDF4JTriple
s andRDF4JQuad
s without re-converting theirRDF4JTripleLike.asStatement()
.Likewise,
asValue(RDFTerm)
can be used to convert any Commons RDFRDFTerm
to a corresponding RDF4JValue
. This recognises previously convertedRDF4JTerm
s without re-converting theirRDF4JTerm.asValue()
.For the purpose of
BlankNode
equivalence, this factory contains an internalUUID
salt that is used by adapter methods likeasQuad(Statement)
,asTriple(Statement)
,asRDFTerm(Value)
as well ascreateBlankNode(String)
. As RDF4JBNode
instances from multiple repositories or models may have the sameBNode.getID()
, converting them with the above methods might cause accidentalBlankNode
equivalence. Note that theGraph
andDataset
adapter methods likeasDataset(Repository, Option...)
andasGraph(Repository, Option...)
therefore uses a uniqueRDF4J
internally.- See Also:
RDF
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RDF4J.Option
-
Field Summary
Fields Modifier and Type Field Description private static InternalRDF4JFactory
rdf4j
InternalRDF4JFactory is deliberately abstractprivate java.util.UUID
salt
private org.eclipse.rdf4j.model.ValueFactory
valueFactory
-
Constructor Summary
Constructors Constructor Description RDF4J()
Construct anRDF4J
.RDF4J(java.util.UUID salt)
Construct anRDF4J
.RDF4J(org.eclipse.rdf4j.model.ValueFactory valueFactory)
Construct anRDF4J
.RDF4J(org.eclipse.rdf4j.model.ValueFactory valueFactory, java.util.UUID salt)
Construct anRDF4J
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RDF4JDataset
asDataset(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFDataset
.RDF4JGraph
asGraph(org.eclipse.rdf4j.model.Model model)
Adapt an RDF4JModel
as a Commons RDFGraph
.RDF4JGraph
asGraph(org.eclipse.rdf4j.repository.Repository repository, java.util.Set<? extends BlankNodeOrIRI> contexts, RDF4J.Option... option)
Adapt an RDF4JRepository
as a Commons RDFGraph
.RDF4JGraph
asGraph(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFGraph
.RDF4JGraph
asGraphUnion(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFGraph
.RDF4JQuad
asQuad(org.eclipse.rdf4j.model.Statement statement)
Adapt a RDF4JStatement
as a Commons RDFQuad
.RDF4JBlankNode
asRDFTerm(org.eclipse.rdf4j.model.BNode value)
Adapt a RDF4JBNode
as a Commons RDFBlankNode
RDF4JIRI
asRDFTerm(org.eclipse.rdf4j.model.IRI value)
Adapt a RDF4JIRI
as a Commons RDFIRI
RDF4JLiteral
asRDFTerm(org.eclipse.rdf4j.model.Literal value)
Adapt a RDF4JLiteral
as a Commons RDFLiteral
RDF4JBlankNodeOrIRI
asRDFTerm(org.eclipse.rdf4j.model.Resource value)
Adapt a RDF4JResource
as a Commons RDFBlankNodeOrIRI
RDF4JTerm
asRDFTerm(org.eclipse.rdf4j.model.Value value)
Adapt a RDF4JValue
as a Commons RDFRDFTerm
.static RDF4JTerm
asRDFTerm(org.eclipse.rdf4j.model.Value value, java.util.UUID salt)
Adapt a RDF4JValue
as a Commons RDFRDFTerm
.org.eclipse.rdf4j.model.Statement
asStatement(TripleLike tripleLike)
RDF4JTriple
asTriple(org.eclipse.rdf4j.model.Statement statement)
Adapt a RDF4JStatement
as a Commons RDFTriple
.org.eclipse.rdf4j.model.Value
asValue(RDFTerm term)
Adapt a Commons RDFRDFTerm
as a RDF4JValue
.RDF4JBlankNode
createBlankNode()
Create a new blank node.RDF4JBlankNode
createBlankNode(java.lang.String name)
Create a blank node based on the given name.RDF4JDataset
createDataset()
Create a new dataset.RDF4JGraph
createGraph()
Create a new graph.RDF4JIRI
createIRI(java.lang.String iri)
Create an IRI from a (possibly escaped) String.RDF4JLiteral
createLiteral(java.lang.String lexicalForm)
Create a simple literal.Literal
createLiteral(java.lang.String lexicalForm, java.lang.String languageTag)
Create a language-tagged literal.Literal
createLiteral(java.lang.String lexicalForm, IRI dataType)
Create a literal with the specified data type.Quad
createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
Create a quad.RDF4JTriple
createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
Create a triple.org.eclipse.rdf4j.model.ValueFactory
getValueFactory()
private java.util.EnumSet<RDF4J.Option>
optionSet(RDF4J.Option... options)
-
-
-
Field Detail
-
rdf4j
private static InternalRDF4JFactory rdf4j
InternalRDF4JFactory is deliberately abstract
-
salt
private final java.util.UUID salt
-
valueFactory
private final org.eclipse.rdf4j.model.ValueFactory valueFactory
-
-
Constructor Detail
-
RDF4J
public RDF4J()
Construct anRDF4J
.
-
RDF4J
public RDF4J(org.eclipse.rdf4j.model.ValueFactory valueFactory)
Construct anRDF4J
.This constructor is intended for use with the value factory from
Repository.getValueFactory()
when using Repository-based graphs and datasets.- Parameters:
valueFactory
- The RDF4JValueFactory
to use
-
RDF4J
public RDF4J(java.util.UUID salt)
Construct anRDF4J
.This constructor may be used if reproducible
BlankNode.uniqueReference()
inBlankNode
is desirable.- Parameters:
salt
- AnUUID
salt to be used by any createdBlankNode
s for the purpose ofBlankNode.uniqueReference()
-
RDF4J
public RDF4J(org.eclipse.rdf4j.model.ValueFactory valueFactory, java.util.UUID salt)
Construct anRDF4J
.This constructor may be used if reproducible
BlankNode.uniqueReference()
inBlankNode
is desirable.- Parameters:
valueFactory
- The RDF4JValueFactory
to usesalt
- AnUUID
salt to be used by any createdBlankNode
s for the purpose ofBlankNode.uniqueReference()
-
-
Method Detail
-
asQuad
public RDF4JQuad asQuad(org.eclipse.rdf4j.model.Statement statement)
Adapt a RDF4JStatement
as a Commons RDFQuad
.For the purpose of
BlankNode
equivalence, this method will use an internal salt UUID that is unique per instance ofRDF4J
.NOTE: If combining RDF4J
Statement
s multiple repositories or models, then theirBNode
s may have the sameBNode.getID()
, which with this method would become equivalent according toBlankNode.equals(Object)
andBlankNode.uniqueReference()
, unless a separateRDF4J
instance is used per RDF4J repository/model.- Parameters:
statement
- The statement to convert- Returns:
- A
RDF4JQuad
that is equivalent to the statement
-
asRDFTerm
public RDF4JTerm asRDFTerm(org.eclipse.rdf4j.model.Value value)
Adapt a RDF4JValue
as a Commons RDFRDFTerm
.The value will be of the same kind as the term, e.g. a
BNode
is converted to aBlankNode
, aIRI
is converted to aIRI
and aLiteral
. is converted to aLiteral
For the purpose of
BlankNode
equivalence, this method will use an internal salt UUID that is unique per instance ofRDF4J
.NOTE: If combining RDF4J values from multiple repositories or models, then their
BNode
s may have the sameBNode.getID()
, which with this method would become equivalent according toBlankNode.equals(Object)
andBlankNode.uniqueReference()
, unless a separateRDF4J
instance is used per RDF4J repository/model.- Parameters:
value
- The RDF4JValue
to convert.- Returns:
- A
RDFTerm
that corresponds to the RDF4J value - Throws:
java.lang.IllegalArgumentException
- if the value is not a BNode, Literal or IRI
-
asRDFTerm
public RDF4JBlankNode asRDFTerm(org.eclipse.rdf4j.model.BNode value)
Adapt a RDF4JBNode
as a Commons RDFBlankNode
For the purpose of
BlankNode
equivalence, this method will use an internal salt UUID that is unique per instance ofRDF4J
.NOTE: If combining RDF4J values from multiple repositories or models, then their
BNode
s may have the sameBNode.getID()
, which with this method would become equivalent according toBlankNode.equals(Object)
andBlankNode.uniqueReference()
, unless a separateRDF4J
instance is used per RDF4J repository/model.- Parameters:
value
- The RDF4JBNode
to convert.- Returns:
- A
RDF4JBlankNode
that corresponds to the RDF4J BNode
-
asRDFTerm
public RDF4JLiteral asRDFTerm(org.eclipse.rdf4j.model.Literal value)
Adapt a RDF4JLiteral
as a Commons RDFLiteral
- Parameters:
value
- The RDF4JLiteral
to convert.- Returns:
- A
RDF4JLiteral
that corresponds to the RDF4J literal
-
asRDFTerm
public RDF4JIRI asRDFTerm(org.eclipse.rdf4j.model.IRI value)
Adapt a RDF4JIRI
as a Commons RDFIRI
- Parameters:
value
- The RDF4JValue
to convert.- Returns:
- A
RDF4JIRI
that corresponds to the RDF4J IRI
-
asRDFTerm
public RDF4JBlankNodeOrIRI asRDFTerm(org.eclipse.rdf4j.model.Resource value)
Adapt a RDF4JResource
as a Commons RDFBlankNodeOrIRI
- Parameters:
value
- The RDF4JValue
to convert.- Returns:
- A
RDF4JBlankNodeOrIRI
that corresponds to the RDF4J Resource
-
asRDFTerm
public static RDF4JTerm asRDFTerm(org.eclipse.rdf4j.model.Value value, java.util.UUID salt)
Adapt a RDF4JValue
as a Commons RDFRDFTerm
.The value will be of the same kind as the term, e.g. a
BNode
is converted to aBlankNode
, aIRI
is converted to aIRI
and aLiteral
. is converted to aLiteral
- Parameters:
value
- The RDF4JValue
to convert.salt
- AUUID
salt to use for uniquely mapping anyBNode
s. The salt should typically be the same for multiple statements in the sameRepository
orModel
to ensureBlankNode.equals(Object)
andBlankNode.uniqueReference()
works as intended.- Returns:
- A
RDFTerm
that corresponds to the RDF4J value - Throws:
java.lang.IllegalArgumentException
- if the value is not a BNode, Literal or IRI
-
asDataset
public RDF4JDataset asDataset(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFDataset
.Changes to the dataset are reflected in the repository, and vice versa.
Note: Some operations on the
RDF4JDataset
requires the use of try-with-resources to close underlyingRepositoryConnection
s, includingRDF4JDataset.iterate()
,RDF4JDataset.stream()
andRDF4JDataset.getGraphNames()
.- Parameters:
repository
- RDF4JRepository
to connect to.options
- Zero or moreRDF4J.Option
- Returns:
- A
Dataset
backed by the RDF4J repository.
-
asGraph
public RDF4JGraph asGraph(org.eclipse.rdf4j.model.Model model)
Adapt an RDF4JModel
as a Commons RDFGraph
.Changes to the graph are reflected in the model, and vice versa.
- Parameters:
model
- RDF4JModel
to adapt.- Returns:
- Adapted
Graph
.
-
asGraph
public RDF4JGraph asGraph(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFGraph
.The graph will only include triples in the default graph (equivalent to context
new Resource[0]{null})
in RDF4J).Changes to the graph are reflected in the repository, and vice versa.
Note: Some operations on the
RDF4JGraph
requires the use of try-with-resources to close underlyingRepositoryConnection
s, includingRDF4JGraph.iterate()
andRDF4JGraph.stream()
.- Parameters:
repository
- RDF4JRepository
to connect to.options
- Zero or moreRDF4J.Option
- Returns:
- A
Graph
backed by the RDF4J repository.
-
asGraphUnion
public RDF4JGraph asGraphUnion(org.eclipse.rdf4j.repository.Repository repository, RDF4J.Option... options)
Adapt an RDF4JRepository
as a Commons RDFGraph
.The graph will include triples in any contexts (e.g. the union graph).
Changes to the graph are reflected in the repository, and vice versa.
- Parameters:
repository
- RDF4JRepository
to connect to.options
- Zero or moreRDF4J.Option
- Returns:
- A union
Graph
backed by the RDF4J repository.
-
asGraph
public RDF4JGraph asGraph(org.eclipse.rdf4j.repository.Repository repository, java.util.Set<? extends BlankNodeOrIRI> contexts, RDF4J.Option... option)
Adapt an RDF4JRepository
as a Commons RDFGraph
.The graph will include triples in the specified contexts.
Changes to the graph are reflected in the repository, and vice versa. Triples added/removed to the graph are reflected in all the specified contexts.
Note: Some operations on the
RDF4JGraph
requires the use of try-with-resources to close underlyingRepositoryConnection
s, includingRDF4JGraph.iterate()
andRDF4JGraph.stream()
.- Parameters:
repository
- RDF4JRepository
to connect to.contexts
- ASet
ofBlankNodeOrIRI
specifying the graph names to use as a context. The set may include the valuenull
to indicate the default graph. The empty set indicates any context, e.g. the union graph.option
- Zero or moreRDF4J.Option
s- Returns:
- A
Graph
backed by the RDF4J repository.
-
asStatement
public org.eclipse.rdf4j.model.Statement asStatement(TripleLike tripleLike)
Adapt a Commons RDFTriple
orQuad
as a RDF4JStatement
.If the
tripleLike
argument is anRDF4JTriple
or aRDF4JQuad
, then itsRDF4JTripleLike.asStatement()
is returned as-is. Note that this means that aRDF4JTriple
would preserve itsStatement.getContext()
, and that anyBlankNode
s would be deemed equivalent in RDF4J if they have the sameBNode.getID()
.
-
asTriple
public RDF4JTriple asTriple(org.eclipse.rdf4j.model.Statement statement)
Adapt a RDF4JStatement
as a Commons RDFTriple
.For the purpose of
BlankNode
equivalence, this method will use an internal salt UUID that is unique per instance ofRDF4J
.NOTE: If combining RDF4J statements from multiple repositories or models, then their
BNode
s may have the sameBNode.getID()
, which with this method would become equivalent according toBlankNode.equals(Object)
andBlankNode.uniqueReference()
, unless a separateRDF4J
instance is used per RDF4J repository/model.- Parameters:
statement
- The RDF4JStatement
to adapt.- Returns:
- A
RDF4JTriple
that is equivalent to the statement
-
asValue
public org.eclipse.rdf4j.model.Value asValue(RDFTerm term)
Adapt a Commons RDFRDFTerm
as a RDF4JValue
.The value will be of the same kind as the term, e.g. a
BlankNode
is converted to aBNode
, aIRI
is converted to aIRI
and aLiteral
is converted to aLiteral
.If the provided
RDFTerm
isnull
, then the returned value isnull
.If the provided term is an instance of
RDF4JTerm
, then theRDF4JTerm.asValue()
is returned without any conversion. Note that this could mean that aValue
from a different kind ofValueFactory
could be returned.- Parameters:
term
- RDFTerm to adapt to RDF4J Value- Returns:
- Adapted RDF4J
Value
-
createBlankNode
public RDF4JBlankNode createBlankNode()
Description copied from interface:RDF
Create a new blank node.The returned blank node MUST NOT be equal to any existing
BlankNode
instances according toBlankNode.equals(Object)
.- Specified by:
createBlankNode
in interfaceRDF
- Returns:
- A new, unique
BlankNode
-
createBlankNode
public RDF4JBlankNode createBlankNode(java.lang.String name)
Description copied from interface:RDF
Create a blank node based on the given name.All
BlankNode
s created with the givenname
on a particular instance ofRDF
MUST be equivalent according toBlankNode.equals(Object)
,The returned BlankNode MUST NOT be equal to
BlankNode
instances returned for any othername
or those returned fromRDF.createBlankNode()
.The returned BlankNode SHOULD NOT be equivalent to any BlankNodes created on a different
RDF
instance, e.g. different instances ofRDF
should produce different blank nodes for the samename
unless they purposely are intending to create equivalentBlankNode
instances (e.g. a reinstatedSerializable
factory).- Specified by:
createBlankNode
in interfaceRDF
- Parameters:
name
- A non-empty, non-null, String that is unique to this blank node in the context of thisRDF
.- Returns:
- A BlankNode for the given name
-
createDataset
public RDF4JDataset createDataset()
Create a new dataset. It is undefined if the dataset will be persisted by any underlying storage mechanism.Note: Some operations on the
RDF4JDataset
requires the use of try-with-resources to close underlyingRepositoryConnection
s, includingRDF4JDataset.iterate()
,RDF4JDataset.stream()
andRDF4JDataset.getGraphNames()
.- Specified by:
createDataset
in interfaceRDF
- Returns:
- A new Dataset
-
createGraph
public RDF4JGraph createGraph()
Description copied from interface:RDF
Create a new graph. It is undefined if the graph will be persisted by any underlying storage mechanism.- Specified by:
createGraph
in interfaceRDF
- Returns:
- A new Graph
-
createIRI
public RDF4JIRI createIRI(java.lang.String iri) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create an IRI from a (possibly escaped) String. The provided iri string MUST be valid according to the W3C RDF-1.1 IRI definition.
-
createLiteral
public RDF4JLiteral createLiteral(java.lang.String lexicalForm) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create a simple literal. The provided lexical form should not be escaped in any sense, e.g. should not include "quotes" unless those are part of the literal value. The returned Literal MUST have aLiteral.getLexicalForm()
that is equal to the provided lexical form, MUST NOT have aLiteral.getLanguageTag()
present, and SHOULD return aLiteral.getDatatype()
that is equal to the IRIhttp://www.w3.org/2001/XMLSchema#string
.- Specified by:
createLiteral
in interfaceRDF
- Parameters:
lexicalForm
- The literal value in plain text- Returns:
- The created Literal
- Throws:
java.lang.IllegalArgumentException
- If the provided lexicalForm is not acceptable, e.g. because it is too large for an underlying storage.
-
createLiteral
public Literal createLiteral(java.lang.String lexicalForm, IRI dataType) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create a literal with the specified data type. The provided lexical form should not be escaped in any sense, e.g. should not include "quotes" unless those are part of the literal value. It is RECOMMENDED that the provided dataType is one of the RDF-compatible XSD types. The provided lexical form SHOULD be in the lexical space of the provided dataType. The returned Literal SHOULD have aLiteral.getLexicalForm()
that is equal to the provided lexicalForm, MUST NOT have aLiteral.getLanguageTag()
present, and MUST return aLiteral.getDatatype()
that is equivalent to the provided dataType IRI.- Specified by:
createLiteral
in interfaceRDF
- Parameters:
lexicalForm
- The literal valuedataType
- The data type IRI for the literal value, e.g.http://www.w3.org/2001/XMLSchema#integer
- Returns:
- The created Literal
- Throws:
java.lang.IllegalArgumentException
- If any of the provided arguments are not acceptable, e.g. because the provided dataType is not permitted.
-
createLiteral
public Literal createLiteral(java.lang.String lexicalForm, java.lang.String languageTag) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create a language-tagged literal. The provided lexical form should not be escaped in any sense, e.g. should not include "quotes" unless those are part of the literal value. The provided language tag MUST be valid according to BCP47, e.g.en
. The provided language tag MAY be converted to lower case. The returned Literal SHOULD have aLiteral.getLexicalForm()
which is equal to the provided lexicalForm, MUST return aLiteral.getDatatype()
that is equal to the IRIhttp://www.w3.org/1999/02/22-rdf-syntax-ns#langString
, and MUST have aLiteral.getLanguageTag()
present which SHOULD be equal to the provided language tag (compared asString.toLowerCase(Locale)
usingLocale.ENGLISH
).- Specified by:
createLiteral
in interfaceRDF
- Parameters:
lexicalForm
- The literal valuelanguageTag
- The non-empty language tag as defined by BCP47- Returns:
- The created Literal
- Throws:
java.lang.IllegalArgumentException
- If the provided values are not acceptable, e.g. because the languageTag was syntactically invalid.
-
createTriple
public RDF4JTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create a triple. The returned Triple SHOULD have aTriple.getSubject()
that is equal to the provided subject, aTriple.getPredicate()
that is equal to the provided predicate, and aTriple.getObject()
that is equal to the provided object.- Specified by:
createTriple
in interfaceRDF
- Parameters:
subject
- The IRI or BlankNode that is the subject of the triplepredicate
- The IRI that is the predicate of the tripleobject
- The IRI, BlankNode or Literal that is the object of the triple- Returns:
- The created Triple
- Throws:
java.lang.IllegalArgumentException
- If any of the provided arguments are not acceptable, e.g. because a Literal has a lexicalForm that is too large for an underlying storage.
-
createQuad
public Quad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm object) throws java.lang.IllegalArgumentException
Description copied from interface:RDF
Create a quad.The returned Quad SHOULD have a
Quad.getGraphName()
that is equal to the provided graphName, aQuad.getSubject()
that is equal to the provided subject, aQuad.getPredicate()
that is equal to the provided predicate, and aQuad.getObject()
that is equal to the provided object.- Specified by:
createQuad
in interfaceRDF
- Parameters:
graphName
- The IRI or BlankNode that this quad belongs to, ornull
for the public graphsubject
- The IRI or BlankNode that is the subject of the quadpredicate
- The IRI that is the predicate of the quadobject
- The IRI, BlankNode or Literal that is the object of the quad- Returns:
- The created Quad
- Throws:
java.lang.IllegalArgumentException
- If any of the provided arguments are not acceptable, e.g. because a Literal has a lexicalForm that is too large for an underlying storage.
-
getValueFactory
public org.eclipse.rdf4j.model.ValueFactory getValueFactory()
-
optionSet
private java.util.EnumSet<RDF4J.Option> optionSet(RDF4J.Option... options)
-
-