You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ja...@apache.org on 2013/10/25 16:17:24 UTC

[3/3] git commit: Added ContextAwareSesameConnectionBackend to be used with grap-contrained ldpath programs (this is a follow-up on MARMOTTA-215)

Added ContextAwareSesameConnectionBackend to be used with grap-contrained ldpath programs
(this is a follow-up on MARMOTTA-215)


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

Branch: refs/heads/develop
Commit: ae6559f675dafe00b23af329fb531cae9cd7e3d4
Parents: 97b40b6
Author: Jakob Frank <ja...@apache.org>
Authored: Fri Oct 25 16:04:09 2013 +0200
Committer: Jakob Frank <ja...@apache.org>
Committed: Fri Oct 25 16:04:09 2013 +0200

----------------------------------------------------------------------
 .../backend/sesame/AbstractSesameBackend.java   | 15 ++++---
 .../ContextAwareSesameConnectionBackend.java    | 47 ++++++++++++++++++++
 .../backend/sesame/SesameConnectionBackend.java | 28 +++++++++---
 3 files changed, 78 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae6559f6/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/AbstractSesameBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/AbstractSesameBackend.java b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/AbstractSesameBackend.java
index 3e99b72..62fd5fe 100644
--- a/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/AbstractSesameBackend.java
+++ b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/AbstractSesameBackend.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -59,12 +59,12 @@ public abstract class AbstractSesameBackend extends SesameValueBackend implement
 		}
 	}
 
