Search in sources :

Example 1 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.

the class VisibilityStatementSerdeTest method serializeAndDeserialize.

@Test
public void serializeAndDeserialize() {
    // Create the object that will be serialized.
    final ValueFactory vf = new ValueFactoryImpl();
    final Statement statement = vf.createStatement(vf.createURI("urn:person1"), vf.createURI("urn:hasName"), vf.createLiteral("alice"), vf.createURI("urn:testContext"));
    final VisibilityStatement original = new VisibilityStatement(statement, "a|b|c");
    // Serialize it.
    try (final Serde<VisibilityStatement> serde = new VisibilityStatementSerde()) {
        final byte[] bytes = serde.serializer().serialize("topic", original);
        // Deserialize it.
        final VisibilityStatement deserialized = serde.deserializer().deserialize("topic", bytes);
        // Show the deserialized value matches the original.
        assertEquals(original, deserialized);
    }
}
Also used : Statement(org.openrdf.model.Statement) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) Test(org.junit.Test)

Example 2 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.

the class KafkaGetQueryResultStreamIT method fromStart_visibilityStatements.

@Test
public void fromStart_visibilityStatements() throws Exception {
    // Create an ID for the query.
    final String ryaInstance = UUID.randomUUID().toString();
    final UUID queryId = UUID.randomUUID();
    // Create some statements that will be written to the result topic.
    final List<VisibilityStatement> original = new ArrayList<>();
    final ValueFactory vf = new ValueFactoryImpl();
    original.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob")), "a"));
    original.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:age"), vf.createLiteral(63)), "b"));
    original.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:age"), vf.createLiteral("urn:34")), ""));
    // Write the entries to the query result topic in Kafka.
    try (final Producer<?, VisibilityStatement> producer = KafkaTestUtil.makeProducer(kafka, StringSerializer.class, VisibilityStatementSerializer.class)) {
        final String resultTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
        for (final VisibilityStatement visStmt : original) {
            producer.send(new ProducerRecord<>(resultTopic, visStmt));
        }
    }
    // Use the interactor that is being tested to read all of the visibility binding sets.
    final GetQueryResultStream<VisibilityStatement> interactor = new KafkaGetQueryResultStream<>(kafka.getKafkaHostname(), kafka.getKafkaPort(), VisibilityStatementDeserializer.class);
    final List<VisibilityStatement> read = pollForResults(500, 3, 3, interactor.fromStart(ryaInstance, queryId));
    // Show the fetched binding sets match the original, as well as their order.
    assertEquals(original, read);
}
Also used : ArrayList(java.util.ArrayList) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) UUID(java.util.UUID) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) Test(org.junit.Test)

Example 3 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.

the class KafkaRunQueryIT method runQuery.

@Test
public void runQuery() throws Exception {
    // Setup some constant that will be used through the test.
    final String ryaInstance = UUID.randomUUID().toString();
    final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
    // This query is completely in memory, so it doesn't need to be closed.
    final QueryRepository queries = new InMemoryQueryRepository(new InMemoryQueryChangeLog(), Scheduler.newFixedRateSchedule(0L, 5, TimeUnit.SECONDS));
    // Add the query to the query repository.
    final StreamsQuery sQuery = queries.add("SELECT * WHERE { ?person <urn:worksAt> ?business . }", true, false);
    final UUID queryId = sQuery.getQueryId();
    final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
    // The thread that will run the tested interactor.
    final Thread testThread = new Thread() {

        @Override
        public void run() {
            final RunQuery runQuery = new KafkaRunQuery(kafka.getKafkaHostname(), kafka.getKafkaPort(), statementsTopic, resultsTopic, queries, new TopologyFactory());
            try {
                runQuery.run(queryId);
            } catch (final RyaStreamsException e) {
            // Do nothing. Test will still fail because the expected results will be missing.
            }
        }
    };
    // Create the topics.
    kafka.createTopic(statementsTopic);
    kafka.createTopic(resultsTopic);
    // Create the statements that will be loaded.
    final ValueFactory vf = new ValueFactoryImpl();
    final List<VisibilityStatement> statements = new ArrayList<>();
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Charlie"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoShop")), "a"));
    // Create the expected results.
    final List<VisibilityBindingSet> expected = new ArrayList<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("business", vf.createURI("urn:BurgerJoint"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Bob"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new MapBindingSet();
    bs.addBinding("person", vf.createURI("urn:Charlie"));
    bs.addBinding("business", vf.createURI("urn:TacoShop"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    // Execute the test. This will result in a set of results that were read from the results topic.
    final List<VisibilityBindingSet> results;
    try {
        // Wait for the program to start.
        testThread.start();
        Thread.sleep(2000);
        // Write some statements to the program.
        final LoadStatements loadStatements = new KafkaLoadStatements(statementsTopic, producer);
        loadStatements.fromCollection(statements);
        // Read the output of the streams program.
        consumer.subscribe(Lists.newArrayList(resultsTopic));
        results = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
    } finally {
        // Tear down the test.
        testThread.interrupt();
        testThread.join(3000);
    }
    // Show the read results matched the expected ones.
    assertEquals(expected, results);
}
Also used : InMemoryQueryChangeLog(org.apache.rya.streams.api.queries.InMemoryQueryChangeLog) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) RunQuery(org.apache.rya.streams.api.interactor.RunQuery) LoadStatements(org.apache.rya.streams.api.interactor.LoadStatements) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) ValueFactory(org.openrdf.model.ValueFactory) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) InMemoryQueryRepository(org.apache.rya.streams.api.queries.InMemoryQueryRepository) QueryRepository(org.apache.rya.streams.api.queries.QueryRepository) MapBindingSet(org.openrdf.query.impl.MapBindingSet) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.

the class StatementOutputFormatterTest method binaryResult.

@Test
public void binaryResult() {
    // 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.createURI("urn: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 BinaryResult(Side.LEFT, visBs)));
    // Verify the mock was invoked with the expected output.
    final VisibilityStatement expectedStmt = new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:age"), vf.createLiteral(34)), "a");
    verify(context, times(1)).forward(eq("key"), eq(expectedStmt));
}
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) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) BinaryResult(org.apache.rya.streams.kafka.processors.ProcessorResult.BinaryResult) Test(org.junit.Test)

Example 5 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement in project incubator-rya by apache.

the class StatementOutputFormatterTest method unaryResult.

@Test
public void unaryResult() {
    // 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.createURI("urn: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 invoked with the expected output.
    final VisibilityStatement expectedStmt = new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:age"), vf.createLiteral(34)), "a");
    verify(context, times(1)).forward(eq("key"), eq(expectedStmt));
}
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) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) Test(org.junit.Test)

Aggregations

VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)43 Test (org.junit.Test)36 ValueFactory (org.openrdf.model.ValueFactory)35 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)32 ArrayList (java.util.ArrayList)29 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)28 UUID (java.util.UUID)26 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)25 HashSet (java.util.HashSet)24 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)24 MapBindingSet (org.openrdf.query.impl.MapBindingSet)24 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)23 LoadStatements (org.apache.rya.streams.api.interactor.LoadStatements)4 KafkaLoadStatements (org.apache.rya.streams.kafka.interactor.KafkaLoadStatements)4 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)4 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)3 Statement (org.openrdf.model.Statement)3 Properties (java.util.Properties)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2