commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject [01/18] incubator-commonsrdf git commit: Map blank nodes to UUID's without storing local scope for each object
Date Mon, 30 Mar 2015 01:09:55 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/master cf5f5158d -> 20b0b000f


Map blank nodes to UUID's without storing local scope for each object


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/9522337b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/9522337b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/9522337b

Branch: refs/heads/master
Commit: 9522337ba110a195c3adb4f80429f8cdaef7a2f3
Parents: a6d0a03
Author: Peter Ansell <p_ansell@yahoo.com>
Authored: Sat Mar 28 19:04:41 2015 +1100
Committer: Peter Ansell <p_ansell@yahoo.com>
Committed: Sat Mar 28 19:04:41 2015 +1100

----------------------------------------------------------------------
 .../commons/rdf/api/AbstractGraphTest.java      | 36 +++++-----
 .../rdf/api/AbstractRDFTermFactoryTest.java     | 11 +--
 .../commons/rdf/simple/BlankNodeImpl.java       | 36 +++++-----
 .../apache/commons/rdf/simple/GraphImpl.java    | 40 ++++++++++-
 .../apache/commons/rdf/simple/TripleImpl.java   | 70 +++-----------------
 5 files changed, 89 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/9522337b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
index ff21dd3..4c1d035 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
@@ -48,8 +48,8 @@ public abstract class AbstractGraphTest {
 	private IRI name;
 	private IRI knows;
 	private IRI member;
-	private BlankNode org1;
-	private BlankNode org2;
+	private BlankNode bnode1;
+	private BlankNode bnode2;
 	private Literal aliceName;
 	private Literal bobName;
 	private Literal secretClubName;
@@ -70,8 +70,8 @@ public abstract class AbstractGraphTest {
 		knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows");
 		member = factory.createIRI("http://xmlns.com/foaf/0.1/member");
 		try {
-			org1 = factory.createBlankNode("org1");
-			org2 = factory.createBlankNode("org2");
+			bnode1 = factory.createBlankNode("org1");
+			bnode2 = factory.createBlankNode("org2");
 		} catch (UnsupportedOperationException ex) {
 			// leave as null
 		}
@@ -90,8 +90,8 @@ public abstract class AbstractGraphTest {
 		}
 		graph.add(alice, knows, bob);
 
-		if (org1 != null) {
-			graph.add(alice, member, org1);
+		if (bnode1 != null) {
+			graph.add(alice, member, bnode1);
 		}
 
 		if (bobName != null) {
@@ -104,12 +104,12 @@ public abstract class AbstractGraphTest {
 				graph.add(bobNameTriple);
 			}
 		}
-		if (org1 != null) {
-			graph.add(factory.createTriple(bob, member, org1));
-			graph.add(factory.createTriple(bob, member, org2));
+		if (bnode1 != null) {
+			graph.add(factory.createTriple(bob, member, bnode1));
+			graph.add(factory.createTriple(bob, member, bnode2));
 			if (secretClubName != null) {
-				graph.add(org1, name, secretClubName);
-				graph.add(org2, name, companyName);
+				graph.add(bnode1, name, secretClubName);
+				graph.add(bnode2, name, companyName);
 			}
 		}
 	}
@@ -123,14 +123,14 @@ public abstract class AbstractGraphTest {
 				.startsWith(
 						"<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> \"Alice\"
."));
 		assertTrue(graph.toString().endsWith(
-				"_:org2 <http://xmlns.com/foaf/0.1/name> \"A company\" ."));
+				" <http://xmlns.com/foaf/0.1/name> \"A company\" ."));
 
 	}
 
 	@Test
 	public void size() throws Exception {
 		assertTrue(graph.size() > 0);
-		Assume.assumeNotNull(org1, org2, aliceName, bobName, secretClubName,
+		Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName,
 				companyName, bobNameTriple);
 		// Can only reliably predict size if we could create all triples
 		assertEquals(8, graph.size());
@@ -211,7 +211,7 @@ public abstract class AbstractGraphTest {
 		assertTrue(tripleCount > 0);
 		assertTrue(graph.getTriples().allMatch(t -> graph.contains(t)));
 		// Check exact count
-		Assume.assumeNotNull(org1, org2, aliceName, bobName, secretClubName,
+		Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName,
 				companyName, bobNameTriple);
 		assertEquals(8, tripleCount);
 	}
@@ -224,10 +224,10 @@ public abstract class AbstractGraphTest {
 		Assume.assumeNotNull(aliceName);
 		assertEquals(3, aliceCount);
 
-		Assume.assumeNotNull(org1, org2, bobName, companyName, secretClubName);
+		Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName);
 		assertEquals(4, graph.getTriples(null, name, null).count());
-		Assume.assumeNotNull(org1);
-		assertEquals(2, graph.getTriples(null, member, org1).count());
+		Assume.assumeNotNull(bnode1);
+		assertEquals(3, graph.getTriples(null, member, null).count());
 	}
 
 	/**
@@ -249,7 +249,7 @@ public abstract class AbstractGraphTest {
 	 */
 	@Test
 	public void whyJavaStreamsMightNotTakeOverFromSparql() throws Exception {
-		Assume.assumeNotNull(org1, org2, secretClubName);
+		Assume.assumeNotNull(bnode1, bnode2, secretClubName);
 		// Find a secret organizations
 		assertEquals(
 				"\"The Secret Club\"",

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/9522337b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java
b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java
index a4f6e98..b45f6ba 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTermFactoryTest.java
@@ -83,16 +83,14 @@ public abstract class AbstractRDFTermFactoryTest {
 
 	@Test
 	public void testCreateBlankNodeIdentifier() throws Exception {
-		BlankNode bnode;
 		try {
-			bnode = factory.createBlankNode("example1");
+			factory.createBlankNode("example1");
 		} catch (UnsupportedOperationException ex) {
 			Assume.assumeNoException(ex);
 			return;
 		}
-		assertEquals("example1", bnode.internalIdentifier());
-		// .. but we can't assume the internal identifier leaks into
-		// ntriplesString
+		// We can't assume anything about the resulting bnode
+		// assertEquals("example1", bnode.internalIdentifier());
 		// assertEquals("_:example1", bnode.ntriplesString());
 	}
 
@@ -107,10 +105,13 @@ public abstract class AbstractRDFTermFactoryTest {
 			Assume.assumeNoException(ex);
 			return;
 		}
+		// We don't know what the identifier is, but it MUST be the same
 		assertEquals(bnode1.internalIdentifier(), bnode2.internalIdentifier());
 		// We don't know what the ntriplesString is, but it MUST be the same
 		assertEquals(bnode1.ntriplesString(), bnode2.ntriplesString());
 		// and here it MUST differ
+		assertNotEquals(bnode1.internalIdentifier(),
+				bnode3.internalIdentifier());
 		assertNotEquals(bnode1.ntriplesString(), bnode3.ntriplesString());
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/9522337b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
index 398ad37..47cc211 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/BlankNodeImpl.java
@@ -33,21 +33,28 @@ import org.apache.commons.rdf.api.Graph;
 final class BlankNodeImpl implements BlankNode {
 
 	private static AtomicLong bnodeCounter = new AtomicLong();
+
+	private static final Object DEFAULT_SEED = new Object();
+
 	private final String id;
-	private final Graph localScope;
 
 	public BlankNodeImpl() {
-		this(Optional.empty(), "b:" + bnodeCounter.incrementAndGet());
+		this(DEFAULT_SEED, "genid:" + bnodeCounter.incrementAndGet());
 	}
 
-	public BlankNodeImpl(Optional<Graph> localScope, String id) {
-		this.localScope = Objects.requireNonNull(localScope).orElse(null);
+	public BlankNodeImpl(Object uuidSeed, String id) {
 		if (Objects.requireNonNull(id).isEmpty()) {
 			throw new IllegalArgumentException("Invalid blank node id: " + id);
-			// NOTE: It is valid for the id to not be a valid ntriples bnode id.
-			// See ntriplesString().
 		}
-		this.id = id;
+		String uuidInput = uuidSeed.toString() + ":" + id;
+		// Both the scope and the id are used to create the UUID, ensuring that
+		// a caller can reliably create the same bnode if necessary by sending
+		// in the same scope.
+		// In addition, it would be very difficult for the default constructor
+		// to interfere with this process since it uses a local object as its
+		// reference.
+		this.id = UUID.nameUUIDFromBytes(
+				uuidInput.getBytes(StandardCharsets.UTF_8)).toString();
 	}
 
 	@Override
@@ -57,11 +64,6 @@ final class BlankNodeImpl implements BlankNode {
 
 	@Override
 	public String ntriplesString() {
-		if (id.contains(":")) {
-			return "_:u"
-					+ UUID.nameUUIDFromBytes(id
-							.getBytes(StandardCharsets.UTF_8));
-		}
 		return "_:" + id;
 	}
 
@@ -72,7 +74,7 @@ final class BlankNodeImpl implements BlankNode {
 
 	@Override
 	public int hashCode() {
-		return Objects.hash(localScope, id);
+		return id.hashCode();
 	}
 
 	@Override
@@ -83,6 +85,7 @@ final class BlankNodeImpl implements BlankNode {
 		if (obj == null) {
 			return false;
 		}
+		// We don't support equality with other implementations
 		if (!(obj instanceof BlankNodeImpl)) {
 			return false;
 		}
@@ -94,13 +97,6 @@ final class BlankNodeImpl implements BlankNode {
 		} else if (!id.equals(other.id)) {
 			return false;
 		}
-		if (localScope == null) {
-			if (other.localScope != null) {
-				return false;
-			}
-		} else if (!localScope.equals(other.localScope)) {
-			return false;
-		}
 		return true;
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/9522337b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
index 30ab15e..81bbcee 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
@@ -25,9 +25,11 @@ import java.util.function.Predicate;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
 import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Literal;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.Triple;
 
@@ -51,10 +53,44 @@ final class GraphImpl implements Graph {
 
 	}
 
+	private <T extends RDFTerm> RDFTerm internallyMap(T object) {
+		if (object instanceof BlankNode) {
+			BlankNode blankNode = (BlankNode) object;
+			// This guarantees that adding the same BlankNode multiple times to
+			// this graph will generate a local object that is mapped to an
+			// equivalent object, based on the code in the package private
+			// BlankNodeImpl class
+			return new BlankNodeImpl(this, blankNode.internalIdentifier());
+		} else if (object instanceof IRI && !(object instanceof IRIImpl)) {
+			IRI iri = (IRI) object;
+			return new IRIImpl(iri.getIRIString());
+		} else if (object instanceof Literal
+				&& !(object instanceof LiteralImpl)) {
+			Literal literal = (Literal) object;
+			if (literal.getLanguageTag().isPresent()) {
+				return new LiteralImpl(literal.getLexicalForm(), literal
+						.getLanguageTag().get());
+			} else {
+				return new LiteralImpl(literal.getLexicalForm(),
+						(IRI) internallyMap(literal.getDatatype()));
+			}
+		} else {
+			return object;
+		}
+	}
+
 	@Override
 	public void add(Triple triple) {
-		triples.add(new TripleImpl(Optional.of(this), Objects
-				.requireNonNull(triple)));
+		if (!(triple instanceof TripleImpl)) {
+			BlankNodeOrIRI subject = (BlankNodeOrIRI) internallyMap(triple
+					.getSubject());
+			IRI predicate = (IRI) internallyMap(triple.getPredicate());
+			RDFTerm object = internallyMap(triple.getObject());
+			triples.add(new TripleImpl(subject, predicate, object));
+		} else {
+			triples.add(new TripleImpl(triple.getSubject(), triple
+					.getPredicate(), triple.getObject()));
+		}
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/9522337b/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java
index 6c9c825..0fe76d9 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/TripleImpl.java
@@ -18,13 +18,9 @@
 package org.apache.commons.rdf.simple;
 
 import java.util.Objects;
-import java.util.Optional;
 
-import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
-import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
-import org.apache.commons.rdf.api.Literal;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.Triple;
 
@@ -41,64 +37,20 @@ final class TripleImpl implements Triple {
 	/**
 	 * Construct Triple from its constituent parts.
 	 * <p>
-	 * The parts may be copied to ensure they are in scope.
+	 * The objects are not changed. All mapping of BNode objects is done in
+	 * {@link SimpleRDFTermFactory#createTriple(BlankNodeOrIRI, IRI, RDFTerm)}.
 	 * 
-	 * @param subject subject of triple
-	 * @param predicate predicate of triple
-	 * @param object object of triple
+	 * @param subject
+	 *            subject of triple
+	 * @param predicate
+	 *            predicate of triple
+	 * @param object
+	 *            object of triple
 	 */
 	public TripleImpl(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-		this.subject = (BlankNodeOrIRI) inScope(Optional.empty(),
-				Objects.requireNonNull(subject));
-		this.predicate = (IRI) inScope(null, Objects.requireNonNull(predicate));
-		this.object = inScope(Optional.empty(), Objects.requireNonNull(object));
-	}
-
-	/**
-	 * Construct Triple by cloning another Triple and its constituent parts.
-	 * <p>
-	 * The parts of the triple may be copied to ensure they are in scope.
-	 * 
-	 * @param localScope
-	 *            Scope to create new triple in.
-	 * @param triple
-	 *            Triple to clone
-	 */
-	public TripleImpl(Optional<Graph> localScope, Triple triple) {
-		Objects.requireNonNull(localScope);
-		Objects.requireNonNull(triple);
-
-		this.subject = (BlankNodeOrIRI) inScope(localScope, triple.getSubject());
-		this.predicate = (IRI) inScope(localScope, triple.getPredicate());
-		this.object = inScope(localScope, triple.getObject());
-	}
-
-	private RDFTerm inScope(Optional<Graph> localScope, RDFTerm object) {
-		if (!(object instanceof BlankNode) && !(object instanceof IRI)
-				& !(object instanceof Literal)) {
-			throw new IllegalArgumentException(
-					"RDFTerm must be BlankNode, IRI or Literal");
-		}
-		if (object instanceof BlankNode) {
-			BlankNode blankNode = (BlankNode) object;
-			return new BlankNodeImpl(Objects.requireNonNull(localScope),
-					blankNode.internalIdentifier());
-		} else if (object instanceof IRI && !(object instanceof IRIImpl)) {
-			IRI iri = (IRI) object;
-			return new IRIImpl(iri.getIRIString());
-		} else if (object instanceof Literal
-				&& !(object instanceof LiteralImpl)) {
-			Literal literal = (Literal) object;
-			if (literal.getLanguageTag().isPresent()) {
-				return new LiteralImpl(literal.getLexicalForm(), literal
-						.getLanguageTag().get());
-			} else {
-				IRI dataType = (IRI) inScope(localScope, literal.getDatatype());
-				return new LiteralImpl(literal.getLexicalForm(), dataType);
-			}
-		} else {
-			return object;
-		}
+		this.subject = Objects.requireNonNull(subject);
+		this.predicate = Objects.requireNonNull(predicate);
+		this.object = Objects.requireNonNull(object);
 	}
 
 	@Override


Mime
View raw message