-	protected Collection<Value> listObjectsInternal(RepositoryConnection connection, Resource subject, org.openrdf.model.URI property)
+	protected Collection<Value> listObjectsInternal(RepositoryConnection connection, Resource subject, org.openrdf.model.URI property, Resource... contexts)
 			throws RepositoryException {
-        ValueFactory valueFactory = connection.getValueFactory();
+        final ValueFactory valueFactory = connection.getValueFactory();
 
 		Set<Value> result = new HashSet<Value>();
-		RepositoryResult<Statement> qResult = connection.getStatements(merge(subject, connection.getValueFactory()), merge(property, connection.getValueFactory()), null, true);
+		RepositoryResult<Statement> qResult = connection.getStatements(merge(subject, valueFactory), merge(property, valueFactory), null, true);
 		try {
 			while(qResult.hasNext()) {
 				result.add(qResult.next().getObject());
@@ -75,10 +75,12 @@ public abstract class AbstractSesameBackend extends SesameValueBackend implement
 		return  result;
 	}
 
-	protected Collection<Value> listSubjectsInternal(final RepositoryConnection connection, org.openrdf.model.URI property, Value object)
+	protected Collection<Value> listSubjectsInternal(final RepositoryConnection connection, org.openrdf.model.URI property, Value object, Resource... contexts)
 			throws RepositoryException {
+	    final ValueFactory valueFactory = connection.getValueFactory();
+	    
 		Set<Value> result = new HashSet<Value>();
-		RepositoryResult<Statement> qResult = connection.getStatements(null, merge(property, connection.getValueFactory()), merge(object, connection.getValueFactory()), true);
+        RepositoryResult<Statement> qResult = connection.getStatements(null, merge(property, valueFactory), merge(object, valueFactory), true);
 		try {
 			while(qResult.hasNext()) {
 				result.add(qResult.next().getSubject());
@@ -96,6 +98,7 @@ public abstract class AbstractSesameBackend extends SesameValueBackend implement
      * @param <T>
      * @return
      */
+    @SuppressWarnings("unchecked")
     protected <T extends Value> T merge(T value, ValueFactory vf) {
         if(value instanceof org.openrdf.model.URI) {
             return (T)vf.createURI(value.stringValue());

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae6559f6/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/ContextAwareSesameConnectionBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/ContextAwareSesameConnectionBackend.java b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/ContextAwareSesameConnectionBackend.java
new file mode 100644
index 0000000..3368269
--- /dev/null
+++ b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/ContextAwareSesameConnectionBackend.java
@@ -0,0 +1,47 @@
+/*
+ * 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.marmotta.ldpath.backend.sesame;
+
+import org.openrdf.model.Resource;
+import org.openrdf.repository.RepositoryConnection;
+
+/**
+ * {@link SesameConnectionBackend} that considers only data from the provided contexts.
+ * @author Jakob Frank <ja...@apache.org>
+ *
+ */
+public class ContextAwareSesameConnectionBackend extends SesameConnectionBackend {
+
+    /**
+     * Create a new {@link ContextAwareSesameConnectionBackend} that considers only data from the provided contexts.
+     * @param connection the {@link RepositoryConnection} to use.
+     * @param contexts the contexts to look for, providing an empty array here will revert the functionality to {@link SesameConnectionBackend}.
+     */
+    public ContextAwareSesameConnectionBackend(RepositoryConnection connection, Resource... contexts) {
+        super(connection, contexts);
+    }
+
+    /**
+     * Create a new {@link ContextAwareSesameConnectionBackend} that considers only data from the provided contexts.
+     * @param connection the {@link RepositoryConnection} to use.
+     * @param contexts the contexts to look for, providing an empty array here will revert the functionality to {@link SesameConnectionBackend}.
+     */
+    public static ContextAwareSesameConnectionBackend withConnection(RepositoryConnection connection, Resource... contexts) {
+        return new ContextAwareSesameConnectionBackend(connection, contexts);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/ae6559f6/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/SesameConnectionBackend.java
----------------------------------------------------------------------
diff --git a/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/SesameConnectionBackend.java b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/SesameConnectionBackend.java
index b6a4f51..afa3dea 100644
--- a/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/SesameConnectionBackend.java
+++ b/libraries/ldpath/ldpath-backend-sesame/src/main/java/org/apache/marmotta/ldpath/backend/sesame/SesameConnectionBackend.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -32,13 +32,24 @@ public class SesameConnectionBackend extends AbstractSesameBackend {
 
 	private final RepositoryConnection connection;
 	private final ValueFactory valueFactory;
+    private final Resource[] contexts;
 
+    /**
+     * Create a new {@link SesameConnectionBackend}. This backend is context-agnostig (ignores all context information). 
+     * @param connection the {@link RepositoryConnection} to use.
+     * @see ContextAwareSesameConnectionBackend#ContextAwareSesameConnectionBackend(RepositoryConnection, Resource...)
+     */
 	public SesameConnectionBackend(RepositoryConnection connection) {
-		this.connection = connection;
-		valueFactory = connection.getValueFactory();
+	    this(connection, new Resource[] {});
 	}
 
-	@Override
+	protected SesameConnectionBackend(RepositoryConnection connection, Resource[] contexts) {
+	    this.connection = connection;
+        this.contexts = contexts;
+	    valueFactory = connection.getValueFactory();
+    }
+
+    @Override
 	public Literal createLiteral(String content) {
 		return createLiteralInternal(valueFactory, content);
 	}
@@ -56,7 +67,7 @@ public class SesameConnectionBackend extends AbstractSesameBackend {
 	@Override
 	public Collection<Value> listObjects(Value subject, Value property) {
 		try {
-			return listObjectsInternal(connection, (Resource) subject, (org.openrdf.model.URI) property);
+			return listObjectsInternal(connection, (Resource) subject, (org.openrdf.model.URI) property, contexts);
 		} catch (RepositoryException e) {
 			throw new RuntimeException(
 					"error while querying Sesame repository!", e);
@@ -71,7 +82,7 @@ public class SesameConnectionBackend extends AbstractSesameBackend {
 	@Override
 	public Collection<Value> listSubjects(Value property, Value object) {
         try {
-        	return listSubjectsInternal(connection, (org.openrdf.model.URI) property, object);
+        	return listSubjectsInternal(connection, (org.openrdf.model.URI) property, object, contexts);
         } catch (RepositoryException e) {
             throw new RuntimeException("error while querying Sesame repository!",e);
         } catch (ClassCastException e) {
@@ -81,6 +92,11 @@ public class SesameConnectionBackend extends AbstractSesameBackend {
         }
 	}
 
+    /**
+     * Create a new {@link SesameConnectionBackend}. This backend is context-agnostig (ignores all context information). 
+     * @param connection the {@link RepositoryConnection} to use.
+     * @see ContextAwareSesameConnectionBackend#withConnection(RepositoryConnection, Resource...)
+     */
 	public static SesameConnectionBackend withConnection(RepositoryConnection connection) {
 		return new SesameConnectionBackend(connection);
 	}