Search in sources :

Example 36 with Projection

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

the class ProjectionEvaluatorTest method addsConstantBinding.

/**
 * This projection creates a Binding Set that represents a Statement and add a constant value to it.
 */
@Test
public void addsConstantBinding() throws Exception {
    // Read the projection object from a SPARQL query.
    final Projection projection = getProjection("CONSTRUCT { ?person <urn:hasGrandchild> ?grandchild } " + "WHERE {" + "?person <urn:hasChild> ?child ." + "?child <urn:hasChild> ?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("child", vf.createURI("urn:Bob"));
    bs.addBinding("grandchild", vf.createURI("urn:Charlie"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // The expected binding set represents a statement.
    bs = new MapBindingSet();
    bs.addBinding("subject", vf.createURI("urn:Alice"));
    bs.addBinding("predicate", vf.createURI("urn:hasGrandchild"));
    bs.addBinding("object", vf.createURI("urn:Charlie"));
    final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
    // Execute the projection.
    final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
    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 37 with Projection

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

the class ProjectionEvaluatorTest method dropsBinding.

/**
 * This projection drops a binding from the original Binding Set.
 */
@Test
public void dropsBinding() throws Exception {
    // Read the projection object from a SPARQL query.
    final Projection projection = getProjection("SELECT ?person " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}");
    // 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("employee", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // The expected binding set only has the "person" binding.
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
    // Execute the projection.
    final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
    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 38 with Projection

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

the class ProjectionEvaluatorTest method renameBindings.

/**
 * This Projection replaces some of the variables names with different names.
 */
@Test
public void renameBindings() throws Exception {
    // Read the projection object from a SPARQL query.
    final Projection projection = getProjection("SELECT (?person AS ?p) (?employee AS ?e) ?business " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}");
    // 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("employee", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // The expected binding set changes the "person" binding name to "p" and "employee" to "e".
    bs = new MapBindingSet();
    bs.addBinding("p", vf.createURI("urn:Alice"));
    bs.addBinding("e", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
    // Execute the projection.
    final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
    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 39 with Projection

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

the class SparqlFluoQueryBuilder method getConstructGraphVarOrder.

private static VariableOrder getConstructGraphVarOrder(final Reduced node) {
    // get child node
    final QueryModelNode child = node.getArg();
    Preconditions.checkArgument(child instanceof Projection || child instanceof MultiProjection);
    final UnaryTupleOperator unary = (UnaryTupleOperator) child;
    // get ProjectionElemList to build ConstructGraph
    final List<ProjectionElemList> projections = new ArrayList<>();
    if (unary instanceof Projection) {
        projections.add(((Projection) unary).getProjectionElemList());
    } else {
        projections.addAll(((MultiProjection) unary).getProjections());
    }
    return getConstructGraphVarOrder(projections);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) UnaryTupleOperator(org.openrdf.query.algebra.UnaryTupleOperator) ArrayList(java.util.ArrayList) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) MultiProjection(org.openrdf.query.algebra.MultiProjection) Projection(org.openrdf.query.algebra.Projection) ConstructProjection(org.apache.rya.indexing.pcj.fluo.app.ConstructProjection) MultiProjection(org.openrdf.query.algebra.MultiProjection)

Example 40 with Projection

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

the class ProjectionProcessorTest method showProjectionFunctionIsCalled.

@Test
public void showProjectionFunctionIsCalled() throws Exception {
    // A query whose projection does not change anything.
    final Projection projection = RdfTestUtil.getProjection("SELECT (?person AS ?p) (?employee AS ?e) ?business " + "WHERE { " + "?person <urn:talksTo> ?employee . " + "?employee <urn:worksAt> ?business . " + "}");
    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet inputBs = new MapBindingSet();
    inputBs.addBinding("person", vf.createURI("urn:Alice"));
    inputBs.addBinding("employee", vf.createURI("urn:Bob"));
    inputBs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(inputBs, "a");
    // The expected binding set changes the "person" binding name to "p" and "employee" to "e".
    final MapBindingSet expectedBs = new MapBindingSet();
    expectedBs.addBinding("p", vf.createURI("urn:Alice"));
    expectedBs.addBinding("e", vf.createURI("urn:Bob"));
    expectedBs.addBinding("business", vf.createURI("urn:TacoJoint"));
    final VisibilityBindingSet expectedVisBs = new VisibilityBindingSet(expectedBs, "a");
    // Show it resulted in the correct output BindingSet.
    final ProcessorContext context = mock(ProcessorContext.class);
    final ProjectionProcessor processor = new ProjectionProcessor(ProjectionEvaluator.make(projection), result -> ProcessorResult.make(new UnaryResult(result)));
    processor.init(context);
    processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs)));
    // Verify the expected binding set was emitted.
    final ProcessorResult expected = ProcessorResult.make(new UnaryResult(expectedVisBs));
    verify(context, times(1)).forward(eq("key"), eq(expected));
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ProcessorResult(org.apache.rya.streams.kafka.processors.ProcessorResult) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Projection(org.openrdf.query.algebra.Projection) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProjectionProcessor(org.apache.rya.streams.kafka.processors.projection.ProjectionProcessorSupplier.ProjectionProcessor) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) 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