use of org.openrdf.query.algebra.MultiProjection in project incubator-rya by apache.
the class MultiProjectionProcessorTest method showProjectionFunctionIsCalled.
@Test
public void showProjectionFunctionIsCalled() throws Exception {
// The SPARQL that will define the projection.
final MultiProjection multiProjection = RdfTestUtil.getMultiProjection("CONSTRUCT {" + "_:b a <urn:movementObservation> ; " + "<urn:location> ?location ; " + "<urn:direction> ?direction ; " + "}" + "WHERE {" + "?thing <urn:corner> ?location ." + "?thing <urn:compass> ?direction." + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
final MapBindingSet inputBs = new MapBindingSet();
inputBs.addBinding("location", vf.createURI("urn:corner1"));
inputBs.addBinding("direction", vf.createURI("urn:NW"));
final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(inputBs, "a|b");
// Make the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
final String blankNodeId = UUID.randomUUID().toString();
final BNode blankNode = vf.createBNode(blankNodeId);
MapBindingSet expectedBs = new MapBindingSet();
expectedBs.addBinding("subject", blankNode);
expectedBs.addBinding("predicate", RDF.TYPE);
expectedBs.addBinding("object", vf.createURI("urn:movementObservation"));
expected.add(new VisibilityBindingSet(expectedBs, "a|b"));
expectedBs = new MapBindingSet();
expectedBs.addBinding("subject", blankNode);
expectedBs.addBinding("predicate", vf.createURI("urn:direction"));
expectedBs.addBinding("object", vf.createURI("urn:NW"));
expected.add(new VisibilityBindingSet(expectedBs, "a|b"));
expectedBs = new MapBindingSet();
expectedBs.addBinding("subject", blankNode);
expectedBs.addBinding("predicate", vf.createURI("urn:location"));
expectedBs.addBinding("object", vf.createURI("urn:corner1"));
expected.add(new VisibilityBindingSet(expectedBs, "a|b"));
// Mock the processor context that will be invoked.
final ProcessorContext context = mock(ProcessorContext.class);
// Run the test.
final MultiProjectionProcessor processor = new MultiProjectionProcessor(MultiProjectionEvaluator.make(multiProjection, () -> blankNodeId), result -> ProcessorResult.make(new UnaryResult(result)));
processor.init(context);
processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs)));
final ArgumentCaptor<ProcessorResult> results = ArgumentCaptor.forClass(ProcessorResult.class);
verify(context, times(3)).forward(any(), results.capture());
final Set<VisibilityBindingSet> resultBindingSets = results.getAllValues().stream().map(result -> {
return (result.getType() == ResultType.UNARY) ? result.getUnary().getResult() : result.getBinary().getResult();
}).collect(Collectors.toSet());
assertEquals(expected, resultBindingSets);
}
use of org.openrdf.query.algebra.MultiProjection in project incubator-rya by apache.
the class MultiProjectionEvaluatorTest method getMultiProjection.
/**
* Get the first {@link MultiProjection} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link MultiProjection} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new QueryModelVisitorBase<Exception>() {
@Override
public void meet(final MultiProjection node) throws Exception {
multiProjection.set(node);
}
});
return multiProjection.get();
}
use of org.openrdf.query.algebra.MultiProjection in project incubator-rya by apache.
the class RdfTestUtil method getMultiProjection.
/**
* Get the first {@link MultiProjection} node from a SPARQL query.
*
* @param sparql - The query that contains a single Projection node.
* @return The first {@link MultiProjection} that is encountered.
* @throws Exception The query could not be parsed.
*/
@Nullable
public static MultiProjection getMultiProjection(final String sparql) throws Exception {
requireNonNull(sparql);
final AtomicReference<MultiProjection> multiProjection = new AtomicReference<>();
final ParsedQuery parsed = new SPARQLParser().parseQuery(sparql, null);
parsed.getTupleExpr().visit(new QueryModelVisitorBase<Exception>() {
@Override
public void meet(final MultiProjection node) throws Exception {
multiProjection.set(node);
}
});
return multiProjection.get();
}
use of org.openrdf.query.algebra.MultiProjection in project incubator-rya by apache.
the class MultiProjectionEvaluatorTest method singleBlankNode.
@Test
public void singleBlankNode() throws Exception {
// Read the multi projection object from a SPARQL query.
final MultiProjection multiProjection = getMultiProjection("CONSTRUCT {" + "_:b a <urn:movementObservation> ; " + "<urn:location> ?location ; " + "<urn:direction> ?direction ; " + "}" + "WHERE {" + "?thing <urn:corner> ?location ." + "?thing <urn:compass> ?direction." + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("location", vf.createLiteral("South St and 5th St"));
bs.addBinding("direction", vf.createLiteral("NW"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Create the expected results.
final Set<VisibilityBindingSet> expected = new HashSet<>();
final String blankNodeId = UUID.randomUUID().toString();
final BNode blankNode = vf.createBNode(blankNodeId);
bs = new MapBindingSet();
bs.addBinding("subject", blankNode);
bs.addBinding("predicate", RDF.TYPE);
bs.addBinding("object", vf.createURI("urn:movementObservation"));
expected.add(new VisibilityBindingSet(bs, "a|b"));
bs = new MapBindingSet();
bs.addBinding("subject", blankNode);
bs.addBinding("predicate", vf.createURI("urn:location"));
bs.addBinding("object", vf.createLiteral("South St and 5th St"));
expected.add(new VisibilityBindingSet(bs, "a|b"));
bs = new MapBindingSet();
bs.addBinding("subject", blankNode);
bs.addBinding("predicate", vf.createURI("urn:direction"));
bs.addBinding("object", vf.createLiteral("NW"));
expected.add(new VisibilityBindingSet(bs, "a|b"));
// Run the projection evaluator.
final Set<VisibilityBindingSet> results = MultiProjectionEvaluator.make(multiProjection, () -> blankNodeId).project(original);
// The expected binding sets.
assertEquals(expected, results);
}
use of org.openrdf.query.algebra.MultiProjection in project incubator-rya by apache.
the class MultiProjectionEvaluatorTest method multipleBlanknodes.
@Test
public void multipleBlanknodes() throws Exception {
// Read the multi projection object from a SPARQL query.
final MultiProjection multiProjection = getMultiProjection("CONSTRUCT {" + "_:b a <urn:vehicle> . " + "_:b <urn:tiresCount> 4 ." + "_:c a <urn:pet> . " + "_:c <urn:isDead> false . " + "}" + "WHERE {" + "?vehicle <urn:owner> ?owner . " + "?vehicle <urn:plates> ?plates . " + "?pet <urn:owner> ?owner . " + "?pet <urn:isLiving> true . " + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("vehicle", vf.createLiteral("Alice's car"));
bs.addBinding("owner", vf.createURI("urn:Alice"));
bs.addBinding("plates", vf.createLiteral("XXXXXXX"));
bs.addBinding("pet", vf.createURI("urn:Kitty"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Run the projection evaluator.
final Set<VisibilityBindingSet> results = MultiProjectionEvaluator.make(multiProjection, new RandomUUIDFactory()).project(original);
// Figure out the blank nodes.
Value vehicalBNode = null;
Value petBNode = null;
for (final VisibilityBindingSet result : results) {
final Value object = result.getValue("object");
if (object.equals(vf.createURI("urn:vehicle"))) {
vehicalBNode = result.getValue("subject");
} else if (object.equals(vf.createURI("urn:pet"))) {
petBNode = result.getValue("subject");
}
}
// The expected binding sets.
final Set<VisibilityBindingSet> expected = new HashSet<>();
bs = new MapBindingSet();
bs.addBinding("subject", vehicalBNode);
bs.addBinding("predicate", RDF.TYPE);
bs.addBinding("object", vf.createURI("urn:vehicle"));
expected.add(new VisibilityBindingSet(bs, "a|b"));
bs = new MapBindingSet();
bs.addBinding("subject", vehicalBNode);
bs.addBinding("predicate", vf.createURI("urn:tiresCount"));
bs.addBinding("object", vf.createLiteral("4", XMLSchema.INTEGER));
expected.add(new VisibilityBindingSet(bs, "a|b"));
bs = new MapBindingSet();
bs.addBinding("subject", petBNode);
bs.addBinding("predicate", RDF.TYPE);
bs.addBinding("object", vf.createURI("urn:pet"));
expected.add(new VisibilityBindingSet(bs, "a|b"));
bs = new MapBindingSet();
bs.addBinding("subject", petBNode);
bs.addBinding("predicate", vf.createURI("urn:isDead"));
bs.addBinding("object", vf.createLiteral(false));
expected.add(new VisibilityBindingSet(bs, "a|b"));
assertEquals(expected, results);
}
Aggregations