cleaning up: removing remains from SPARQL client implementations
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Tue, 27 Oct 2015 09:28:37 +0000 (10:28 +0100)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Tue, 27 Oct 2015 09:28:37 +0000 (10:28 +0100)
src/main/java/org/bgbm/biovel/drf/checklist/EEA_BDC_Client.java
src/main/java/org/bgbm/biovel/drf/query/TinkerPopClient.java

index 2145b40b40bf36b218042786bc098aaa93780218..eb12d9a87609a183947f1f22b9a6dc0f6961d430 100644 (file)
@@ -11,12 +11,9 @@ import org.apache.jena.rdf.model.Model;
 import org.apache.jena.rdf.model.Resource;
 import org.apache.lucene.queryParser.QueryParser;
 import org.bgbm.biovel.drf.client.ServiceProviderInfo;
-import org.bgbm.biovel.drf.query.IQueryClient;
-import org.bgbm.biovel.drf.query.SparqlClient;
 import org.bgbm.biovel.drf.query.TinkerPopClient;
 import org.bgbm.biovel.drf.store.Neo4jStore;
 import org.bgbm.biovel.drf.store.Store;
-import org.bgbm.biovel.drf.store.TDBStore;
 import org.bgbm.biovel.drf.tnr.msg.NameType;
 import org.bgbm.biovel.drf.tnr.msg.Query;
 import org.bgbm.biovel.drf.tnr.msg.Query.Request;
