commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [3/9] commons-rdf git commit: COMMONSRDF-55: only adapt our "own" JenaIRI instances
Date Wed, 08 Feb 2017 14:42:12 GMT
COMMONSRDF-55: only adapt our "own" JenaIRI instances


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

Branch: refs/heads/master
Commit: a383e1f2ec18249cab42f73e734cbf742d48fa05
Parents: 98da22a
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Feb 6 15:35:17 2017 +0000
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Feb 6 15:35:17 2017 +0000

----------------------------------------------------------------------
 .../commons/rdf/jena/impl/AbstractQuadLike.java | 30 +++++++++++++++-
 .../rdf/jena/DefaultGraphInQuadTest.java        | 36 +++++++++++++-------
 2 files changed, 53 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/a383e1f2/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java
index 6127ee6..fbe4085 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AbstractQuadLike.java
@@ -27,6 +27,7 @@ import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.jena.JenaQuad;
 import org.apache.commons.rdf.jena.JenaQuadLike;
 import org.apache.commons.rdf.jena.JenaRDF;
+import org.apache.commons.rdf.jena.JenaRDFTerm;
 import org.apache.commons.rdf.jena.JenaTriple;
 import org.apache.jena.graph.Triple;
 import org.apache.jena.sparql.core.Quad;
@@ -67,9 +68,35 @@ abstract class AbstractQuadLike<S extends RDFTerm, P extends RDFTerm,
O extends
         private RDFTerm defaultGraph = internalJenaFactory.createRDFTerm(Quad.defaultGraphIRI,
salt);
         private RDFTerm defaultGraphNodeGenerated = internalJenaFactory.createRDFTerm(Quad.defaultGraphNodeGenerated,
salt);
         
+        /**
+         * Check if RDFTerm is an IRI that matches the two Jena default graph
+         * constants (Even if they are from another RDF implementation).
+         * <p>
+         * This checker is "softer" than {@link #isNotDefaultGraphJenaNode(RDFTerm)}
+         * 
+         * @param graphName
+         *            potential graph name IRI or BlankNode
+         * @return <code>true</code> if the RDFTerm does not indicate a default
+         *         graph in Jena
+         */
         public boolean isNotDefaultGraph(RDFTerm graphName) {
             return !(graphName.equals(defaultGraph) || graphName.equals(defaultGraphNodeGenerated));

         }
+
+        /**
+         * Check if RDFTerm has an IRI that matches the two Jena default graph
+         * constants (but only if it is an JenaRDFTerm instance)
+         * 
+         * @param graphName
+         *            potential graph name IRI or BlankNode
+         * @return <code>true</code> if the RDFTerm does not indicate a default
+         *         graph in Jena
+         */         
+        public boolean isNotDefaultGraphJenaNode(RDFTerm graphName) {
+            return ! (graphName instanceof JenaRDFTerm) ||
+                    ! Quad.isDefaultGraph(((JenaRDFTerm)graphName).asJenaNode());       
              
+             
+        }
     }
     
     private static DefaultGraphChecker defaultGraphChecker = new DefaultGraphChecker();
@@ -86,7 +113,8 @@ abstract class AbstractQuadLike<S extends RDFTerm, P extends RDFTerm,
O extends
         this.subject = Objects.requireNonNull(subject);
         this.predicate = Objects.requireNonNull(predicate);
         this.object = Objects.requireNonNull(object);
