Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions jena-arq/src/main/java/org/apache/jena/system/G.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.engine.iterator.IterAbortable;
import org.apache.jena.sparql.graph.NodeConst;
import org.apache.jena.sparql.util.graph.GNode;
import org.apache.jena.sparql.util.graph.GraphList;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.apache.jena.util.iterator.WrappedIterator;

Expand Down Expand Up @@ -270,7 +268,7 @@ public static Node getZeroOrOneSP(Graph graph, Node subject, Node predicate) {
*/
public static Node getPO(Graph graph, Node predicate, Node object) {
Objects.requireNonNull(graph, "graph");
return object(first(find(graph, Node.ANY, predicate, object)));
return subject(first(find(graph, Node.ANY, predicate, object)));
}

/**
Expand All @@ -287,7 +285,7 @@ public static Node getOnePO(Graph graph, Node predicate, Node object) {
*/
public static boolean hasOnePO(Graph graph, Node predicate, Node object) {
Objects.requireNonNull(graph, "graph");
return findUniqueTriple(graph, Node.ANY, predicate, object) != null;
return findZeroOneTriple(graph, Node.ANY, predicate, object) != null;
}

/**
Expand Down Expand Up @@ -374,6 +372,12 @@ public static List<Node> listSP(Graph graph, Node subject, Node predicate) {
return iterSP(graph, subject, predicate).toList();
}

/** Return a set of all objects for subject-predicate */
public static Set<Node> allSP(Graph graph, Node subject, Node predicate) {
Objects.requireNonNull(graph, "graph");
return find(graph, subject, predicate, null).mapWith(Triple::getObject).toSet();
}

/** Count matches of subject-predicate (which can be wildcards). */
public static long countSP(Graph graph, Node subject, Node predicate) {
Objects.requireNonNull(graph, "graph");
Expand All @@ -399,6 +403,12 @@ public static List<Node> listPO(Graph graph, Node predicate, Node object) {
return iterPO(graph, predicate, object).toList();
}

/** Return a set of all subjects for predicate-object */
public static Set<Node> allPO(Graph graph, Node predicate, Node object) {
Objects.requireNonNull(graph, "graph");
return find(graph, null, predicate, object).mapWith(Triple::getSubject).toSet();
}

/** Count matches of predicate-object (which can be wildcards). */
public static long countPO(Graph graph, Node predicate, Node object) {
Objects.requireNonNull(graph, "graph");
Expand Down Expand Up @@ -499,34 +509,30 @@ public static Set<Node> typesOfNodeAsSet(Graph graph, Node node) {
public static List<Node> rdfList(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
GNode gNode = GNode.create(graph, node);
if ( ! GraphList.isListNode(gNode) )
return null;
return GraphList.members(gNode);
List<Node> nodes = GList.members(graph, node);
return nodes;
}

/** Return a the length of an RDF list. */
public static int listLength(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
GNode gNode = GNode.create(graph, node);
if ( ! GraphList.isListNode(gNode) )
if ( ! GList.isListNode(graph, node) )
return -1;
return GraphList.length(gNode);
return (int)GList.listLength(graph, node);
}

/**
* Return a java list where the {@code node} is an RDF list of nodes or a single
* node (returned a singleton list).
* Return a java list where the {@code node} is an RDF list of nodes,
* of if the node is not a list, return the node as a list of one.
*/
public static List<Node> getOneOrList(Graph graph, Node node) {
Objects.requireNonNull(graph, "graph");
Objects.requireNonNull(node, "node");
GNode gNode = GNode.create(graph, node);
// An element on its own is a list of one
if ( ! GraphList.isListNode(gNode) )
if ( ! GList.isListNode(graph, node) )
return List.of(node);
return GraphList.members(gNode);
return GList.members(graph, node);
}

// Sub-class / super-class
Expand Down Expand Up @@ -647,18 +653,6 @@ private static void accNodesOfTypes(Collection<Node> acc, Graph graph, Collectio
);
}

/** Return a set of all objects for subject-predicate */
public static Set<Node> allSP(Graph graph, Node subject, Node predicate) {
Objects.requireNonNull(graph, "graph");
return find(graph, subject, predicate, null).mapWith(Triple::getObject).toSet();
}

/** Return a set of all subjects for predicate-object */
public static Set<Node> allPO(Graph graph, Node predicate, Node object) {
Objects.requireNonNull(graph, "graph");
return find(graph, null, predicate, object).mapWith(Triple::getSubject).toSet();
}

// --- Graph walking.

/** Count the number of in-arc to an object */
Expand Down Expand Up @@ -841,15 +835,15 @@ public static void copyGraphSrcToDst(Graph src, Graph dst) {

/**
* Creates a copy of the given graph.
* If the graph implements Copyable<Graph> then the copy method is called.
* If the graph implements {@code Copyable<Graph>} then the copy method is called.
* Otherwise, a new system default memory-based graph is created and the triples are copied
* into it.
* @param src the graph to copy
* @return a copy of the graph
*/
@SuppressWarnings("unchecked")
public static Graph copy(Graph src) {
if(src instanceof Copyable<?> copyable) {
@SuppressWarnings("unchecked")
Copyable<Graph> copyableGraph = (Copyable<Graph>)copyable;
return copyableGraph.copy();
}
Expand Down
Loading