Search in sources :

Example 26 with ValueFactory

use of org.openrdf.model.ValueFactory in project incubator-rya by apache.

the class BindingSetOutputFormatterTest method unaryResult.

@Test
public void unaryResult() {
    // Create the input binding set.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bindingSet = new MapBindingSet();
    bindingSet.addBinding("person", vf.createURI("urn:Alice"));
    bindingSet.addBinding("age", 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 BindingSetOutputFormatter formatter = new BindingSetOutputFormatter();
    formatter.init(context);
    formatter.process("key", ProcessorResult.make(new UnaryResult(visBs)));
    // Verify the mock was invoked with the expected output.
    verify(context, times(1)).forward(eq("key"), eq(visBs));
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) BindingSetOutputFormatter(org.apache.rya.streams.kafka.processors.output.BindingSetOutputFormatterSupplier.BindingSetOutputFormatter) 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 27 with ValueFactory

use of org.openrdf.model.ValueFactory 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 28 with ValueFactory

use of org.openrdf.model.ValueFactory in project incubator-rya by apache.

the class VisibilityBindingSetKafkaIT method readAndWrite.

@Test
public void readAndWrite() throws Exception {
    // Create the object that will be written to the topic.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bs = new MapBindingSet();
    bs.addBinding("urn:name", vf.createURI("urn:alice"));
    bs.addBinding("urn:age", vf.createLiteral(32));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b|c");
    // Write a VisibilityBindingSet to the test topic.
    try (Producer<String, VisibilityBindingSet> producer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityBindingSetSerializer.class)) {
        producer.send(new ProducerRecord<String, VisibilityBindingSet>(kafka.getKafkaTopicName(), original));
    }
    // Read a VisibilityBindingSet from the test topic.
    try (Consumer<String, VisibilityBindingSet> consumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityBindingSetDeserializer.class)) {
        // Register the topic.
        consumer.subscribe(Arrays.asList(kafka.getKafkaTopicName()));
        // Poll for the result.
        final List<VisibilityBindingSet> results = KafkaTestUtil.pollForResults(500, 6, 1, consumer);
        // Show the written statement matches the read one.
        final VisibilityBindingSet read = results.iterator().next();
        assertEquals(original, read);
    }
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 29 with ValueFactory

use of org.openrdf.model.ValueFactory in project incubator-rya by apache.

the class VisibilityStatementKafkaIT method readAndWrite.

@Test
public void readAndWrite() throws Exception {
    // Create the object that will be written to the topic.
    final ValueFactory vf = new ValueFactoryImpl();
    final VisibilityStatement original = new VisibilityStatement(vf.createStatement(vf.createURI("urn:alice"), vf.createURI("urn:age"), vf.createLiteral(32), vf.createURI("urn:context")), "a|b|c");
    // Write a VisibilityStatement to the test topic.
    try (Producer<String, VisibilityStatement> producer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityStatementSerializer.class)) {
        producer.send(new ProducerRecord<String, VisibilityStatement>(kafka.getKafkaTopicName(), original));
    }
    // Read a VisibilityStatement from the test topic.
    try (Consumer<String, VisibilityStatement> consumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityStatementDeserializer.class)) {
        // Register the topic.
        consumer.subscribe(Arrays.asList(kafka.getKafkaTopicName()));
        // Poll for the result.
        final List<VisibilityStatement> results = KafkaTestUtil.pollForResults(500, 6, 1, consumer);
        // Show the written statement matches the read one.
        final VisibilityStatement read = results.iterator().next();
        assertEquals(original, read);
    }
}
Also used : ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) Test(org.junit.Test)

Example 30 with ValueFactory

use of org.openrdf.model.ValueFactory in project incubator-rya by apache.

the class LoadStatementsCommandIT method shortParams.

@Test
public void shortParams() throws Exception {
    // Load a file of statements into Kafka.
    final String visibilities = "a|b|c";
    final String[] args = new String[] { "-r", "" + ryaInstance, "-i", kafka.getKafkaHostname(), "-p", kafka.getKafkaPort(), "-f", TURTLE_FILE.toString(), "-v", visibilities };
    // Load the file of statements into the Statements topic.
    new LoadStatementsCommand().execute(args);
    // Show that the statements were loaded into the topic.
    final List<VisibilityStatement> read = new ArrayList<>();
    try (final Consumer<String, VisibilityStatement> consumer = KafkaTestUtil.fromStartConsumer(kafka, StringDeserializer.class, VisibilityStatementDeserializer.class)) {
        // Subscribe for messages.
        consumer.subscribe(Arrays.asList(KafkaTopics.statementsTopic(ryaInstance)));
        // Read the messages and extract their values.
        final Iterator<ConsumerRecord<String, VisibilityStatement>> iter = consumer.poll(3000).iterator();
        while (iter.hasNext()) {
            read.add(iter.next().value());
        }
    }
    final ValueFactory VF = ValueFactoryImpl.getInstance();
    final List<VisibilityStatement> expected = new ArrayList<>();
    expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#alice"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#bob")), visibilities));
    expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#bob"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#charlie")), visibilities));
    expected.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#charlie"), VF.createURI("http://example#likes"), VF.createURI("http://example#icecream")), visibilities));
    // Show the written statements matches the read ones.
    assertEquals(expected, read);
}
Also used : ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Test(org.junit.Test)

Aggregations

ValueFactory (org.openrdf.model.ValueFactory)230 Test (org.junit.Test)195 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)187 Statement (org.openrdf.model.Statement)114 MapBindingSet (org.openrdf.query.impl.MapBindingSet)99 URI (org.openrdf.model.URI)83 HashSet (java.util.HashSet)72 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)66 Value (org.openrdf.model.Value)57 BindingSet (org.openrdf.query.BindingSet)51 Resource (org.openrdf.model.Resource)39 ArrayList (java.util.ArrayList)35 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)35 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)34 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)33 UUID (java.util.UUID)29 StatementImpl (org.openrdf.model.impl.StatementImpl)27 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)25 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24