Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 98DE7200B16 for ; Mon, 20 Jun 2016 10:58:04 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 97B76160A55; Mon, 20 Jun 2016 08:58:04 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7342B160A66 for ; Mon, 20 Jun 2016 10:58:03 +0200 (CEST) Received: (qmail 57595 invoked by uid 500); 20 Jun 2016 08:58:02 -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 57539 invoked by uid 99); 20 Jun 2016 08:58:02 -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; Mon, 20 Jun 2016 08:58:02 +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 0E84FC2D48 for ; Mon, 20 Jun 2016 08:58:02 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.646 X-Spam-Level: X-Spam-Status: No, score=-4.646 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=-1.426] 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 coxRvbgWng9H for ; Mon, 20 Jun 2016 08:58:01 +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 D36E460E27 for ; Mon, 20 Jun 2016 08:57:57 +0000 (UTC) Received: (qmail 55742 invoked by uid 99); 20 Jun 2016 08:57:56 -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; Mon, 20 Jun 2016 08:57:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A4B1EED311; Mon, 20 Jun 2016 08:57:56 +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: Mon, 20 Jun 2016 08:58:39 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [45/50] incubator-commonsrdf git commit: Added DatasetGraphView archived-at: Mon, 20 Jun 2016 08:58:04 -0000 Added DatasetGraphView Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/580484c9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/580484c9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/580484c9 Branch: refs/heads/parser-with-quads Commit: 580484c9d7d02d34b64e5276744c1f644dbbd2f2 Parents: 14e505e Author: Stian Soiland-Reyes Authored: Tue Jun 14 16:44:24 2016 +0100 Committer: Stian Soiland-Reyes Committed: Tue Jun 14 16:44:24 2016 +0100 ---------------------------------------------------------------------- .../commons/rdf/simple/DatasetGraphView.java | 138 +++++++++++++++++++ .../apache/commons/rdf/simple/DatasetImpl.java | 8 +- 2 files changed, 139 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/580484c9/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java new file mode 100644 index 0000000..a347c3e --- /dev/null +++ b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java @@ -0,0 +1,138 @@ +/** + * 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.simple; + +import java.util.Optional; +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.Dataset; +import org.apache.commons.rdf.api.Graph; +import org.apache.commons.rdf.api.IRI; +import org.apache.commons.rdf.api.Quad; +import org.apache.commons.rdf.api.RDFTerm; +import org.apache.commons.rdf.api.Triple; + +/** + * A {@link Graph} view on a {@link Dataset}. + *

+ * This view is backed by a {@link Dataset}, and can be constructed in two ways: + * + *

+ *
{@link #DatasetGraphView(Dataset)}
+ *
Expose a union graph view of the Dataset, where all the + * {@link Quad}s of the Dataset is represented as a {@link Triple}. Adding + * triples will add them to the default graph, while removing triples + * will remove from all graphs.
+* + *
{@link #DatasetGraphView(Dataset, IRI)}
+ *
Expose a particular graph of the Dataset, either named by an {@link IRI}, a + * {@link BlankNode}, or null for the default graph.
+ *
+ *

+ * Changes in the Graph are reflected directly in the Dataset and vice versa. + * This class is thread-safe is the underlying Dataset is thread-safe. + */ +public class DatasetGraphView implements Graph { + + private final boolean unionGraph; + private final BlankNodeOrIRI namedGraph; + private final Dataset dataset; + + public DatasetGraphView(Dataset dataset) { + this.dataset = dataset; + this.namedGraph = null; + this.unionGraph = true; + } + + public DatasetGraphView(Dataset dataset, BlankNodeOrIRI namedGraph) { + this.dataset = dataset; + this.namedGraph = namedGraph; + this.unionGraph = false; + } + + @Override + public void close() throws Exception { + dataset.close(); + + } + + @Override + public void add(Triple triple) { + dataset.add(namedGraph, triple.getSubject(), triple.getPredicate(), triple.getObject()); + } + + @Override + public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + dataset.add(namedGraph, subject, predicate, object); + } + + @Override + public boolean contains(Triple triple) { + return dataset.contains(unionOrNamedGraph(), triple.getSubject(), triple.getPredicate(), triple.getObject()); + } + + private Optional unionOrNamedGraph() { + if (unionGraph) { + return null; + } + return Optional.ofNullable(namedGraph); + } + + @Override + public boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + return dataset.contains(unionOrNamedGraph(), subject, predicate, object); + } + + @Override + public void remove(Triple triple) { + dataset.remove(unionOrNamedGraph(), triple.getSubject(), triple.getPredicate(), triple.getObject()); + } + + @Override + public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + dataset.remove(unionOrNamedGraph(), subject, predicate, object); + } + + @Override + public void clear() { + dataset.remove(unionOrNamedGraph(), null, null, null); + } + + @Override + public long size() { + return stream().count(); + } + + @Override + public Stream stream() { + return stream(null, null, null); + } + + @Override + public Stream stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + Stream stream = dataset.stream(unionOrNamedGraph(), subject, predicate, object).map(Quad::asTriple); + if (unionGraph) { + // remove duplicates + return stream.distinct(); + } + return stream; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/580484c9/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java ---------------------------------------------------------------------- diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java index 9de358f..c372012 100644 --- a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java +++ b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java @@ -204,13 +204,7 @@ final class DatasetImpl implements Dataset { @Override public Optional getGraph(BlankNodeOrIRI graphName) { - // NOTE: Always returns a new copy - Graph g = new GraphImpl(factory); - stream(Optional.ofNullable(graphName), null, null, null) - .map(Quad::asTriple) - .sequential() - .forEach(g::add); - return Optional.of(g); + return Optional.of(new DatasetGraphView(this, graphName)); } @Override