commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [40/45] commons-rdf git commit: COMMONSRDF-56: Workaround for JSON-LD Literal comparisons
Date Fri, 10 Feb 2017 16:55:42 GMT
COMMONSRDF-56: Workaround for JSON-LD Literal comparisons


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/e351d55d
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/e351d55d
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/e351d55d

Branch: refs/heads/COMMONSRDF-47
Commit: e351d55d1a1e529f88398c6233d3a01dd799c87a
Parents: dc4af99
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Wed Feb 8 22:36:26 2017 +0000
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Wed Feb 8 22:36:26 2017 +0000

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdGraphLike.java |  18 ++-
 .../commons/rdf/jsonldjava/JsonLdLiteral.java   |  12 +-
 .../rdf/jsonldjava/JsonLdComparisonTest.java    | 141 +++++++++++++++++++
 3 files changed, 162 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/e351d55d/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
index 1549109..e5fc75e 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
@@ -204,9 +204,23 @@ abstract class AbstractJsonLdGraphLike<T extends TripleLike> implements
JsonLdGr
             if (predicateNode.isPresent() && predicateNode.get().compareTo(q.getPredicate())
!= 0) {
                 return false;
             }
-            if (objectNode.isPresent() && objectNode.get().compareTo(q.getObject())
!= 0) {
-                return false;
+            if (objectNode.isPresent()) {
+                if (object instanceof Literal && q.getObject().isLiteral()) { 
+                    // Special handling for COMMONSRDF-56, COMMONSRDF-51:
+                    // Less efficient wrapper to a Commons RDF Literal so 
+                    // we can use our RDF 1.1-compliant .equals()
+                    RDFTerm otherObj = factory.asRDFTerm(q.getObject());
+                    if (! (object.equals(otherObj))) {
+                        return false;
+                    }
+                } else {
+                    // JSONLD-Java's .compareTo can handle IRI, BlankNode and type-mismatch
+                    if (objectNode.get().compareTo(q.getObject()) != 0) {
+                        return false;
+                    }
+                }
             }
+            // All patterns checked, must be good!
             return true;
         };
     }

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/e351d55d/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
index 0d63c29..7b0aa45 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
@@ -87,17 +87,15 @@ class JsonLdLiteralImpl extends JsonLdTermImpl implements JsonLdLiteral
{
 
     @Override
     public boolean equals(final Object obj) {
-        if (obj instanceof JsonLdLiteral) {
-            final JsonLdLiteral other = (JsonLdLiteral) obj;
-            return asJsonLdNode().compareTo(other.asJsonLdNode()) == 0;
-        }
+        // COMMONSRDF-56: Do **not** use 
+        // asJsonLdNode().compareTo(other.asJsonLdNode())
         if (obj instanceof Literal) {
             final Literal other = (Literal) obj;
-            return getLexicalForm().equals(other.getLexicalForm()) && getDatatype().equals(other.getDatatype())
+            return getLexicalForm().equals(other.getLexicalForm()) 
+                    && getDatatype().equals(other.getDatatype())
                     && getLanguageTag().map(JsonLdLiteralImpl::lowerCase)
-                    .equals(other.getLanguageTag().map(JsonLdLiteralImpl::lowerCase));
+                        .equals(other.getLanguageTag().map(JsonLdLiteralImpl::lowerCase));
         }
         return false;
-
     }
 }

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/e351d55d/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdComparisonTest.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdComparisonTest.java
b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdComparisonTest.java
new file mode 100644
index 0000000..f3981be
--- /dev/null
+++ b/jsonld-java/src/test/java/org/apache/commons/rdf/jsonldjava/JsonLdComparisonTest.java
@@ -0,0 +1,141 @@
+/**
+ * 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.commons.rdf.jsonldjava;
+
+import static org.junit.Assert.*;
+
+import java.util.Optional;
+
+import org.apache.commons.rdf.simple.Types;
+import org.junit.Test;
+
+/**
+ * COMMONSRDF-56: Test Literal comparisons with JSONLD-Java
+ */
+public class JsonLdComparisonTest {
+    
+    JsonLdRDF rdf = new JsonLdRDF();
+    
+    @Test
+    public void literalEqual() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        JsonLdLiteral lit2 = rdf.createLiteral("Hello");
+        JsonLdLiteral lit3 = rdf.createLiteral("Hello", Types.XSD_STRING);
+        assertEquals(lit1, lit2);
+        assertEquals(lit1, lit3);
+    }
+
+    @Test
+    public void literalNotEqual() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        JsonLdLiteral lit2 = rdf.createLiteral("Hello there");
+        assertNotEquals(lit1, lit2);
+    }
+
+    @Test
+    public void literalEqualLang() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("Allo Allo", "fr");
+        JsonLdLiteral lit2 = rdf.createLiteral("Allo Allo", "fr");
+        assertEquals(lit1, lit2);
+    }
+    
+    @Test
+    public void literalNotEqualLang() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello", "en");
+        JsonLdLiteral lit2 = rdf.createLiteral("Hello", "en-us");
+        assertNotEquals(lit1, lit2);
+    }
+
+    @Test
+    public void literalEqualType() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("1", Types.XSD_INTEGER);
+        JsonLdLiteral lit2 = rdf.createLiteral("1", Types.XSD_INTEGER);
+        assertEquals(lit1, lit2);
+    }
+
+    
+    @Test
+    public void literalNotEqualType() throws Exception {
+        JsonLdLiteral lit1 = rdf.createLiteral("1", Types.XSD_INTEGER);
+        JsonLdLiteral lit2 = rdf.createLiteral("2", Types.XSD_INTEGER);
+        JsonLdLiteral lit3 = rdf.createLiteral("1", Types.XSD_STRING);
+
+        assertNotEquals(lit1, lit2);
+        assertNotEquals(lit1, lit3);
+    }
+
+
+    @Test
+    public void grahContains() throws Exception {
+        JsonLdGraph graph = rdf.createGraph();
+        JsonLdIRI s = rdf.createIRI("http://example.com/s");
+        JsonLdIRI p = rdf.createIRI("http://example.com/p");
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        
+        graph.add(s, p, lit1);
+        assertTrue(graph.contains(s, p, rdf.createLiteral("Hello")));
+        assertTrue(graph.contains(s, p, rdf.createLiteral("Hello", Types.XSD_STRING)));
+        assertFalse(graph.contains(s, p, rdf.createLiteral("Hello", Types.XSD_NORMALIZEDSTRING)));
+        assertFalse(graph.contains(s, p, rdf.createLiteral("Hello", "en")));
+        assertFalse(graph.contains(s, p, rdf.createLiteral("Other")));
+    }
+    
+    @Test
+    public void datasetContains() throws Exception {
+        JsonLdDataset dataset = rdf.createDataset();
+        JsonLdIRI s = rdf.createIRI("http://example.com/s");
+        JsonLdIRI p = rdf.createIRI("http://example.com/p");
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        
+        dataset.add(null, s, p, lit1);
+        assertTrue(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello")));
+        assertTrue(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", Types.XSD_STRING)));
+        assertFalse(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", Types.XSD_NORMALIZEDSTRING)));
+        assertFalse(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Hello", "en")));
+        assertFalse(dataset.contains(Optional.empty(), s, p, rdf.createLiteral("Other")));
+    }
+    
+    @Test
+    public void datasetRemove() throws Exception {
+        JsonLdDataset dataset = rdf.createDataset();
+        JsonLdIRI s = rdf.createIRI("http://example.com/s");
+        JsonLdIRI p = rdf.createIRI("http://example.com/p");
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        
+        dataset.add(null, s, p, lit1);
+        assertTrue(dataset.contains(Optional.empty(), s, p, lit1));
+        dataset.remove(null, null, null, rdf.createLiteral("Other")); // should NOT match
+        assertTrue(dataset.contains(Optional.empty(), s, p, lit1));
+        dataset.remove(null, null, null, rdf.createLiteral("Hello", Types.XSD_STRING)); //
SHOULD  match
+        assertFalse(dataset.contains(Optional.empty(), s, p, lit1));
+    }
+
+    @Test
+    public void datasetStream() throws Exception {
+        JsonLdDataset dataset = rdf.createDataset();
+        JsonLdIRI s = rdf.createIRI("http://example.com/s");
+        JsonLdIRI p = rdf.createIRI("http://example.com/p");
+        JsonLdLiteral lit1 = rdf.createLiteral("Hello");
+        JsonLdLiteral lit2 = rdf.createLiteral("Other");
+        
+        dataset.add(null, s, p, lit1);
+        assertTrue(dataset.stream(Optional.empty(), s, p, lit1).findAny().isPresent()); 
      
+        assertFalse(dataset.stream(Optional.empty(), s, p, lit2).findAny().isPresent());
       
+    }
+    
+}


Mime
View raw message