Java Code Examples for org.eclipse.rdf4j.query.algebra.QueryRoot#visit()
The following examples show how to use
org.eclipse.rdf4j.query.algebra.QueryRoot#visit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryJoinOptimizerTest.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void bindSubselectJoinOrder() throws Exception { String query = "SELECT * WHERE {\n" + " BIND (bnode() as ?ct01) \n" + " { SELECT ?s WHERE {\n" + " ?s ?p ?o .\n" + " }\n" + " LIMIT 10\n" + " }\n" + "}"; SPARQLParser parser = new SPARQLParser(); ParsedQuery q = parser.parseQuery(query, null); QueryJoinOptimizer opt = new QueryJoinOptimizer(); QueryRoot optRoot = new QueryRoot(q.getTupleExpr()); opt.optimize(optRoot, null, null); JoinFinder joinFinder = new JoinFinder(); optRoot.visit(joinFinder); Join join = joinFinder.getJoin(); assertThat(join.getLeftArg()).as("BIND clause should be left-most argument of join") .isInstanceOf(Extension.class); }
Example 2
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@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()); }
Example 3
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@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()); }
Example 4
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@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()); }
Example 5
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@Test public void testMultiProjection() 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 courseHasRelation = new ProjectionElemList( new ProjectionElem("p", "relation"), new ProjectionElem("course")); ProjectionElemList studentHasRelation = new ProjectionElemList( new ProjectionElem("p", "relation"), new ProjectionElem("x", "student")); QueryRoot queryTree = new QueryRoot(new MultiProjection( new Join(new Join(isCourse, hasEdge), isUndergrad), Arrays.asList(courseHasRelation, studentHasRelation))); SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection); queryTree.visit(visitor); Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode); AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg(); Assert.assertEquals(Sets.newHashSet("relation"), pipelineNode.getAssuredBindingNames()); Assert.assertEquals(Sets.newHashSet("relation", "course", "student"), pipelineNode.getBindingNames()); }
Example 6
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 6 votes |
@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()); }
Example 7
Source File: SparqlQueryPigEngine.java From rya with Apache License 2.0 | 6 votes |
public String generatePigScript(final String sparql) throws Exception { Preconditions.checkNotNull(sparql, "Sparql query cannot be null"); final QueryParser parser = new SPARQLParser(); final ParsedQuery parsedQuery = parser.parseQuery(sparql, null); final QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr()); // SimilarVarJoinOptimizer similarVarJoinOptimizer = new SimilarVarJoinOptimizer(); // similarVarJoinOptimizer.optimize(tupleExpr, null, null); if (inference || stats) { if (inference) { tupleExpr.visit(new TransitivePropertyVisitor(conf, inferenceEngine)); tupleExpr.visit(new SymmetricPropertyVisitor(conf, inferenceEngine)); tupleExpr.visit(new InverseOfVisitor(conf, inferenceEngine)); } if (stats) { (new QueryJoinOptimizer(rdfCloudTripleStoreEvaluationStatistics)).optimize(tupleExpr, null, null); } } sparqlToPigTransformVisitor.meet(tupleExpr); return sparqlToPigTransformVisitor.getPigScript(); }
Example 8
Source File: PipelineQueryIT.java From rya with Apache License 2.0 | 5 votes |
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); }
Example 9
Source File: PipelineQueryIT.java From rya with Apache License 2.0 | 5 votes |
@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.createIRI("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" + "}"; final Multiset<BindingSet> expectedSolutions = HashMultiset.create(); expectedSolutions.add(new EmptyBindingSet()); // Execute pipeline and verify results final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr()); final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection()); queryTree.visit(visitor); Assert.assertTrue(queryTree.getArg() instanceof Projection); final Projection projection = (Projection) queryTree.getArg(); Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode); final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.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); }
Example 10
Source File: PipelineQueryIT.java From rya with Apache License 2.0 | 5 votes |
@Test public void testTriplePipeline() throws Exception { final IRI alice = VF.createIRI("urn:Alice"); final IRI bob = VF.createIRI("urn:Bob"); final IRI eve = VF.createIRI("urn:Eve"); final IRI friend = VF.createIRI("urn:friend"); final IRI knows = VF.createIRI("urn:knows"); final IRI year = VF.createIRI("urn:year"); final Literal yearLiteral = VF.createLiteral("2017", XMLSchema.GYEAR); final String query = "CONSTRUCT {\n" + " ?x <urn:knows> ?y .\n" + " ?x <urn:year> \"2017\"^^<" + XMLSchema.GYEAR + "> .\n" + "} WHERE { ?x <urn:friend> ?y }"; insert(alice, friend, bob); insert(bob, knows, eve); insert(eve, knows, alice); // 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); // Get pipeline, add triple conversion, and verify that the result is a // properly serialized statement Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode); final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg(); final List<Bson> triplePipeline = pipelineNode.getTriplePipeline(System.currentTimeMillis(), false); final SimpleMongoDBStorageStrategy strategy = new SimpleMongoDBStorageStrategy(); final List<Statement> results = new LinkedList<>(); for (final Document doc : getRyaCollection().aggregate(triplePipeline)) { final RyaStatement rstmt = strategy.deserializeDocument(doc); final Statement stmt = RyaToRdfConversions.convertStatement(rstmt); results.add(stmt); } Assert.assertEquals(2, results.size()); Assert.assertTrue(results.contains(VF.createStatement(alice, knows, bob))); Assert.assertTrue(results.contains(VF.createStatement(alice, year, yearLiteral))); }
Example 11
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testStatementPattern() throws Exception { QueryRoot query = new QueryRoot(new StatementPattern( new Var("x"), constant(RDF.TYPE), constant(UNDERGRAD))); SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection); query.visit(visitor); Assert.assertTrue(query.getArg() instanceof AggregationPipelineQueryNode); AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) query.getArg(); Assert.assertEquals(Sets.newHashSet("x"), pipelineNode.getAssuredBindingNames()); }
Example 12
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 5 votes |
@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()); }
Example 13
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 5 votes |
@Test public void testEmptyProjection() throws Exception { StatementPattern isClass = new StatementPattern(constant(UNDERGRAD), constant(RDF.TYPE), constant(OWL.CLASS)); QueryRoot queryTree = new QueryRoot(new Projection(isClass, new ProjectionElemList())); SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(collection); queryTree.visit(visitor); Assert.assertTrue(queryTree.getArg() instanceof Projection); Projection projectNode = (Projection) queryTree.getArg(); Assert.assertTrue(projectNode.getArg() instanceof AggregationPipelineQueryNode); AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projectNode.getArg(); Assert.assertEquals(Sets.newHashSet(), pipelineNode.getAssuredBindingNames()); }
Example 14
Source File: SparqlToPipelineTransformVisitorTest.java From rya with Apache License 2.0 | 5 votes |
@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()); }
Example 15
Source File: PipelineQueryIT.java From rya with Apache License 2.0 | 4 votes |
@Test public void testRequiredDerivationLevel() throws Exception { // Insert data final IRI person = VF.createIRI("urn:Person"); final IRI livingThing = VF.createIRI("urn:LivingThing"); final IRI human = VF.createIRI("urn:Human"); final IRI programmer = VF.createIRI("urn:Programmer"); final IRI thing = VF.createIRI("urn:Thing"); insert(programmer, RDFS.SUBCLASSOF, person); insert(person, RDFS.SUBCLASSOF, FOAF.PERSON); insert(FOAF.PERSON, RDFS.SUBCLASSOF, person); insert(person, OWL.EQUIVALENTCLASS, human); insert(person, RDFS.SUBCLASSOF, livingThing); insert(livingThing, RDFS.SUBCLASSOF, thing); insert(thing, RDFS.SUBCLASSOF, OWL.THING, 1); insert(OWL.THING, RDFS.SUBCLASSOF, thing); dao.flush(); // Define query and expected results final String query = "SELECT ?A ?B WHERE {\n" + " ?A rdfs:subClassOf ?B .\n" + " ?B rdfs:subClassOf ?A .\n" + "}"; final List<String> varNames = Arrays.asList("A", "B"); Multiset<BindingSet> expectedSolutions = HashMultiset.create(); expectedSolutions.add(new ListBindingSet(varNames, person, FOAF.PERSON)); expectedSolutions.add(new ListBindingSet(varNames, FOAF.PERSON, person)); expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING)); expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing)); // 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); Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode); final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg(); // Extend the pipeline by requiring a derivation level of zero (should have no effect) pipelineNode.requireSourceDerivationDepth(0); Multiset<BindingSet> solutions = HashMultiset.create(); CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet()); while (iter.hasNext()) { solutions.add(iter.next()); } Assert.assertEquals(expectedSolutions, solutions); // Extend the pipeline by requiring a derivation level of one (should produce the thing/thing pair) expectedSolutions = HashMultiset.create(); expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING)); expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing)); pipelineNode.requireSourceDerivationDepth(1); solutions = HashMultiset.create(); iter = pipelineNode.evaluate(new QueryBindingSet()); while (iter.hasNext()) { solutions.add(iter.next()); } Assert.assertEquals(expectedSolutions, solutions); }
Example 16
Source File: PipelineQueryIT.java From rya with Apache License 2.0 | 4 votes |
@Test public void testRequiredTimestamp() throws Exception { // Insert data final IRI person = VF.createIRI("urn:Person"); final IRI livingThing = VF.createIRI("urn:LivingThing"); final IRI human = VF.createIRI("urn:Human"); final IRI programmer = VF.createIRI("urn:Programmer"); final IRI thing = VF.createIRI("urn:Thing"); insert(programmer, RDFS.SUBCLASSOF, person); insert(person, RDFS.SUBCLASSOF, FOAF.PERSON, 2); insert(FOAF.PERSON, RDFS.SUBCLASSOF, person); insert(person, OWL.EQUIVALENTCLASS, human); insert(person, RDFS.SUBCLASSOF, livingThing); insert(livingThing, RDFS.SUBCLASSOF, thing); insert(thing, RDFS.SUBCLASSOF, OWL.THING); insert(OWL.THING, RDFS.SUBCLASSOF, thing); dao.flush(); // Define query and expected results final String query = "SELECT ?A ?B WHERE {\n" + " ?A rdfs:subClassOf ?B .\n" + " ?B rdfs:subClassOf ?A .\n" + "}"; final List<String> varNames = Arrays.asList("A", "B"); final Multiset<BindingSet> expectedSolutions = HashMultiset.create(); expectedSolutions.add(new ListBindingSet(varNames, person, FOAF.PERSON)); expectedSolutions.add(new ListBindingSet(varNames, FOAF.PERSON, person)); expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING)); expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing)); // 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); Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode); final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg(); // Extend the pipeline by requiring a timestamp of zero (should have no effect) pipelineNode.requireSourceTimestamp(0); final Multiset<BindingSet> solutions = HashMultiset.create(); CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet()); while (iter.hasNext()) { solutions.add(iter.next()); } Assert.assertEquals(expectedSolutions, solutions); // Extend the pipeline by requiring a future timestamp (should produce no results) final long delta = 1000 * 60 * 60 * 24; pipelineNode.requireSourceTimestamp(System.currentTimeMillis() + delta); iter = pipelineNode.evaluate(new QueryBindingSet()); Assert.assertFalse(iter.hasNext()); }