Search in sources :

Example 1 with GraphQuery

use of org.openrdf.query.GraphQuery in project incubator-rya by apache.

the class RdfController method performGraphQuery.

private void performGraphQuery(final String query, final RepositoryConnection conn, final String auth, final String infer, final String nullout, final RDFHandler handler) throws RepositoryException, MalformedQueryException, QueryEvaluationException, RDFHandlerException {
    final GraphQuery graphQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, query);
    if (auth != null && auth.length() > 0) {
        graphQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, VALUE_FACTORY.createLiteral(auth));
    }
    if (infer != null && infer.length() > 0) {
        graphQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_INFER, VALUE_FACTORY.createLiteral(Boolean.parseBoolean(infer)));
    }
    if (nullout != null && nullout.length() > 0) {
        // output nothing, but still run query
        // TODO this seems like a strange use case.
        graphQuery.evaluate(new RDFHandler() {

            @Override
            public void startRDF() throws RDFHandlerException {
            }

            @Override
            public void endRDF() throws RDFHandlerException {
            }

            @Override
            public void handleNamespace(final String prefix, final String uri) throws RDFHandlerException {
            }

            @Override
            public void handleStatement(final Statement st) throws RDFHandlerException {
            }

            @Override
            public void handleComment(final String comment) throws RDFHandlerException {
            }
        });
    } else {
        final long startTime = System.currentTimeMillis();
        graphQuery.evaluate(handler);
        log.info(String.format("Query Time = %.3f\n", (System.currentTimeMillis() - startTime) / 1000.));
    }
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) GraphQuery(org.openrdf.query.GraphQuery) RDFHandler(org.openrdf.rio.RDFHandler)

Example 2 with GraphQuery

use of org.openrdf.query.GraphQuery in project incubator-rya by apache.

the class SailExecutionStrategy method executeConstructRule.

/**
 * Executes a CONSTRUCT query through the SAIL and inserts the results into
 * the DAO.
 * @param rule A construct query; not null.
 * @param metadata Metadata to add to any inferred triples; not null.
 * @return The number of inferred triples.
 * @throws ForwardChainException if query execution or data insert fails.
 */
@Override
public long executeConstructRule(AbstractConstructRule rule, StatementMetadata metadata) throws ForwardChainException {
    Preconditions.checkNotNull(rule);
    Preconditions.checkNotNull(metadata);
    if (!initialized) {
        initialize();
    }
    ParsedGraphQuery graphQuery = rule.getQuery();
    long statementsAdded = 0;
    logger.info("Applying inference rule " + rule + "...");
    for (String line : graphQuery.getTupleExpr().toString().split("\n")) {
        logger.debug("\t" + line);
    }
    InferredStatementHandler<?> handler = new InferredStatementHandler<>(dao, metadata);
    try {
        GraphQuery executableQuery = new SailGraphQuery(graphQuery, conn) {
        };
        executableQuery.evaluate(handler);
        statementsAdded = handler.getNumStatementsAdded();
        logger.info("Added " + statementsAdded + " inferred statements.");
        return statementsAdded;
    } catch (QueryEvaluationException e) {
        throw new ForwardChainException("Error evaluating query portion of construct rule", e);
    } catch (RDFHandlerException e) {
        throw new ForwardChainException("Error processing results of construct rule", e);
    }
}
Also used : QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) SailGraphQuery(org.openrdf.repository.sail.SailGraphQuery) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) GraphQuery(org.openrdf.query.GraphQuery) SailGraphQuery(org.openrdf.repository.sail.SailGraphQuery)

Aggregations

GraphQuery (org.openrdf.query.GraphQuery)2 ParsedGraphQuery (org.openrdf.query.parser.ParsedGraphQuery)2 RDFHandlerException (org.openrdf.rio.RDFHandlerException)2 ForwardChainException (org.apache.rya.forwardchain.ForwardChainException)1 Statement (org.openrdf.model.Statement)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 SailGraphQuery (org.openrdf.repository.sail.SailGraphQuery)1 RDFHandler (org.openrdf.rio.RDFHandler)1