Search in sources :

Example 6 with UnaryResult

use of org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult 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);
}
Also used : ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) ResultType(org.apache.rya.streams.kafka.processors.ProcessorResult.ResultType) MultiProjectionProcessor(org.apache.rya.streams.kafka.processors.projection.MultiProjectionProcessorSupplier.MultiProjectionProcessor) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) RDF(org.openrdf.model.vocabulary.RDF) ProcessorResult(org.apache.rya.streams.kafka.processors.ProcessorResult) MultiProjection(org.openrdf.query.algebra.MultiProjection) ValueFactory(org.openrdf.model.ValueFactory) RdfTestUtil(org.apache.rya.streams.kafka.RdfTestUtil) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) BNode(org.openrdf.model.BNode) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) MultiProjectionEvaluator(org.apache.rya.api.function.projection.MultiProjectionEvaluator) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BNode(org.openrdf.model.BNode) ProcessorResult(org.apache.rya.streams.kafka.processors.ProcessorResult) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) MultiProjectionProcessor(org.apache.rya.streams.kafka.processors.projection.MultiProjectionProcessorSupplier.MultiProjectionProcessor) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MultiProjection(org.openrdf.query.algebra.MultiProjection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with UnaryResult

use of org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult in project incubator-rya by apache.

the class StatementOutputFormatterTest method predicateWrongType.

@Test
public void predicateWrongType() {
    // Create the input binding set.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bindingSet = new MapBindingSet();
    bindingSet.addBinding("subject", vf.createURI("urn:Alice"));
    bindingSet.addBinding("predicate", vf.createLiteral("age"));
    bindingSet.addBinding("object", vf.createLiteral(34));
    final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a");
    // Mock the processor context that will be invoked.
    final ProcessorContext context = mock(ProcessorContext.class);
    // Run the test.
    final StatementOutputFormatter formatter = new StatementOutputFormatter();
    formatter.init(context);
    formatter.process("key", ProcessorResult.make(new UnaryResult(visBs)));
    // Verify the mock was never invoked.
    verify(context, times(0)).forward(any(), any());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) StatementOutputFormatter(org.apache.rya.streams.kafka.processors.output.StatementOutputFormatterSupplier.StatementOutputFormatter) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Test(org.junit.Test)

Example 8 with UnaryResult

use of org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult in project incubator-rya by apache.

the class StatementOutputFormatterTest method missingPredicate.

@Test
public void missingPredicate() {
    // Create the input binding set.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bindingSet = new MapBindingSet();
    bindingSet.addBinding("subject", vf.createURI("urn:Alice"));
    bindingSet.addBinding("object", vf.createLiteral(34));
    final VisibilityBindingSet visBs = new VisibilityBindingSet(bindingSet, "a");
    // Mock the processor context that will be invoked.
    final ProcessorContext context = mock(ProcessorContext.class);
    // Run the test.
    final StatementOutputFormatter formatter = new StatementOutputFormatter();
    formatter.init(context);
    formatter.process("key", ProcessorResult.make(new UnaryResult(visBs)));
    // Verify the mock was never invoked.
    verify(context, times(0)).forward(any(), any());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) StatementOutputFormatter(org.apache.rya.streams.kafka.processors.output.StatementOutputFormatterSupplier.StatementOutputFormatter) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Test(org.junit.Test)

Example 9 with UnaryResult

use of org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult 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)

Example 10 with UnaryResult

use of org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult in project incubator-rya by apache.

the class FilterProcessorTest method showFilterFunctionIsCalled.

@Test
public void showFilterFunctionIsCalled() throws Exception {
    // Read the filter object from a SPARQL query.
    final Filter filter = RdfTestUtil.getFilter("SELECT * " + "WHERE { " + "FILTER(?age < 10)" + "?person <urn:age> ?age " + "}");
    // Create a Binding Set that will be passed into the Filter function based on the where clause.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("age", vf.createLiteral(9));
    final VisibilityBindingSet inputVisBs = new VisibilityBindingSet(bs, "a");
    // Mock the processor context that will be invoked.
    final ProcessorContext context = mock(ProcessorContext.class);
    // Run the test.
    final FilterProcessor processor = new FilterProcessor(FilterEvaluator.make(filter), result -> ProcessorResult.make(new UnaryResult(result)));
    processor.init(context);
    processor.process("key", ProcessorResult.make(new UnaryResult(inputVisBs)));
    // Verify the binding set was passed through.
    verify(context, times(1)).forward(eq("key"), eq(ProcessorResult.make(new UnaryResult(inputVisBs))));
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Filter(org.openrdf.query.algebra.Filter) FilterProcessor(org.apache.rya.streams.kafka.processors.filter.FilterProcessorSupplier.FilterProcessor) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) UnaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Test(org.junit.Test)

Aggregations

ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)10 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)10 UnaryResult (org.apache.rya.streams.kafka.processors.ProcessorResult.UnaryResult)10 Test (org.junit.Test)10 ValueFactory (org.openrdf.model.ValueFactory)10 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)10 MapBindingSet (org.openrdf.query.impl.MapBindingSet)10 StatementOutputFormatter (org.apache.rya.streams.kafka.processors.output.StatementOutputFormatterSupplier.StatementOutputFormatter)6 ProcessorResult (org.apache.rya.streams.kafka.processors.ProcessorResult)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 MultiProjectionEvaluator (org.apache.rya.api.function.projection.MultiProjectionEvaluator)1 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)1 RdfTestUtil (org.apache.rya.streams.kafka.RdfTestUtil)1 ResultType (org.apache.rya.streams.kafka.processors.ProcessorResult.ResultType)1 FilterProcessor (org.apache.rya.streams.kafka.processors.filter.FilterProcessorSupplier.FilterProcessor)1 BindingSetOutputFormatter (org.apache.rya.streams.kafka.processors.output.BindingSetOutputFormatterSupplier.BindingSetOutputFormatter)1 MultiProjectionProcessor (org.apache.rya.streams.kafka.processors.projection.MultiProjectionProcessorSupplier.MultiProjectionProcessor)1