Search in sources :

Example 11 with QueryRoot

use of org.eclipse.rdf4j.query.algebra.QueryRoot in project incubator-rya by apache.

the class SparqlToPipelineTransformVisitorTest method testProjection.

@Test
public void testProjection() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isCourse = new StatementPattern(new Var("course"), constant(RDF.TYPE), constant(COURSE));
    StatementPattern hasEdge = new StatementPattern(new Var("x"), new Var("p"), new Var("course"));
    ProjectionElemList projectionElements = new ProjectionElemList(new ProjectionElem("p", "relation"), new ProjectionElem("course"));
    QueryRoot queryTree = new QueryRoot(new Projection(new Join(new Join(isCourse, hasEdge), isUndergrad), projectionElements));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("relation", "course"), pipelineNode.getAssuredBindingNames());
}
Also used : ProjectionElemList(org.eclipse.rdf4j.query.algebra.ProjectionElemList) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) Var(org.eclipse.rdf4j.query.algebra.Var) MultiProjection(org.eclipse.rdf4j.query.algebra.MultiProjection) Projection(org.eclipse.rdf4j.query.algebra.Projection) Join(org.eclipse.rdf4j.query.algebra.Join) ProjectionElem(org.eclipse.rdf4j.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 12 with QueryRoot

use of org.eclipse.rdf4j.query.algebra.QueryRoot in project incubator-rya by apache.

the class SparqlToPipelineTransformVisitorTest method testComplexJoin.

@Test
public void testComplexJoin() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isProfessor = new StatementPattern(new Var("y"), constant(RDF.TYPE), constant(PROFESSOR));
    StatementPattern takesCourse = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    StatementPattern teachesCourse = new StatementPattern(new Var("y"), constant(TEACHES), new Var("c"));
    QueryRoot queryTree = new QueryRoot(new Join(new Join(isUndergrad, takesCourse), new Join(isProfessor, teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Join);
    Join topJoin = (Join) queryTree.getArg();
    Assert.assertTrue(topJoin.getLeftArg() instanceof AggregationPipelineQueryNode);
    Assert.assertTrue(topJoin.getRightArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode leftPipeline = (AggregationPipelineQueryNode) topJoin.getLeftArg();
    AggregationPipelineQueryNode rightPipeline = (AggregationPipelineQueryNode) topJoin.getRightArg();
    Assert.assertEquals(Sets.newHashSet("x", "c"), leftPipeline.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("y", "c"), rightPipeline.getAssuredBindingNames());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) Var(org.eclipse.rdf4j.query.algebra.Var) Join(org.eclipse.rdf4j.query.algebra.Join) Test(org.junit.Test)

Example 13 with QueryRoot

use of org.eclipse.rdf4j.query.algebra.QueryRoot in project incubator-rya by apache.

the class SparqlToPipelineTransformVisitorTest method testNestedJoins.

@Test
public void testNestedJoins() throws Exception {
    StatementPattern isUndergrad = new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD));
    StatementPattern isProfessor = new StatementPattern(new Var("y"), constant(RDF.TYPE), constant(PROFESSOR));
    StatementPattern takesCourse = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    StatementPattern teachesCourse = new StatementPattern(new Var("y"), constant(TEACHES), new Var("c"));
    QueryRoot queryTree = new QueryRoot(new Join(isProfessor, new Join(new Join(isUndergrad, takesCourse), teachesCourse)));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "y", "c"), pipelineNode.getAssuredBindingNames());
}
Also used : StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) Var(org.eclipse.rdf4j.query.algebra.Var) Join(org.eclipse.rdf4j.query.algebra.Join) Test(org.junit.Test)

Example 14 with QueryRoot

use of org.eclipse.rdf4j.query.algebra.QueryRoot 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(final AbstractConstructRule rule, final int sourceLevel, final long timestamp) throws ForwardChainException {
    TupleExpr tupleExpr = rule.getQuery().getTupleExpr();
    if (!(tupleExpr instanceof QueryRoot)) {
        tupleExpr = new QueryRoot(tupleExpr);
    }
    try {
        tupleExpr.visit(pipelineVisitor);
    } catch (final Exception e) {
        throw new ForwardChainException("Error converting construct rule to an aggregation pipeline", e);
    }
    if (tupleExpr instanceof QueryRoot) {
        final QueryRoot root = (QueryRoot) tupleExpr;
        if (root.getArg() instanceof AggregationPipelineQueryNode) {
            final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) root.getArg();
            // require distinct triples
            pipelineNode.distinct();
            pipelineNode.requireSourceDerivationDepth(sourceLevel);
            final 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.eclipse.rdf4j.query.algebra.QueryRoot) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) MongoDbBatchWriterException(org.apache.rya.mongodb.batch.MongoDbBatchWriterException) ForwardChainException(org.apache.rya.forwardchain.ForwardChainException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException)

Example 15 with QueryRoot

use of org.eclipse.rdf4j.query.algebra.QueryRoot in project incubator-rya by apache.

the class PipelineQueryIT method testPipelineQuery.

private void testPipelineQuery(final String query, final Multiset<BindingSet> expectedSolutions) throws Exception {
    // Prepare query and convert to pipeline
    final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    // Execute pipeline and verify results
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    final Multiset<BindingSet> solutions = HashMultiset.create();
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Aggregations

QueryRoot (org.eclipse.rdf4j.query.algebra.QueryRoot)27 Test (org.junit.Test)13 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)11 QueryParser (org.eclipse.rdf4j.query.parser.QueryParser)11 SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)11 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)9 Var (org.eclipse.rdf4j.query.algebra.Var)8 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)5 Join (org.eclipse.rdf4j.query.algebra.Join)5 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)5 EmptyBindingSet (org.eclipse.rdf4j.query.impl.EmptyBindingSet)5 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)4 IRI (org.eclipse.rdf4j.model.IRI)3 Projection (org.eclipse.rdf4j.query.algebra.Projection)3 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)3 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)2 QueryJoinOptimizer (org.apache.rya.rdftriplestore.evaluation.QueryJoinOptimizer)2 InverseOfVisitor (org.apache.rya.rdftriplestore.inference.InverseOfVisitor)2 Extension (org.eclipse.rdf4j.query.algebra.Extension)2