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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static class ParserOutputSinkTriplesToQuads extends StreamRDFWrapper
{ super(base); this.gn = gn; }

@Override public void triple(Triple triple)
{ other.quad(new Quad(gn, triple)); }
{ other.quad(Quad.create(gn, triple)); }
}

private static class ParserOutputSinkTriples extends StreamRDFBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static boolean isQuadBlock(Op op)
public static OpQuadBlock create(Node quadNode, BasicPattern triples) {
QuadPattern qp = new QuadPattern();
for ( Triple t : triples ) {
qp.add(new Quad(quadNode, t));
qp.add(Quad.create(quadNode, t));
}
return new OpQuadBlock(qp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void initQuads() {
if ( quads == null ) {
quads = new QuadPattern();
for ( Triple t : triples )
quads.add(new Quad(graphNode, t));
quads.add(Quad.create(graphNode, t));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public Graph getUnionGraph() {
public void delete(Quad quad) { throw new UnsupportedOperationException("DatasetGraph.delete(Quad)") ; }

@Override
public void add(Node g, Node s, Node p, Node o) { add(new Quad(g,s,p,o)) ; }
public void add(Node g, Node s, Node p, Node o) { add(Quad.create(g,s,p,o)) ; }
@Override
public void delete(Node g, Node s, Node p, Node o) { delete(new Quad(g,s,p,o)) ; }
public void delete(Node g, Node s, Node p, Node o) { delete(Quad.create(g,s,p,o)) ; }

@Override
/** Simple implementation but done without assuming iterator.remove() */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
import org.apache.jena.graph.Triple ;
import org.apache.jena.sparql.core.mem.DatasetGraphInMemory ;

/**
* DatasetGraph framework.
* This class contains a convenience implementation of find that maps to a split between
/**
* DatasetGraph framework.
* This class contains a convenience implementation of find that maps to a split between
* defaultGraph/named graphs.
* @see DatasetGraphTriplesQuads
* @see DatasetGraphCollection
* @see DatasetGraphOne
* @see DatasetGraphInMemory
*/
abstract public class DatasetGraphBaseFind extends DatasetGraphBase
abstract public class DatasetGraphBaseFind extends DatasetGraphBase
{
protected DatasetGraphBaseFind() {}

/** Implementation of find based on splitting into triples (default graph) and quads (named graph) */
@Override
public Iterator<Quad> find(Node g, Node s, Node p, Node o) {
Expand All @@ -49,7 +49,7 @@ public Iterator<Quad> find(Node g, Node s, Node p, Node o) {
return findNG(g, s, p, o) ;
return findAny(s, p, o) ;
}

@Override
public Iterator<Quad> findNG(Node g, Node s, Node p , Node o) {
Iterator<Quad> qIter ;
Expand Down Expand Up @@ -104,7 +104,7 @@ public Iterator<Triple> findInUnionGraph(Node s, Node p , Node o) {
* For example, it may be possible to avoid "distinct".
*/
public Iterator<Quad> findQuadsInUnionGraph(Node s, Node p , Node o) {
return findUnionGraphTriples(s,p,o).map(t -> new Quad(Quad.unionGraph, t)).iterator() ;
return findUnionGraphTriples(s,p,o).map(t -> Quad.create(Quad.unionGraph, t)).iterator() ;
}

/** Find matches in the notional union of all named graphs - return as triples.
Expand All @@ -120,7 +120,7 @@ private Stream<Triple> findUnionGraphTriples(Node s, Node p , Node o) {

/** Find in a specific named graph - {@code g} is a ground term (IRI or bNode), not a wild card (or null). */
protected abstract Iterator<Quad> findInSpecificNamedGraph(Node g, Node s, Node p , Node o) ;

/** Find in any named graph - return quads.
* If a triple matches in two different graph, return a quad for each.
* See {@link #findInUnionGraph} for matching without duplicate triples.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ protected PrefixMapping createPrefixMapping() {
@Override
protected ExtendedIterator<Triple> graphBaseFind(Triple m) {
if ( m == null ) m = Triple.ANY ;
Node s = m.getMatchSubject() ;
Node p = m.getMatchPredicate() ;
Node o = m.getMatchObject() ;
Node s = m.getSubject() ;
Node p = m.getPredicate() ;
Node o = m.getObject() ;
return graphBaseFind(s, p, o) ;
}

Expand Down
88 changes: 66 additions & 22 deletions jena-arq/src/main/java/org/apache/jena/sparql/core/Quad.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@ public class Quad implements Serializable
* (and the default graph when parsing N-Quads or TriG)
* Not for access to the default graph by name - use Quad.defaultGraphIRI.
*/
public static final Node tripleInQuad = null;
public static final Node tripleInQuad = null;

/** A {@code Quad} that has a wildcard in all fields. */
public static final Quad ANY = Quad.create( Node.ANY, Node.ANY, Node.ANY, Node.ANY );

private final Node graph, subject, predicate, object;

public Quad(Node graph, Triple triple)
{
/** @deprecated Prefer {@link Quad#create(Node, Triple)}. */
@Deprecated
public Quad(Node graph, Triple triple) {
this(graph, triple.getSubject(), triple.getPredicate(), triple.getObject());
}

public Quad(Node g, Node s, Node p, Node o)
{
/** @deprecated Prefer {@link Quad#create(Node, Node, Node, Node)}. */
@Deprecated
public Quad(Node g, Node s, Node p, Node o) {
// Null means it's a triple really.
// if ( g == null )
// throw new UnsupportedOperationException("Quad: graph cannot be null");
Expand All @@ -77,29 +79,81 @@ public Quad(Node g, Node s, Node p, Node o)
this.object = o;
}

public static Quad create(Node g, Node s, Node p, Node o) { return new Quad(g,s,p,o); }
public static Quad create(Node g, Triple t) { return new Quad(g,t); }
/**
* Create a {@code Quad}. Nulls are not allowed for subject/predicate/object.
* <p>
* Null for graph means this is a triple, but for type checking reasons, it is
* being carried as a {@code Quad}.
*/
public static Quad create(Node g, Node s, Node p, Node o) {
return new Quad(g, s, p, o);
}

/**
* Create a {@code Quad}. Nulls for for subject/predicate/object are converted to
* {@link Node#ANY}.
* <p>
* Null for graph means this is a triple, but for type checking
* reasons, it is being carried as a {@code Quad}.
*/
public static Quad createMatch(Node g, Node s, Node p, Node o) {
return Quad.create(g, nullToAny(s), nullToAny(p), nullToAny(o));
}

public static Quad create(Node g, Triple t) {
Objects.requireNonNull(t);
return new Quad(g, t);
}

private static Node anyToNull( Node n )
{ return Node.ANY.equals( n ) ? null : n; }

private static Node nullToAny( Node n )
{ return n == null ? Node.ANY : n; }

private static boolean isAny(Node n)
{ return n == null || Node.ANY.equals(n); }

public final Node getGraph() { return graph; }
public final Node getSubject() { return subject; }
public final Node getPredicate() { return predicate; }
public final Node getObject() { return object; }

/**
* Get as a triple - useful because quads often come in blocks for the same graph
* Get the subject/predicate/object as a triple.
*/
public Triple asTriple() {
// Should we keep the triple around esp from the Quad(n,triple) constructor.
// Still have s,p,o for quads.
// Cost : one slot.
// Saving - (re)creating triples.
return Triple.create(subject, predicate, object);
}

public boolean isConcrete() {
return subject.isConcrete() && predicate.isConcrete() && object.isConcrete() && (graph == null || graph.isConcrete());
}

/**
* Does this quad, match the other quad, allowing for wildcards.
* The wildcard node is {@link Node#ANY} and it matches any node, including a wildcard.
* Both this quad and the argument quad may contain wildcards,
* that is "matches" is symmetric:
* {@code this.matches(that) == that.matches(this)}.
*/
public boolean matches(Quad other) {
return matches(other.getGraph(), other.getSubject(), other.getPredicate(), other.getObject());
}

public boolean matches(Node g, Node s, Node p, Node o) {
return matches(this.graph, g) && matches(this.subject, s) && matches(this.predicate, p) && matches(this.object, o);
}

/** Match with possible wildcards (Node.ANY) in either argument. */
private static boolean matches(Node patternNode, Node node) {
if ( isAny(patternNode) )
return true;
if ( isAny(node) )
return true;
return patternNode.sameTermAs(node);
}

/**
* Test whether this is a quad for the default graph (not the default graphs by
* explicit name)
Expand Down Expand Up @@ -220,16 +274,6 @@ public boolean equals(Object other) {
return true;
}

public boolean matches(Node g, Node s, Node p, Node o) {
return nodeMatches(getGraph(), g) && nodeMatches(getSubject(), s) &&
nodeMatches(getPredicate(), p) && nodeMatches(getObject(), o);
}

private static boolean nodeMatches(Node thisNode, Node otherNode) {
// otherNode may be Node.ANY, and this works out.
return otherNode.matches(thisNode);
}

@Override
public String toString() {
String str = (graph==null)?"_":graph.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static Quad substitute(Quad quad, Binding binding) {

Quad q = quad;
if ( s1 != s || p1 != p || o1 != o || g1 != g )
q = new Quad(g1, s1, p1, o1);
q = Quad.create(g1, s1, p1, o1);
return q;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public Graph getUnionGraph() {
}

private Consumer<Graph> addGraph(final Node name) {
return g -> g.find().mapWith(t -> new Quad(name, t)).forEach(this::add);
return g -> g.find().mapWith(t -> Quad.create(name, t)).forEach(this::add);
}

private final Consumer<Graph> removeGraph = g -> g.find().forEach(g::delete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected <X> TFunction4<Node, X> map(final TFunction4<Node, X> f) {
}

protected Quad unmap(final Node x1, final Node x2, final Node x3, final Node x4) {
return apply(reverse, x1, x2, x3, x4, Quad::new);
return apply(reverse, x1, x2, x3, x4, Quad::create);
}

protected Consumer<Triple> map(final TConsumer3<Node> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,37 @@ public static boolean sameTerm(Node node1, Node node2) {
// && sameTerm(t1.getObject(), t2.getObject());
// }

// -------- sameValue

public static boolean sameValue(NodeValue nv1, NodeValue nv2) {
return NodeValue.sameValueAs(nv1, nv2);
}

public static boolean sameValue(Node node1, Node node2) {
NodeValue nv1 = NodeValue.makeNode(node1);
NodeValue nv2 = NodeValue.makeNode(node2);
return NodeValue.sameValueAs(nv1, nv2);
}

public static boolean notSameValue(NodeValue nv1, NodeValue nv2) {
return NodeValue.notSameValueAs(nv1, nv2);
}

public static boolean notSameValue(Node node1, Node node2) {
NodeValue nv1 = NodeValue.makeNode(node1);
NodeValue nv2 = NodeValue.makeNode(node2);
return NodeValue.notSameValueAs(nv1, nv2);
}

// -------- RDFterm-equals -- raises an exception on "don't know" for literals.

// Exact as defined by SPARQL spec, when there are no value extensions.
// Exception for two literals that might be equal but we don't know because of language tags.

// THIS IS NOT:
// SPARQL 1.1 = RDFterm-equals
// SPARQL 1.2 = sameValue

public static boolean rdfTermEquals(Node n1, Node n2) {
if ( n1.equals(n2) )
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ public GraphSPARQLService(String serviceURI, String graphIRI)
@Override
protected ExtendedIterator<Triple> graphBaseFind(Triple m)
{
Node s = m.getMatchSubject() ;
Node s = m.getSubject() ;
Var sVar = null ;
if ( s == null )
{
sVar = Var.alloc("s") ;
s = sVar ;
}

Node p = m.getMatchPredicate() ;
Node p = m.getPredicate() ;
Var pVar = null ;
if ( p == null )
{
pVar = Var.alloc("p") ;
p = pVar ;
}

Node o = m.getMatchObject() ;
Node o = m.getObject() ;
Var oVar = null ;
if ( o == null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static Quad transform(NodeTransform nodeTransform, Quad quad) {

if ( !change )
return quad;
return new Quad(g, s, p, o);
return Quad.create(g, s, p, o);
}

public static Table transform(Table table, NodeTransform transform) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static List<Quad> remapDefaultGraph(List<Quad> quads, final Node dftGraph
if ( dftGraph == null || Quad.isDefaultGraph(dftGraph) )
return quads ;
Stream<Quad> remappedStream = quads.stream().map(q->
!q.isDefaultGraph() ? q : new Quad(dftGraph, q.getSubject(), q.getPredicate(), q.getObject())
!q.isDefaultGraph() ? q : Quad.create(dftGraph, q.getSubject(), q.getPredicate(), q.getObject())
) ;
return remappedStream.toList();
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public static Quad subst(Quad quad, Binding b, Map<Node, Node> bNodeMap) {

Quad q = quad;
if ( s1 != s || p1 != p || o1 != o || g1 != g )
q = new Quad(g1, s1, p1, o1);
q = Quad.create(g1, s1, p1, o1);

Quad q2 = Substitute.substitute(q, b);
return q2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void addQuad(Quad quad)
public void addTriple(Triple triple)
{
check(triple) ;
sink.send(new Quad(graphNode, triple)) ;
sink.send(Quad.create(graphNode, triple)) ;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int mark() {
@Override
public void addTriple(int index, Triple triple) {
check(triple);
quads.add(index, new Quad(graphNode, triple));
quads.add(index, Quad.create(graphNode, triple));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,6 @@ private static Quad _buildNode4(ItemList list) {
Node s = BuilderNode.buildNode(list.get(1));
Node p = BuilderNode.buildNode(list.get(2));
Node o = BuilderNode.buildNode(list.get(3));
return new Quad(g, s, p, o);
return Quad.create(g, s, p, o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public List<Quad> getQuads() {
if ( this.bgp != null ) {
List<Quad> quads = new ArrayList<>();
for ( Triple triple : this.bgp.getList() ) {
quads.add(new Quad(Quad.defaultGraphNodeGenerated, triple));
quads.add(Quad.create(Quad.defaultGraphNodeGenerated, triple));
}
return quads;
}
Expand Down
Loading