Commits

Sam Adams  committed 89fd556

Add base class for remote triple stores

  • Participants
  • Parent commits 98e2995

Comments (0)

Files changed (1)

File chempound-app/src/main/java/net/chempound/datastore/RemoteTripleStore.java

+package net.chempound.datastore;
+
+import com.hp.hpl.jena.query.Query;
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.QueryExecutionFactory;
+import com.hp.hpl.jena.query.QuerySolution;
+import com.hp.hpl.jena.shared.Lock;
+import com.hp.hpl.jena.shared.LockMRSW;
+import com.hp.hpl.jena.sparql.util.FmtUtils;
+
+import javax.inject.Singleton;
+import java.util.Iterator;
+import java.util.regex.Pattern;
+
+/**
+ * @author sea36
+ */
+@Singleton
+public abstract class RemoteTripleStore extends AbstractTripleStore {
+
+    private final String endpoint;
+    private final Lock lock = new LockMRSW();
+
+    protected RemoteTripleStore(final String endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    @Override
+    public QueryExecution createQueryExecution(final Query query) {
+        return QueryExecutionFactory.sparqlService(endpoint, query);
+    }
+
+    @Override
+    protected final QueryExecution createQueryExecution(final Query query, final QuerySolution initialBindings) {
+        final String queryString = generateQueryString(query, initialBindings);
+        return QueryExecutionFactory.sparqlService(endpoint, queryString);
+    }
+
+    protected String generateQueryString(final Query query, final QuerySolution initialBindings) {
+        String queryString = query.toString();
+        for (final Iterator<String> iterator = initialBindings.varNames(); iterator.hasNext();) {
+            final String name =  iterator.next();
+            final String value = FmtUtils.stringForNode(initialBindings.get(name).asNode());
+            queryString = queryString.replaceAll("\\?" + Pattern.quote(name) + "\\b", value);
+        }
+        return queryString;
+    }
+
+    @Override
+    protected final Lock getLock() {
+        return lock;
+    }
+
+    @Override
+    public void close() {
+        // not applicable
+    }
+
+}