Search in sources :

Example 21 with Projection

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

the class ProjectionEvaluatorTest method addsBlankNodeBinding.

/**
 * This projection creates a Binding Set that represents a Statement that has a blank node added to it.
 */
@Test
public void addsBlankNodeBinding() throws Exception {
    // Read the projection object from a SPARQL query.
    final Projection projection = getProjection("CONSTRUCT { ?person <urn:hasChild> _:b } " + "WHERE {" + "?person <urn:hasGrandchild> ?grandchild ." + "}");
    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = new ValueFactoryImpl();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("hasGrandchild", vf.createURI("urn:Bob"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // Execute the projection.
    final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
    // The expected binding set represents a statement. We need to get the blank node's id from the
    // result since that is different every time.
    bs = new MapBindingSet();
    bs.addBinding("subject", vf.createURI("urn:Alice"));
    bs.addBinding("predicate", vf.createURI("urn:hasChild"));
    bs.addBinding("object", result.getValue("object"));
    final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
    assertEquals(expected, result);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Projection(org.openrdf.query.algebra.Projection) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 22 with Projection

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

the class ProjectionEvaluatorTest method getProjection.

/**
 * Get the first {@link Projection} node from a SPARQL query.
 *
 * @param sparql - The query that contains a single Projection node.
 * @return The first {@link Projection} that is encountered.
 * @throws Exception The query could not be parsed.
 */
@Nullable
public static Projection getProjection(final String sparql) throws Exception {
    requireNonNull(sparql);
    final AtomicReference<Projection> projection = new AtomicReference<>();
    final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
    parsed.getTupleExpr().visit(new QueryModelVisitorBase<Exception>() {

        @Override
        public void meet(final Projection node) throws Exception {
            projection.set(node);
        }
    });
    return projection.get();
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Projection(org.openrdf.query.algebra.Projection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 23 with Projection

use of org.openrdf.query.algebra.Projection 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 24 with Projection

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

Example 25 with Projection

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

the class AllValuesFromVisitorTest method testRewriteTypePattern.

@Test
public void testRewriteTypePattern() throws Exception {
    // Configure a mock instance engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<Resource, Set<URI>> personAVF = new HashMap<>();
    personAVF.put(parentsAreTallPeople, new HashSet<>());
    personAVF.put(parentsArePeople, new HashSet<>());
    personAVF.put(relativesArePeople, new HashSet<>());
    personAVF.get(parentsAreTallPeople).add(parent);
    personAVF.get(parentsArePeople).add(parent);
    personAVF.get(relativesArePeople).add(relative);
    personAVF.get(relativesArePeople).add(parent);
    Map<Resource, Set<URI>> dogAVF = new HashMap<>();
    dogAVF.put(parentsAreDogs, new HashSet<>());
    dogAVF.get(parentsAreDogs).add(parent);
    when(inferenceEngine.getAllValuesFromByValueType(person)).thenReturn(personAVF);
    when(inferenceEngine.getAllValuesFromByValueType(dog)).thenReturn(dogAVF);
    // Query for a specific type and rewrite using the visitor:
    StatementPattern originalSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", person));
    final Projection query = new Projection(originalSP, new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new AllValuesFromVisitor(conf, inferenceEngine));
    // Expected structure: a union of two elements: one is equal to the original statement
    // pattern, and the other one joins a list of predicate/restriction type combinations
    // with another join querying for values of that predicate for members of that type.
    Assert.assertTrue(query.getArg() instanceof Union);
    TupleExpr left = ((Union) query.getArg()).getLeftArg();
    TupleExpr right = ((Union) query.getArg()).getRightArg();
    final Join join;
    if (left instanceof StatementPattern) {
        Assert.assertEquals(originalSP, left);
        Assert.assertTrue(right instanceof Join);
        join = (Join) right;
    } else {
        Assert.assertEquals(originalSP, right);
        Assert.assertTrue(left instanceof Join);
        join = (Join) left;
    }
    Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
    Assert.assertTrue(join.getRightArg() instanceof Join);
    FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
    left = ((Join) join.getRightArg()).getLeftArg();
    right = ((Join) join.getRightArg()).getRightArg();
    Assert.assertTrue(left instanceof StatementPattern);
    Assert.assertTrue(right instanceof StatementPattern);
    // Verify expected predicate/restriction pairs
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsArePeople, OWL.ONPROPERTY, parent)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, relative)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, parent)));
    Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsAreTallPeople, OWL.ONPROPERTY, parent)));
    Assert.assertEquals(4, fsp.statements.size());
    // Verify general pattern for matching instances of each pair: Join on unknown subject; left
    // triple states it belongs to the restriction while right triple relates it to the original
    // subject variable by the relevant property. Restriction and property variables are given
    // by the FixedStatementPattern.
    StatementPattern leftSP = (StatementPattern) left;
    StatementPattern rightSP = (StatementPattern) right;
    Assert.assertEquals(rightSP.getSubjectVar(), leftSP.getSubjectVar());
    Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
    Assert.assertEquals(fsp.getSubjectVar(), leftSP.getObjectVar());
    Assert.assertEquals(fsp.getObjectVar(), rightSP.getPredicateVar());
    Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getObjectVar());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) Test(org.junit.Test)

Aggregations

Projection (org.openrdf.query.algebra.Projection)76 Test (org.junit.Test)64 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)37 ParsedQuery (org.openrdf.query.parser.ParsedQuery)34 TupleExpr (org.openrdf.query.algebra.TupleExpr)33 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)27 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)27 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)24 Var (org.openrdf.query.algebra.Var)24 ArrayList (java.util.ArrayList)23 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)23 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)22 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)17 Join (org.openrdf.query.algebra.Join)15 HashSet (java.util.HashSet)14 Union (org.openrdf.query.algebra.Union)10 Resource (org.openrdf.model.Resource)9 MultiProjection (org.openrdf.query.algebra.MultiProjection)9 HashMap (java.util.HashMap)8