@@ -51,18 +48,11 @@ public class EEA_BDC_Client extends AggregateChecklistClient<TinkerPopClient> {
     public static final String DOC_URL = "http://semantic.eea.europa.eu/documentation";
     public static final String COPYRIGHT_URL = "http://www.eea.europa.eu/legal/eea-data-policy";
 
-    private static final String SPARQL_ENDPOINT_URL = "http://semantic.eea.europa.eu/sparql";
-    private static final boolean USE_REMOTE_SERVICE = false;
-
     private static final String SPECIES_RDF_FILE_URL = "http://localhost/download/species.rdf.gz"; // http://eunis.eea.europa.eu/rdf/species.rdf.gz
     private static final String LEGALREFS_RDF_FILE_URL = "http://localhost/download/legalrefs.rdf.gz"; // http://eunis.eea.europa.eu/rdf/legalrefs.rdf.gz
     private static final String REFERENCES_RDF_FILE_URL = "http://localhost/download/references.rdf.gz"; // http://eunis.eea.europa.eu/rdf/references.rdf.gz
     private static final boolean REFRESH_TDB = false;
 
-    private static final Class<? extends IQueryClient> clientClass = TinkerPopClient.class;
-
-    private static final int MAX_PAGING_LIMIT = 50;
-
     public static final EnumSet<SearchMode> SEARCH_MODES = EnumSet.of(
             SearchMode.scientificNameExact,
             SearchMode.scientificNameLike,
@@ -142,43 +132,16 @@ public class EEA_BDC_Client extends AggregateChecklistClient<TinkerPopClient> {
     @Override
     public void initQueryClient() {
 
-        if(SparqlClient.class.isAssignableFrom(clientClass)) {
-            if(USE_REMOTE_SERVICE) {
-                // use SPARQL end point
-                //FIXME queryClient = new SparqlClient(SPARQL_ENDPOINT_URL);
-            } else {
-                TDBStore tripleStore;
-                try {
-                    tripleStore = new TDBStore();
-                } catch (Exception e1) {
-                    throw new RuntimeException("Creation of TripleStore failed",  e1);
-                }
-                if(REFRESH_TDB) {
-                    updateStore(tripleStore);
-                }
-              //FIXME queryClient = new SparqlClient(tripleStore);
-
+            Neo4jStore neo4jStore;
+            try {
+                neo4jStore = new Neo4jStore();
+            } catch (Exception e1) {
+                throw new RuntimeException("Creation of Neo4jStore failed",  e1);
             }
-        } else if(TinkerPopClient.class.isAssignableFrom(clientClass)) {
-            if(USE_REMOTE_SERVICE) {
-                throw new RuntimeException("USE_REMOTE_SERVICE not suported by QueryClient class "+ clientClass);
-            } else {
-                Neo4jStore neo4jStore;
-                try {
-                    neo4jStore = new Neo4jStore();
-                } catch (Exception e1) {
-                    throw new RuntimeException("Creation of Neo4jStore failed",  e1);
-                }
-                if(REFRESH_TDB) {
-                    updateStore(neo4jStore);
-                }
-                queryClient = new TinkerPopClient(neo4jStore);
-
+            if(REFRESH_TDB) {
+                updateStore(neo4jStore);
             }
-
-        } else {
-            throw new RuntimeException("Unsuported QueryClient class "+ clientClass);
-        }
+            queryClient = new TinkerPopClient(neo4jStore);
     }
 
     /**
index 2a3f407cd54d1cab1a0a186650f02b015661feb3..a20c3eec4e07a4ca7d1f13db723893557af5bff8 100644 (file)
@@ -11,25 +11,17 @@ package org.bgbm.biovel.drf.query;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
-import java.util.List;
 import java.util.NoSuchElementException;
 
-import org.apache.jena.rdf.model.Model;
-import org.apache.jena.rdf.model.Property;
-import org.apache.jena.rdf.model.RDFNode;
 import org.apache.jena.rdf.model.Resource;
 import org.apache.jena.rdf.model.StmtIterator;
-import org.bgbm.biovel.drf.checklist.DRFChecklistException;
 import org.bgbm.biovel.drf.checklist.EEA_BDC_Client.RdfSchema;
 import org.bgbm.biovel.drf.store.Neo4jStore;
 import org.neo4j.graphdb.Node;
 import org.neo4j.graphdb.index.AutoIndexer;
 import org.neo4j.graphdb.index.IndexHits;
 import org.openrdf.query.BindingSet;
-import org.openrdf.query.MalformedQueryException;
 import org.openrdf.query.QueryEvaluationException;
-import org.openrdf.query.QueryLanguage;
-import org.openrdf.query.TupleQuery;
 import org.openrdf.query.TupleQueryResult;
 import org.openrdf.repository.RepositoryException;
 import org.openrdf.repository.sail.SailRepositoryConnection;
@@ -43,7 +35,6 @@ import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph;
 import com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Vertex;
 import com.tinkerpop.blueprints.oupls.sail.GraphSail;
 import com.tinkerpop.gremlin.java.GremlinPipeline;
-import com.tinkerpop.pipes.PipeFunction;
 import com.tinkerpop.pipes.util.FastNoSuchElementException;
 
 /**
@@ -68,51 +59,19 @@ import com.tinkerpop.pipes.util.FastNoSuchElementException;
  */
 public class TinkerPopClient implements IQueryClient {
 
-    public static final String KIND = GraphSail.KIND;
-
-    public static final String VALUE = GraphSail.VALUE;
-
-    public static final Object URI = GraphSail.URI;
-
     protected Logger logger = LoggerFactory.getLogger(TinkerPopClient.class);
 
-    private final String baseUri = null;
-
-    private Neo4jStore tripleStore = null;
+    private Neo4jStore graphStore = null;
 
     /**
-     * @param tripleStore
+     * @param store
      */
-    public TinkerPopClient(Neo4jStore tripleStore) {
-        this.tripleStore = tripleStore;
-    }
-
-    public GremlinPipeline<Graph, Graph> newPipe() {
-        GremlinPipeline<Graph, Object> pipe = new GremlinPipeline<Graph, Object>();
-        return pipe.start(graph());
+    public TinkerPopClient(Neo4jStore store) {
+        this.graphStore = store;
     }
 
     public Graph graph() {
-        return tripleStore.graph();
-    }
-
-    /**
-     * @param sparql
-     * @return
-     * @throws MalformedQueryException
-     * @throws RepositoryException
-     * @throws QueryEvaluationException
-     *
-     * @deprecated directly use connection() and do not forget to close is after
-     *             doing the query. See
-     *             {@link org.openrdf.repository.sail.SailRepository#getConnection()}
-     */
-    @Deprecated
-    public TupleQueryResult execute(String sparql) throws MalformedQueryException, RepositoryException,
-            QueryEvaluationException {
-        TupleQuery query = connection().prepareTupleQuery(QueryLanguage.SPARQL, sparql);
-        TupleQueryResult result = query.evaluate();
-        return result;
+        return graphStore.graph();
     }
 
     /**
@@ -122,170 +81,10 @@ public class TinkerPopClient implements IQueryClient {
      * @throws RepositoryException
      */
     public SailRepositoryConnection connection() throws RepositoryException {
-        SailRepositoryConnection connection = tripleStore.getSailRepo().getConnection();
+        SailRepositoryConnection connection = graphStore.getSailRepo().getConnection();
         return connection;
     }
 
-    public PipeFunction<Vertex, Boolean> createRegexMatchFilter(final String regex) {
-        return new PipeFunction<Vertex, Boolean>() {
-
-            @Override
-            public Boolean compute(Vertex v) {
-                return v.toString().matches(regex);
-            }
-        };
-    }
-
-    public PipeFunction<Vertex, Boolean> createEqualsFilter(final String string) {
-        return new PipeFunction<Vertex, Boolean>() {
-
-            @Override
-            public Boolean compute(Vertex v) {
-                return v.toString().equals(string);
-            }
-        };
-    }
-
-    public PipeFunction<Vertex, Boolean> createStarttWithFilter(final String string) {
-        return new PipeFunction<Vertex, Boolean>() {
-
-            @Override
-            public Boolean compute(Vertex v) {
-                return v.toString().startsWith(string);
-            }
-        };
-    }
-
-    public Model describe(String queryString) throws DRFChecklistException, QueryEvaluationException {
-
-        // directly execute SPARQL queries in Gremlin over Sail-based graphs
-        // using the method SailGraph.executeSparql().
-
-        TupleQuery qe = executionFor(queryString);
-        TupleQueryResult result = qe.evaluate();
-        System.err.println(result.toString());
-
-        if (result != null && logger.isDebugEnabled()) {
-            StringBuilder msg = new StringBuilder();
-            msg.append("subjects in response:\n");
-            int i = 1;
-            try {
-                for (; result.hasNext(); ++i) {
-                    BindingSet res = result.next();
-                    msg.append("    " + i + ": " + res.toString() + "\n");
-                }
-            } catch (QueryEvaluationException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-            }
-            logger.debug(msg.toString());
-        }
-
-        return null; // FIXME result;
-    }
-
-    /**
-     * @param queryString
-     * @return
-     */
-    private TupleQuery executionFor(String queryString) {
-
-        if (baseUri != null) {
-            // see
-            // https://github.com/tinkerpop/blueprints/wiki/Sail-Implementation
-            // FIXME
-            throw new RuntimeException("Mode unsupported");
-            // Graph graph = new SparqlRepositorySailGraph(baseUri);
-            // return QueryExecutionFactory.sparqlService(baseUri, query);
-        }
-        if (tripleStore != null) {
-            // local TDB Store
-            try {
-                SailRepositoryConnection connection = connection();
-                return connection.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
-            } catch (MalformedQueryException | RepositoryException e) {
-                // TODO Auto-generated catch block
-                logger.error("Error while perparing query", e);
-            }
-
-        }
-
-        return null;
-    }
-
-    /**
-     * @param subject
-     * @param nameSpace
-     * @param localName
-     * @return
-     */
-    public RDFNode asSingleObject(Resource subject, RdfSchema nameSpace, String localName) {
-        RDFNode node = null;
-        StmtIterator propertyIt = null;
-        Resource _subject = subject;
-        try {
-
-            Model _model = _subject.getModel();
-            Property property = _model.getProperty(nameSpace.schemaUri(), localName);
-            propertyIt = _subject.listProperties(property);
-
-            boolean propertyInGraph = propertyIt.hasNext();
-            if (!propertyInGraph) {
-                _subject = getFromUri(subject.getURI());
-                propertyIt = _subject.listProperties(property);
-            }
-
-            node = propertyIt.next().getObject();
-        } catch (NoSuchElementException e) {
-            if (logger.isTraceEnabled()) {
-                logger.debug(_subject.getURI() + " " + nameSpace + ":" + localName + " not found in current graph");
-                printProperties(_subject);
-            }
-        }
-        return node;
-    }
-
-    /**
-     * @param subject
-     * @param nameSpace
-     * @param localName
-     * @return
-     */
-    public StmtIterator listProperties(Resource subject, RdfSchema nameSpace, String localName) {
-        RDFNode node = null;
-        StmtIterator propertyIt = null;
-        Resource _subject = subject;
-        try {
-
-            Model _model = _subject.getModel();
-            Property property = _model.getProperty(nameSpace.schemaUri(), localName);
-            propertyIt = _subject.listProperties(property);
-
-            boolean propertyInGraph = propertyIt.hasNext();
-            if (!propertyInGraph) {
-                _subject = getFromUri(subject.getURI());
-                propertyIt = _subject.listProperties(property);
-            }
-
-        } catch (NoSuchElementException e) {
-            if (logger.isTraceEnabled()) {
-                logger.debug(_subject.getURI() + " " + nameSpace + ":" + localName + " not found in current graph");
-                printProperties(_subject);
-            }
-        }
-        return propertyIt;
-    }
-
-    public List<RDFNode> listObjects(Resource subject, RdfSchema nameSpace, String localName) {
-
-        List<RDFNode> list = new ArrayList<RDFNode>();
-        StmtIterator it = listProperties(subject, nameSpace, localName);
-        while (it.hasNext()) {
-            list.add(it.next().getObject());
-        }
-        return list;
-    }
-
     /**
      * @param subject
      * @param nameSpace
@@ -334,68 +133,20 @@ public class TinkerPopClient implements IQueryClient {
         return txt;
     }
 
-    //
-
     /**
      * @param subject
      * @param nameSpace
      * @param localName
      * @return
      */
-    public Resource objectAsResource(Resource subject, RdfSchema nameSpace, String localName) {
-        Resource resource = null;
-        RDFNode node = asSingleObject(subject, nameSpace, localName);
-        if (node != null) {
-            node.isResource();
-            resource = node.asResource();
-        }
-        return resource;
-    }
-
-    /**
-     * @param subject
-     * @param nameSpace
-     * @param localName
-     * @return
-     * @deprecated unused
-     */
-    @Deprecated
-    public URI relatedVertexURI(GremlinPipeline<Graph, Vertex> pipe, RdfSchema nameSpace, String localName) {
-        URI uri = null;
-        String edgeLabel = nameSpace.propertyURI(localName);
-        try {
-            Vertex v = pipe.outE(1, edgeLabel).inV().next();
-            if (v.getProperty(GraphSail.KIND).equals(GraphSail.URI)) {
-                uri = (URI) v.getProperty(GraphSail.VALUE);
-            } else {
-                logger.warn("target vertex of '" + edgeLabel + "' is not an URI");
-            }
-        } catch (FastNoSuchElementException e) {
-            logger.warn("edge with '" + edgeLabel + "' not found");
-        }
-
-        return uri;
-    }
-
-    /**
-     * @param subject
-     * @param nameSpace
-     * @param localName
-     * @return
-     * @deprecated possibly broken
-     */
-    @Deprecated
     public URI relatedVertexURI(Vertex v, RdfSchema nameSpace, String localName) {
         URI uri = null;
-        String edgeLabel = nameSpace.propertyURI(localName);
         try {
             if (v.getProperty(GraphSail.KIND).equals(GraphSail.URI)) {
                 uri = new URI(v.getProperty(GraphSail.VALUE).toString());
             } else {
-                logger.warn("target vertex of '" + edgeLabel + "' is not an URI");
+                logger.warn("vertex of '" + v.toString() + "' is not an URI");
             }
-        } catch (NoSuchElementException e) {
-            logger.warn("edge with '" + edgeLabel + "' not found");
         } catch (URISyntaxException e) {
             logger.error("Invalid URI id in " + v, e);
         }
@@ -430,22 +181,6 @@ public class TinkerPopClient implements IQueryClient {
         }
     }
 
-    /**
-     * @param matchedResourceURI
-     * @return
-     * @throws DRFChecklistException
-     */
-    public Resource getFromUri(URI matchedResourceURI) {
-        return getFromUri(matchedResourceURI.toString());
-    }
-
-    public Resource getFromUri(String uri) {
-
-        // not needed
-        return null;
-
-    }
-
     public void showResults(TupleQueryResult result) throws QueryEvaluationException {
         int i = 0;
         while (result.hasNext()) {