use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class MongoStatementMetadataNodeIT method simpleQueryWithoutBindingSet.
@Test
public void simpleQueryWithoutBindingSet() throws Exception {
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
dao.add(statement);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<?> node = new StatementMetadataNode<>(spList, conf);
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());
QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("x", new LiteralImpl("CoffeeShop"));
bs.addBinding("y", new LiteralImpl("Joe"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(bs, bsList.get(0));
dao.delete(statement, conf);
} finally {
dao.destroy();
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class PeriodicNotificationProcessorIT method periodicProcessorTest.
@Test
public void periodicProcessorTest() throws Exception {
String id = UUID.randomUUID().toString().replace("-", "");
BlockingQueue<TimestampedNotification> notifications = new LinkedBlockingQueue<>();
BlockingQueue<NodeBin> bins = new LinkedBlockingQueue<>();
BlockingQueue<BindingSetRecord> bindingSets = new LinkedBlockingQueue<>();
TimestampedNotification ts1 = new TimestampedNotification(PeriodicNotification.builder().id(id).initialDelay(0).period(2000).timeUnit(TimeUnit.SECONDS).build());
long binId1 = (ts1.getTimestamp().getTime() / ts1.getPeriod()) * ts1.getPeriod();
Thread.sleep(2000);
TimestampedNotification ts2 = new TimestampedNotification(PeriodicNotification.builder().id(id).initialDelay(0).period(2000).timeUnit(TimeUnit.SECONDS).build());
long binId2 = (ts2.getTimestamp().getTime() / ts2.getPeriod()) * ts2.getPeriod();
Set<NodeBin> expectedBins = new HashSet<>();
expectedBins.add(new NodeBin(id, binId1));
expectedBins.add(new NodeBin(id, binId2));
Set<BindingSet> expected = new HashSet<>();
Set<VisibilityBindingSet> storageResults = new HashSet<>();
QueryBindingSet bs1 = new QueryBindingSet();
bs1.addBinding("periodicBinId", vf.createLiteral(binId1));
bs1.addBinding("id", vf.createLiteral(1));
expected.add(bs1);
storageResults.add(new VisibilityBindingSet(bs1));
QueryBindingSet bs2 = new QueryBindingSet();
bs2.addBinding("periodicBinId", vf.createLiteral(binId1));
bs2.addBinding("id", vf.createLiteral(2));
expected.add(bs2);
storageResults.add(new VisibilityBindingSet(bs2));
QueryBindingSet bs3 = new QueryBindingSet();
bs3.addBinding("periodicBinId", vf.createLiteral(binId2));
bs3.addBinding("id", vf.createLiteral(3));
expected.add(bs3);
storageResults.add(new VisibilityBindingSet(bs3));
QueryBindingSet bs4 = new QueryBindingSet();
bs4.addBinding("periodicBinId", vf.createLiteral(binId2));
bs4.addBinding("id", vf.createLiteral(4));
expected.add(bs4);
storageResults.add(new VisibilityBindingSet(bs4));
PeriodicQueryResultStorage periodicStorage = new AccumuloPeriodicQueryResultStorage(super.getAccumuloConnector(), RYA_INSTANCE_NAME);
periodicStorage.createPeriodicQuery(id, "select ?id where {?obs <urn:hasId> ?id.}", new VariableOrder("periodicBinId", "id"));
periodicStorage.addPeriodicQueryResults(id, storageResults);
NotificationProcessorExecutor processor = new NotificationProcessorExecutor(periodicStorage, notifications, bins, bindingSets, 1);
processor.start();
notifications.add(ts1);
notifications.add(ts2);
Thread.sleep(5000);
Assert.assertEquals(expectedBins.size(), bins.size());
Assert.assertEquals(true, bins.containsAll(expectedBins));
Set<BindingSet> actual = new HashSet<>();
bindingSets.forEach(x -> actual.add(x.getBindingSet()));
Assert.assertEquals(expected, actual);
processor.stop();
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet 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);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternMatcher method match.
/**
* Matches a {@link Statement} against the provided {@link StatementPattern} and returns a {@link BindingSet}
* if the statement matched the pattern.
*
* @param statement - The statement that will be matched against the pattern. (not null)
* @return A {@link BinidngSet} containing the statement's values filled in for the pattern's variables if
* the statement's values match the pattern's constants; otherwise empty.
*/
public Optional<BindingSet> match(final Statement statement) {
requireNonNull(statement);
// Setup the resulting binding set that could be built from this Statement.
final QueryBindingSet bs = new QueryBindingSet();
if (matchesValue(pattern.getSubjectVar(), statement.getSubject(), bs) && matchesValue(pattern.getPredicateVar(), statement.getPredicate(), bs) && matchesValue(pattern.getObjectVar(), statement.getObject(), bs) && matchesContext(pattern.getContextVar(), statement.getContext(), bs)) {
return Optional.of(bs);
} else {
return Optional.empty();
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternMatcherTest method matchesContext.
@Test
public void matchesContext() throws Exception {
// Create a matcher against a pattern that matches a specific context.
final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "GRAPH <urn:testGraph> {" + "?s ?p ?o ." + "}" + "}"));
// Create a statement that matches the pattern.
final ValueFactory vf = new ValueFactoryImpl();
final Statement statement = vf.createStatement(vf.createURI("urn:Alice"), vf.createURI("urn:talksTo"), vf.createURI("urn:Bob"), vf.createURI("urn:testGraph"));
// Create the expected resulting Binding Set.
final QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("s", vf.createURI("urn:Alice"));
expected.addBinding("p", vf.createURI("urn:talksTo"));
expected.addBinding("o", vf.createURI("urn:Bob"));
// Show the expected Binding Set matches the resulting Binding Set.
final Optional<BindingSet> bs = matcher.match(statement);
assertEquals(expected, bs.get());
}
Aggregations