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 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

    Constructors
    Constructor and Description
    `ModelBuilder()` Create a new `ModelBuilder`.
    `ModelBuilder(Model model)` Create a new `ModelBuilder` which will append to the supplied `Model`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `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).
    `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(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).
    `Model` | `build()` Return the created `Model`
    `ModelBuilder` | `defaultGraph()` Set the current graph in which to add new statements to the default graph.
    `ModelBuilder` | `namedGraph(Resource namedGraph)` Set the current graph in which to add new statements to the supplied named graph.
    `ModelBuilder` | `namedGraph(String prefixedNameOrIRI)` Set the current graph in which to add new statements to the supplied named graph.
    `ModelBuilder` | `setNamespace(Namespace ns)` Set the supplied `Namespace` mapping.
    `ModelBuilder` | `setNamespace(String prefix, String namespace)` Set the namespace mapping defined by the supplied prefix and name
    `ModelBuilder` | `subject(Resource subject)` Set the subject resource about which statements are to be added to the model.
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 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`.

  - ### Method Detail

    - #### setNamespace

          public ModelBuilder setNamespace(Namespace ns)

      Set the supplied `Namespace` mapping.
      Parameters:
        `ns` - a `Namespace` 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, 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(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,
                                  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(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, 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(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, 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,
                                  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(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, 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)`.

    - #### build

          public Model build()

      Return the created `Model`
      Returns:
        the `Model`

Copyright © 2015-2020 Eclipse Foundation. All Rights Reserved.