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
671 changes: 671 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/other/G.java

Large diffs are not rendered by default.

94 changes: 0 additions & 94 deletions jena-arq/src/main/java/org/apache/jena/riot/other/GLib.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* limitations under the License.
*/

package org.apache.jena.shacl.lib;
package org.apache.jena.riot.other;

import org.apache.jena.shacl.ShaclException;
import org.apache.jena.shared.JenaException;

public class RDFDataException extends ShaclException {
public class RDFDataException extends JenaException {
public RDFDataException(String msg) { super(msg);}
public RDFDataException(String msg, Throwable th) { super(msg, th); }
}
106 changes: 106 additions & 0 deletions jena-arq/src/main/java/org/apache/jena/riot/other/Transitive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.util.iterator.ExtendedIterator;

public class Transitive {
// GLib.

// (?x :p ?z) <- (?x :p ?y) (?y :p ?z)

/**
* Calculate the transitive closure of a property.
*
* Returns a map of node to all reachable nodes.
*/
public static Map<Node, Collection<Node>> transitive(Graph graph, Node property) {
//Test against c.f. "SELECT ?x ?y { ?x property ?y }"

Map<Node, Collection<Node>> reachable = new HashMap<>();
ExtendedIterator<Triple> props = G.find(graph, null, property, null);
// Old school loop.
try {
for ( ; props.hasNext() ; ) {
Triple triple = props.next();
Node node = triple.getSubject();
if ( ! reachable.containsKey(node) ) {
// Does not take advantage of intermediate results.
// Given cycles, that isn't so easy as it would be if it were a tree.
Collection<Node> subs = new HashSet<>();
transitiveExc(graph, true, node, property, subs);
reachable.put(node, subs);
}
}
} finally { props.close(); }
return reachable;
}

/**
* Transitive closure of a property from a start node, and including the start node.
*/
public static void transitiveInc(Graph graph, boolean forward, Node node, Node predicate, Collection<Node> output) {
Set<Node> visited = new HashSet<>();
recurse(graph, forward, 0, -1, node, predicate, visited, output);
}

/**
* Transitive closure of a property from a start node, excluding the start node unless reachable via a cycle.
*/
public static void transitiveExc(Graph graph, boolean forward, Node node, Node predicate, Collection<Node> output) {
ExtendedIterator<Node> iter = singleStep(graph, forward, node, predicate);
try {
Set<Node> visited = new HashSet<>();
for (; iter.hasNext();) {
Node n1 = iter.next() ;
recurse(graph, forward, 1, -1, n1, predicate, visited, output) ;
}
} finally { iter.close(); }
}

private static void recurse(Graph graph, boolean forward, int stepCount, int maxStepCount, Node node, Node predicate, Set<Node> visited, Collection<Node> output) {
if ( maxStepCount >= 0 && stepCount > maxStepCount )
return ;
if ( !visited.add(node) )
return ;
output.add(node);
ExtendedIterator<Node> iter1 = singleStep(graph, forward, node, predicate) ;
try {
// For each step, add to results and recurse.
for (; iter1.hasNext();) {
Node n1 = iter1.next() ;
recurse(graph, forward, stepCount + 1, maxStepCount, n1, predicate, visited, output) ;
}
} finally { iter1.close(); }
}

// A single step of a transitive properties.
// Because for SP? or ?PO, no duplicates occur, so works for both strategies.
static ExtendedIterator<Node> singleStep(Graph graph, boolean forward, Node node, Node property) {
if ( forward )
return G.find(graph, node, property, Node.ANY).mapWith(Triple::getObject);
else
return G.find(graph, Node.ANY, property, node).mapWith(Triple::getSubject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NormalizeValue
// hexBinary, base64Binary.

// Auxillary class of datatype handers, placed here to avoid static initialization
// ordering problems (if in CanonicalizeLiteral, all this low-level machinary would
// ordering problems (if in CanonicalizeLiteral, all this low-level machinery would
// need to be in the file before the external API, which I consider bad style). It
// is a source of obscure bugs.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,45 +305,42 @@ private static Object createJsonldContext(Graph g, PrefixMap prefixMap, boolean

/** Add properties to jsonld context. */
static void addProperties(Map<String, Object> ctx, Graph g) {
Consumer<Triple> x = new Consumer<Triple>() {
@Override
public void accept(Triple item) {
Node p = item.getPredicate() ;
Node o = item.getObject() ;

if ( p.equals(RDF.type.asNode()) )
return ;
// JENA-1744 : split as a "Curie" (at the last / or #, regardless of th characters in a lcoal name).
// Curie : https://www.w3.org/TR/curie/
String x = SplitIRI.localname(p.getURI());

if ( ctx.containsKey(x) ) {
} else if ( o.isBlank() || o.isURI() ) {
// add property as a property (the object is an IRI)
Consumer<Triple> x = (Triple item) -> {
Node p = item.getPredicate() ;
Node o = item.getObject() ;

if ( p.equals(RDF.type.asNode()) )
return ;
// JENA-1744 : split as a "Curie" (at the last / or #, regardless of the characters in a local name).
// Curie : https://www.w3.org/TR/curie/
String uriStr = SplitIRI.localname(p.getURI());

if ( ctx.containsKey(uriStr) ) {
} else if ( o.isBlank() || o.isURI() ) {
// add property as a property (the object is an IRI)
Map<String, Object> x2 = new LinkedHashMap<>() ;
x2.put("@id", p.getURI()) ;
x2.put("@type", "@id") ;
ctx.put(uriStr, x2) ;
} else if ( o.isLiteral() ) {
String literalDatatypeURI = o.getLiteralDatatypeURI() ;
if ( literalDatatypeURI != null ) {
// add property as a typed attribute (the object is a
// typed literal)
Map<String, Object> x2 = new LinkedHashMap<>() ;
x2.put("@id", p.getURI()) ;
x2.put("@type", "@id") ;
ctx.put(x, x2) ;
} else if ( o.isLiteral() ) {
String literalDatatypeURI = o.getLiteralDatatypeURI() ;
if ( literalDatatypeURI != null ) {
// add property as a typed attribute (the object is a
// typed literal)
Map<String, Object> x2 = new LinkedHashMap<>() ;
x2.put("@id", p.getURI()) ;
if (! isLangString(o) && ! isSimpleString(o) )
// RDF 1.1 : Skip if rdf:langString or xsd:string.
x2.put("@type", literalDatatypeURI) ;
ctx.put(x, x2) ;
} else {
// add property as an untyped attribute (the object is
// an untyped literal)
ctx.put(x, p.getURI()) ;
}
if (! isLangString(o) && ! isSimpleString(o) )
// RDF 1.1 : Skip if rdf:langString or xsd:string.
x2.put("@type", literalDatatypeURI) ;
ctx.put(uriStr, x2) ;
} else {
// add property as an untyped attribute (the object is
// an untyped literal)
ctx.put(uriStr, p.getURI()) ;
}
}
} ;
g.find(ANY).forEachRemaining(x);
g.find(ANY).forEach(x);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.jena.graph.Node_Triple;
import org.apache.jena.graph.Triple;
import org.apache.jena.riot.RIOT;
import org.apache.jena.riot.other.GLib;
import org.apache.jena.riot.other.G;
import org.apache.jena.riot.out.NodeFormatter;
import org.apache.jena.riot.out.NodeFormatterTTL;
import org.apache.jena.riot.out.NodeFormatterTTL_MultiLine;
Expand Down Expand Up @@ -326,7 +326,7 @@ private Collection<Triple> triplesOfSubject(Node subj) {
}

private Iterator<Node> listSubjects() {
return GLib.listSubjects(graph) ;
return G.listSubjects(graph) ;
}

// ---- Data access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple ;
import org.apache.jena.riot.other.GLib ;
import org.apache.jena.riot.other.G;
import org.apache.jena.riot.system.RiotLib ;
import org.apache.jena.sparql.core.Quad ;
import org.apache.jena.sparql.util.Context;
Expand Down Expand Up @@ -93,7 +93,7 @@ protected void printBatchQuads(Node g, Node s, List<Quad> quads) {
startGraph(g) ;
lastGraph = g ;
}
List<Triple> triples = GLib.quads2triples(quads.iterator()).toList() ;
List<Triple> triples = G.quads2triples(quads.iterator()).toList() ;
printBatch(s, triples) ;
// No trailing "." has been printed.
lastSubject = s ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.apache.jena.sparql.core;

import static org.apache.jena.sparql.util.graph.GraphUtils.triples2quadsDftGraph ;

import java.util.Iterator ;
import java.util.List ;
import java.util.Objects ;
Expand All @@ -28,8 +26,8 @@
import org.apache.jena.atlas.iterator.IteratorConcat ;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.riot.other.G;
import org.apache.jena.shared.JenaException ;
import static org.apache.jena.sparql.util.graph.GraphUtils.* ;

/** Base class for implementations of a DatasetGraph as a set of graphs.
* This can be a fixed collection or a changeable collection depending
Expand Down Expand Up @@ -58,7 +56,7 @@ public void delete(Quad quad)
@Override
protected Iterator<Quad> findInDftGraph(Node s, Node p , Node o)
{
return triples2quadsDftGraph(getDefaultGraph().find(s, p, o)) ;
return G.triples2quadsDftGraph(getDefaultGraph().find(s, p, o)) ;
}

@Override
Expand All @@ -67,7 +65,7 @@ protected Iter<Quad> findInSpecificNamedGraph(Node g, Node s, Node p , Node o)
Graph graph = fetchGraph(g) ;
if ( graph == null )
return Iter.nullIter() ;
return triples2quads(g, graph.find(s, p, o)) ;
return G.triples2quads(g, graph.find(s, p, o)) ;
}

@Override
Expand Down
Loading