Search in sources :

Example 1 with TupleQueryResultHandler

use of org.eclipse.rdf4j.query.TupleQueryResultHandler in project incubator-rya by apache.

the class InferenceIT method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    dao = new AccumuloRyaDAO();
    connector = new MockInstance().getConnector("", new PasswordToken(""));
    dao.setConnector(connector);
    conf = new AccumuloRdfConfiguration();
    conf.setInfer(true);
    dao.setConf(conf);
    dao.init();
    store = new RdfCloudTripleStore();
    store.setConf(conf);
    store.setRyaDAO(dao);
    inferenceEngine = new InferenceEngine();
    inferenceEngine.setRyaDAO(dao);
    store.setInferenceEngine(inferenceEngine);
    inferenceEngine.refreshGraph();
    store.initialize();
    repository = new SailRepository(store);
    conn = repository.getConnection();
    solutions = new LinkedList<>();
    resultHandler = new TupleQueryResultHandler() {

        @Override
        public void endQueryResult() throws TupleQueryResultHandlerException {
        }

        @Override
        public void handleBoolean(final boolean value) throws QueryResultHandlerException {
        }

        @Override
        public void handleLinks(final List<String> linkUrls) throws QueryResultHandlerException {
        }

        @Override
        public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
            if (bindingSet != null && bindingSet.iterator().hasNext()) {
                solutions.add(bindingSet);
            }
        }

        @Override
        public void startQueryResult(final List<String> bindingNames) throws TupleQueryResultHandlerException {
            solutions.clear();
        }
    };
}
Also used : AccumuloRyaDAO(org.apache.rya.accumulo.AccumuloRyaDAO) RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) MockInstance(org.apache.accumulo.core.client.mock.MockInstance) TupleQueryResultHandler(org.eclipse.rdf4j.query.TupleQueryResultHandler) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 2 with TupleQueryResultHandler

use of org.eclipse.rdf4j.query.TupleQueryResultHandler in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testNamespaceUsage.

public void testNamespaceUsage() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    conn.setNamespace("lit", litdupsNS);
    IRI loadPerc = VF.createIRI(litdupsNS, "loadPerc");
    final IRI uri1 = VF.createIRI(litdupsNS, "uri1");
    conn.add(cpu, loadPerc, uri1);
    conn.commit();
    String query = "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {lit:cpu lit:loadPerc ?o.}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.evaluate(new TupleQueryResultHandler() {

        @Override
        public void startQueryResult(List<String> strings) throws TupleQueryResultHandlerException {
        }

        @Override
        public void endQueryResult() throws TupleQueryResultHandlerException {
        }

        @Override
        public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
            assertTrue(uri1.toString().equals(bindingSet.getBinding("o").getValue().stringValue()));
        }

        @Override
        public void handleBoolean(boolean paramBoolean) throws QueryResultHandlerException {
        }

        @Override
        public void handleLinks(List<String> paramList) throws QueryResultHandlerException {
        }
    });
    conn.close();
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) IRI(org.eclipse.rdf4j.model.IRI) BindingSet(org.eclipse.rdf4j.query.BindingSet) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) TupleQueryResultHandler(org.eclipse.rdf4j.query.TupleQueryResultHandler) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 3 with TupleQueryResultHandler

use of org.eclipse.rdf4j.query.TupleQueryResultHandler in project incubator-rya by apache.

the class RdfController method queryRdf.

