Class GraphUtil


  • public class GraphUtil
    extends java.lang.Object
    An ad-hoc collection of useful code for graphs
    • Method Detail

      • listSubjects

        public static ExtendedIterator<Node> listSubjects​(Graph g,
                                                          Node p,
                                                          Node o)
        Return an iterator over the unique subjects with predicate p and object o. p and o can be wildcards (Node.ANY)
        Parameters:
        g - Graph
        p - Predicate - may be Node.ANY
        o - Object - may be Node.ANY
        Returns:
        ExtendedIterator
      • listPredicates

        public static ExtendedIterator<Node> listPredicates​(Graph g,
                                                            Node s,
                                                            Node o)
        Return an iterator over the unique predicate between s and o. s and o can be wildcards (Node.ANY)
        Parameters:
        g - Graph
        s - Subject - may be Node.ANY
        o - Object - may be Node.ANY
        Returns:
        ExtendedIterator
      • listObjects

        public static ExtendedIterator<Node> listObjects​(Graph g,
                                                         Node s,
                                                         Node p)
        Return an iterator over the unique objects with a given subject and object. s and p can be wildcards (Node.ANY)
        Parameters:
        g - Graph
        s - Subject - may be Node.ANY
        p - Predicate - may be Node.ANY
        Returns:
        ExtendedIterator
      • containsNode

        public static boolean containsNode​(Graph graph,
                                           Node node)
        Does the graph use the node anywhere as a subject, predicate or object?
      • findAll

        public static ExtendedIterator<Triple> findAll​(Graph g)
        Answer an iterator covering all the triples in the specified graph.
        Parameters:
        g - the graph from which to extract triples
        Returns:
        an iterator over all the graph's triples
      • add

        public static void add​(Graph graph,
                               Triple[] triples)
      • add

        public static void add​(Graph graph,
                               java.util.List<Triple> triples)
      • add

        public static void add​(Graph graph,
                               java.util.Iterator<Triple> it)
      • addInto

        public static void addInto​(Graph dstGraph,
                                   Graph srcGraph)
        Add triples into the destination (arg 1) from the source (arg 2)
      • delete

        public static void delete​(Graph graph,
                                  Triple[] triples)
      • delete

        public static void delete​(Graph graph,
                                  java.util.List<Triple> triples)
      • delete

        public static void delete​(Graph graph,
                                  java.util.Iterator<Triple> it)
      • remove

        public static void remove​(Graph g,
                                  Node s,
                                  Node p,
                                  Node o)
        A safe and cautious remove() function that converts the remove to a number of Graph.delete(Triple) operations.

        To avoid any possible ConcurrentModificationExceptions, it finds batches of triples, deletes them and tries again until no more triples matching the input can be found.

      • deleteLoopSrc

        public static void deleteLoopSrc​(Graph dstGraph,
                                         Graph srcGraph)
        Delete triples in srcGraph from dstGraph by looping on srcGraph.
      • deleteLoopDst

        public static void deleteLoopDst​(Graph dstGraph,
                                         Graph srcGraph)
        Delete the triple in srcGraph from dstGraph by checking the contents of dsgGraph against the srcGraph. This involves calling srcGraph.contains.
        Implementation Note:
        dstGraph.size() is used by this method.
      • deleteFrom

        public static void deleteFrom​(Graph dstGraph,
                                      Graph srcGraph)
        Delete triples in the destination (arg 1) as given in the source (arg 2).
        Implementation Note:
        This is designed for the case of dstGraph being comparable or much larger than srcGraph or srcGraph having a lot of triples to actually be deleted from dstGraph. This includes the case of large, persistent dstGraph.

        It is not designed for a large srcGraph and large dstGraph with only a few triples in common to delete from dstGraph. It is better to calculate the difference in some way, and copy into a small graph to use as the srcGraph.

        To force delete by looping on srcGraph, use deleteLoopSrc(Graph, Graph).

        For large srcGraph and small dstGraph, use deleteLoopDst(org.apache.jena.graph.Graph, org.apache.jena.graph.Graph). See discussion on jena/pull/212, (archived at JENA-1284).