Search in sources :

Example 1 with AggregationPipelineQueryNode

use of org.apache.rya.mongodb.aggregation.AggregationPipelineQueryNode in project incubator-rya by apache.

the class MongoPipelineStrategy method toPipeline.

/**
 * Converts a construct rule into a series of documents representing
 * aggregation pipeline steps.
 * @param rule A construct query rule.
 * @param sourceLevel Only make derivations whose source triples have this
 *  derivation level or higher, i.e. took some number of forward chaining
 *  steps to infer. Set to zero to skip this check.
 * @param timestamp Timestamp to be set for all inferred triples.
 * @return An aggregation pipeline.
 * @throws ForwardChainException if pipeline construction fails.
 */
private List<Bson> toPipeline(AbstractConstructRule rule, int sourceLevel, long timestamp) throws ForwardChainException {
    TupleExpr tupleExpr = rule.getQuery().getTupleExpr();
    if (!(tupleExpr instanceof QueryRoot)) {
        tupleExpr = new QueryRoot(tupleExpr);
    }
    try {
        tupleExpr.visit(pipelineVisitor);
    } catch (Exception e) {
        throw new ForwardChainException("Error converting construct rule to an aggregation pipeline", e);
    }
    if (tupleExpr instanceof QueryRoot) {
        QueryRoot root = (QueryRoot) tupleExpr;
        if (root.getArg() instanceof AggregationPipelineQueryNode) {
            AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) root.getArg();
            // require distinct triples
            pipelineNode.distinct();
            pipelineNode.requireSourceDerivationDepth(sourceLevel);
            long latestTime = executionTimes.getOrDefault(rule, 0L);
            if (latestTime > 0) {
                pipelineNode.requireSourceTimestamp(latestTime);
            }
            return pipelineNode.getTriplePipeline(timestamp, false);
        }
    }
    return null;
}
Also used : AggregationPipelineQueryNode(org.apache.rya.mongodb.aggregation.AggregationPipelineQueryNode) QueryRoot(org.openrdf.query.algebra.QueryRoot) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) TupleExpr(org.openrdf.query.algebra.TupleExpr) MongoDbBatchWriterException(org.apache.rya.mongodb.batch.MongoDbBatchWriterException) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException)

Aggregations

RyaDAOException (org.apache.rya.api.persist.RyaDAOException)1 ForwardChainException (org.apache.rya.forwardchain.ForwardChainException)1 AggregationPipelineQueryNode (org.apache.rya.mongodb.aggregation.AggregationPipelineQueryNode)1 MongoDbBatchWriterException (org.apache.rya.mongodb.batch.MongoDbBatchWriterException)1 QueryRoot (org.openrdf.query.algebra.QueryRoot)1 TupleExpr (org.openrdf.query.algebra.TupleExpr)1