Search in sources :

Example 6 with QueryRoot

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

the class PipelineQueryIT method testNoVariableSP.

@Test
public void testNoVariableSP() throws Exception {
    // Insert data
    insert(OWL.THING, RDF.TYPE, OWL.CLASS);
    insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
    insert(VF.createURI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT * WHERE {\n" + "  owl:Thing a owl:Class .\n" + "}";
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new EmptyBindingSet());
    // Execute pipeline and verify results
    QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Projection);
    Projection projection = (Projection) queryTree.getArg();
    Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
    Multiset<BindingSet> solutions = HashMultiset.create();
    CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) QueryRoot(org.openrdf.query.algebra.QueryRoot) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Projection(org.openrdf.query.algebra.Projection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 7 with QueryRoot

use of org.openrdf.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.openrdf.query.algebra.StatementPattern) QueryRoot(org.openrdf.query.algebra.QueryRoot) Var(org.openrdf.query.algebra.Var) Join(org.openrdf.query.algebra.Join) Test(org.junit.Test)

Example 8 with QueryRoot

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

the class SparqlToPipelineTransformVisitorTest method testUnsupportedExtension.

@Test
public void testUnsupportedExtension() throws Exception {
    StatementPattern sp = new StatementPattern(new Var("x"), constant(TAKES), new Var("c"));
    List<ExtensionElem> elements = Arrays.asList(new ExtensionElem(new Var("x"), "renamed"), new ExtensionElem(new Not(new ValueConstant(VF.createLiteral(true))), "notTrue"), new ExtensionElem(new ValueConstant(TAKES), "constant"));
    Extension extensionNode = new Extension(sp, elements);
    QueryRoot queryTree = new QueryRoot(extensionNode);
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Extension);
    Assert.assertEquals(elements, ((Extension) queryTree.getArg()).getElements());
    TupleExpr innerQuery = ((Extension) queryTree.getArg()).getArg();
    Assert.assertTrue(innerQuery instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) innerQuery;
    Assert.assertEquals(Sets.newHashSet("x", "c"), pipelineNode.getAssuredBindingNames());
}
Also used : Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) Not(org.openrdf.query.algebra.Not) QueryRoot(org.openrdf.query.algebra.QueryRoot) Var(org.openrdf.query.algebra.Var) ValueConstant(org.openrdf.query.algebra.ValueConstant) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 9 with QueryRoot

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

the class SparqlToPipelineTransformVisitorTest method testExtension.

@Test
public void testExtension() throws Exception {
    QueryRoot queryTree = new QueryRoot(new Extension(new StatementPattern(new Var("x"), constant(TAKES), new Var("c")), new ExtensionElem(new Var("x"), "renamed"), new ExtensionElem(new ValueConstant(TAKES), "constant")));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "c", "renamed", "constant"), pipelineNode.getAssuredBindingNames());
}
Also used : Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryRoot(org.openrdf.query.algebra.QueryRoot) Var(org.openrdf.query.algebra.Var) ValueConstant(org.openrdf.query.algebra.ValueConstant) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Test(org.junit.Test)

Example 10 with QueryRoot

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

the class SparqlToPipelineTransformVisitorTest method testJoin.

@Test
public void testJoin() throws Exception {
    QueryRoot query = new QueryRoot(new Join(new StatementPattern(new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD)), new StatementPattern(new Var("x"), constant(TAKES), new Var("course"))));
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection);
    query.visit(visitor);
    Assert.assertTrue(query.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) query.getArg();
    Assert.assertEquals(Sets.newHashSet("x", "course"), pipelineNode.getAssuredBindingNames());
}
Also used : StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryRoot(org.openrdf.query.algebra.QueryRoot) Var(org.openrdf.query.algebra.Var) Join(org.openrdf.query.algebra.Join) Test(org.junit.Test)

Aggregations

QueryRoot (org.openrdf.query.algebra.QueryRoot)27 Test (org.junit.Test)13 ParsedQuery (org.openrdf.query.parser.ParsedQuery)11 QueryParser (org.openrdf.query.parser.QueryParser)11 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)11 StatementPattern (org.openrdf.query.algebra.StatementPattern)9 Var (org.openrdf.query.algebra.Var)8 BindingSet (org.openrdf.query.BindingSet)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 Join (org.openrdf.query.algebra.Join)5 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)5 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)5 TupleExpr (org.openrdf.query.algebra.TupleExpr)4 URI (org.openrdf.model.URI)3 Projection (org.openrdf.query.algebra.Projection)3 ListBindingSet (org.openrdf.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.openrdf.query.algebra.Extension)2