Class ModelBuilder


  • public class ModelBuilder
    extends java.lang.Object
    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();
     
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ModelBuilder add​(java.lang.String predicate, java.lang.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​(java.lang.String subject, java.lang.String predicate, java.lang.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​(java.lang.String subject, IRI predicate, java.lang.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, java.lang.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, java.lang.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 created Model
      private IRI convertPrefixedName​(java.lang.String prefixedName)
      Convert the given prefixed name string to an IRI if possible.
      ModelBuilder defaultGraph()
      Set the current graph in which to add new statements to the default graph.
      private Namespace[] getDefaultNamespaces()  
      private IRI mapToIRI​(java.lang.String prefixedNameOrIRI)  
      ModelBuilder namedGraph​(java.lang.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​(java.lang.String prefix, java.lang.String namespace)
      Set the namespace mapping defined by the supplied prefix and name
      ModelBuilder setNamespace​(Namespace ns)
      Set the supplied Namespace mapping.
      ModelBuilder subject​(java.lang.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • model

        private final Model model
      • currentSubject

        private Resource currentSubject
      • currentNamedGraph

        private Resource currentNamedGraph
    • Constructor Detail

      • ModelBuilder

        public ModelBuilder()
        Create a new ModelBuilder.
      • ModelBuilder

        public ModelBuilder​(Model model)
        Create a new ModelBuilder which will append to the supplied Model.
        Parameters:
        model -
    • Method Detail

      • setNamespace

        public ModelBuilder setNamespace​(java.lang.String prefix,
                                         java.lang.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​(java.lang.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, the ModelBuilder 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​(java.lang.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, the ModelBuilder 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,
                                java.lang.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
        predicate - the statement's predicate
        object - the statement's object. If the supplied object is a BNode, IRI, or Literal, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typed Literal 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, and XSD.STRING as the datatype. Recognized types are Boolean , Byte, Double, Float, Integer, Long, Short, XMLGregorianCalendar , and Date.
        Returns:
        this ModelBuilder
        See Also:
        namedGraph(Resource), defaultGraph(), Literals.createLiteral(ValueFactory, Object)
      • add

        public ModelBuilder add​(java.lang.String subject,
                                IRI predicate,
                                java.lang.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, the ModelBuilder will need to have a namespace mapping for the prefix.
        predicate - the statement's predicate
        object - the statement's object. If the supplied object is a BNode, IRI, or Literal, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typed Literal 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, and XSD.STRING as the datatype. Recognized types are Boolean , Byte, Double, Float, Integer, Long, Short, XMLGregorianCalendar , and Date.
        Returns:
        this ModelBuilder
        See Also:
        namedGraph(Resource), defaultGraph(), Literals.createLiteral(ValueFactory, Object)
      • add

        public ModelBuilder add​(java.lang.String subject,
                                java.lang.String predicate,
                                java.lang.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, the ModelBuilder 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, the ModelBuilder will need to have a namespace mapping for the prefix.
        object - the statement's object. If the supplied object is a BNode, IRI, or Literal, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typed Literal 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, and XSD.STRING as the datatype. Recognized types are Boolean , Byte, Double, Float, Integer, Long, Short, XMLGregorianCalendar , and Date.
        Returns:
        this ModelBuilder
        See Also:
        namedGraph(Resource), defaultGraph(), Literals.createLiteral(ValueFactory, Object)
      • add

        public ModelBuilder add​(IRI predicate,
                                java.lang.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 a BNode, IRI, or Literal, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typed Literal 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, and XSD.STRING as the datatype. Recognized types are Boolean , Byte, Double, Float, Integer, Long, Short, XMLGregorianCalendar , and Date.
        Returns:
        this ModelBuilder
        Throws:
        ModelException - if the current subject is not set using subject(Resource) or subject(String).
      • add

        public ModelBuilder add​(java.lang.String predicate,
                                java.lang.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, the ModelBuilder will need to have a namespace mapping for the prefix.
        object - the statement's object. If the supplied object is a BNode, IRI, or Literal, the object is used directly. If it is a prefixed name String with a known prefix, it is mapped to an IRI. Otherwise a typed Literal 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, and XSD.STRING as the datatype. Recognized types are Boolean , Byte, Double, Float, Integer, Long, Short, XMLGregorianCalendar , and Date.
        Returns:
        this ModelBuilder
        Throws:
        ModelException - if the current subject is not set using subject(Resource) or subject(String).
      • convertPrefixedName

        private IRI convertPrefixedName​(java.lang.String prefixedName)
        Convert the given prefixed name string to an IRI if possible.
        Parameters:
        prefixedName - a prefixed name string, e.g. "rdf:type"
        Returns:
        the IRI corresponding to the prefixed name, or null if the supplied string couldn't be converted.
      • mapToIRI

        private IRI mapToIRI​(java.lang.String prefixedNameOrIRI)
      • getDefaultNamespaces

        private Namespace[] getDefaultNamespaces()