commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [4/8] incubator-commonsrdf git commit: some attempt to propagate a selected Resource[] contexts
Date Tue, 14 Jun 2016 15:45:04 GMT
some attempt to propagate a selected Resource[] contexts

(currently broken)


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

Branch: refs/heads/rdf4j
Commit: ccd9e97226cdb6892f0db15fe7db73b83433f347
Parents: e477b93
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jun 3 15:53:47 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jun 3 15:53:47 2016 +0100

----------------------------------------------------------------------
 .../rdf4j/impl/AbstractRepositoryGraphLike.java |   1 -
 .../rdf/rdf4j/impl/RepositoryDatasetImpl.java   | 142 +++++++++++++++++++
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     |  18 +--
 3 files changed, 152 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccd9e972/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
index 1283e2e..b048674 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
@@ -22,7 +22,6 @@ import java.util.Optional;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.RDFTerm;
-//import org.apache.commons.rdf.api.tripleLike;
 import org.apache.commons.rdf.api.TripleLike;
 import org.apache.commons.rdf.rdf4j.RDF4JGraphLike;
 import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccd9e972/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
new file mode 100644
index 0000000..50c7d20
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
@@ -0,0 +1,142 @@
+/**
+ * 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.rdf4j.impl;
+
+import java.util.ConcurrentModificationException;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+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.rdf4j.RDF4JDataset;
+import org.apache.commons.rdf.rdf4j.RDF4JQuad;
+import org.apache.commons.rdf.rdf4j.RDF4JTriple;
+import org.eclipse.rdf4j.common.iteration.Iterations;
+import org.eclipse.rdf4j.model.Resource;
+import org.eclipse.rdf4j.model.Statement;
+import org.eclipse.rdf4j.model.Value;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
+import org.eclipse.rdf4j.repository.RepositoryResult;
+
+public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad> implements
RDF4JDataset, Dataset {
+
+
+	@Override
+	public void add(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
+		Resource context = (Resource) rdf4jTermFactory.asValue(graphName);
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.add(subj, pred, obj,  context);
+			conn.commit();
+		}
+	}
+
+	@Override
+	public boolean contains(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject,
IRI predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		Resource[] contexts = asContexts(graphName);
+		
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			return conn.hasStatement(subj, pred, obj, includeInferred, contexts);
+		}
+	}
+
+	private Resource[] asContexts(Optional<BlankNodeOrIRI> graphName) {
+		Resource[] contexts;
+		if (graphName == null) {
+			// no contexts == any contexts
+			 contexts = new Resource[0];
+		} else {	
+			BlankNodeOrIRI g = graphName.orElse(null);
+			Resource context = (Resource) rdf4jTermFactory.asValue(g);
+			contexts = new Resource[] { context };
+		}
+		return contexts;
+	}
+
+	@Override
+	public void remove(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI
predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		Resource[] contexts = asContexts(graphName);
+
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.remove(subj, pred, obj, contexts);
+			conn.commit();
+		}
+	}
+
+	@Override
+	public Stream<RDF4JQuad> stream() {
+		return stream(null, null, null, null);
+	}
+	
+	@Override
+	public Stream<RDF4JQuad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		Resource[] contexts = asContexts(graphName);
+		
+		RepositoryConnection conn = getRepositoryConnection();
+		// NOTE: connection will be closed outside by the Iterations.stream()
+		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj, includeInferred,
contexts);
+		return Iterations.stream(statements).map(this::asTripleLike);
+	}
+
+	@Override
+	protected RDF4JQuad asTripleLike(Statement s) {
+		return rdf4jTermFactory.asQuad(s);
+	}
+
+	@Override
+	public Iterable<Quad> iterate() throws ConcurrentModificationException, IllegalStateException
{
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Graph getGraph() {
+		// TODO:  Specify default context only
+		return new RepositoryGraphImpl(repository, includeInferred);		
+	}
+
+	@Override
+	public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
+		Resource context = (Resource) rdf4jTermFactory.asValue(graphName);
+		return new RepositoryGraphImpl(repository, includeInferred, context);		
+	}
+
+	@Override
+	public Stream<BlankNodeOrIRI> getGraphNames() {
+		RepositoryConnection conn = getRepositoryConnection();
+		RepositoryResult<Resource> contexts = conn.getContextIDs();
+		// NOTE: connection will be closed outside by the Iterations.stream()
+		return Iterations.stream(contexts).map(g -> (BlankNodeOrIRI) rdf4jTermFactory.asRDFTerm(g));
+	}
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccd9e972/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
index 1fe1498..574ddb1 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
@@ -36,12 +36,16 @@ import org.eclipse.rdf4j.repository.RepositoryResult;
 
 public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple> implements
Graph, RDF4JGraph {
 
+	private Resource[] contextFilter;
+
 	public RepositoryGraphImpl(Repository repository) {
-		super(repository, false);
+		// All contexts (supplying null would mean default graph only)
+		this(repository, false);
 	}
 
-	public RepositoryGraphImpl(Repository repository, boolean includeInferred) {
+	public RepositoryGraphImpl(Repository repository, boolean includeInferred, Resource... contextFilter)
{
 		super(repository, includeInferred);
+		this.contextFilter = contextFilter;
 	}
 
 	@Override
@@ -50,12 +54,10 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
 		Value obj = rdf4jTermFactory.asValue(object);
 		try (RepositoryConnection conn = getRepositoryConnection()) {
-			conn.add(subj, pred, obj);
+			conn.add(subj, pred, obj, contextFilter);
 			conn.commit();
 		}
 	}
-	
-	
 
 	@Override
 	public boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
@@ -63,7 +65,7 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
 		Value obj = rdf4jTermFactory.asValue(object);
 		try (RepositoryConnection conn = getRepositoryConnection()) {
-			return conn.hasStatement(subj, pred, obj, includeInferred);
+			return conn.hasStatement(subj, pred, obj, includeInferred, contextFilter);
 		}
 	}
 
@@ -73,7 +75,7 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
 		Value obj = rdf4jTermFactory.asValue(object);
 		try (RepositoryConnection conn = getRepositoryConnection()) {
-			conn.remove(subj, pred, obj);
+			conn.remove(subj, pred, obj, contextFilter);
 			conn.commit();
 		}
 	}
@@ -90,7 +92,7 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		Value obj = rdf4jTermFactory.asValue(object);
 		RepositoryConnection conn = getRepositoryConnection();
 		// FIXME: Is it OK that we don't close the connection?
-		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj);
+		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj, contextFilter);
 		return Iterations.stream(statements).map(this::asTripleLike);
 	}
 	


Mime
View raw message