Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public CompletableFuture<AskResult> executeAskInteraction(BindingSet someBinding

return this.finalBindingSetFuture.thenApply((bs) -> {
if (myKnowledgeInteraction.getKnowledgeInteraction().knowledgeGapsEnabled()) {
this.knowledgeGaps = bs.isEmpty() ? getKnowledgeGaps(this.reasonerPlan.getStartNode())
this.knowledgeGaps = bs.isEmpty() ? getKnowledgeGaps(this.reasonerPlan.getStartNode(), new HashSet<>())
: new HashSet<KnowledgeGap>();
}

Expand Down Expand Up @@ -666,15 +666,17 @@ public ReasonerPlan getReasonerPlan() {
* {@code A} <i><b>AND</b></i> {@code B} need to be added to solve the
* gap.
*/
public Set<KnowledgeGap> getKnowledgeGaps(RuleNode plan) {
public Set<KnowledgeGap> getKnowledgeGaps(RuleNode node, Set<RuleNode> someVisitedNodes) {

assert plan instanceof AntSide;
assert node instanceof AntSide;

Set<KnowledgeGap> existingOrGaps = new HashSet<>();

someVisitedNodes.add(node);

// TODO do we need to include the parent if we are not backward chaining?
Map<TriplePattern, Set<RuleNode>> nodeCoverage = plan
.findAntecedentCoverage(((AntSide) plan).getAntecedentNeighbours());
Map<TriplePattern, Set<RuleNode>> nodeCoverage = node
.findAntecedentCoverage(((AntSide) node).getAntecedentNeighbours());

// collect triple patterns that have an empty set
Set<KnowledgeGap> collectedOrGaps, someGaps = new HashSet<>();
Expand All @@ -689,7 +691,7 @@ public Set<KnowledgeGap> getKnowledgeGaps(RuleNode plan) {
for (RuleNode neighbor : entry.getValue()) {
LOG.trace("Neighbor is {}", neighbor);

if (!neighbor.getRule().getAntecedent().isEmpty()) {
if (!someVisitedNodes.contains(neighbor) && !neighbor.getRule().getAntecedent().isEmpty()) {
// make sure neighbor has no knowledge gaps
LOG.trace("Neighbor has antecedents, so check if the neighbor has gaps");

Expand All @@ -699,7 +701,7 @@ public Set<KnowledgeGap> getKnowledgeGaps(RuleNode plan) {
boolean isMeta = isMetaKI(neighbor);

// TODO what if the graph contains loops?
if (!isMeta && (someGaps = getKnowledgeGaps(neighbor)).isEmpty()) {
if (!isMeta && (someGaps = getKnowledgeGaps(neighbor, someVisitedNodes)).isEmpty()) {
// found neighbor without knowledge gaps for the current triple, so current
// triple is covered.
LOG.trace("Neighbor has no gaps");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.knowledge.engine.smartconnector.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -61,6 +62,7 @@ public void test() throws InterruptedException, ExecutionException {

BindingSet argument = new BindingSet();
Binding binding = new Binding();
binding.put("b", "<http://example.org/building1>");
argument.add(binding);
AskPlan pp = appKb.planAsk(appKbAsk, new RecipientSelector());

Expand All @@ -72,22 +74,34 @@ public void test() throws InterruptedException, ExecutionException {
BindingSet expectedBS = new BindingSet();
Binding expectedB = new Binding();
expectedB.put("s", "<http://example.org/sensor1>");
expectedB.put("b", "<http://example.org/building1>");
expectedB.put("value", "\"10\"^^<http://www.w3.org/2001/XMLSchema#integer>");
expectedBS.add(expectedB);

expectedB = new Binding();
expectedB.put("s", "<http://example.org/sensor2>");
expectedB.put("b", "<http://example.org/building1>");
expectedB.put("value", "\"20\"^^<http://www.w3.org/2001/XMLSchema#integer>");
expectedBS.add(expectedB);

assertEquals(expectedBS, result.getBindings());

// test again, but now trigger knowledge gaps detection (in loop scenario's like
// this test contains)
BindingSet bs = new BindingSet();
Binding b = new Binding();
b.put("b", "<http://example.org/building3>");
bs.add(b);
AskResult ar = appKb.ask(appKbAsk, bs).get();
assertNotNull(ar);

}

private AskKnowledgeInteraction configureAppKb() {
GraphPattern appGP = new GraphPattern(prefixes,
"?s rdf:type ex:Sensor . ?s ex:isPartOf ex:building1 . ?s ex:hasLatestValue ?value .");
AskKnowledgeInteraction appKbAsk = new AskKnowledgeInteraction(new CommunicativeAct(), appGP);
"?s rdf:type ex:Sensor . ?s ex:isPartOf ?b . ?s ex:hasLatestValue ?value .");
AskKnowledgeInteraction appKbAsk = new AskKnowledgeInteraction(new CommunicativeAct(), appGP, "askLatestValue",
true);
appKb.register(appKbAsk);

HashSet<TriplePattern> rule1ant = new HashSet<>(
Expand Down
Loading