Class Models

java.lang.Object
org.eclipse.rdf4j.model.util.Models

public class Models extends Object
Utility functions for working with Models and other Statement collections.
Author:
Jeen Broekstra, Arjohn Kampman
See Also:
  • Constructor Details

    • Models

      protected Models()
  • Method Details

    • object

      public static Optional<Value> object(Iterable<Statement> statements)
      Retrieves an object Value from the supplied statements. If more than one possible object value exists, any one value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve an object value.
      Returns:
      an object value from the given statement collection, or Optional.empty() if no such value exists.
    • object

      public static Optional<Value> object(Model m)
      Retrieves an object Value from the statements in the given model. If more than one possible object value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      Returns:
      an object value from the given model, or Optional.empty() if no such value exists.
    • objectLiteral

      public static Optional<Literal> objectLiteral(Iterable<Statement> statements)
      Retrieves an object Literal value from the supplied statements. If more than one possible Literal value exists, any one Literal value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve an object Literal value.
      Returns:
      an object Literal value from the given model, or Optional.empty() if no such value exists.
    • objectLiteral

      public static Optional<Literal> objectLiteral(Model m)
      Retrieves an object Literal value from the statements in the given model. If more than one possible Literal value exists, any one Literal value is picked and returned.
      Parameters:
      m - the Model from which to retrieve an object Literal value.
      Returns:
      an object Literal value from the given model, or Optional.empty() if no such value exists.
    • objectLiterals

      public static Set<Literal> objectLiterals(Iterable<Statement> statements)
      Retrieves all object Literal values from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve all object Literal values.
      Returns:
      a Set containing object Literal values from the given model, which will be empty if no such value exists.
      See Also:
    • objectLiterals

      public static Set<Literal> objectLiterals(Model m)
      Retrieves all object Literal values from the statements in the given model.
      Parameters:
      m - the model from which to retrieve all object Literal values.
      Returns:
      a Set containing object Literal values from the given model, which will be empty if no such value exists.
      See Also:
    • objectResource

      public static Optional<Resource> objectResource(Iterable<Statement> statements)
      Retrieves an object Resource value from the supplied statements. If more than one possible Resource value exists, any one Resource value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve an object Resource value.
      Returns:
      an Optional object Resource value from the given model, which will be empty if no such value exists.
    • objectResource

      public static Optional<Resource> objectResource(Model m)
      Retrieves an object Resource value from the statements in the given model. If more than one possible Resource value exists, any one Resource value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object Resource value.
      Returns:
      an Optional object Resource value from the given model, which will be empty if no such value exists.
    • objectResources

      public static Set<Resource> objectResources(Iterable<Statement> statements)
      Retrieves all object Resource values from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve all object Resource values.
      Returns:
      a Set containing object Resource values from the given model, which will be empty if no such value exists.
      See Also:
    • objectResources

      public static Set<Resource> objectResources(Model m)
      Retrieves all object Resource values from the supplied model.
      Parameters:
      m - the Model from which to retrieve all object Resource values.
      Returns:
      a Set containing object Resource values from the given model, which will be empty if no such value exists.
      See Also:
    • objectIRI

      public static Optional<IRI> objectIRI(Iterable<Statement> statements)
      Retrieves an object IRI value from the supplied statements. If more than one possible IRI value exists, any one value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve an object IRI value.
      Returns:
      an Optional object IRI value from the given model, which will be empty if no such value exists.
    • objectIRI

      public static Optional<IRI> objectIRI(Model m)
      Retrieves an object IRI value from the supplied statements in the given model. If more than one possible IRI value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object IRI value.
      Returns:
      an Optional object IRI value from the given model, which will be empty if no such value exists.
    • objectIRIs

      public static Set<IRI> objectIRIs(Iterable<Statement> statements)
      Retrieves all object IRI values from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve all object IRI values.
      Returns:
      a Set containing object IRI values from the given model, which will be empty if no such value exists.
      See Also:
    • objectIRIs

      public static Set<IRI> objectIRIs(Model m)
      Retrieves all object IRI values from the statements in the given model.
      Parameters:
      m - the Model from which to retrieve all object IRI values.
      Returns:
      a Set containing object IRI values from the given model, which will be empty if no such value exists.
      See Also:
    • objectString

      public static Optional<String> objectString(Iterable<Statement> statements)
      Retrieves an object value as a String from the supplied statements. If more than one possible object value exists, any one value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve an object String value.
      Returns:
      an Optional object String value from the given model, which will be empty if no such value exists.
    • objectString

      public static Optional<String> objectString(Model m)
      Retrieves an object value as a String from the statements in the given model. If more than one possible object value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object String value.
      Returns:
      an Optional object String value from the given model, which will be empty if no such value exists.
    • objectStrings

      public static Set<String> objectStrings(Iterable<Statement> statements)
      Retrieves all object String values from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve all object String values.
      Returns:
      a Set containing object String values from the given model, which will be empty if no such value exists.
      See Also:
    • objectStrings

      public static Set<String> objectStrings(Model m)
      Retrieves all object String values from the statements in the given model.
      Parameters:
      m - the model from which to retrieve all object String values.
      Returns:
      a Set containing object String values from the given model, which will be empty if no such value exists.
      See Also:
    • subject

      public static Optional<Resource> subject(Iterable<Statement> statements)
      Retrieves a subject Resource from the supplied statements. If more than one possible resource value exists, any one resource value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve a subject Resource.
      Returns:
      an Optional subject resource from the given model, which will be empty if no such value exists.
    • subject

      public static Optional<Resource> subject(Model m)
      Retrieves a subject Resource from the statements in the given model. If more than one possible resource value exists, any one resource value is picked and returned.
      Parameters:
      m - the model from which to retrieve a subject Resource.
      Returns:
      an Optional subject resource from the given model, which will be empty if no such value exists.
    • subjectIRI

      public static Optional<IRI> subjectIRI(Iterable<Statement> statements)
      Retrieves a subject IRI from the supplied statements. If more than one possible IRI value exists, any one IRI value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve a subject IRI value.
      Returns:
      an Optional subject IRI value from the given model, which will be empty if no such value exists.
    • subjectIRI

      public static Optional<IRI> subjectIRI(Model m)
      Retrieves a subject IRI from the statements in the given model. If more than one possible IRI value exists, any one IRI value is picked and returned.
      Parameters:
      m - the model from which to retrieve a subject IRI value.
      Returns:
      an Optional subject IRI value from the given model, which will be empty if no such value exists.
    • subjectIRIs

      public static Set<IRI> subjectIRIs(Iterable<Statement> statements)
      Retrieves all subject IRIs from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve a subject IRI value.
      Returns:
      a Set of subject IRI values from the given model. The returned Set may be empty.
    • subjectIRIs

      public static Set<IRI> subjectIRIs(Model m)
      Retrieves all subject IRIs from the statements in the given model.
      Parameters:
      m - the model from which to retrieve a subject IRI value.
      Returns:
      a Set of subject IRI values from the given model. The returned Set may be empty.
    • subjectBNode

      public static Optional<BNode> subjectBNode(Iterable<Statement> statements)
      Retrieves a subject BNode from the supplied statements. If more than one possible blank node value exists, any one blank node value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve a subject BNode value.
      Returns:
      an Optional subject BNode value from the given model, which will be empty if no such value exists.
    • subjectBNode

      public static Optional<BNode> subjectBNode(Model m)
      Retrieves a subject BNode from the statements in the given model. If more than one possible blank node value exists, any one blank node value is picked and returned.
      Parameters:
      m - the model from which to retrieve a subject BNode value.
      Returns:
      an Optional subject BNode value from the given model, which will be empty if no such value exists.
    • subjectBNodes

      public static Set<BNode> subjectBNodes(Iterable<Statement> statements)
      Retrieves all subject BNodes from the supplied statements.
      Parameters:
      statements - the Statement Iterable from which to retrieve a subject IRI value.
      Returns:
      a Set of subject BNode values from the given model. The returned Set may be empty.
    • subjectBNodes

      public static Set<BNode> subjectBNodes(Model m)
      Retrieves all subject BNodes from the statements in the given model.
      Parameters:
      m - the model from which to retrieve a subject IRI value.
      Returns:
      a Set of subject BNode values from the given model. The returned Set may be empty.
    • predicate

      public static Optional<IRI> predicate(Iterable<Statement> statements)
      Retrieves a predicate from the supplied statements. If more than one possible predicate value exists, any one value is picked and returned.
      Parameters:
      statements - the Statement Iterable from which to retrieve a predicate value.
      Returns:
      an Optional predicate value from the given model, which will be empty if no such value exists.
    • predicate

      public static Optional<IRI> predicate(Model m)
      Retrieves a predicate from the statements in the given model. If more than one possible predicate value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve a predicate value.
      Returns:
      an Optional predicate value from the given model, which will be empty if no such value exists.
    • setProperty

      public static Model setProperty(Model m, Resource subject, IRI property, Value value, Resource... contexts)
      Sets the property value for the given subject to the given object value, replacing any existing value(s) for the subject's property. This method updates the original input Model and then returns that same Model object.
      Parameters:
      m - the model in which to set the property value. May not be null.
      subject - the subject for which to set/replace the property value. May not be null.
      property - the property for which to set/replace the value. May not be null.
      value - the value to set for the given subject and property. May not be null.
      contexts - the context(s) in which to set/replace the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      the Model object, containing the updated property value.
    • getProperty

      public static Optional<Value> getProperty(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve a property value for the supplied subject from the given model. If more than one property value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      subject - the subject resource for which to retrieve a property value.
      property - the property for which to retrieve a value.
      contexts - the contexts from which to retrieve the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a property value from the given model, or Optional.empty() if no such value exists.
    • getProperties

      public static Set<Value> getProperties(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve all property values for the supplied subject and property from the given model.
      Parameters:
      m - the model from which to retrieve the property values.
      subject - the subject resource for which to retrieve all property values.
      property - the property for which to retrieve all values.
      contexts - the contexts from which to retrieve the property values. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a Set of all property values for the supplied input. The resulting set may be empty.
    • getPropertyResource

      public static Optional<Resource> getPropertyResource(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve a property value as an IRI for the supplied subject from the given model. If more than one property value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      subject - the subject resource for which to retrieve a property value.
      property - the property for which to retrieve a value.
      contexts - the contexts from which to retrieve the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a property value Resource from the given model, or Optional.empty() if no such value exists.
    • getPropertyResources

      public static Set<Resource> getPropertyResources(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve all property Resource values for the supplied subject and property from the given model.
      Parameters:
      m - the model from which to retrieve the property Resource values.
      subject - the subject resource for which to retrieve all property Resource values.
      property - the property for which to retrieve all Resource values.
      contexts - the contexts from which to retrieve the property values. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a Set of all property Resource values for the supplied input. The resulting set may be empty.
    • getPropertyIRI

      public static Optional<IRI> getPropertyIRI(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve a property value as an IRI for the supplied subject from the given model. If more than one property value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      subject - the subject resource for which to retrieve a property value.
      property - the property for which to retrieve a value.
      contexts - the contexts from which to retrieve the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a property value IRI from the given model, or Optional.empty() if no such value exists.
    • getPropertyIRIs

      public static Set<IRI> getPropertyIRIs(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve all property IRI values for the supplied subject and property from the given model.
      Parameters:
      m - the model from which to retrieve the property IRI values.
      subject - the subject resource for which to retrieve all property IRI values.
      property - the property for which to retrieve all IRI values.
      contexts - the contexts from which to retrieve the property values. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a Set of all property IRI values for the supplied input. The resulting set may be empty.
    • getPropertyLiteral

      public static Optional<Literal> getPropertyLiteral(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve a property value as a Literal for the supplied subject from the given model. If more than one property value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      subject - the subject resource for which to retrieve a property literal value.
      property - the property for which to retrieve a value.
      contexts - the contexts from which to retrieve the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a property value Literal from the given model, or Optional.empty() if no such value exists.
    • getPropertyLiterals

      public static Set<Literal> getPropertyLiterals(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve all property Literal values for the supplied subject and property from the given model.
      Parameters:
      m - the model from which to retrieve the property Literal values.
      subject - the subject resource for which to retrieve all property Literal values.
      property - the property for which to retrieve all Literal values.
      contexts - the contexts from which to retrieve the property values. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a Set of all property IRI values for the supplied input. The resulting set may be empty.
    • getPropertyString

      public static Optional<String> getPropertyString(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve a property value as a String for the supplied subject from the given model. If more than one property value exists, any one value is picked and returned.
      Parameters:
      m - the model from which to retrieve an object value.
      subject - the subject resource for which to retrieve a property literal value.
      property - the property for which to retrieve a value.
      contexts - the contexts from which to retrieve the property value. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a property value String from the given model, or Optional.empty() if no such value exists.
    • getPropertyStrings

      public static Set<String> getPropertyStrings(Model m, Resource subject, IRI property, Resource... contexts)
      Retrieve all property values as Strings for the supplied subject and property from the given model.
      Parameters:
      m - the model from which to retrieve the property values as Strings.
      subject - the subject resource for which to retrieve all property values as Strings.
      property - the property for which to retrieve all values as Strings.
      contexts - the contexts from which to retrieve the property values. Optional vararg argument. If not specified the operations works on the entire Model.
      Returns:
      a Set of all property values as Strings for the supplied input. The resulting set may be empty.
    • isomorphic

      public static boolean isomorphic(Iterable<? extends Statement> model1, Iterable<? extends Statement> model2)
      Compares two RDF models, and returns true if they consist of isomorphic graphs and the isomorphic graph identifiers map 1:1 to each other. RDF graphs are isomorphic graphs if statements from one graphs can be mapped 1:1 on to statements in the other graphs. In this mapping, blank nodes are not considered mapped when having an identical internal id, but are mapped from one graph to the other by looking at the statements in which the blank nodes occur. A Model can consist of more than one graph (denoted by context identifiers). Two models are considered isomorphic if for each of the graphs in one model, an isomorphic graph exists in the other model, and the context identifiers of these graphs are either identical or (in the case of blank nodes) map 1:1 on each other.
      See Also:
    • legacyIsomorphic

      @Experimental @Deprecated public static boolean legacyIsomorphic(Iterable<? extends Statement> model1, Iterable<? extends Statement> model2)
      Deprecated.
      since 3.6.0 - use isomorphic(Iterable, Iterable) instead.
      Legacy implementation of isomorphic comparison. This method is offered as a temporary fallback for corner cases where the newly introduced isomorphism algorithm (in release 3.6.0) has worse performance or an unexpected result.
      Since:
      3.6.0
      See Also:
    • isSubset

      public static boolean isSubset(Iterable<? extends Statement> model1, Iterable<? extends Statement> model2)
      Compares two RDF models, and returns true if the first model is a subset of the second model, using graph isomorphism to map statements between models.
    • isSubset

      public static boolean isSubset(Set<? extends Statement> model1, Set<? extends Statement> model2)
      Compares two RDF models, and returns true if the first model is a subset of the second model, using graph isomorphism to map statements between models.
    • stripContexts

      public static Model stripContexts(Model model, Resource... contexts)
      Strips contexts from the input model. This method provides a new Model containing all statements from the input model, with the supplied contexts removed from those statements.
      Parameters:
      model - the input model
      contexts - the contexts to remove. This is a vararg and as such is optional. If not supplied, the method strips all contexts.
      Returns:
      a new Model object containg the same statements as the input model, with the supplied contexts stripped.
    • modelException

      public static Supplier<ModelException> modelException(String message)
      Creates a Supplier of ModelException objects that be passed to Optional.orElseThrow(Supplier) to generate exceptions as necessary.
      Parameters:
      message - The message to be used for the exception
      Returns:
      A Supplier that will create ModelException objects with the given message.
    • synchronizedModel

      public static Model synchronizedModel(Model toSynchronize)
      Make a model thread-safe by synchronizing all its methods. Iterators will still not be thread-safe!
      Parameters:
      toSynchronize - the model that should be synchronized
      Returns:
      Synchronized Model
    • convertRDFStarToReification

      @Experimental public static void convertRDFStarToReification(ValueFactory vf, Model model, Consumer<Statement> consumer)
      Converts the supplied RDF-star model to RDF reification statements. The converted statements are sent to the supplied consumer function.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      consumer - the Consumer function for the produced statements.
    • convertRDFStarToReification

      @Experimental public static void convertRDFStarToReification(Model model, Consumer<Statement> consumer)
      Converts the supplied RDF-star model to RDF reification statements. The converted statements are sent to the supplied consumer function.
      Parameters:
      model - the Model to convert.
      consumer - the Consumer function for the produced statements.
    • convertRDFStarToReification

      @Experimental public static Model convertRDFStarToReification(ValueFactory vf, Model model)
      Converts the statements in supplied RDF-star model to a new RDF model using reificiation.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      Returns:
      a new Model with RDF-star statements converted to reified triples.
    • convertRDFStarToReification

      @Experimental public static Model convertRDFStarToReification(ValueFactory vf, Model model, ModelFactory modelFactory)
      Converts the statements in supplied RDF-star model to a new RDF model using reificiation.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      modelFactory - the ModelFactory used to create the new output Model.
      Returns:
      a new Model with RDF-star statements converted to reified triples.
    • convertRDFStarToReification

      @Experimental public static Model convertRDFStarToReification(Model model)
      Converts the statements in the supplied RDF-star model to a new RDF model using reification.
      Parameters:
      model - the Model to convert.
      Returns:
      a new Model with RDF-star statements converted to reified triples.
    • convertReificationToRDFStar

      @Experimental public static void convertReificationToRDFStar(ValueFactory vf, Model model, Consumer<Statement> consumer)
      Converts the supplied RDF reification model to RDF-star statements. The converted statements are sent to the supplied consumer function.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      consumer - the Consumer function for the produced statements.
    • convertReificationToRDFStar

      @Experimental public static void convertReificationToRDFStar(Model model, Consumer<Statement> consumer)
      Converts the supplied RDF reification model to RDF-star statements. The converted statements are sent to the supplied consumer function.
      Parameters:
      model - the Model to convert.
      consumer - the Consumer function for the produced statements.
    • convertReificationToRDFStar

      @Experimental public static Model convertReificationToRDFStar(ValueFactory vf, Model model, ModelFactory modelFactory)
      Converts the statements in supplied RDF reification model to a new RDF-star model.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      modelFactory - the ModelFactory to use for creating a new Model object for the output.
      Returns:
      a new Model with reification statements converted to RDF-star Triples.
    • convertReificationToRDFStar

      @Experimental public static Model convertReificationToRDFStar(ValueFactory vf, Model model)
      Converts the statements in supplied RDF reification model to a new RDF-star model.

      The supplied value factory is used to create all new statements.

      Parameters:
      vf - the ValueFactory to use for creating statements.
      model - the Model to convert.
      Returns:
      a new Model with reification statements converted to RDF-star Triples.
    • convertReificationToRDFStar

      @Experimental public static Model convertReificationToRDFStar(Model model)
      Converts the supplied RDF reification model to a new RDF-star model.
      Parameters:
      model - the Model to convert.
      Returns:
      a new Model with reification statements converted to RDF-star Triples.