Package org.eclipse.rdf4j.model.util
Class ModelBuilder
- java.lang.Object
-
- org.eclipse.rdf4j.model.util.ModelBuilder
-
public class ModelBuilder extends Object
Builder to facilitate easier creation of new RDFModel
objects via a fluent interface. All methods returning aModelBuilder
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
Constructors Constructor Description ModelBuilder()
Create a newModelBuilder
.ModelBuilder(Model model)
Create a newModelBuilder
which will append to the suppliedModel
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ModelBuilder
add(String predicate, Object object)
Add an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).ModelBuilder
add(String subject, String predicate, Object object)
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).ModelBuilder
add(String subject, IRI predicate, Object object)
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).ModelBuilder
add(IRI predicate, Object object)
Add an RDF statement with the predicate and object to the model, using the current subject and graph (either named or default).ModelBuilder
add(Resource subject, IRI predicate, Object object)
Add an RDF statement with the given subject, predicate and object to the model, using the current graph (either named or default).Model
build()
Return the createdModel
ModelBuilder
defaultGraph()
Set the current graph in which to add new statements to the default graph.ModelBuilder
namedGraph(String prefixedNameOrIRI)
Set the current graph in which to add new statements to the supplied named graph.ModelBuilder
namedGraph(Resource namedGraph)
Set the current graph in which to add new statements to the supplied named graph.ModelBuilder
setNamespace(String prefix, String namespace)
Set the namespace mapping defined by the supplied prefix and nameModelBuilder
setNamespace(Namespace ns)
Set the suppliedNamespace
mapping.ModelBuilder
subject(String prefixedNameOrIri)
Set the subject about which statements are to be added to the model, defined by a prefixed name or an IRI reference.ModelBuilder
subject(Resource subject)
Set the subject resource about which statements are to be added to the model.
-
-
-
Constructor Detail
-
ModelBuilder
public ModelBuilder()
Create a newModelBuilder
.
-
ModelBuilder
public ModelBuilder(Model model)
Create a newModelBuilder
which will append to the suppliedModel
.- Parameters:
model
-
-
-
Method Detail
-
setNamespace
public ModelBuilder setNamespace(Namespace ns)
Set the suppliedNamespace
mapping.- Parameters:
ns
- aNamespace
to add to the model- Returns:
- the
ModelBuilder
-
setNamespace
public ModelBuilder setNamespace(String prefix, String namespace)
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
public ModelBuilder subject(Resource 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
public ModelBuilder subject(String prefixedNameOrIri)
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
public ModelBuilder namedGraph(Resource 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
public ModelBuilder namedGraph(String prefixedNameOrIRI)
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
public 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
public ModelBuilder add(Resource subject, IRI predicate, Object object)
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:
namedGraph(Resource)
,defaultGraph()
,Literals.createLiteral(ValueFactory, Object)
-
add
public ModelBuilder add(String subject, IRI predicate, Object object)
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:
namedGraph(Resource)
,defaultGraph()
,Literals.createLiteral(ValueFactory, Object)
-
add
public ModelBuilder add(String subject, String predicate, Object object)
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:
namedGraph(Resource)
,defaultGraph()
,Literals.createLiteral(ValueFactory, Object)
-
add
public ModelBuilder add(IRI predicate, Object object)
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
public ModelBuilder add(String predicate, Object object)
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)
.
-
-