Search in sources :

Example 1 with ParsedOperation

use of org.eclipse.rdf4j.query.parser.ParsedOperation in project rdf4j by eclipse.

the class SPARQLParser method main.

public static void main(String[] args) throws java.io.IOException {
    System.out.println("Your SPARQL query:");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    StringBuilder buf = new StringBuilder();
    String line = null;
    int emptyLineCount = 0;
    while ((line = in.readLine()) != null) {
        if (line.length() > 0) {
            emptyLineCount = 0;
            buf.append(' ').append(line).append('\n');
        } else {
            emptyLineCount++;
        }
        if (emptyLineCount == 2) {
            emptyLineCount = 0;
            String queryStr = buf.toString().trim();
            if (queryStr.length() > 0) {
                try {
                    long start = System.currentTimeMillis();
                    ParsedOperation parsedQuery = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, queryStr, null);
                    long finish = System.currentTimeMillis();
                    System.out.println("Parsed query: ");
                    System.out.println(parsedQuery.toString());
                    System.out.println();
                    System.out.println("parsed in " + (finish - start) + " ms.");
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                    e.printStackTrace();
                }
            }
            buf.setLength(0);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ParsedOperation(org.eclipse.rdf4j.query.parser.ParsedOperation) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) IncompatibleOperationException(org.eclipse.rdf4j.query.IncompatibleOperationException) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)

Example 2 with ParsedOperation

use of org.eclipse.rdf4j.query.parser.ParsedOperation 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)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ParsedOperation (org.eclipse.rdf4j.query.parser.ParsedOperation)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)1 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 TupleQueryResultHandler (org.eclipse.rdf4j.query.TupleQueryResultHandler)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)1 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)1 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)1 ParsedUpdate (org.eclipse.rdf4j.query.parser.ParsedUpdate)1 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)1 VisitorException (org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException)1 SPARQLResultsJSONWriter (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONWriter)1