Return-Path: X-Original-To: apmail-commonsrdf-commits-archive@minotaur.apache.org Delivered-To: apmail-commonsrdf-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6799219FCD for ; Sat, 16 Apr 2016 12:25:39 +0000 (UTC) Received: (qmail 56496 invoked by uid 500); 16 Apr 2016 12:25:39 -0000 Delivered-To: apmail-commonsrdf-commits-archive@commonsrdf.apache.org Received: (qmail 56460 invoked by uid 500); 16 Apr 2016 12:25:39 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 56433 invoked by uid 99); 16 Apr 2016 12:25:39 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 Apr 2016 12:25:39 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id D8C42C187C for ; Sat, 16 Apr 2016 12:25:38 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.216 X-Spam-Level: X-Spam-Status: No, score=-4.216 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.996] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id G0Pou8qnff-E for ; Sat, 16 Apr 2016 12:25:38 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id CC50B5FB56 for ; Sat, 16 Apr 2016 12:25:34 +0000 (UTC) Received: (qmail 56062 invoked by uid 99); 16 Apr 2016 12:25:33 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 Apr 2016 12:25:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 89FDFE03CE; Sat, 16 Apr 2016 12:25:33 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stain@apache.org To: commits@commonsrdf.incubator.apache.org Date: Sat, 16 Apr 2016 12:25:36 -0000 Message-Id: In-Reply-To: <5f191c7ddd524f519898ea3cbb2c4524@git.apache.org> References: <5f191c7ddd524f519898ea3cbb2c4524@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/10] incubator-commonsrdf git commit: COMMONSRDF-8 verify Graph.add() of blank nodes COMMONSRDF-8 verify Graph.add() of blank nodes .. do not accidentally overlap. Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/e1203fda Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/e1203fda Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/e1203fda Branch: refs/heads/master Commit: e1203fda3a546be7cda9478aacb90cffc869ee52 Parents: 3884ef5 Author: Stian Soiland-Reyes Authored: Fri Apr 10 00:24:42 2015 +0100 Committer: Stian Soiland-Reyes Committed: Sat Mar 12 18:28:17 2016 +0000 ---------------------------------------------------------------------- .../commons/rdf/api/AbstractGraphTest.java | 126 +++++++++++++++++++ 1 file changed, 126 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e1203fda/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 eff542e..d6a5f9d 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 @@ -19,11 +19,15 @@ package org.apache.commons.rdf.api; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Optional; import org.junit.Assume; @@ -277,6 +281,128 @@ public abstract class AbstractGraphTest { assertEquals(3, graph.getTriples(null, member, null).count()); } + @Test + public void addBlankNodesFromMultipleGraphs() { + + try { + Graph g1 = createGraph1(); + Graph g2 = createGraph2(); + Graph g3 = factory.createGraph(); + + copyTriples(g1, g3); + copyTriples(g2, g3); + + IRI name = factory.createIRI("http://xmlns.com/foaf/0.1/name"); + + final Map whoIsWho = new HashMap<>(); + System.out.println(g3.getTriples(null, name, null).count()); + // NOTE: sequential as our HashMap is not thread-safe + g3.getTriples(null, name, null).sequential().forEach( t -> + whoIsWho.put( t.getObject().ntriplesString(), t.getSubject())); + assertEquals(4, whoIsWho.size()); + // and contains 4 unique values + assertEquals(4, new HashSet(whoIsWho.values()).size()); + + BlankNodeOrIRI b1Alice = whoIsWho.get("\"Alice\""); + assertNotNull(b1Alice); + BlankNodeOrIRI b2Bob = whoIsWho.get("\"Bob\""); + assertNotNull(b2Bob); + BlankNodeOrIRI b1Charlie = whoIsWho.get("\"Charlie\""); + assertNotNull(b1Charlie); + BlankNodeOrIRI b2Dave = whoIsWho.get("\"Dave\""); + assertNotNull(b2Dave); + + notEquals(b1Alice, b2Bob); + notEquals(b1Alice, b1Charlie); + notEquals(b1Alice, b2Dave); + notEquals(b2Bob, b1Charlie); + notEquals(b2Bob, b2Dave); + notEquals(b1Charlie, b2Dave); + + IRI hasChild = factory.createIRI("http://example.com/hasChild"); + assertTrue(g3.contains(b1Alice, hasChild, b2Bob)); + assertTrue(g3.contains(b2Dave, hasChild, b1Charlie)); + // But not + assertFalse(g3.contains(b1Alice, hasChild, b1Alice)); + assertFalse(g3.contains(b1Alice, hasChild, b1Charlie)); + assertFalse(g3.contains(b1Alice, hasChild, b2Dave)); + // nor + assertFalse(g3.contains(b2Dave, hasChild, b1Alice)); + assertFalse(g3.contains(b2Dave, hasChild, b1Alice)); + + // and these don't have any children (as far as we know) + assertFalse(g3.contains(b2Bob, hasChild, null)); + assertFalse(g3.contains(b1Charlie, hasChild, null)); + } catch (UnsupportedOperationException ex) { + Assume.assumeNoException(ex); + } + } + + private void notEquals(BlankNodeOrIRI node1, BlankNodeOrIRI node2) { + assertFalse(node1.equals(node2)); + // in which case we should be able to assume + // (as they are in the same graph) + assertFalse(node1.ntriplesString().equals(node2.ntriplesString())); + } + + /** + * Add all triples from the source to the target. + *

+ * The triples may be copied in any order. + * No special conversion or adaptation of {@link BlankNode}s are performed. + * + * @param source Source Graph to copy triples from + * @param target Target Graph where triples will be added + */ + protected static void copyTriples(Graph source, Graph target) { + + // unordered() as we don't need to preserve triple order + // sequential() as we don't require target Graph to be thread-safe + source.getTriples().unordered().sequential().forEach(t -> target.add(t)); + } + + private Graph createGraph1() { + RDFTermFactory factory1 = createFactory(); + // Let's assume this is parsed from + // a Turtle file , and faithfully keeps its + // internal blank node identifiers _:b1 and _:b2 + + IRI name = factory1.createIRI("http://xmlns.com/foaf/0.1/name"); + Graph g1 = factory1.createGraph(); + BlankNode b1 = factory1.createBlankNode(); + g1.add(b1, name, factory1.createLiteral("Alice")); + + BlankNode b2 = factory1.createBlankNode(); + g1.add(b2, name, factory1.createLiteral("Bob")); + + IRI hasChild = factory1.createIRI("http://example.com/hasChild"); + g1.add(b1, hasChild, b2); + + return g1; + } + + private Graph createGraph2() { + // Let's assume this is parsed from + // a Turtle file , which also uses the + // internal blank node identifiers _:b1 and _:b2, + // but is describing someone else. + RDFTermFactory factory2 = createFactory(); + IRI name = factory2.createIRI("http://xmlns.com/foaf/0.1/name"); + + Graph g2 = factory2.createGraph(); + + BlankNode b1 = factory2.createBlankNode(); + g2.add(b1, name, factory2.createLiteral("Charlie")); + + BlankNode b2 = factory2.createBlankNode(); + g2.add(b2, name, factory2.createLiteral("Dave")); + + IRI hasChild = factory2.createIRI("http://example.com/hasChild"); + // NOTE: Opposite direction of loadGraph1 + g2.add(b2, hasChild, b1); + return g2; + } + /** * An attempt to use the Java 8 streams to look up a more complicated query. *