Class Graphs


  • public final class Graphs
    extends java.lang.Object

    Utilities for working with collections of RDF statements.

    Since:
    1.0
    Version:
    1.0
    Author:
    Michael Grove
    • Constructor Summary

      Constructors 
      Constructor Description
      Graphs()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Set<Statement> applyContext​(java.util.Collection<Statement> theStatements, Resource theContext)
      Create a copy of the statements, modifying them, to apply the given context to each.
      static java.util.List<Value> asList​(java.util.Collection<Statement> theGraph, Resource theRes)
      Return the contents of the given list by following the `rdf:first`/`rdf:rest` structure of the list
      static java.util.Set<Resource> contexts​(java.util.Collection<Statement> theStatements)  
      static boolean isList​(java.util.Collection<Statement> theGraph, Resource theRes)
      Returns whether or not the given resource is a `rdf:List`
      static boolean isomorphic​(java.lang.Iterable<? extends Statement> model1, java.lang.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.
      static java.util.stream.Stream<Statement> matching​(java.lang.Iterable<Statement> theStmts, StatementPattern thePattern)
      Return all the statements that match the pattern.
      static java.util.Set<Statement> named​(java.lang.Iterable<Statement> theStatements, Resource theContext)
      Apply the given context to all of the statements, including Statement values in the subject position.
      static java.util.Optional<Value> object​(java.lang.Iterable<Statement> theStatements, Resource theSubj, IRI thePred)
      Return the object for the given SP.
      static java.util.Set<Value> objects​(java.util.Collection<Statement> theStatements)  
      static java.util.Set<IRI> predicates​(java.util.Collection<Statement> theStatements)  
      static java.util.Set<Resource> subjects​(java.util.Collection<Statement> theStatements)  
      static java.util.stream.Stream<Resource> subjectsOfType​(java.lang.Iterable<Statement> theStatements, IRI theClass)  
      static java.util.Set<Statement> toList​(java.util.List<Resource> theResources)
      Return the contents of the list serialized as an RDF list
      • Methods inherited from class java.lang.Object

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

      • Graphs

        public Graphs()
    • Method Detail

      • subjects

        @Nonnull
        public static java.util.Set<Resource> subjects​(@Nonnull
                                                       java.util.Collection<Statement> theStatements)
      • predicates

        @Nonnull
        public static java.util.Set<IRI> predicates​(@Nonnull
                                                    java.util.Collection<Statement> theStatements)
      • objects

        @Nonnull
        public static java.util.Set<Value> objects​(@Nonnull
                                                   java.util.Collection<Statement> theStatements)
      • contexts

        @Nonnull
        public static java.util.Set<Resource> contexts​(@Nonnull
                                                       java.util.Collection<Statement> theStatements)
      • applyContext

        @Nonnull
        public static java.util.Set<Statement> applyContext​(@Nonnull
                                                            java.util.Collection<Statement> theStatements,
                                                            @Nonnull
                                                            Resource theContext)
        Create a copy of the statements, modifying them, to apply the given context to each.
        Parameters:
        theStatements - the statements
        theContext - the new context
        Returns:
        the new statements
      • matching

        @Nonnull
        public static java.util.stream.Stream<Statement> matching​(@Nonnull
                                                                  java.lang.Iterable<Statement> theStmts,
                                                                  @Nonnull
                                                                  StatementPattern thePattern)
        Return all the statements that match the pattern.
        Parameters:
        theStmts - the statements
        thePattern - the pattern to match
        Returns:
        the matching statements
      • object

        @Nonnull
        public static java.util.Optional<Value> object​(@Nonnull
                                                       java.lang.Iterable<Statement> theStatements,
                                                       @Nonnull
                                                       Resource theSubj,
                                                       @Nonnull
                                                       IRI thePred)
        Return the object for the given SP. If the SP combination has more than one object associated with it, only the first one will be returned.
        Parameters:
        theStatements - the statements
        theSubj - the subject
        thePred - the predicate
        Returns:
        the object for the given SP
      • named

        @Nonnull
        public static java.util.Set<Statement> named​(@Nonnull
                                                     java.lang.Iterable<Statement> theStatements,
                                                     @Nonnull
                                                     Resource theContext)
        Apply the given context to all of the statements, including Statement values in the subject position.
        Parameters:
        theStatements - the graph of statements
        theContext - the context to apply
        Returns:
        A *new* set of statements all of which are in the given context
      • isList

        public static boolean isList​(@Nonnull
                                     java.util.Collection<Statement> theGraph,
                                     @Nonnull
                                     Resource theRes)
        Returns whether or not the given resource is a `rdf:List`
        Parameters:
        theGraph - the graph
        theRes - the resource to check
        Returns:
        `true` if its a list, `false` otherwise
      • asList

        @Nonnull
        public static java.util.List<Value> asList​(@Nonnull
                                                   java.util.Collection<Statement> theGraph,
                                                   @Nonnull
                                                   Resource theRes)
        Return the contents of the given list by following the `rdf:first`/`rdf:rest` structure of the list
        Parameters:
        theGraph - the graph
        theRes - the resource which is the head of the list
        Returns:
        the contents of the list.
      • toList

        @Nonnull
        public static java.util.Set<Statement> toList​(@Nonnull
                                                      java.util.List<Resource> theResources)
        Return the contents of the list serialized as an RDF list
        Parameters:
        theResources - the list
        Returns:
        the list as RDF
      • isomorphic

        public static boolean isomorphic​(@Nonnull
                                         java.lang.Iterable<? extends Statement> model1,
                                         @Nonnull
                                         java.lang.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:
        RDF Concepts & Abstract Syntax, section 3.6 (Graph Comparison)
      • subjectsOfType

        public static java.util.stream.Stream<Resource> subjectsOfType​(java.lang.Iterable<Statement> theStatements,
                                                                       IRI theClass)