Search in sources :

Example 11 with VisibilityStatement

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

the class StatementPatternProcessorIT method singlePattern_manyStatements.

@Test
public void singlePattern_manyStatements() throws Exception {
    // Enumerate some topics that will be re-used
    final String ryaInstance = UUID.randomUUID().toString();
    final UUID queryId = UUID.randomUUID();
    final String statementsTopic = KafkaTopics.statementsTopic(ryaInstance);
    final String resultsTopic = KafkaTopics.queryResultsTopic(ryaInstance, queryId);
    // Setup a topology.
    final String query = "SELECT * WHERE { ?person <urn:talksTo> ?otherPerson }";
    final TopologyFactory factory = new TopologyFactory();
    final TopologyBuilder builder = factory.build(query, statementsTopic, resultsTopic, new RandomUUIDFactory());
    // Create some statements where some generates SP results and others do not.
    final ValueFactory vf = new ValueFactoryImpl();
    final List<VisibilityStatement> statements = new ArrayList<>();
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob")), "a"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:worksAt"), vf.createURI("urn:TacoJoin")), "b"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:talksTo"), vf.createURI("urn:Alice")), "a|b"));
    statements.add(new VisibilityStatement(vf.createStatement(vf.createURI("urn:Bob"), vf.createURI("urn:worksAt"), vf.createURI("urn:BurgerJoint")), "c"));
    // Show the correct binding set results from the job.
    final Set<VisibilityBindingSet> expected = new HashSet<>();
    QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding("person", vf.createURI("urn:Alice"));
    bs.addBinding("otherPerson", vf.createURI("urn:Bob"));
    expected.add(new VisibilityBindingSet(bs, "a"));
    bs = new QueryBindingSet();
    bs.addBinding("person", vf.createURI("urn:Bob"));
    bs.addBinding("otherPerson", vf.createURI("urn:Alice"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    // Run the test.
    RyaStreamsTestUtil.runStreamProcessingTest(kafka, statementsTopic, resultsTopic, builder, statements, expected, VisibilityBindingSetDeserializer.class);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) TopologyFactory(org.apache.rya.streams.kafka.topology.TopologyFactory) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RandomUUIDFactory(org.apache.rya.api.function.projection.RandomUUIDFactory) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with VisibilityStatement

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

the class KafkaLoadStatementsIT method testTurtle.

@Test
public void testTurtle() throws Exception {
    final String visibilities = "a|b|c";
    // Load the statements into the kafka topic.
    try (final Producer<?, VisibilityStatement> producer = KafkaTestUtil.makeProducer(rule, StringSerializer.class, VisibilityStatementSerializer.class)) {
        final KafkaLoadStatements command = new KafkaLoadStatements(rule.getKafkaTopicName(), producer);
        command.fromFile(TURTLE_FILE, visibilities);
    }
    // Read a VisibilityBindingSets from the test topic.
    // = new ArrayList<>();
    final List<VisibilityStatement> read;
    try (Consumer<String, VisibilityStatement> consumer = KafkaTestUtil.fromStartConsumer(rule, StringDeserializer.class, VisibilityStatementDeserializer.class)) {
        consumer.subscribe(Arrays.asList(rule.getKafkaTopicName()));
        read = KafkaTestUtil.pollForResults(500, 6, 3, consumer);
    }
    final List<VisibilityStatement> original = new ArrayList<>();
    final ValueFactory VF = ValueFactoryImpl.getInstance();
    original.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#alice"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#bob")), visibilities));
    original.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#bob"), VF.createURI("http://example#talksTo"), VF.createURI("http://example#charlie")), visibilities));
    original.add(new VisibilityStatement(VF.createStatement(VF.createURI("http://example#charlie"), VF.createURI("http://example#likes"), VF.createURI("http://example#icecream")), visibilities));
    // Show the written statement matches the read one.
    assertEquals(original, read);
}
Also used : ArrayList(java.util.ArrayList) ValueFactory(org.openrdf.model.ValueFactory) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) Test(org.junit.Test)

Example 13 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement 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 14 with VisibilityStatement

use of org.apache.rya.api.model.VisibilityStatement 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)

Example 15 with VisibilityStatement

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

the class QueryResultsOutputUtilTest method toNtriplesFile.

@Test
public void toNtriplesFile() throws Exception {
    // Mock a result stream that signals shutdown when it returns a set of results.
    final AtomicBoolean shutdownSignal = new AtomicBoolean(false);
    final QueryResultStream<VisibilityStatement> resultsStream = mock(QueryResultStream.class);
    when(resultsStream.poll(anyLong())).thenAnswer(invocation -> {
        shutdownSignal.set(true);
        final List<VisibilityStatement> results = new ArrayList<>();
        Statement stmt = VF.createStatement(VF.createURI("urn:alice"), VF.createURI("urn:age"), VF.createLiteral(23));
        results.add(new VisibilityStatement(stmt));
        stmt = VF.createStatement(VF.createURI("urn:bob"), VF.createURI("urn:worksAt"), VF.createLiteral("Taco Shop"));
        results.add(new VisibilityStatement(stmt));
        return results;
    });
    // The stream the JSON will be written to.
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    // Invoke the test method. This will write the NTriples.
    QueryResultsOutputUtil.toNtriplesFile(out, resultsStream, shutdownSignal);
    // Show the produced NTriples matches the expected NTriples.
    final String expected = "<urn:alice> <urn:age> \"23\"^^<http://www.w3.org/2001/XMLSchema#int> .\n" + "<urn:bob> <urn:worksAt> \"Taco Shop\" .\n";
    final String nTriples = new String(out.toByteArray(), Charsets.UTF_8);
    assertEquals(expected, nTriples);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Statement(org.openrdf.model.Statement) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) 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