Search in sources :

Example 1 with MultiProjection

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);
}
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 2 with MultiProjection

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();
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiProjection(org.openrdf.query.algebra.MultiProjection) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 3 with MultiProjection

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();
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) AtomicReference(java.util.concurrent.atomic.AtomicReference) MultiProjection(org.openrdf.query.algebra.MultiProjection) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 4 with MultiProjection

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BNode(org.openrdf.model.BNode) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MultiProjection(org.openrdf.query.algebra.MultiProjection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with MultiProjection

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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MultiProjection(org.openrdf.query.algebra.MultiProjection) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MultiProjection (org.openrdf.query.algebra.MultiProjection)8 Test (org.junit.Test)5 HashSet (java.util.HashSet)3 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)3 ValueFactory (org.openrdf.model.ValueFactory)3 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)3 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)3 MapBindingSet (org.openrdf.query.impl.MapBindingSet)3 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BNode (org.openrdf.model.BNode)2 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)2 StatementPattern (org.openrdf.query.algebra.StatementPattern)2 ParsedQuery (org.openrdf.query.parser.ParsedQuery)2 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)1