use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class MongoStatementMetadataIT method simpleQueryWithBindingSetJoinPropertyToSubject.
/**
* Tests to see if correct result is passed back when a metadata statement
* is joined with a StatementPattern statement (i.e. a common variable
* appears in a StatementPattern statement and a metadata statement).
* StatementPattern statements have either rdf:subject, rdf:predicate, or
* rdf:object as the predicate while a metadata statement is any statement
* in the reified query whose predicate is not rdf:type and not a
* StatementPattern predicate.
*
* @throws MalformedQueryException
* @throws QueryEvaluationException
* @throws RyaDAOException
*/
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception {
Sail sail = RyaSailFactory.getInstance(conf);
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
final StatementMetadata metadata1 = new StatementMetadata();
metadata1.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Doug"));
metadata1.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
final StatementMetadata metadata2 = new StatementMetadata();
metadata2.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Bob"));
metadata2.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-04"));
final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaURI("http://BurgerShack"), new RyaURI("http://context"), "", metadata1);
final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Betty"), new RyaURI("http://context"), "", metadata1);
final RyaStatement statement3 = new RyaStatement(new RyaURI("http://Fred"), new RyaURI("http://talksTo"), new RyaURI("http://Amanda"), new RyaURI("http://context"), "", metadata1);
final RyaStatement statement4 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Wanda"), new RyaURI("http://context"), "", metadata2);
dao.add(statement1);
dao.add(statement2);
dao.add(statement3);
dao.add(statement4);
SailRepositoryConnection conn = new SailRepository(sail).getConnection();
final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query2).evaluate();
final Set<BindingSet> expected = new HashSet<>();
final QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("b", new URIImpl("http://Betty"));
expected1.addBinding("a", new URIImpl("http://Joe"));
expected1.addBinding("c", new URIImpl("http://Doug"));
expected.add(expected1);
final Set<BindingSet> bsSet = new HashSet<>();
while (result.hasNext()) {
bsSet.add(result.next());
}
assertEquals(expected, bsSet);
dao.delete(statement1, conf);
dao.delete(statement2, conf);
dao.delete(statement3, conf);
dao.delete(statement4, conf);
} finally {
dao.destroy();
sail.shutDown();
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class MongoStatementMetadataIT method simpleQueryWithBindingSet.
@Test
public void simpleQueryWithBindingSet() throws Exception {
Sail sail = RyaSailFactory.getInstance(conf);
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
final 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"));
final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SailRepositoryConnection conn = new SailRepository(sail).getConnection();
final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
final Set<BindingSet> expected = new HashSet<>();
final QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
expected1.addBinding("y", new LiteralImpl("Joe"));
final QueryBindingSet expected2 = new QueryBindingSet();
expected2.addBinding("x", new LiteralImpl("HardwareStore"));
expected2.addBinding("y", new LiteralImpl("Joe"));
expected.add(expected1);
expected.add(expected2);
final Set<BindingSet> bsSet = new HashSet<>();
while (result.hasNext()) {
bsSet.add(result.next());
}
assertEquals(expected, bsSet);
dao.delete(statement1, conf);
dao.delete(statement2, conf);
} finally {
dao.destroy();
sail.shutDown();
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class PeriodicNotificationApplicationIT method periodicApplicationWithAggAndGroupByTest.
@Test
public void periodicApplicationWithAggAndGroupByTest() throws Exception {
final String sparql = // n
"prefix function: <http://org.apache.rya/function#> " + // n
"prefix time: <http://www.w3.org/2006/time#> " + // n
"select ?type (count(?obs) as ?total) where {" + // n
"Filter(function:periodic(?time, 1, .25, time:minutes)) " + // n
"?obs <uri:hasTime> ?time. " + // n
"?obs <uri:hasObsType> ?type } group by ?type";
// make data
final int periodMult = 15;
final ValueFactory vf = new ValueFactoryImpl();
final DatatypeFactory dtf = DatatypeFactory.newInstance();
// results more predictable
while (System.currentTimeMillis() % (periodMult * 1000) > 500) {
;
}
final ZonedDateTime time = ZonedDateTime.now();
final ZonedDateTime zTime1 = time.minusSeconds(2 * periodMult);
final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
final ZonedDateTime zTime2 = zTime1.minusSeconds(periodMult);
final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
final ZonedDateTime zTime3 = zTime2.minusSeconds(periodMult);
final String time3 = zTime3.format(DateTimeFormatter.ISO_INSTANT);
final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasObsType"), vf.createLiteral("ship")), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasObsType"), vf.createLiteral("airplane")), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasObsType"), vf.createLiteral("ship")), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasObsType"), vf.createLiteral("airplane")), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasObsType"), vf.createLiteral("automobile")));
try (FluoClient fluo = FluoClientFactory.getFluoClient(conf.getFluoAppName(), Optional.of(conf.getFluoTableName()), conf)) {
final Connector connector = ConfigUtils.getConnector(conf);
final PeriodicQueryResultStorage storage = new AccumuloPeriodicQueryResultStorage(connector, conf.getTablePrefix());
final CreatePeriodicQuery periodicQuery = new CreatePeriodicQuery(fluo, storage);
final String id = FluoQueryUtils.convertFluoQueryIdToPcjId(periodicQuery.createPeriodicQuery(sparql, registrar).getQueryId());
addData(statements);
app.start();
final Multimap<Long, BindingSet> actual = HashMultimap.create();
try (KafkaConsumer<String, BindingSet> consumer = new KafkaConsumer<>(kafkaProps, new StringDeserializer(), new BindingSetSerDe())) {
consumer.subscribe(Arrays.asList(id));
final long end = System.currentTimeMillis() + 4 * periodMult * 1000;
long lastBinId = 0L;
long binId = 0L;
final List<Long> ids = new ArrayList<>();
while (System.currentTimeMillis() < end) {
final ConsumerRecords<String, BindingSet> records = consumer.poll(periodMult * 1000);
for (final ConsumerRecord<String, BindingSet> record : records) {
final BindingSet result = record.value();
binId = Long.parseLong(result.getBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID).getValue().stringValue());
if (lastBinId != binId) {
lastBinId = binId;
ids.add(binId);
}
actual.put(binId, result);
}
}
final Map<Long, Set<BindingSet>> expected = new HashMap<>();
final Set<BindingSet> expected1 = new HashSet<>();
final QueryBindingSet bs1 = new QueryBindingSet();
bs1.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
bs1.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
bs1.addBinding("type", vf.createLiteral("airplane"));
final QueryBindingSet bs2 = new QueryBindingSet();
bs2.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
bs2.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
bs2.addBinding("type", vf.createLiteral("ship"));
final QueryBindingSet bs3 = new QueryBindingSet();
bs3.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
bs3.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
bs3.addBinding("type", vf.createLiteral("automobile"));
expected1.add(bs1);
expected1.add(bs2);
expected1.add(bs3);
final Set<BindingSet> expected2 = new HashSet<>();
final QueryBindingSet bs4 = new QueryBindingSet();
bs4.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(1)));
bs4.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
bs4.addBinding("type", vf.createLiteral("airplane"));
final QueryBindingSet bs5 = new QueryBindingSet();
bs5.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(1)));
bs5.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
bs5.addBinding("type", vf.createLiteral("ship"));
expected2.add(bs4);
expected2.add(bs5);
final Set<BindingSet> expected3 = new HashSet<>();
final QueryBindingSet bs6 = new QueryBindingSet();
bs6.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(2)));
bs6.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
bs6.addBinding("type", vf.createLiteral("ship"));
final QueryBindingSet bs7 = new QueryBindingSet();
bs7.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(2)));
bs7.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
bs7.addBinding("type", vf.createLiteral("airplane"));
expected3.add(bs6);
expected3.add(bs7);
expected.put(ids.get(0), expected1);
expected.put(ids.get(1), expected2);
expected.put(ids.get(2), expected3);
Assert.assertEquals(3, actual.asMap().size());
for (final Long ident : ids) {
Assert.assertEquals(expected.get(ident), actual.get(ident));
}
}
final Set<BindingSet> expectedResults = new HashSet<>();
try (CloseableIterator<BindingSet> results = storage.listResults(id, Optional.empty())) {
results.forEachRemaining(x -> expectedResults.add(x));
Assert.assertEquals(0, expectedResults.size());
}
}
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class PipelineResultIterationTest method testIteration.
@Test
public void testIteration() throws QueryEvaluationException {
HashMap<String, String> nameMap = new HashMap<>();
nameMap.put("bName", "b");
nameMap.put("eName", "e");
PipelineResultIteration iter = new PipelineResultIteration(documentIterator(new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Beth")), new Document("<VALUES>", new Document("a", "urn:Alice").append("bName", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("c", "urn:Carol")), new Document("<VALUES>", new Document("cName", "urn:Carol").append("d", "urn:Dan"))), nameMap, new QueryBindingSet());
Assert.assertTrue(iter.hasNext());
BindingSet bs = iter.next();
Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
Assert.assertTrue(iter.hasNext());
bs = iter.next();
Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Beth", bs.getBinding("b").getValue().stringValue());
Assert.assertTrue(iter.hasNext());
bs = iter.next();
Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
Assert.assertTrue(iter.hasNext());
bs = iter.next();
Assert.assertEquals(Sets.newHashSet("a", "c"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Carol", bs.getBinding("c").getValue().stringValue());
bs = iter.next();
Assert.assertEquals(Sets.newHashSet("cName", "d"), bs.getBindingNames());
Assert.assertEquals("urn:Carol", bs.getBinding("cName").getValue().stringValue());
Assert.assertEquals("urn:Dan", bs.getBinding("d").getValue().stringValue());
Assert.assertFalse(iter.hasNext());
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BindingSetHashJoinIterator method joinBindingSets.
/**
* This method verifies that all common variables have a common value and
* then joins the BindingSets together. In the case that the PCJ contains a
* LeftJoin, if the leftBs and rightBs have a common variable with distinct
* values and that common variable is unassured (only appears in LeftJoin),
* this method uses the value corresponding to leftBs.
*
* @param leftBs
* - BindingSet passed into PCJ
* @param rightBs
* - PCJ BindingSet
* @return - joined BindingSet
*/
private BindingSet joinBindingSets(BindingSet leftBs, BindingSet rightBs) {
Set<String> commonVars = Sets.intersection(leftBs.getBindingNames(), rightBs.getBindingNames());
// add value corresponding to leftBs
for (String s : commonVars) {
if (!leftBs.getValue(s).equals(rightBs.getValue(s)) && !unAssuredVariables.contains(s)) {
return EMPTY_BINDINGSET;
}
}
QueryBindingSet bs = new QueryBindingSet(removeConstants(leftBs));
rightBs = removeConstants(rightBs);
// to leftBs, which is effectively performing a LeftJoin.
for (String s : rightBs.getBindingNames()) {
if (bs.getValue(s) == null) {
bs.addBinding(s, rightBs.getValue(s));
}
}
return bs;
}
Aggregations