Package org.eclipse.rdf4j.model.util
Class ModelBuilder
java.lang.Object
org.eclipse.rdf4j.model.util.ModelBuilder
Builder to facilitate easier creation of new RDF
Model
objects via a fluent interface. All methods returning
a ModelBuilder
return an immutable reference to the current object, allowing method chaining.
Usage example:
ModelBuilder builder = new ModelBuilder();
// set some namespaces
builder.setNamespace("ex", "http://example.org/").setNamespace(FOAF.NS);
// add a new named graph to the model
builder.namedGraph("ex:graph1")
// add statements about resource ex:john
.subject("ex:john")
.add(FOAF.NAME, "John") // add the triple (ex:john, foaf:name "John") to the named graph
.add(FOAF.AGE, 42)
.add(FOAF.MBOX, "john@example.org");
// add a triple to the default graph
builder.defaultGraph().subject("ex:graph1").add(RDF.TYPE, "ex:Graph");
// return the Model object
Model m = builder.build();
- Author:
- Jeen Broekstra
-
Constructor Summary
ConstructorDescriptionCreate a newModelBuilder
.ModelBuilder
(Model model) Create a newModelBuilder
which will append to the suppliedModel
. -
Method Summary
Modifier and TypeMethodDescriptionAdd an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).Add an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).build()
Return the createdModel
Set the current graph in which to add new statements to the default graph.namedGraph
(String prefixedNameOrIRI) Set the current graph in which to add new statements to the supplied named graph.namedGraph
(Resource namedGraph) Set the current graph in which to add new statements to the supplied named graph.setNamespace
(String prefix, String namespace) Set the namespace mapping defined by the supplied prefix and nameSet the suppliedNamespace
mapping.Set the subject about which statements are to be added to the model, defined by a prefixed name or an IRI reference.Set the subject resource about which statements are to be added to the model.
-
Constructor Details
-
ModelBuilder
public ModelBuilder()Create a newModelBuilder
. -
ModelBuilder
Create a newModelBuilder
which will append to the suppliedModel
.- Parameters:
model
-
-
-
Method Details
-
setNamespace
Set the suppliedNamespace
mapping.- Parameters:
ns
- aNamespace
to add to the model- Returns:
- the
ModelBuilder
-
setNamespace
Set the namespace mapping defined by the supplied prefix and name- Parameters:
prefix
- prefix of the namespace to add to the model.namespace
- namespace name to add to the model.- Returns:
- the
ModelBuilder
-
subject
Set the subject resource about which statements are to be added to the model.- Parameters:
subject
- the subject resource about which statements are to be added.- Returns:
- the
ModelBuilder
-
subject
Set the subject about which statements are to be added to the model, defined by a prefixed name or an IRI reference.- Parameters:
prefixedNameOrIri
- the subject resource about which statements are to be added. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.- Returns:
- the
ModelBuilder
-
namedGraph
Set the current graph in which to add new statements to the supplied named graph. This method resets the current subject.- Parameters:
namedGraph
- a named graph identifier- Returns:
- this
ModelBuilder
-
namedGraph
Set the current graph in which to add new statements to the supplied named graph. This method clears the current subject.- Parameters:
prefixedNameOrIRI
- a named graph identifier. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.- Returns:
- this
ModelBuilder
-
defaultGraph
Set the current graph in which to add new statements to the default graph. This method clears the current subject.- Returns:
- this
ModelBuilder
-
add
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).- Parameters:
subject
- the statement's subjectpredicate
- the statement's predicateobject
- the statement's object. If the supplied object is aBNode
,IRI
, orLiteral
, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typedLiteral
is created out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method creates a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Returns:
- this
ModelBuilder
- See Also:
-
add
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).- Parameters:
subject
- the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.predicate
- the statement's predicateobject
- the statement's object. If the supplied object is aBNode
,IRI
, orLiteral
, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typedLiteral
is created out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method creates a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Returns:
- this
ModelBuilder
- See Also:
-
add
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).- Parameters:
subject
- the statement's subject. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.predicate
- the statement's predicate. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.object
- the statement's object. If the supplied object is aBNode
,IRI
, orLiteral
, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typedLiteral
is created out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method creates a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Returns:
- this
ModelBuilder
- See Also:
-
add
Add an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).- Parameters:
predicate
- the statement's predicate.object
- the statement's object. If the supplied object is aBNode
,IRI
, orLiteral
, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typedLiteral
is created out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method creates a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Returns:
- this
ModelBuilder
- Throws:
ModelException
- if the current subject is not set usingsubject(Resource)
orsubject(String)
.
-
add
Add an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).- Parameters:
predicate
- the statement's predicate. This can be defined either as a prefixed name string (e.g. "ex:john"), or as a full IRI (e.g. "http://example.org/john"). If supplied as a prefixed name, theModelBuilder
will need to have a namespace mapping for the prefix.object
- the statement's object. If the supplied object is aBNode
,IRI
, orLiteral
, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typedLiteral
is created out of the supplied object, mapping the runtime type of the object to the appropriate XML Schema type. If no mapping is available, the method creates a literal with the string representation of the supplied object as the value, andXSD.STRING
as the datatype. Recognized types areBoolean
,Byte
,Double
,Float
,Integer
,Long
,Short
,XMLGregorianCalendar
, andDate
.- Returns:
- this
ModelBuilder
- Throws:
ModelException
- if the current subject is not set usingsubject(Resource)
orsubject(String)
.
-
build
Return the createdModel
- Returns:
- the
Model
-