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);
}
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);
}
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);
}
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);
}
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));
}
Aggregations