@RequestMapping(value = "/queryrdf", method = { RequestMethod.GET, RequestMethod.POST })
public void queryRdf(@RequestParam("query") final String query, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, required = false) String auth, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_CV, required = false) final String vis, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_INFER, required = false) final String infer, @RequestParam(value = "nullout", required = false) final String nullout, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_RESULT_FORMAT, required = false) final String emit, @RequestParam(value = "padding", required = false) final String padding, @RequestParam(value = "callback", required = false) final String callback, final HttpServletRequest request, final HttpServletResponse response) {
    // WARNING: if you add to the above request variables,
    // Be sure to validate and encode since they come from the outside and could contain odd damaging character sequences.
    SailRepositoryConnection conn = null;
    final Thread queryThread = Thread.currentThread();
    auth = StringUtils.arrayToCommaDelimitedString(provider.getUserAuths(request));
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            log.debug("interrupting");
            queryThread.interrupt();
        }
    }, QUERY_TIME_OUT_SECONDS * 1000);
    try {
        final ServletOutputStream os = response.getOutputStream();
        conn = repository.getConnection();
        final Boolean isBlankQuery = StringUtils.isEmpty(query);
        final ParsedOperation operation = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, null);
        final Boolean requestedCallback = !StringUtils.isEmpty(callback);
        final Boolean requestedFormat = !StringUtils.isEmpty(emit);
        if (!isBlankQuery) {
            if (operation instanceof ParsedGraphQuery) {
                // Perform Graph Query
                final RDFHandler handler = new RDFXMLWriter(os);
                response.setContentType("text/xml");
                performGraphQuery(query, conn, auth, infer, nullout, handler);
            } else if (operation instanceof ParsedTupleQuery) {
                // Perform Tuple Query
                TupleQueryResultHandler handler;
                if (requestedFormat && emit.equalsIgnoreCase("json")) {
                    handler = new SPARQLResultsJSONWriter(os);
                    response.setContentType("application/json");
                } else {
                    handler = new SPARQLResultsXMLWriter(os);
                    response.setContentType("text/xml");
                }
                performQuery(query, conn, auth, infer, nullout, handler);
            } else if (operation instanceof ParsedUpdate) {
                // Perform Update Query
                performUpdate(query, conn, os, infer, vis);
            } else {
                throw new MalformedQueryException("Cannot process query. Query type not supported.");
            }
        }
        if (requestedCallback) {
            os.print(")");
        }
    } catch (final Exception e) {
        log.error("Error running query", e);
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (final RepositoryException e) {
                log.error("Error closing connection", e);
            }
        }
    }
    timer.cancel();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) SPARQLResultsJSONWriter(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONWriter) ParsedOperation(org.eclipse.rdf4j.query.parser.ParsedOperation) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) SPARQLResultsXMLWriter(org.eclipse.rdf4j.query.resultio.sparqlxml.SPARQLResultsXMLWriter) ParsedUpdate(org.eclipse.rdf4j.query.parser.ParsedUpdate) Timer(java.util.Timer) TimerTask(java.util.TimerTask) RDFXMLWriter(org.eclipse.rdf4j.rio.rdfxml.RDFXMLWriter) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) TupleQueryResultHandler(org.eclipse.rdf4j.query.TupleQueryResultHandler) RDFHandler(org.eclipse.rdf4j.rio.RDFHandler) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with TupleQueryResultHandler

use of org.eclipse.rdf4j.query.TupleQueryResultHandler in project incubator-rya by apache.

the class RdfController method performQuery.

private void performQuery(final String query, final RepositoryConnection conn, final String auth, final String infer, final String nullout, final TupleQueryResultHandler handler) throws RepositoryException, MalformedQueryException, QueryEvaluationException, TupleQueryResultHandlerException {
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    if (auth != null && auth.length() > 0) {
        tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, VALUE_FACTORY.createLiteral(auth));
    }
    if (infer != null && infer.length() > 0) {
        tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_INFER, VALUE_FACTORY.createLiteral(Boolean.parseBoolean(infer)));
    }
    if (nullout != null && nullout.length() > 0) {
        // output nothing, but still run query
        tupleQuery.evaluate(new TupleQueryResultHandler() {

            @Override
            public void startQueryResult(final List<String> strings) throws TupleQueryResultHandlerException {
            }

            @Override
            public void endQueryResult() throws TupleQueryResultHandlerException {
            }

            @Override
            public void handleSolution(final BindingSet bindings) throws TupleQueryResultHandlerException {
            }

            @Override
            public void handleBoolean(final boolean arg0) throws QueryResultHandlerException {
            }

            @Override
            public void handleLinks(final List<String> arg0) throws QueryResultHandlerException {
            }
        });
    } else {
        final CountingTupleQueryResultHandlerWrapper sparqlWriter = new CountingTupleQueryResultHandlerWrapper(handler);
        final long startTime = System.currentTimeMillis();
        tupleQuery.evaluate(sparqlWriter);
        log.info(String.format("Query Time = %.3f   Result Count = %s\n", (System.currentTimeMillis() - startTime) / 1000., sparqlWriter.getCount()));
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery) TupleQueryResultHandler(org.eclipse.rdf4j.query.TupleQueryResultHandler) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Aggregations

QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)4 TupleQueryResultHandler (org.eclipse.rdf4j.query.TupleQueryResultHandler)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)2 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)2 IOException (java.io.IOException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 MockInstance (org.apache.accumulo.core.client.mock.MockInstance)1 PasswordToken (org.apache.accumulo.core.client.security.tokens.PasswordToken)1 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)1 AccumuloRyaDAO (org.apache.rya.accumulo.AccumuloRyaDAO)1 RdfCloudTripleStore (org.apache.rya.rdftriplestore.RdfCloudTripleStore)1 IRI (org.eclipse.rdf4j.model.IRI)1 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)1 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)1 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)1 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)1