You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@jena.apache.org by GitBox <gi...@apache.org> on 2020/10/01 15:20:02 UTC

[GitHub] [jena] ajs6f commented on a change in pull request #804: JENA-1974: G, a library of functions for working with Graph.

ajs6f commented on a change in pull request #804:
URL: https://github.com/apache/jena/pull/804#discussion_r498327157



##########
File path: jena-arq/src/main/java/org/apache/jena/riot/other/G.java
##########
@@ -0,0 +1,593 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.riot.other;
+
+import java.util.*;
+import java.util.function.Consumer;
+
+import org.apache.jena.atlas.iterator.Iter;
+import org.apache.jena.graph.Graph;
+import org.apache.jena.graph.GraphUtil;
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.Triple;
+import org.apache.jena.riot.out.NodeFmtLib;
+//import org.apache.jena.shacl.lib.GN;
+//import org.apache.jena.shacl.lib.RDFDataException;
+//import org.apache.jena.shacl.lib.Transitive;
+//import org.apache.jena.shacl.sys.C;
+import org.apache.jena.sparql.core.Quad;
+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;
+
+/** A library of functions for working with {@link Graph}. */
+public class G {
+    private G() {}
+    private static Node rdfType = NodeConst.nodeRDFType;
+
+    /** Return the subject of a triple, or null if the triple is null. */
+    public static Node subject(Triple triple) {
+        return triple == null ? null : triple.getSubject();
+    }
+
+    /** Return the predicate of a triple, or null if the triple is null. */
+    public static Node predicate(Triple triple) {
+        return triple == null ? null : triple.getPredicate();
+    }
+
+    /** Return the object of a triple, or null if the triple is null. */
+    public static Node object(Triple triple) {
+        return triple == null ? null : triple.getObject();
+    }
+
+    // ---- Node filter tests.
+    public static boolean isURI(Node n)         { return n != null && n.isURI(); }
+    public static boolean isBlank(Node n)       { return n != null && n.isBlank(); }
+    public static boolean isLiteral(Node n)     { return n != null && n.isLiteral(); }
+    public static boolean isResource(Node n)    { return n != null && (n.isURI()||n.isBlank()); }
+
+    /** Convert null to Node.ANY */
+    public static Node nullAsAny(Node x) { return nullAsDft(x, Node.ANY) ; }
+
+    /** Convert null to some default Node */
+    public static Node nullAsDft(Node x, Node dft) { return x==null ? dft : x ; }
+
+    /** Does the graph match the s/p/o pattern? */ 
+    public static boolean contains(Graph g, Node s, Node p, Node o) {
+        return g.contains(s, p, o);
+    }
+    
+    /** Does the graph use the node anywhere as a subject, predicate or object? */
+    public static boolean containsNode(Graph graph, Node node) {
+        return GraphUtil.containsNode(graph, node);
+//        return
+//            contains(graph, node, Node.ANY, Node.ANY) ||
+//            contains(graph, Node.ANY, Node.ANY, node) ||
+//            contains(graph, Node.ANY, node, Node.ANY) ;
+    }
+    
+    /** Test whether the node has the type or is rdfs:subclassOf. */
+    public static boolean isOfType(Graph graph, Node x, Node type) {
+        Objects.requireNonNull(x, "Subject");
+        Objects.requireNonNull(type, "Type");
+        List<Node> allClasses = listSubClasses(graph, type);
+        for ( Node c : allClasses ) {
+            if ( hasType(graph, x, c) )
+                return true;
+        }
+        return false;
+    }
+
+    /** Does the node x have the given type (non-RDFS - no rdfs:subclassOf considered)? */ 
+    public static boolean hasType(Graph graph, Node x, Node type) {
+        Objects.requireNonNull(x, "Subject");
+        Objects.requireNonNull(type, "Type");
+        return contains(graph, x, NodeConst.nodeRDFType, type);
+    }
+
+    //---- get/list/iter
+
+    /** Does node {@code s} have property {@code p} in graph {@code g}? */
+    public static boolean hasProperty(Graph g, Node s, Node p) {
+        return g.contains(s, p, null);
+    }
+
+    /** Contains exactly one. */
+    public static boolean containsOne(Graph g, Node s, Node p, Node o) {
+        return g.contains(s, p, o) && Iter.count(g.find(s,p,null)) == 1;

Review comment:
       See https://issues.apache.org/jira/browse/JENA-1956...




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org