Search in sources :

Example 1 with SailGraphQuery

use of org.openrdf.repository.sail.SailGraphQuery 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

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