Search in sources :

Example 1 with SPARQLResult

use of org.apache.jena.sparql.resultset.SPARQLResult in project jena by apache.

the class SPARQL_Query method executeQuery.

/** Perform the {@link QueryExecution} once.
     * @param action
     * @param queryExecution
     * @param query
     * @param queryStringLog Informational string created from the initial query.
     * @return
     */
protected SPARQLResult executeQuery(HttpAction action, QueryExecution queryExecution, Query query, String queryStringLog) {
    setAnyTimeouts(queryExecution, action);
    if (query.isSelectType()) {
        ResultSet rs = queryExecution.execSelect();
        // Force some query execution now.
        // If the timeout-first-row goes off, the output stream has not
        // been started so the HTTP error code is sent.
        rs.hasNext();
        //action.log.info(format("[%d] exec/select", action.id)) ;
        return new SPARQLResult(rs);
    }
    if (query.isConstructType()) {
        Dataset dataset = queryExecution.execConstructDataset();
        //action.log.info(format("[%d] exec/construct", action.id));
        return new SPARQLResult(dataset);
    }
    if (query.isDescribeType()) {
        Model model = queryExecution.execDescribe();
        //action.log.info(format("[%d] exec/describe", action.id)) ;
        return new SPARQLResult(model);
    }
    if (query.isAskType()) {
        boolean b = queryExecution.execAsk();
        //action.log.info(format("[%d] exec/ask", action.id)) ;
        return new SPARQLResult(b);
    }
    ServletOps.errorBadRequest("Unknown query type - " + queryStringLog);
    return null;
}
Also used : Model(org.apache.jena.rdf.model.Model) SPARQLResult(org.apache.jena.sparql.resultset.SPARQLResult)

Example 2 with SPARQLResult

use of org.apache.jena.sparql.resultset.SPARQLResult in project jena by apache.

the class TestItem method getResults.

/**
     * Load results as a SPARQLResult. If the results are a model, no conversion
     * to a result set is attempted here.
     */
public SPARQLResult getResults() {
    if (resultFile == null)
        return null;
    ResultsFormat format = ResultsFormat.guessSyntax(resultFile);
    if (ResultsFormat.isRDFGraphSyntax(format)) {
        Model m = FileManager.get().loadModel(resultFile);
        return new SPARQLResult(m);
    }
    if (ResultsFormat.isDatasetSyntax(format)) {
        Dataset d = RDFDataMgr.loadDataset(resultFile);
        return new SPARQLResult(d);
    }
    // Attempt to handle as a resulset or boolean result.s
    SPARQLResult x = ResultSetFactory.result(resultFile);
    return x;
}
Also used : Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) SPARQLResult(org.apache.jena.sparql.resultset.SPARQLResult) ResultsFormat(org.apache.jena.sparql.resultset.ResultsFormat)

Example 3 with SPARQLResult

use of org.apache.jena.sparql.resultset.SPARQLResult in project jena by apache.

the class SPARQL_Query method execute.

protected void execute(String queryString, HttpAction action) {
    String queryStringLog = ServletOps.formatForLog(queryString);
    if (action.verbose)
        action.log.info(format("[%d] Query = \n%s", action.id, queryString));
    else
        action.log.info(format("[%d] Query = %s", action.id, queryStringLog));
    Query query = null;
    try {
        // NB syntax is ARQ (a superset of SPARQL)
        query = QueryFactory.create(queryString, QueryParseBase, Syntax.syntaxARQ);
        queryStringLog = formatForLog(query);
        validateQuery(action, query);
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (QueryParseException ex) {
        ServletOps.errorBadRequest("Parse error: \n" + queryString + "\n\r" + messageForQueryException(ex));
    }// Should not happen.
     catch (QueryException ex) {
        ServletOps.errorBadRequest("Error: \n" + queryString + "\n\r" + ex.getMessage());
    }
    // Assumes finished whole thing by end of sendResult.
    try {
        action.beginRead();
        Dataset dataset = decideDataset(action, query, queryStringLog);
        try (QueryExecution qExec = createQueryExecution(query, dataset)) {
            SPARQLResult result = executeQuery(action, qExec, query, queryStringLog);
            // Deals with exceptions itself.
            sendResults(action, result, query.getPrologue());
        }
    } catch (QueryParseException ex) {
        // Late stage static error (e.g. bad fixed Lucene query string).
        ServletOps.errorBadRequest("Query parse error: \n" + queryString + "\n\r" + messageForQueryException(ex));
    } catch (QueryCancelledException ex) {
        // Additional counter information.
        incCounter(action.getEndpoint().getCounters(), QueryTimeouts);
        throw ex;
    } finally {
        action.endRead();
    }
}
Also used : HttpNames.paramQuery(org.apache.jena.riot.web.HttpNames.paramQuery) WebContent.ctSPARQLQuery(org.apache.jena.riot.WebContent.ctSPARQLQuery) SPARQLResult(org.apache.jena.sparql.resultset.SPARQLResult)

Aggregations

SPARQLResult (org.apache.jena.sparql.resultset.SPARQLResult)3 Model (org.apache.jena.rdf.model.Model)2 Dataset (org.apache.jena.query.Dataset)1 WebContent.ctSPARQLQuery (org.apache.jena.riot.WebContent.ctSPARQLQuery)1 HttpNames.paramQuery (org.apache.jena.riot.web.HttpNames.paramQuery)1 ResultsFormat (org.apache.jena.sparql.resultset.ResultsFormat)1