You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by wi...@apache.org on 2016/03/03 11:36:18 UTC

[2/2] marmotta git commit: MARMOTTA-631: fixed by forcing to have the default context always, plus added an alternative implementation we may want to switch to

MARMOTTA-631: fixed by forcing to have the default context always, plus added an alternative implementation we may want to switch to


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

Branch: refs/heads/develop
Commit: 503cbc8daae89641b8f5d60c04274e1dcde012ec
Parents: 8710b7e
Author: Sergio Fernández <wi...@apache.org>
Authored: Thu Mar 3 11:36:04 2016 +0100
Committer: Sergio Fernández <wi...@apache.org>
Committed: Thu Mar 3 11:36:04 2016 +0100

----------------------------------------------------------------------
 .../triplestore/ContextServiceImpl.java         | 82 +++++++++++++++++---
 .../test/triplestore/ContextServiceTest.java    |  2 +-
 2 files changed, 74 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/503cbc8d/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/triplestore/ContextServiceImpl.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/triplestore/ContextServiceImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/triplestore/ContextServiceImpl.java
index 59d9fc2..3b34f4e 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/triplestore/ContextServiceImpl.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/triplestore/ContextServiceImpl.java
@@ -17,6 +17,8 @@
  */
 package org.apache.marmotta.platform.core.services.triplestore;
 
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.marmotta.commons.http.UriUtil;
 import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
@@ -25,6 +27,7 @@ import org.apache.marmotta.platform.core.api.importer.ImportService;
 import org.apache.marmotta.platform.core.api.triplestore.ContextService;
 import org.apache.marmotta.platform.core.api.triplestore.SesameService;
 import org.apache.marmotta.platform.core.api.user.UserService;
+import org.apache.marmotta.platform.core.exception.InvalidArgumentException;
 import org.apache.marmotta.platform.core.exception.io.MarmottaImportException;
 import org.apache.marmotta.platform.core.qualifiers.kspace.ActiveKnowledgeSpaces;
 import org.apache.marmotta.platform.core.qualifiers.kspace.DefaultKnowledgeSpace;
@@ -32,9 +35,11 @@ import org.apache.marmotta.platform.core.qualifiers.kspace.InferredKnowledgeSpac
 import org.apache.marmotta.platform.core.qualifiers.kspace.SystemKnowledgeSpace;
 import org.openrdf.model.Resource;
 import org.openrdf.model.URI;
+import org.openrdf.model.Value;
 import org.openrdf.model.ValueFactory;
 import org.openrdf.model.util.Literals;
 import org.openrdf.model.vocabulary.RDFS;
+import org.openrdf.query.*;
 import org.openrdf.repository.RepositoryConnection;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.RepositoryResult;
@@ -48,6 +53,7 @@ import javax.inject.Named;
 import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -108,7 +114,30 @@ public class ContextServiceImpl implements ContextService {
 
     @Override
     public List<URI> listContexts(boolean filter) {
-        List<URI> contexts = new ArrayList<URI>();
+        //TODO: configuration
+        final Set<URI> contexts =  listContextsSesame();
+        //final Set<URI> contexts = listContextsSparql();
+
+        if (filter) {
+            Collections2.filter(contexts, new Predicate<URI>() {
+                @Override
+                public boolean apply(URI uri) {
+                    return uri.stringValue().startsWith(configurationService.getBaseContext());
+                }
+            });
+        }
+
+        return new ArrayList<>(contexts);
+
+    }
+
+    /**
+     * List context using the Sesame native API
+     *
+     * @return
+     */
+    private Set<URI> listContextsSesame() {
+        Set<URI> contexts = new HashSet<>();
         try {
             RepositoryConnection conn = sesameService.getConnection();
             try {
@@ -117,19 +146,54 @@ public class ContextServiceImpl implements ContextService {
                 while(result.hasNext()) {
                     Resource next = result.next();
                     if(next instanceof URI) {
-                        URI uri = (URI)next;
-                        if (filter) {
-                            if (uri.stringValue().startsWith(configurationService.getBaseContext())) {
-                                contexts.add(uri);
-                            }
-                        } else {
-                            contexts.add(uri);
-                        }
+                        contexts.add((URI)next);
                     }
                 }
                 result.close();
+                conn.commit();
             } finally {
+                conn.close();
+            }
+        } catch (RepositoryException e) {
+
+        }
+
+        //MARMOTTA-631: default context should be always there
+        try {
+            contexts.add(getDefaultContext());
+        } catch (URISyntaxException e) {}
+
+        return contexts;
+    }
+
+    /**
+     * Alternative implementation to list contexts using SPARQL
+     *
+     * @return
+     */
+    private Set<URI> listContextsSparql() {
+        Set<URI> contexts = new HashSet<>();
+        try {
+            RepositoryConnection conn = sesameService.getConnection();
+            try {
+                conn.begin();
+                final String query = "SELECT DISTINCT ?graph WHERE { GRAPH ?graph { ?s ?p ?o } }";
+                final TupleQuery sparqlQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query, configurationService.getBaseUri());
+                final TupleQueryResult results = sparqlQuery.evaluate();
+                try {
+                    while (results.hasNext()) {
+                        final Value next = results.next().getValue("graph");
+                        if(next instanceof URI) {
+                            contexts.add((URI)next);
+                        }
+                    }
+                } finally {
+                    results.close();
+                }
                 conn.commit();
+            } catch (MalformedQueryException | QueryEvaluationException e) {
+                log.error("Error evaluating query: {}", e.getMessage());
+            } finally {
                 conn.close();
             }
         } catch (RepositoryException e) {

http://git-wip-us.apache.org/repos/asf/marmotta/blob/503cbc8d/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/triplestore/ContextServiceTest.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/triplestore/ContextServiceTest.java b/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/triplestore/ContextServiceTest.java
index eec0d30..9599180 100644
--- a/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/triplestore/ContextServiceTest.java
+++ b/platform/marmotta-core/src/test/java/org/apache/marmotta/platform/core/test/triplestore/ContextServiceTest.java
@@ -49,7 +49,7 @@ public class ContextServiceTest {
     @Test
     public void testEmpty() {
         final List<URI> contexts = contextService.listContexts();
-        Assert.assertEquals(0, contexts.size());
+        Assert.assertEquals(1, contexts.size());
     }
 
     @Test