use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class PeriodicNotificationExporterIT method testExporter.
@Test
public void testExporter() throws InterruptedException {
final String topic1 = kafkaTestInstanceRule.getKafkaTopicName() + "1";
final String topic2 = kafkaTestInstanceRule.getKafkaTopicName() + "2";
kafkaTestInstanceRule.createTopic(topic1);
kafkaTestInstanceRule.createTopic(topic2);
final BlockingQueue<BindingSetRecord> records = new LinkedBlockingQueue<>();
final KafkaExporterExecutor exporter = new KafkaExporterExecutor(new KafkaProducer<String, BindingSet>(createKafkaProducerConfig()), 1, records);
exporter.start();
final QueryBindingSet bs1 = new QueryBindingSet();
bs1.addBinding(PeriodicQueryResultStorage.PeriodicBinId, vf.createLiteral(1L));
bs1.addBinding("name", vf.createURI("uri:Bob"));
final BindingSetRecord record1 = new BindingSetRecord(bs1, topic1);
final QueryBindingSet bs2 = new QueryBindingSet();
bs2.addBinding(PeriodicQueryResultStorage.PeriodicBinId, vf.createLiteral(2L));
bs2.addBinding("name", vf.createURI("uri:Joe"));
final BindingSetRecord record2 = new BindingSetRecord(bs2, topic2);
records.add(record1);
records.add(record2);
final Set<BindingSet> expected1 = new HashSet<>();
expected1.add(bs1);
final Set<BindingSet> expected2 = new HashSet<>();
expected2.add(bs2);
final Set<BindingSet> actual1 = getBindingSetsFromKafka(topic1);
final Set<BindingSet> actual2 = getBindingSetsFromKafka(topic2);
Assert.assertEquals(expected1, actual1);
Assert.assertEquals(expected2, actual2);
exporter.stop();
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithBindingSetConstantContext.
@Test
public void simpleQueryWithBindingSetConstantContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph <uri:context1> {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("x", new URIImpl("uri:Doug"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", new URIImpl("uri:Doug"));
Assert.assertEquals(expected, bsList.get(0));
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternEvalTest method simpleQueryWithBindingSets.
@Test
public void simpleQueryWithBindingSets() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
// query is used to build statement that will be evaluated
String query = "select ?x ?c where{ graph ?c {?x <uri:talksTo> <uri:Bob>. }}";
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
dao.add(statement1);
RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
dao.add(statement2);
RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Eric"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context3"), "", new StatementMetadata());
dao.add(statement3);
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("c", new URIImpl("uri:context2"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("c", new URIImpl("uri:context1"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(2, bsList.size());
QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", new URIImpl("uri:Joe"));
expected1.addBinding("c", new URIImpl("uri:context1"));
QueryBindingSet expected2 = new QueryBindingSet();
expected2.addBinding("x", new URIImpl("uri:Doug"));
expected2.addBinding("c", new URIImpl("uri:context2"));
Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2));
Set<BindingSet> actual = new HashSet<>(bsList);
Assert.assertEquals(expected, actual);
dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class ParallelEvaluationStrategyImpl method evaluate.
public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final StatementPattern sp, Collection<BindingSet> bindings) throws QueryEvaluationException {
final Var subjVar = sp.getSubjectVar();
final Var predVar = sp.getPredicateVar();
final Var objVar = sp.getObjectVar();
final Var cntxtVar = sp.getContextVar();
List<Map.Entry<Statement, BindingSet>> stmts = new ArrayList<Map.Entry<Statement, BindingSet>>();
Iteration<? extends Map.Entry<Statement, BindingSet>, QueryEvaluationException> iter;
if (sp instanceof FixedStatementPattern) {
Collection<Map.Entry<Statement, BindingSet>> coll = Lists.newArrayList();
for (BindingSet binding : bindings) {
Value subjValue = getVarValue(subjVar, binding);
Value predValue = getVarValue(predVar, binding);
Value objValue = getVarValue(objVar, binding);
Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
for (Statement st : ((FixedStatementPattern) sp).statements) {
if (!((subjValue != null && !subjValue.equals(st.getSubject())) || (predValue != null && !predValue.equals(st.getPredicate())) || (objValue != null && !objValue.equals(st.getObject())))) {
coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
}
}
}
iter = new IteratorIteration(coll.iterator());
} else if (sp instanceof TransitivePropertySP && ((subjVar != null && subjVar.getValue() != null) || (objVar != null && objVar.getValue() != null)) && sp.getPredicateVar() != null) {
// if this is a transitive prop ref, we need to make sure that either the subj or obj is not null
// TODO: Cannot handle a open ended transitive property where subj and obj are null
// TODO: Should one day handle filling in the subj or obj with bindings and working this
// TODO: a lot of assumptions, and might be a large set returned causing an OME
Set<Statement> sts = null;
try {
sts = inferenceEngine.findTransitiveProperty((Resource) getVarValue(subjVar), (URI) getVarValue(predVar), getVarValue(objVar), (Resource) getVarValue(cntxtVar));
} catch (InferenceEngineException e) {
throw new QueryEvaluationException(e);
}
Collection<Map.Entry<Statement, BindingSet>> coll = new ArrayList();
for (BindingSet binding : bindings) {
for (Statement st : sts) {
coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
}
}
iter = new IteratorIteration(coll.iterator());
} else {
for (BindingSet binding : bindings) {
Value subjValue = getVarValue(subjVar, binding);
Value predValue = getVarValue(predVar, binding);
Value objValue = getVarValue(objVar, binding);
Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
if ((subjValue != null && !(subjValue instanceof Resource)) || (predValue != null && !(predValue instanceof URI))) {
continue;
}
stmts.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(new NullableStatementImpl((Resource) subjValue, (URI) predValue, objValue, contxtValue), binding));
}
if (stmts.size() == 0) {
return new EmptyIteration();
}
iter = ((RdfCloudTripleStoreConnection.StoreTripleSource) tripleSource).getStatements(stmts);
}
return new ConvertingIteration<Map.Entry<Statement, BindingSet>, BindingSet, QueryEvaluationException>(iter) {
@Override
protected BindingSet convert(Map.Entry<Statement, BindingSet> stbs) throws QueryEvaluationException {
Statement st = stbs.getKey();
BindingSet bs = stbs.getValue();
QueryBindingSet result = new QueryBindingSet(bs);
// contain a Value for that Var name
if (subjVar != null && !subjVar.isConstant() && !result.hasBinding(subjVar.getName())) {
result.addBinding(subjVar.getName(), st.getSubject());
}
if (predVar != null && !predVar.isConstant() && !result.hasBinding(predVar.getName())) {
result.addBinding(predVar.getName(), st.getPredicate());
}
if (objVar != null && !objVar.isConstant() && !result.hasBinding(objVar.getName())) {
result.addBinding(objVar.getName(), st.getObject());
}
if (cntxtVar != null && !cntxtVar.isConstant() && !result.hasBinding(cntxtVar.getName()) && st.getContext() != null) {
result.addBinding(cntxtVar.getName(), st.getContext());
}
return result;
}
};
}
use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BindingSetSerDe method fromBytes.
private BindingSet fromBytes(final byte[] bsBytes) {
try {
final int firstIndex = Bytes.indexOf(bsBytes, DELIM_BYTE);
final byte[] varOrderBytes = Arrays.copyOf(bsBytes, firstIndex);
final byte[] bsBytesNoVarOrder = Arrays.copyOfRange(bsBytes, firstIndex + 1, bsBytes.length);
final VariableOrder varOrder = new VariableOrder(new String(varOrderBytes, StandardCharsets.UTF_8).split(";"));
return getBindingSet(varOrder, bsBytesNoVarOrder);
} catch (final Exception e) {
log.trace("Unable to deserialize BindingSet: " + bsBytes);
return new QueryBindingSet();
}
}
Aggregations