Skip to content

Commit

Permalink
Merge pull request #508 from TNO/506-askresult-bindingset-different-f…
Browse files Browse the repository at this point in the history
…rom-exchangeinfo-bindingset

506 askresult bindingset different from exchangeinfo bindingset
  • Loading branch information
bnouwt authored May 31, 2024
2 parents 115a923 + 2911abb commit 3d1844b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.jena.sparql.core.TriplePath;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.graph.PrefixMappingZero;
import org.apache.jena.sparql.serializer.SerializationContext;
import org.apache.jena.sparql.syntax.ElementPathBlock;
import org.apache.jena.sparql.util.FmtUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -265,13 +266,17 @@ private void continueReasoningForward(eu.knowledge.engine.reasoner.api.BindingSe
* @param bs a reasoner bindingset
* @return a ke bindingset
*/
private BindingSet translateBindingSetFrom(eu.knowledge.engine.reasoner.api.BindingSet bs) {
protected BindingSet translateBindingSetFrom(eu.knowledge.engine.reasoner.api.BindingSet bs) {
BindingSet newBS = new BindingSet();
Binding newB;

SerializationContext context = new SerializationContext();
context.setUsePlainLiterals(false);

for (eu.knowledge.engine.reasoner.api.Binding b : bs) {
newB = new Binding();
for (Map.Entry<Var, Node_Concrete> entry : b.entrySet()) {
newB.put(entry.getKey().getName(), FmtUtils.stringForNode(entry.getValue()));
newB.put(entry.getKey().getName(), FmtUtils.stringForNode(entry.getValue(), context));
}
newBS.add(newB);
}
Expand All @@ -284,7 +289,7 @@ private BindingSet translateBindingSetFrom(eu.knowledge.engine.reasoner.api.Bind
* @param bs a ke bindingset
* @return a reasoner bindingset
*/
private eu.knowledge.engine.reasoner.api.BindingSet translateBindingSetTo(BindingSet someBindings) {
protected eu.knowledge.engine.reasoner.api.BindingSet translateBindingSetTo(BindingSet someBindings) {

eu.knowledge.engine.reasoner.api.BindingSet newBindingSet = new eu.knowledge.engine.reasoner.api.BindingSet();
eu.knowledge.engine.reasoner.api.Binding newBinding;
Expand Down Expand Up @@ -432,7 +437,8 @@ public CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet> handle(
});

} catch (IOException e) {
LOG.warn("Errors like '{}' should not occur while sending: {}", e.getMessage(), askMessage.getMessageId());
LOG.warn("Errors like '{}' should not occur while sending: {}", e.getMessage(),
askMessage.getMessageId());
LOG.debug("", e);
bsFuture = new CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet>();
bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet());
Expand Down Expand Up @@ -494,7 +500,8 @@ public CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet> handle(
});

} catch (IOException e) {
LOG.warn("Errors like '{}' should not occur while sending: {}", e.getMessage(), postMessage.getMessageId());
LOG.warn("Errors like '{}' should not occur while sending: {}", e.getMessage(),
postMessage.getMessageId());
LOG.debug("", e);
bsFuture = new CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet>();
bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void testAskAnswer() throws InterruptedException {
kb1.setReasonerEnabled(true);
kn.addKB(kb2);

GraphPattern gp1 = new GraphPattern(prefixes, "?a <https://www.tno.nl/example/b> \"test\".");
GraphPattern gp1 = new GraphPattern(prefixes, "?a <https://www.tno.nl/example/b> ?c .");
AnswerKnowledgeInteraction aKI = new AnswerKnowledgeInteraction(new CommunicativeAct(), gp1);
kb1.register(aKI, (AnswerHandler) (anAKI, anAnswerExchangeInfo) -> {
assertTrue(
Expand All @@ -58,7 +58,7 @@ public void testAskAnswer() throws InterruptedException {
BindingSet bindingSet = new BindingSet();
Binding binding = new Binding();
binding.put("a", "<https://www.tno.nl/example/a>");
binding.put("c", "<https://www.tno.nl/example/c>");
binding.put("c", "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>");
bindingSet.add(binding);

return bindingSet;
Expand All @@ -76,6 +76,11 @@ public void testAskAnswer() throws InterruptedException {
LOG.trace("Before ask.");
AskResult result = kb2.ask(askKI, new BindingSet()).get();
bindings = result.getBindings();

System.out.println("Bindings: " + bindings);
System.out
.println("Bindings2: " + result.getExchangeInfoPerKnowledgeBase().iterator().next().getBindings());

LOG.trace("After ask.");
} catch (InterruptedException | ExecutionException e) {
fail();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package eu.knowledge.engine.smartconnector.impl;

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

import java.util.HashSet;
import java.util.Set;

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Node_Concrete;
import org.apache.jena.sparql.core.Var;
import org.junit.jupiter.api.Test;

import eu.knowledge.engine.reasoner.Rule;
import eu.knowledge.engine.smartconnector.api.Binding;
import eu.knowledge.engine.smartconnector.api.BindingSet;

class TestBindingSetConversions {

private OpenReasonerProcessor rp = new OpenReasonerProcessor(new HashSet<>(), null, new HashSet<>());

/**
* Test whether converting typed literals between reasoner and knowledge engine
* binding sets works correctly.
*/
@Test
void testTypedLiteral() {

String varStringVersion = "a";
Var varNodeVersion = Var.alloc(varStringVersion);
Node literalNodeVersion = NodeFactory.createLiteral("true", XSDDatatype.XSDboolean);
var literalStringVersion = "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>";

BindingSet bs = new BindingSet();
Binding b = new Binding();
b.put(varStringVersion, literalStringVersion);
bs.add(b);
eu.knowledge.engine.reasoner.api.BindingSet reasonerBS = rp.testerTo(bs);
assertEquals(literalNodeVersion, reasonerBS.iterator().next().get(varStringVersion));

eu.knowledge.engine.reasoner.api.BindingSet bsR = new eu.knowledge.engine.reasoner.api.BindingSet();
eu.knowledge.engine.reasoner.api.Binding bR = new eu.knowledge.engine.reasoner.api.Binding();
bR.put(varNodeVersion, (Node_Concrete) literalNodeVersion);
bsR.add(bR);
BindingSet otherBS = rp.testerFrom(bsR);
assertEquals(literalStringVersion, otherBS.iterator().next().get(varStringVersion));
}

/**
* Make some binding set translation methods available.
*/
public static class OpenReasonerProcessor extends ReasonerProcessor {
public OpenReasonerProcessor(Set<KnowledgeInteractionInfo> knowledgeInteractions, MessageRouter messageRouter,
Set<Rule> someDomainKnowledge) {
super(knowledgeInteractions, messageRouter, someDomainKnowledge);
}

public eu.knowledge.engine.reasoner.api.BindingSet testerTo(BindingSet bs) {
return this.translateBindingSetTo(bs);
}

public BindingSet testerFrom(eu.knowledge.engine.reasoner.api.BindingSet bs) {
return this.translateBindingSetFrom(bs);
}
}

}

0 comments on commit 3d1844b

Please sign in to comment.