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());
}
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());
}
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());
}
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;
}
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);
}
Aggregations