Enable pre-bound sparql `$thisAtom` for sparql matcher.
This variable can be used to limit results to ones that have
relationship to the source atom. For example, restrict matches to atoms
held by buddies:
`?thisAtom hold:heldBy/buddy:buddy/^hold:holds ${resultName}`
@@ -140,6 +140,7 @@ public class SparqlMatcherActor extends UntypedActor {
}
private static final Var resultName = Var.alloc("result");
+ private static final Var thisAtom = Var.alloc("thisAtom");
private static final Var scoreName = Var.alloc("score");
private static BasicPattern createDetailsQuery(Model model, Statement parentStatement) {
@@ -354,8 +355,8 @@ public class SparqlMatcherActor extends UntypedActor {
if (log.isDebugEnabled()) {
log.debug("transformed query: {}", hintForCounterpartQuery);
}
- return Stream.concat(executeQuery(noHintForCounterpartQuery, atomToCheck),
- executeQuery(hintForCounterpartQuery, atomToCheck)).collect(Collectors.toList());
+ return Stream.concat(executeQuery(noHintForCounterpartQuery, atomToCheck, atom.getAtomUri()),
+ executeQuery(hintForCounterpartQuery, atomToCheck, atom.getAtomUri())).collect(Collectors.toList());
}).orElse(Collections.emptyList());
return atoms;
}
@@ -365,18 +366,26 @@ public class SparqlMatcherActor extends UntypedActor {
*
* @param q
* @param atomToCheck
+ * @param atomURI - the URI of the atom we are matching for
* @return
*/
- private Stream<ScoredAtom> executeQuery(Op q, Optional<AtomModelWrapperAndDataset> atomToCheck) {
+ private Stream<ScoredAtom> executeQuery(Op q, Optional<AtomModelWrapperAndDataset> atomToCheck, String atomURI) {
Query compiledQuery = OpAsQuery.asQuery(q);
// if we were given an atomToCheck, restrict the query result to that uri so
// that
// we get exactly one result if that uri is found for the atom
+ List<Binding> valuesBlockBindings = new ArrayList<>();
+ List<Var> valuesBlockVariables = new ArrayList<>();
+ //bind the ?thisAtom variable to the atom we are matching for
+ valuesBlockBindings.add(BindingFactory.binding(thisAtom, new ResourceImpl(atomURI.toString()).asNode()));
+ valuesBlockVariables.add(thisAtom);
+
if (atomToCheck.isPresent()) {
- Binding binding = BindingFactory.binding(resultName,
- new ResourceImpl(atomToCheck.get().atomModelWrapper.getAtomUri()).asNode());
- compiledQuery.setValuesDataBlock(Collections.singletonList(resultName), Collections.singletonList(binding));
+ valuesBlockBindings.add(BindingFactory.binding(resultName,
+ new ResourceImpl(atomToCheck.get().atomModelWrapper.getAtomUri()).asNode()));
+ valuesBlockVariables.add(resultName);
}
+ compiledQuery.setValuesDataBlock(valuesBlockVariables, valuesBlockBindings);
// make sure we order by score, if present, and we limit the results
if (compiledQuery.getProjectVars().contains(scoreName)) {
compiledQuery.addOrderBy(scoreName, Query.ORDER_DESCENDING);
