Search in sources :

Example 1 with ProjectionElemList

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

the class ProjectionEvaluator method make.

/**
 * Make a {@link ProjectionEvaluator} that processes the logic of a {@link Projection}.
 *
 * @param projection - Defines the projection that will be processed. (not null)
 * @return A {@link ProjectionEvaluator} for the provided {@link Projection}.
 */
public static ProjectionEvaluator make(final Projection projection) {
    requireNonNull(projection);
    final ProjectionElemList projectionElems = projection.getProjectionElemList();
    final TupleExpr arg = projection.getArg();
    final Optional<Extension> extension = arg instanceof Extension ? Optional.of((Extension) arg) : Optional.empty();
    return new ProjectionEvaluator(projectionElems, extension);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Extension(org.openrdf.query.algebra.Extension) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 2 with ProjectionElemList

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

use of org.openrdf.query.algebra.ProjectionElemList 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)

Example 4 with ProjectionElemList

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

the class HasSelfVisitorTest method testPropertyPattern_constantSubj.

@Test
public void testPropertyPattern_constantSubj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s", self);
    subj.setConstant(true);
    final Var obj = new Var("o");
    final Var pred = new Var("p", love);
    pred.setConstant(true);
    final Projection query = new Projection(new StatementPattern(subj, pred, obj), new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));
    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(new StatementPattern(subj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)), new ExtensionElem(subj, "o"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Union(org.openrdf.query.algebra.Union) Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ProjectionElemList

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

the class HasSelfVisitorTest method testPropertyPattern_constantObj.

@Test
public void testPropertyPattern_constantObj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s");
    final Var obj = new Var("o", self);
    obj.setConstant(true);
    final Var pred = new Var("p", love);
    pred.setConstant(true);
    final Projection query = new Projection(new StatementPattern(subj, pred, obj), new ProjectionElemList(new ProjectionElem("s", "subject")));
    query.visit(new HasSelfVisitor(conf, inferenceEngine));
    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
    Assert.assertTrue(union.getLeftArg() instanceof Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(new StatementPattern(obj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)), new ExtensionElem(obj, "s"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Union(org.openrdf.query.algebra.Union) Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)30 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)26 Test (org.junit.Test)25 Projection (org.openrdf.query.algebra.Projection)24 StatementPattern (org.openrdf.query.algebra.StatementPattern)24 Var (org.openrdf.query.algebra.Var)21 MultiProjection (org.openrdf.query.algebra.MultiProjection)10 Union (org.openrdf.query.algebra.Union)10 HashSet (java.util.HashSet)9 Resource (org.openrdf.model.Resource)9 Extension (org.openrdf.query.algebra.Extension)9 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)8 Join (org.openrdf.query.algebra.Join)8 TupleExpr (org.openrdf.query.algebra.TupleExpr)8 HashMap (java.util.HashMap)7 Set (java.util.Set)7 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)6 SingletonSet (org.openrdf.query.algebra.SingletonSet)5 URI (org.openrdf.model.URI)4 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)3