-        this.graphName = Objects.requireNonNull(graphName).filter(defaultGraphChecker::isNotDefaultGraph);
+        // Enforce 
+        this.graphName = Objects.requireNonNull(graphName).filter(defaultGraphChecker::isNotDefaultGraphJenaNode);
     }
 
     AbstractQuadLike(final S subject, final P predicate, final O object) {

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/a383e1f2/jena/src/test/java/org/apache/commons/rdf/jena/DefaultGraphInQuadTest.java
----------------------------------------------------------------------
diff --git a/jena/src/test/java/org/apache/commons/rdf/jena/DefaultGraphInQuadTest.java b/jena/src/test/java/org/apache/commons/rdf/jena/DefaultGraphInQuadTest.java
index 477f460..1665d25 100644
--- a/jena/src/test/java/org/apache/commons/rdf/jena/DefaultGraphInQuadTest.java
+++ b/jena/src/test/java/org/apache/commons/rdf/jena/DefaultGraphInQuadTest.java
@@ -29,12 +29,13 @@ import org.apache.jena.sparql.core.Quad;
 import org.junit.Test;
 
 /**
- * COMMONSRDF-55: Ensure correct handling of 
+ * COMMONSRDF-55: Handling of 
  * Jena's default graph IRI urn:x-arq:DefaultGraph
  */
 public class DefaultGraphInQuadTest {
     
     JenaRDF rdf = new JenaRDF();
+    SimpleRDF simpleRDF = new SimpleRDF();
     IRI example = rdf.createIRI("http://example.com/");
     Node exampleJena = NodeFactory.createURI("http://example.com/");
     
@@ -50,19 +51,28 @@ public class DefaultGraphInQuadTest {
     public void createFromDefaultGraphIRI() throws Exception {
         JenaIRI defaultGraph = (JenaIRI) rdf.asRDFTerm(Quad.defaultGraphIRI);        
         JenaQuad q = rdf.createQuad(defaultGraph, example, example, example);
+        // NOTE: JenaRDF specially recognize this JenaIRI constant, 
+        // even if this breaks the SHOULD of RDF.createQuad()
         assertTrue(q.asJenaQuad().isDefaultGraph());
         assertEquals(Quad.defaultGraphIRI,  q.asJenaQuad().getGraph());
         assertFalse(q.getGraphName().isPresent());
+        // thus we can't require 
+        //assertEquals(defaultGraph, q.getGraphName().get());
     }
 
     @Test
     public void createFromForeignDefaultGraph() throws Exception {
         // What if <urn:x-arq:DefaultGraph> appear in a non-Jena IRI?
-        IRI foreignDefaultGraph = new SimpleRDF().createIRI(Quad.defaultGraphIRI.getURI());
+        IRI foreignDefaultGraph = simpleRDF.createIRI(Quad.defaultGraphIRI.getURI());
         JenaQuad q = rdf.createQuad(foreignDefaultGraph, example, example, example);
+        // As the IRI was NOT a JenaIRI we preserve it as-is,
+        // rather than replacing it with Optional.empty()        
         assertTrue(q.asJenaQuad().isDefaultGraph());
+        assertTrue(q.getGraphName().isPresent()); // INCONSISTENT with above
         assertEquals(Quad.defaultGraphIRI,  q.asJenaQuad().getGraph());
-        assertFalse(q.getGraphName().isPresent());
+        assertEquals(foreignDefaultGraph, q.getGraphName().get());
+        // Note that adding such a quad to a Dataset would still "convert" it to
+        // Optional.empty() 
     }
     
 
@@ -71,23 +81,25 @@ public class DefaultGraphInQuadTest {
         // What if <urn:x-arq:DefaultGraphNode> appear as an IRI instance?
         IRI foreignDefaultGraph = rdf.createIRI(Quad.defaultGraphNodeGenerated.getURI());
         JenaQuad q = rdf.createQuad(foreignDefaultGraph, example, example, example);
+        // NOTE: JenaRDF specially recognize this JenaIRI constant, 
+        // even if this breaks the SHOULD of RDF.createQuad()
         assertTrue(q.asJenaQuad().isDefaultGraph());
-        assertFalse(q.getGraphName().isPresent());
-        // Unfortunately  Quad.defaultGraphNodeGenerated is not preserved
-        // within JenaQuad
+        assertFalse(q.getGraphName().isPresent()); // CONSISTENT with above 
+        // Unfortunately Quad.defaultGraphNodeGenerated is not preserved:
         //assertEquals(Quad.defaultGraphNodeGenerated, q.asJenaQuad().getGraph());
     }
 
     @Test
     public void createFromDefaultGraphNodeGeneratedIRIString() throws Exception {
         // What if <urn:x-arq:DefaultGraphNode> appear in a non-Jena IRI?
-        IRI foreignDefaultGraph = (IRI) rdf.asRDFTerm((Quad.defaultGraphNodeGenerated));
+        IRI foreignDefaultGraph = (IRI) simpleRDF.createIRI(Quad.defaultGraphNodeGenerated.getURI());
         JenaQuad q = rdf.createQuad(foreignDefaultGraph, example, example, example);
-        assertTrue(q.asJenaQuad().isDefaultGraph());
-        assertFalse(q.getGraphName().isPresent());
-        // Unfortunately  Quad.defaultGraphNodeGenerated is not preserved
-        // within JenaQuad
-        assertEquals(Quad.defaultGraphNodeGenerated, q.asJenaQuad().getGraph());
+        // We'll expect JenaRDF to preserve the graph IRI as-is        
+        assertTrue(q.asJenaQuad().isDefaultGraph()); 
+        assertTrue(q.getGraphName().isPresent()); // INCONSISTENT WITH above
+        // Now Quad.defaultGraphNodeGenerated is preserved at both ends
+        assertEquals(Quad.defaultGraphNodeGenerated,  q.asJenaQuad().getGraph());
+        assertEquals(foreignDefaultGraph, q.getGraphName().get());
     }
 
     


Mime
View raw message