This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit 973c0663f43e348f7015add92fb279091f2517b9 Author: Andy Seaborne <[email protected]> AuthorDate: Fri Dec 5 12:32:46 2025 +0000 GH-3615: Remove Node.matches. Change Triple.matches to be wildcard matching --- .../src/main/java/org/apache/jena/graph/Node.java | 26 +++----- .../main/java/org/apache/jena/graph/Node_ANY.java | 5 -- .../java/org/apache/jena/graph/Node_Literal.java | 20 ++---- .../main/java/org/apache/jena/graph/Triple.java | 76 ++++++++++++---------- .../main/java/org/apache/jena/mem/ArrayBunch.java | 36 +++++----- .../main/java/org/apache/jena/mem/BunchMap.java | 8 +-- .../src/main/java/org/apache/jena/mem/ByValue.java | 60 +++++++++++++++++ .../org/apache/jena/mem/GraphTripleStoreBase.java | 42 ++++++------ .../org/apache/jena/mem/GraphTripleStoreMem.java | 12 ++-- .../org/apache/jena/mem/HashedTripleBunch.java | 5 +- .../org/apache/jena/mem/NodeToTriplesMapMem.java | 15 +++-- .../main/java/org/apache/jena/mem/TripleBunch.java | 18 ++--- .../jena/reasoner/rulesys/Node_RuleVariable.java | 16 ++--- .../apache/jena/graph/test/TestFindLiterals.java | 2 - .../java/org/apache/jena/graph/test/TestNode.java | 32 +++++---- .../org/apache/jena/graph/test/TestTriple.java | 14 ---- .../apache/jena/graph/test/TestTypedLiterals.java | 17 ++--- .../apache/jena/mem/test/TestGraphMemModel.java | 25 +------ 18 files changed, 222 insertions(+), 207 deletions(-) diff --git a/jena-core/src/main/java/org/apache/jena/graph/Node.java b/jena-core/src/main/java/org/apache/jena/graph/Node.java index ce51927902..35d1d20679 100644 --- a/jena-core/src/main/java/org/apache/jena/graph/Node.java +++ b/jena-core/src/main/java/org/apache/jena/graph/Node.java @@ -21,7 +21,6 @@ package org.apache.jena.graph; import java.io.IOException; import java.io.ObjectStreamException; import java.io.Serializable; -import java.util.Objects; import java.util.function.Function; import org.apache.jena.datatypes.RDFDatatype ; @@ -248,10 +247,11 @@ public abstract class Node implements Serializable { /** * RDF term equality. + * <p> + * The {@link Node} argument must not be null. */ - public boolean sameTermAs(Object o) { - Objects.requireNonNull(o); - return equals(o); + public boolean sameTermAs(Object node) { + return equals(node); } /** @@ -262,9 +262,12 @@ public abstract class Node implements Serializable { * equivalent but distinguished by the java equals function. * <p>Default implementation is to use {@link #equals} * subclasses should override this.</p> + * <p> + * The {@link Node} argument must not be null. */ - public boolean sameValueAs(Object o) - { return equals( o ); } + public boolean sameValueAs(Object node) { + return equals(node); + } /** Answer a human-readable representation of this Node. */ @Override @@ -280,17 +283,6 @@ public abstract class Node implements Serializable { @Override public abstract int hashCode(); - /** - * Answer true iff this node accepts the other one as a match. The default is an - * equality test; it is over-ridden in subclasses to provide the appropriate - * semantics for literals, ANY, and variables. - * - * @param other a node to test for matching - * @return true iff this node accepts the other as a match - */ - public boolean matches( Node other ) - { return equals( other ); } - // ---- Serializable // Must be "protected", not "private". protected Object writeReplace() throws ObjectStreamException { diff --git a/jena-core/src/main/java/org/apache/jena/graph/Node_ANY.java b/jena-core/src/main/java/org/apache/jena/graph/Node_ANY.java index 2472616eb6..bfa15ac276 100644 --- a/jena-core/src/main/java/org/apache/jena/graph/Node_ANY.java +++ b/jena-core/src/main/java/org/apache/jena/graph/Node_ANY.java @@ -52,11 +52,6 @@ public class Node_ANY extends Node { return v.visitAny(this); } - @Override - public boolean matches(Node other) { - return other != null; - } - @Override public String toString( PrefixMapping pmap ) { return toString(); } diff --git a/jena-core/src/main/java/org/apache/jena/graph/Node_Literal.java b/jena-core/src/main/java/org/apache/jena/graph/Node_Literal.java index 21d97c7941..92050a7aed 100644 --- a/jena-core/src/main/java/org/apache/jena/graph/Node_Literal.java +++ b/jena-core/src/main/java/org/apache/jena/graph/Node_Literal.java @@ -125,23 +125,11 @@ public class Node_Literal extends Node return label.equals(other.label); } - /** - * Test that two nodes are equivalent as values. - * In some cases this may be the same as "same term", in others - * equals is stricter. For example, two xsd:int literals with - * the same value if they are "01" and "1". - * <p>Default implementation is to use equals, subclasses should - * override this.</p> - */ - @Override - public boolean sameValueAs(Object o) { - return o instanceof Node_Literal - && label.sameValueAs( ((Node_Literal) o).getLiteral() ); - } - @Override - public boolean matches(Node x) { - return sameValueAs(x); + public boolean sameValueAs(Object other) { + if ( other instanceof Node_Literal otherLiteral ) + return label.sameValueAs( otherLiteral.getLiteral() ); + return false; } @Override diff --git a/jena-core/src/main/java/org/apache/jena/graph/Triple.java b/jena-core/src/main/java/org/apache/jena/graph/Triple.java index 8111c0824e..938b9149a3 100644 --- a/jena-core/src/main/java/org/apache/jena/graph/Triple.java +++ b/jena-core/src/main/java/org/apache/jena/graph/Triple.java @@ -34,7 +34,28 @@ import org.apache.jena.sys.Serializer; */ public class Triple implements Serializable { - private final Node subj, pred, obj; + /** + * Create triple, + * Nulls are not permnitted. + */ + public static Triple create(Node s, Node p, Node o) { + if ( isAny(s) && isAny(p) && isAny(o) ) + return Triple.ANY; + return new Triple(s, p, o); + } + + /** + * Create triple, + * Nulls are converted to {@link Node#ANY}. + */ + public static Triple createMatch( Node s, Node p, Node o ) + { return Triple.create( nullToAny( s ), nullToAny( p ), nullToAny( o ) ); } + + /** + A Triple that has {@link Node#ANY} in all fields. + */ + public static final Triple ANY = new Triple( Node.ANY, Node.ANY, Node.ANY ); + private final Node subj, pred, obj; protected Triple( Node s, Node p, Node o ) { if (s == null) throw new UnsupportedOperationException( "subject cannot be null" ); @@ -114,11 +135,9 @@ public class Triple implements Serializable public Node getMatchObject() { return anyToNull( getObject() ); } - @Deprecated(forRemoval = true) private static Node anyToNull( Node n ) { return Node.ANY.equals( n ) ? null : n; } - @Deprecated(forRemoval = true) private static Node nullToAny( Node n ) { return n == null ? Node.ANY : n; } @@ -138,28 +157,33 @@ public class Triple implements Serializable /** Answer true iff this triple has subject s, predicate p, and object o. + The relationship is "same term". + Use {@link #matches(Node, Node, Node)} for wildcards. */ public boolean sameAs( Node s, Node p, Node o ) - { return subj.equals( s ) && pred.equals( p ) && obj.equals( o ); } + { return subj.sameTermAs( s ) && pred.sameTermAs( p ) && obj.sameTermAs( o ); } - /** Does this triple, used as a pattern match, the other triple (usually a ground triple) */ + /** + * Does this triple, match the other triple, allowing for wildcards. + * The wildcard node is {@link Node#ANY} and it matches any node, including a wildcard. + * Both this triple and the argument triple may contain wildcards, + * that is "matches" is symmetric: + * {@code this.matches(that) == that.matches(this)}. + */ public boolean matches( Triple other ) - { return other.matchedBy( subj, pred, obj ); } + { return matches(other.getSubject(), other.getPredicate(), other.getObject()); } public boolean matches( Node s, Node p, Node o ) - { return subj.matches( s ) && pred.matches( p ) && obj.matches( o ); } - - private boolean matchedBy( Node s, Node p, Node o ) - { return s.matches( subj ) && p.matches( pred ) && o.matches( obj ); } - - public boolean subjectMatches( Node s ) - { return subj.matches( s ); } - - public boolean predicateMatches( Node p ) - { return pred.matches( p ); } - - public boolean objectMatches( Node o ) - { return obj.matches( o ); } + { return matches(subj, s) && matches(pred, p) && matches(obj, 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); + } // ---- Serializable protected Object writeReplace() throws ObjectStreamException { @@ -194,20 +218,6 @@ public class Triple implements Serializable public static int hashCode( Node s, Node p, Node o ) { return (s.hashCode() >> 1) ^ p.hashCode() ^ (o.hashCode() << 1); } - public static Triple create(Node s, Node p, Node o) { - if ( isAny(s) && isAny(p) && isAny(o) ) - return Triple.ANY; - return new Triple(s, p, o); - } - - public static Triple createMatch( Node s, Node p, Node o ) - { return Triple.create( nullToAny( s ), nullToAny( p ), nullToAny( o ) ); } - - /** - A Triple that has {@link Node#ANY} in all fields. - */ - public static final Triple ANY = new Triple( Node.ANY, Node.ANY, Node.ANY ); - /** A Field is a selector from Triples; it allows selectors to be passed around as if they were functions. diff --git a/jena-core/src/main/java/org/apache/jena/mem/ArrayBunch.java b/jena-core/src/main/java/org/apache/jena/mem/ArrayBunch.java index 6349cf7c18..e7e5b9a668 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/ArrayBunch.java +++ b/jena-core/src/main/java/org/apache/jena/mem/ArrayBunch.java @@ -34,22 +34,28 @@ import org.apache.jena.util.iterator.NiceIterator ; */ public class ArrayBunch implements TripleBunch { - + protected int size = 0; protected Triple [] elements; - protected volatile int changes = 0; + protected volatile int changes = 0; public ArrayBunch() { elements = new Triple[5]; } - + @Override public boolean containsBySameValueAs( Triple t ) { int i = size; - while (i > 0) if (t.matches( elements[--i])) return true; + //while (i > 0) if (t.matches( elements[--i])) return true; + + while (i > 0) { + if ( ByValue.sameByValue(t,elements[--i]) ) + return true; + } + return false; } - + @Override public boolean contains( Triple t ) { @@ -57,24 +63,24 @@ public class ArrayBunch implements TripleBunch while (i > 0) if (t.equals( elements[--i] )) return true; return false; } - + @Override public int size() { return size; } - + @Override public void add( Triple t ) - { + { if (size == elements.length) grow(); - elements[size++] = t; + elements[size++] = t; changes++; } - + /** Note: linear growth is suboptimal (order n<sup>2</sup>) normally, but ArrayBunch's are meant for <i>small</i> sets and are replaced by some sort of hash- or tree- set when they get big; currently "big" means more - than 9 elements, so that's only one growth spurt anyway. + than 9 elements, so that's only one growth spurt anyway. */ protected void grow() { @@ -101,21 +107,21 @@ public class ArrayBunch implements TripleBunch return iterator( new HashCommon.NotifyEmpty() { @Override public void emptied() {} } ); } - + @Override public ExtendedIterator<Triple> iterator( final HashCommon.NotifyEmpty container ) { return new NiceIterator<Triple>() { protected final int initialChanges = changes; - + protected int i = size; @Override public boolean hasNext() - { + { return 0 < i; } - + @Override public Triple next() { if (changes != initialChanges) throw new ConcurrentModificationException(); diff --git a/jena-core/src/main/java/org/apache/jena/mem/BunchMap.java b/jena-core/src/main/java/org/apache/jena/mem/BunchMap.java index 79f97086f8..06b4c2b424 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/BunchMap.java +++ b/jena-core/src/main/java/org/apache/jena/mem/BunchMap.java @@ -35,14 +35,14 @@ public interface BunchMap Clear this map: all entries are removed. */ public void clear(); - + /** The number of items in the bunch. */ public long size(); /** - Answer the TripleBunch associated with <code>key</code>, or + Answer the TripleBunch associated with <code>key</code>, or <code>null</code> if there isn't one. */ public TripleBunch get( Object key ); @@ -53,10 +53,10 @@ public interface BunchMap will now deliver this value. */ public void put( Object key, TripleBunch value ); - + /** Get the <code>key</code> and return the value found there; if nothing, - calculate the <code>value</code> and insert. Return the value now the slot. + calculate the <code>value</code> and insert. Return the value now the slot. */ public TripleBunch getOrSet( Object key, Function<Object, TripleBunch> setter ); diff --git a/jena-core/src/main/java/org/apache/jena/mem/ByValue.java b/jena-core/src/main/java/org/apache/jena/mem/ByValue.java new file mode 100644 index 0000000000..f0df6e67c6 --- /dev/null +++ b/jena-core/src/main/java/org/apache/jena/mem/ByValue.java @@ -0,0 +1,60 @@ +/* + * 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.mem; + +import org.apache.jena.graph.Node; +import org.apache.jena.graph.Triple; + +/** + * Value related "sameness" for the java-GraphMemValue + * in support of the storage in ArrayBunch and HashedTruipleBunch. + */ +class ByValue { + + /** + * Test two triples, for same-by-value. + * <p> + * URIs and blank nodes compare by same term. + * <p> + * Two literals are be "same by value" if they represent the same value + * as mapped by the Jena Model API. This is not (quite) the same as XSD. + * <p> + * Triple terms equate by recursive applications. + * <p> + * The triples should be concrete (Node.ANY is not a wildcard). + */ + + /*package*/ static boolean sameByValue(Triple t1, Triple t2) { + return sameByValue(t1.getSubject(), t2.getSubject()) && + sameByValue(t1.getPredicate(), t2.getPredicate()) && + sameByValue(t1.getObject(), t2.getObject()); + } + + private static boolean sameByValue(Node n1, Node n2) { + if ( n1.isTripleTerm() ) { + if ( n2.isTripleTerm() ) { + // Apply same value recursively. + Triple n1Triple = n1.getTriple(); + Triple n2Triple = n2.getTriple(); + return sameByValue(n1Triple, n2Triple); + } + } + return n1.sameValueAs(n2); + } +} diff --git a/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreBase.java b/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreBase.java index d7570a8a1d..834314787b 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreBase.java +++ b/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreBase.java @@ -33,25 +33,25 @@ public abstract class GraphTripleStoreBase implements TripleStore protected NodeToTriplesMapBase subjects; protected NodeToTriplesMapBase predicates; protected NodeToTriplesMapBase objects; - + protected GraphTripleStoreBase ( Graph parent, NodeToTriplesMapBase subjects, NodeToTriplesMapBase predicates, NodeToTriplesMapBase objects ) - { - this.parent = parent; + { + this.parent = parent; this.subjects = subjects; this.objects = objects; this.predicates = predicates; - } - + } + /** Destroy this triple store - discard the indexes. */ @Override public void close() { subjects = predicates = objects = null; } - + /** Add a triple to this triple store. */ @@ -61,10 +61,10 @@ public abstract class GraphTripleStoreBase implements TripleStore if (subjects.add( t )) { predicates.add( t ); - objects.add( t ); + objects.add( t ); } } - + /** Remove a triple from this triple store. */ @@ -74,10 +74,10 @@ public abstract class GraphTripleStoreBase implements TripleStore if (subjects.remove( t )) { predicates.remove( t ); - objects.remove( t ); + objects.remove( t ); } } - + /** Clear this store, ie remove all triples from it. */ @@ -95,25 +95,25 @@ public abstract class GraphTripleStoreBase implements TripleStore @Override public int size() { return subjects.size(); } - + /** Answer true iff this triple store is empty. */ @Override public boolean isEmpty() { return subjects.isEmpty(); } - + @Override public ExtendedIterator<Node> listSubjects() { return expectOnlyNodes( subjects.domain() ); } - + @Override public ExtendedIterator<Node> listPredicates() { return expectOnlyNodes( predicates.domain() ); } - + private ExtendedIterator<Node> expectOnlyNodes( Iterator<Object> elements ) { return WrappedIterator.createNoRemove( elements ).mapWith( o -> (Node) o ); } - + @Override public ExtendedIterator<Node> listObjects() { @@ -123,7 +123,7 @@ public abstract class GraphTripleStoreBase implements TripleStore { return objects.iteratorForIndexed( y ); } }; } - + /** Answer true iff this triple store contains the (concrete) triple <code>t</code>. */ @@ -147,16 +147,16 @@ public abstract class GraphTripleStoreBase implements TripleStore return !this.isEmpty(); } - /** + /** Answer an ExtendedIterator returning all the triples from this store that match the pattern <code>m = (S, P, O)</code>. - + <p>Because the node-to-triples maps index on each of subject, predicate, and (non-literal) object, concrete S/P/O patterns can immediately select an appropriate map. Because the match for literals must be by sameValueAs, not equality, the optimisation is not applied for literals. [This is probably a Bad Thing for strings.] - + <p>Practice suggests doing the predicate test <i>last</i>, because there are "usually" many more statements than predicates, so the predicate doesn't cut down the search space very much. By "practice suggests" I mean that @@ -167,10 +167,10 @@ public abstract class GraphTripleStoreBase implements TripleStore @Override public ExtendedIterator<Triple> find( Triple t ) { + Node sm = t.getSubject(); Node pm = t.getPredicate(); Node om = t.getObject(); - Node sm = t.getSubject(); - + if (sm.isConcrete()) return new StoreTripleIterator( parent, subjects.iterator( sm, pm, om ), subjects, predicates, objects ); else if (om.isConcrete()) diff --git a/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreMem.java b/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreMem.java index 38974581ce..03aa49cd7e 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreMem.java +++ b/jena-core/src/main/java/org/apache/jena/mem/GraphTripleStoreMem.java @@ -23,23 +23,23 @@ import org.apache.jena.graph.Triple.Field ; import org.apache.jena.graph.impl.TripleStore ; public class GraphTripleStoreMem extends GraphTripleStoreBase implements TripleStore - { + { public GraphTripleStoreMem( Graph parent ) - { + { super( parent, new NodeToTriplesMapMem( Field.fieldSubject, Field.fieldPredicate, Field.fieldObject ), new NodeToTriplesMapMem( Field.fieldPredicate, Field.fieldObject, Field.fieldSubject ), new NodeToTriplesMapMem( Field.fieldObject, Field.fieldSubject, Field.fieldPredicate ) - ); + ); } - + public NodeToTriplesMapMem getSubjects() { return (NodeToTriplesMapMem) subjects; } public NodeToTriplesMapMem getPredicates() { return (NodeToTriplesMapMem) predicates; } - + public NodeToTriplesMapMem getObjects() { return (NodeToTriplesMapMem) objects; } - + } diff --git a/jena-core/src/main/java/org/apache/jena/mem/HashedTripleBunch.java b/jena-core/src/main/java/org/apache/jena/mem/HashedTripleBunch.java index 5369e89d6d..7daba35859 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/HashedTripleBunch.java +++ b/jena-core/src/main/java/org/apache/jena/mem/HashedTripleBunch.java @@ -44,9 +44,10 @@ public class HashedTripleBunch extends HashCommon<Triple> implements TripleBunch int index = initialIndexFor( key ); while (true) { - Object current = keys[index]; + Triple current = keys[index]; if (current == null) return index; - if (key.matches( (Triple) current )) return ~index; + if ( ByValue.sameByValue(key, current) ) + return ~index; if (--index < 0) index += capacity; } } diff --git a/jena-core/src/main/java/org/apache/jena/mem/NodeToTriplesMapMem.java b/jena-core/src/main/java/org/apache/jena/mem/NodeToTriplesMapMem.java index 8c7dc32100..20a52257c2 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/NodeToTriplesMapMem.java +++ b/jena-core/src/main/java/org/apache/jena/mem/NodeToTriplesMapMem.java @@ -130,15 +130,16 @@ public class NodeToTriplesMapMem extends NodeToTriplesMapBase { Object indexValue = index.getIndexingValue(); TripleBunch s = bunchMap.get( indexValue ); - if (s == null) return NullIterator.<Triple>instance(); - var filter = FieldFilter.filterOn(f2, n2, f3, n3); - return filter.hasFilter() - ? s.iterator( new NotifyMe( indexValue ) ).filterKeep( filter.getFilter() ) - : s.iterator( new NotifyMe( indexValue ) ); + if (s == null) + return NullIterator.instance(); + var filter = FieldFilter.filterOn(f2, n2, f3, n3); + return filter.hasFilter() + ? s.iterator( new NotifyMe( indexValue ) ).filterKeep( filter.getFilter() ) + : s.iterator( new NotifyMe( indexValue ) ); } - protected TripleBunch get( Object index ) - { return bunchMap.get( index ); } + protected TripleBunch get( Object index ) + { return bunchMap.get( index ); } /** Answer an iterator over all the triples that are indexed by the item <code>y</code>. diff --git a/jena-core/src/main/java/org/apache/jena/mem/TripleBunch.java b/jena-core/src/main/java/org/apache/jena/mem/TripleBunch.java index 2dfb353932..27255d8336 100644 --- a/jena-core/src/main/java/org/apache/jena/mem/TripleBunch.java +++ b/jena-core/src/main/java/org/apache/jena/mem/TripleBunch.java @@ -25,17 +25,17 @@ import java.util.Spliterator; /** A bunch of triples - a stripped-down set with specialized methods. A - bunch is expected to store triples that share some useful property + bunch is expected to store triples that share some useful property (such as having the same subject or predicate). */ -public interface TripleBunch +public interface TripleBunch { /** Answer true iff this TripleBunch contains a triple .equals to <code>t</code>. */ public abstract boolean contains( Triple t ); - + /** Answer true iff this TripleBunch contains a triple with .sameValueAs subject, predicate, and object. (Typically this only matters for the @@ -43,31 +43,31 @@ public interface TripleBunch zeroes can be .sameValueAs but not .equals). */ public abstract boolean containsBySameValueAs( Triple t ); - + /** Answer the number of triples in this bunch. */ public abstract int size(); - + /** Add <code>t</code> to the triples in this bunch. If <code>t</code> is already a member, nothing happens. The bunch now .contains this triple. */ public abstract void add( Triple t ); - + /** Remove <code>t</code> from the triples in this bunch. If it wasn't a member, nothing happens. The bunch no longer .contains this triple. */ public abstract void remove( Triple t ); - + /** Answer an iterator over all the triples in this bunch. It is unwise to .remove from this iterator. (It may become illegal.) */ public abstract ExtendedIterator<Triple> iterator(); - + /** Answer an iterator over all the triples in this bunch. If use of .remove on this iterator empties the bunch, the <code>emptied</code> method of @@ -79,5 +79,5 @@ public interface TripleBunch Answer a spliterator over all the triples in this bunch. */ public abstract Spliterator<Triple> spliterator(); - + } diff --git a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Node_RuleVariable.java b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Node_RuleVariable.java index 2c4764c115..5f54e3c2dd 100755 --- a/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Node_RuleVariable.java +++ b/jena-core/src/main/java/org/apache/jena/reasoner/rulesys/Node_RuleVariable.java @@ -134,7 +134,7 @@ public class Node_RuleVariable extends Node_Variable { } /** - * Clone the rule variable to allow multiple rule instaces to be active at the same time. + * Clone the rule variable to allow multiple rule instances to be active at the same time. */ public Node_RuleVariable cloneNode() { return new Node_RuleVariable(getName(), index); @@ -149,12 +149,12 @@ public class Node_RuleVariable extends Node_Variable { @Override public boolean equals(Object o) { - if (o instanceof Node_RuleVariable) { + if (o instanceof Node_RuleVariable nrv) { String name = getName(); if (name == null) { return this == o; } else { - return name.equals( ((Node_RuleVariable)o).getName() ); + return name.equals( nrv.getName() ); } } else return false; @@ -174,17 +174,17 @@ public class Node_RuleVariable extends Node_Variable { * Test that two nodes are semantically equivalent. */ @Override - public boolean sameValueAs(Object o) { - return o instanceof Node_RuleVariable; + public boolean sameValueAs(Object other) { + return other instanceof Node_RuleVariable; } /** * Compare two nodes, taking into account variable indices. */ public static boolean sameNodeAs(Node n, Node m) { - if (n instanceof Node_RuleVariable) { - if (m instanceof Node_RuleVariable) { - return ((Node_RuleVariable)n).getIndex() == ((Node_RuleVariable)m).getIndex(); + if (n instanceof Node_RuleVariable nrv_n) { + if (m instanceof Node_RuleVariable nrv_m) { + return nrv_n.getIndex() == nrv_m.getIndex(); } else { return false; } diff --git a/jena-core/src/test/java/org/apache/jena/graph/test/TestFindLiterals.java b/jena-core/src/test/java/org/apache/jena/graph/test/TestFindLiterals.java index f6e6397c5b..3054043805 100644 --- a/jena-core/src/test/java/org/apache/jena/graph/test/TestFindLiterals.java +++ b/jena-core/src/test/java/org/apache/jena/graph/test/TestFindLiterals.java @@ -140,8 +140,6 @@ public class TestFindLiterals extends GraphTestBase { assertFalse(A.equals(B)); assertFalse(A.sameValueAs(B)); assertFalse(B.sameValueAs(A)); - assertFalse(A.matches(B)); - assertFalse(B.matches(A)); } @SuppressWarnings("deprecation") diff --git a/jena-core/src/test/java/org/apache/jena/graph/test/TestNode.java b/jena-core/src/test/java/org/apache/jena/graph/test/TestNode.java index 62be285e83..ed7271d26e 100644 --- a/jena-core/src/test/java/org/apache/jena/graph/test/TestNode.java +++ b/jena-core/src/test/java/org/apache/jena/graph/test/TestNode.java @@ -621,22 +621,21 @@ public class TestNode extends GraphTestBase { } public void testSimpleMatches() { - assertTrue(NodeCreateUtils.create("S").matches(NodeCreateUtils.create("S"))); - assertFalse("", NodeCreateUtils.create("S").matches(NodeCreateUtils.create("T"))); - assertFalse("", NodeCreateUtils.create("S").matches(null)); - assertTrue(NodeCreateUtils.create("_X").matches(NodeCreateUtils.create("_X"))); - assertFalse("", NodeCreateUtils.create("_X").matches(NodeCreateUtils.create("_Y"))); - assertFalse("", NodeCreateUtils.create("_X").matches(null)); - assertTrue(NodeCreateUtils.create("10").matches(NodeCreateUtils.create("10"))); - assertFalse("", NodeCreateUtils.create("10").matches(NodeCreateUtils.create("11"))); - assertFalse("", NodeCreateUtils.create("10").matches(null)); - assertTrue(Node.ANY.matches(NodeCreateUtils.create("S"))); - assertTrue(Node.ANY.matches(NodeCreateUtils.create("_X"))); - assertTrue(Node.ANY.matches(NodeCreateUtils.create("10"))); - assertFalse("", Node.ANY.matches(null)); - } - - public void testDataMatches() { + assertTrue(NodeCreateUtils.create("S").sameTermAs(NodeCreateUtils.create("S"))); + assertFalse("", NodeCreateUtils.create("S").sameTermAs(NodeCreateUtils.create("T"))); + + assertTrue(NodeCreateUtils.create("_X").sameTermAs(NodeCreateUtils.create("_X"))); + assertFalse("", NodeCreateUtils.create("_X").sameTermAs(NodeCreateUtils.create("_Y"))); + assertTrue(NodeCreateUtils.create("10").sameTermAs(NodeCreateUtils.create("10"))); + assertFalse("", NodeCreateUtils.create("10").sameTermAs(NodeCreateUtils.create("11"))); + + assertFalse("", NodeCreateUtils.create("S").sameTermAs(null)); + assertFalse("", NodeCreateUtils.create("_X").sameTermAs(null)); + assertFalse("", NodeCreateUtils.create("10").sameTermAs(null)); + assertFalse("", Node.ANY.sameTermAs(null)); + } + + public void testDataSameValue() { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dt1 = tm.getTypeByValue(Integer.valueOf(10)); RDFDatatype dt2 = tm.getTypeByValue(Short.valueOf((short)10)); @@ -644,7 +643,6 @@ public class TestNode extends GraphTestBase { Node b = NodeFactory.createLiteralDT("10", dt2); assertDiffer("types must make a difference", a, b); assertTrue("A and B must express the same value", a.sameValueAs(b)); - assertTrue("matching literals must respect sameValueAs", a.matches(b)); } public void testLiteralToString() { diff --git a/jena-core/src/test/java/org/apache/jena/graph/test/TestTriple.java b/jena-core/src/test/java/org/apache/jena/graph/test/TestTriple.java index 58502e371a..29b05e2a6b 100644 --- a/jena-core/src/test/java/org/apache/jena/graph/test/TestTriple.java +++ b/jena-core/src/test/java/org/apache/jena/graph/test/TestTriple.java @@ -180,20 +180,6 @@ public class TestTriple extends GraphTestBase { assertFalse(NodeCreateUtils.createTriple("S P O").matches(node("Z"), node("P"), node("I"))); } - public void testElementMatches() { - assertTrue(NodeCreateUtils.createTriple("S P O").subjectMatches(node("S"))); - assertTrue(NodeCreateUtils.createTriple("S P O").predicateMatches(node("P"))); - assertTrue(NodeCreateUtils.createTriple("S P O").objectMatches(node("O"))); - /* */ - assertFalse(NodeCreateUtils.createTriple("S P O").subjectMatches(node("Z"))); - assertFalse(NodeCreateUtils.createTriple("S P O").predicateMatches(node("Q"))); - assertFalse(NodeCreateUtils.createTriple("S P O").objectMatches(node("I"))); - /* */ - assertTrue(NodeCreateUtils.createTriple("?? P O").subjectMatches(node("SUB"))); - assertTrue(NodeCreateUtils.createTriple("S ?? O").predicateMatches(node("PRED"))); - assertTrue(NodeCreateUtils.createTriple("S P ??").objectMatches(node("OBJ"))); - } - public void testConcrete() { assertTrue(NodeCreateUtils.createTriple("S P O").isConcrete()); assertTrue(NodeCreateUtils.createTriple("S P 11").isConcrete()); diff --git a/jena-core/src/test/java/org/apache/jena/graph/test/TestTypedLiterals.java b/jena-core/src/test/java/org/apache/jena/graph/test/TestTypedLiterals.java index 22872a010f..f13989200f 100644 --- a/jena-core/src/test/java/org/apache/jena/graph/test/TestTypedLiterals.java +++ b/jena-core/src/test/java/org/apache/jena/graph/test/TestTypedLiterals.java @@ -774,20 +774,21 @@ public class TestTypedLiterals extends TestCase { assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass()); assertEquals("DateTime from date", "2004-04-21T19:50:42Z", l1.getValue().toString()); // System.err.println("date is: "+ncal.getTime()); - } /** * Test query applied to graphs containing typed values */ public void testTypedContains() { - Model model = ModelFactory.createDefaultModel(); + Model model = ModelFactory.createModelSameValue(); Property p = model.createProperty("urn:x-eg/p"); Literal l1 = model.createTypedLiteral("10", "http://www.w3.org/2001/XMLSchema#integer"); Literal l2 = model.createTypedLiteral("010", "http://www.w3.org/2001/XMLSchema#integer"); assertSameValueAs("sameas test", l1, l2); + Resource a = model.createResource("urn:x-eg/a"); - a.addProperty(p, l1); + model.add(a, p, l1); + assertTrue(model.getGraph().contains(a.asNode(), p.asNode(), l1.asNode())); assertTrue(model.getGraph().contains(a.asNode(), p.asNode(), l2.asNode())); } @@ -1279,7 +1280,7 @@ class RationalType extends BaseDatatype { /** * Parse a lexical form of this datatype to a value - * + * * @throws DatatypeFormatException if the lexical form is not legal */ @Override @@ -1322,7 +1323,7 @@ class Rational { /** * Returns the denominator. - * + * * @return int */ public int getDenominator() { @@ -1331,7 +1332,7 @@ class Rational { /** * Returns the numerator. - * + * * @return int */ public int getNumerator() { @@ -1340,7 +1341,7 @@ class Rational { /** * Sets the denominator. - * + * * @param denominator The denominator to set */ public void setDenominator(int denominator) { @@ -1349,7 +1350,7 @@ class Rational { /** * Sets the numerator. - * + * * @param numerator The numerator to set */ public void setNumerator(int numerator) { diff --git a/jena-core/src/test/java/org/apache/jena/mem/test/TestGraphMemModel.java b/jena-core/src/test/java/org/apache/jena/mem/test/TestGraphMemModel.java index f53083cf3a..d8961757ed 100644 --- a/jena-core/src/test/java/org/apache/jena/mem/test/TestGraphMemModel.java +++ b/jena-core/src/test/java/org/apache/jena/mem/test/TestGraphMemModel.java @@ -23,9 +23,10 @@ import static org.apache.jena.testing_framework.GraphHelper.txnCommit; import static org.apache.jena.testing_framework.GraphHelper.txnRollback; import static org.apache.jena.testing_framework.GraphHelper.txnRun; -import java.util.Iterator; import java.util.Set; +import org.junit.Test; + import junit.framework.JUnit4TestAdapter; import org.apache.jena.atlas.iterator.Iter; import org.apache.jena.graph.*; @@ -34,7 +35,6 @@ import org.apache.jena.graph.test.AbstractTestGraph; import org.apache.jena.mem.GraphMemValue; import org.apache.jena.testing_framework.NodeCreateUtils; import org.apache.jena.util.iterator.ExtendedIterator; -import org.junit.Test; /** * Tests of a graph which has support for (Java object) value handling. @@ -290,25 +290,4 @@ public class TestGraphMemModel extends AbstractTestGraph { it.removeNext(); assertFalse(g.find(Node.ANY, Node.ANY, Node.ANY).hasNext()); } - - @Test - public void testUnnecessaryMatches() { - Node special = new Node_URI("eg:foo") { - @Override - public boolean matches(Node s) { - fail("Matched called superfluously."); - return true; - } - }; - Graph g = getGraphWith("x p y"); - g.add(Triple.create(special, special, special)); - exhaust(g.find(special, Node.ANY, Node.ANY)); - exhaust(g.find(Node.ANY, special, Node.ANY)); - exhaust(g.find(Node.ANY, Node.ANY, special)); - } - - protected void exhaust(Iterator<? > it) { - while (it.hasNext()) - it.next(); - } }
