afs commented on code in PR #1752:
URL: https://github.com/apache/jena/pull/1752#discussion_r1105064901
##########
jena-arq/src/main/java/org/apache/jena/riot/other/G.java:
##########
@@ -845,4 +850,110 @@ public static <X> X calcTxn(Graph graph, Supplier<X>
action) {
return action.get();
}
+
+ /**
+ * Find triples and execute an action on each triple found.
+ */
+ public static void findExec(Graph graph, Consumer<Triple> action, Node s,
Node p , Node o) {
+ ExtendedIterator<Triple> eIter = G.find(graph, s, p, o);
+ try {
+ eIter.forEach(action);
+ } finally { eIter.close(); }
+ }
+
+ // From GraphUtils.
+
+ /** Add triples into the destination (arg 1) from the source (arg 2)*/
+ public static void addInto(Graph dstGraph, Graph srcGraph ) {
+ if ( dstGraph == srcGraph && ! dstGraph.getEventManager().listening() )
+ return ;
+ dstGraph.getPrefixMapping().setNsPrefixes(srcGraph.getPrefixMapping())
;
+ findExec(srcGraph, dstGraph::add, ANY, ANY, ANY);
+ dstGraph.getEventManager().notifyAddGraph( dstGraph, srcGraph );
+ }
+
+ private static void addIteratorWorker( Graph graph, Iterator<Triple> it ) {
+ List<Triple> s = IteratorCollection.iteratorToList( it );
+ addIteratorWorkerDirect(graph, s.iterator());
+ }
+
+ private static void addIteratorWorkerDirect( Graph graph, Iterator<Triple>
it ) {
+ it.forEachRemaining(graph::add);
+// if ( OldStyle && graph instanceof GraphWithPerform ) {
+// GraphWithPerform g = (GraphWithPerform)graph;
+// it.forEachRemaining(g::performAdd);
+// } else {
+// it.forEachRemaining(graph::add);
+// }
+ }
+
+ /**
+ * Test whether a graph contains by "same term", regardless of whether the
graph
+ * implements "same value" or "same term".
+ */
+ public static boolean containsBySameTerm(Graph graph, Triple triple) {
+ return containsBySameTerm(graph, triple.getSubject(),
triple.getPredicate(), triple.getObject());
+ }
+
+ /**
+ * Test whether a graph contains by "same term", regardless of whether the
graph
+ * implements "same value" or "same term".
+ */
+ public static boolean containsBySameTerm(Graph graph, Node s, Node p, Node
o) {
+ // Do direct for efficiency.
+ if ( ! graph.contains(s,p,o) )
+ return false;
+ // May have matched by value. Do a term test find to restrict to RDF
terms.
+ ExtendedIterator<Triple> iter = graph.find(s, p, o);
+ // Unless generalized RDF, only need to test object.
+ Predicate<Triple> predicate = (dataTriple) -> sameTermMatch(s, p, o,
dataTriple);
+ iter = iter.filterKeep(predicate);
+ try {
+ return iter.hasNext();
+ } finally { iter.close(); }
+
+// // For reference - just object
+// if ( !o.isConcrete() || o.isLiteral() ) {
+// Predicate<Triple> predicate = (t) -> sameTermMatch(o, t.getObject())
;
+// iter = iter.filterKeep(predicate) ;
+// }
+
+ }
+
+ /**
+ * Match a ground triple (even ANY and variables are considered ground
terms in the
+ * data triple) with S/P/O which can be wildcards (ANY or null).
+ */
+ public static boolean sameTermMatch(Node matchSubj, Node matchPred, Node
matchObj, Triple dataTriple) {
+ return sameTermMatch(matchSubj, dataTriple.getSubject()) &&
+ sameTermMatch(matchPred, dataTriple.getPredicate()) &&
+ sameTermMatch(matchObj, dataTriple.getObject());
+ }
+
+ /**
+ * Match a ground RDF Term (ANY and variables are considered ground terms
in the
+ * data term) with a node which can be a wildcard (ANY or null).
+ * Language tags compare case-insensitively.
+ */
+ public static boolean sameTermMatch(Node match, Node data) {
+ if ( false ) {
+ if ( match==null || Node.ANY.equals(match) )
+ return true;
+ // If lang tags are compared case-insensitively.
+ return match.equals(data);
+ }
+
+ // Allow for case-insensitive language tag comparison.
+ // Two literals.
+ if ( ! Util.isLangString(data) || ! Util.isLangString(match) )
+ // Not both strings with lang tags.
+ return (match==null) || (match == Node.ANY) || match.equals(data) ;
+
+ // Language tags compare case insensitively.
+ String lex1 = data.getLiteralLexicalForm();
+ String lex2 = data.getLiteralLexicalForm();
+ String lang1 = data.getLiteralLanguage();
+ String lang2 = data.getLiteralLanguage();
Review Comment:
Good catch - 1 should be "match".
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]