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 @@ -19,6 +19,7 @@
import org.apache.jena.sparql.util.FmtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import eu.knowledge.engine.reasoner.BaseRule;
import eu.knowledge.engine.reasoner.BaseRule.MatchStrategy;
Expand Down Expand Up @@ -47,6 +48,7 @@
import eu.knowledge.engine.smartconnector.impl.KnowledgeInteractionInfo.Type;
import eu.knowledge.engine.smartconnector.messaging.AnswerMessage;
import eu.knowledge.engine.smartconnector.messaging.AskMessage;
import eu.knowledge.engine.smartconnector.messaging.KnowledgeMessage;
import eu.knowledge.engine.smartconnector.messaging.PostMessage;
import eu.knowledge.engine.smartconnector.messaging.ReactMessage;

Expand Down Expand Up @@ -420,19 +422,22 @@ public CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet> handle(
Instant aPreviousSend = Instant.now();

bsFuture = sendAskMessage.exceptionally((Throwable t) -> {
LOG.warn("Error '{}' occurred while waiting for response to: {}",
t.getMessage() != null ? t.getMessage() : t.getClass().getSimpleName(),
askMessage.getMessageId());

String failedMessage = MessageFormatter
.basicArrayFormat("Error '{}' occurred while waiting for response to message: {}",
new String[] {
t.getMessage() != null ? t.getMessage() : t.getClass().getSimpleName(),
askMessage.getMessageId().toString() });

LOG.warn(failedMessage);
LOG.debug("", t);
return null;
return ReasonerProcessor.this
.<AskMessage, AnswerMessage>createFailedResponseMessageFromRequestMessage(askMessage,
failedMessage);
}).thenApply((answerMessage) -> {
assert answerMessage != null;
LOG.debug("Received ANSWER message from KI '{}'", answerMessage.getFromKnowledgeInteraction());
BindingSet resultBindingSet = null;
if (answerMessage != null)
resultBindingSet = answerMessage.getBindings();

if (resultBindingSet == null)
resultBindingSet = new BindingSet();
BindingSet resultBindingSet = answerMessage.getBindings();

ReasonerProcessor.this.askExchangeInfos
.add(convertMessageToExchangeInfo(resultBindingSet, answerMessage, aPreviousSend));
Expand All @@ -450,6 +455,28 @@ public CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet> handle(
}
return bsFuture;
}

}

@SuppressWarnings("unchecked")
private <T extends KnowledgeMessage, S extends KnowledgeMessage> S createFailedResponseMessageFromRequestMessage(
T incomingMessage, String failedMessage) {

S outgoingMessage = null;
if (incomingMessage instanceof AskMessage) {

outgoingMessage = (S) new AnswerMessage(incomingMessage.getToKnowledgeBase(),
incomingMessage.getToKnowledgeInteraction(), incomingMessage.getFromKnowledgeBase(),
incomingMessage.getFromKnowledgeInteraction(), incomingMessage.getMessageId(), failedMessage);

} else if (incomingMessage instanceof PostMessage) {
outgoingMessage = (S) new AnswerMessage(incomingMessage.getToKnowledgeBase(),
incomingMessage.getToKnowledgeInteraction(), incomingMessage.getFromKnowledgeBase(),
incomingMessage.getFromKnowledgeInteraction(), incomingMessage.getMessageId(), failedMessage);
}

assert outgoingMessage != null;
return outgoingMessage;
}

/**
Expand Down Expand Up @@ -488,18 +515,19 @@ public CompletableFuture<eu.knowledge.engine.reasoner.api.BindingSet> handle(
.sendPostMessage(postMessage);
Instant aPreviousSend = Instant.now();
bsFuture = sendPostMessage.exceptionally((Throwable t) -> {
LOG.warn("Error '{}' occurred while waiting for response to: {}",
t.getMessage() != null ? t.getMessage() : t.getClass().getSimpleName(),
postMessage.getMessageId());
String failedMessage = MessageFormatter
.basicArrayFormat("Error '{}' occurred while waiting for response to message: {}",
new String[] {
t.getMessage() != null ? t.getMessage() : t.getClass().getSimpleName(),
postMessage.getMessageId().toString() });
LOG.warn(failedMessage);
LOG.debug("", t);
return null;
return ReasonerProcessor.this
.<PostMessage, ReactMessage>createFailedResponseMessageFromRequestMessage(postMessage,
failedMessage);
}).thenApply((reactMessage) -> {
BindingSet resultBindingSet = null;
if (reactMessage != null)
resultBindingSet = reactMessage.getResult();

if (resultBindingSet == null)
resultBindingSet = new BindingSet();
assert reactMessage != null;
BindingSet resultBindingSet = reactMessage.getResult();

ReasonerProcessor.this.postExchangeInfos.add(
convertMessageToExchangeInfo(newBS, reactMessage.getResult(), reactMessage, aPreviousSend));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.knowledge.engine.smartconnector.api.AnswerHandler;
import eu.knowledge.engine.smartconnector.api.AnswerKnowledgeInteraction;
import eu.knowledge.engine.smartconnector.api.AskExchangeInfo;
import eu.knowledge.engine.smartconnector.api.AskKnowledgeInteraction;
import eu.knowledge.engine.smartconnector.api.AskResult;
import eu.knowledge.engine.smartconnector.api.Binding;
import eu.knowledge.engine.smartconnector.api.BindingSet;
import eu.knowledge.engine.smartconnector.api.CommunicativeAct;
import eu.knowledge.engine.smartconnector.api.GraphPattern;
import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork;
import eu.knowledge.engine.smartconnector.util.MockedKnowledgeBase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ public void testPostReact() throws InterruptedException {
var prefixes = new PrefixMappingMem();
prefixes.setNsPrefixes(PrefixMapping.Standard);
prefixes.setNsPrefix("ke", Vocab.ONTO_URI);
var metaGraphPattern = new GraphPattern(prefixes, "?kb rdf:type ke:KnowledgeBase .", "?kb ke:hasName ?name .",
"?kb ke:hasDescription ?description .", "?kb ke:hasKnowledgeInteraction ?ki .",
"?ki rdf:type ?kiType .", "?ki ke:isMeta ?isMeta .", "?ki ke:hasCommunicativeAct ?act .",
"?act rdf:type ke:CommunicativeAct .", "?act ke:hasRequirement ?req .",
"?act ke:hasSatisfaction ?sat .", "?req rdf:type ?reqType .", "?sat rdf:type ?satType .",
"?ki ke:hasGraphPattern ?gp .", "?gp rdf:type ?patternType .", "?gp ke:hasPattern ?pattern .");
var metaGraphPattern = new GraphPattern(prefixes,
// @formatter:off
"?kb rdf:type ke:KnowledgeBase .",
"?kb ke:hasName ?name .",
"?kb ke:hasDescription ?description .",
"?kb ke:hasKnowledgeInteraction ?ki .",
"?ki rdf:type ?kiType .",
"?ki ke:isMeta ?isMeta .",
"?ki ke:hasCommunicativeAct ?act .",
"?act rdf:type ke:CommunicativeAct .",
"?act ke:hasRequirement ?req .",
"?act ke:hasSatisfaction ?sat .",
"?req rdf:type ?reqType .",
"?sat rdf:type ?satType .",
"?ki ke:hasGraphPattern ?gp .",
"?gp rdf:type ?patternType .",
"?gp ke:hasPattern ?pattern .");
// @formatter:on
PostKnowledgeInteraction ki1 = new PostKnowledgeInteraction(
new CommunicativeAct(new HashSet<>(Arrays.asList(Vocab.INFORM_PURPOSE)),
new HashSet<>(Arrays.asList(Vocab.NEW_KNOWLEDGE_PURPOSE))),
Expand Down Expand Up @@ -101,9 +113,9 @@ public void testPostReact() throws InterruptedException {
binding.put("reqType", "<https://w3id.org/knowledge-engine/InformPurpose>");
binding.put("satType", "<https://w3id.org/knowledge-engine/InformPurpose>");
binding.put("gp", "<https://example.org/gp>");
binding.put("patternType", "https://w3id.org/knowledge-engine/ArgumentGraphPattern");
binding.put("patternType", "<https://w3id.org/knowledge-engine/ArgumentGraphPattern>");
binding.put("pattern",
"?kb rdf:type kb:KnowledgeBase . ?kb kb:hasName ?name . ?kb kb:hasDescription ?description . ?kb kb:hasKnowledgeInteraction ?ki . ?ki rdf:type ?kiType . ?ki kb:isMeta ?isMeta . ?ki kb:hasCommunicativeAct ?act . ?act rdf:type kb:CommunicativeAct . ?act kb:hasRequirement ?req . ?act kb:hasSatisfaction ?sat . ?req rdf:type ?reqType . ?sat rdf:type ?satType . ?ki kb:hasGraphPattern ?gp . ?gp rdf:type ?patternType . ?gp kb:hasPattern ?pattern .");
"\"?kb rdf:type kb:KnowledgeBase . ?kb kb:hasName ?name . ?kb kb:hasDescription ?description . ?kb kb:hasKnowledgeInteraction ?ki . ?ki rdf:type ?kiType . ?ki kb:isMeta ?isMeta . ?ki kb:hasCommunicativeAct ?act . ?act rdf:type kb:CommunicativeAct . ?act kb:hasRequirement ?req . ?act kb:hasSatisfaction ?sat . ?req rdf:type ?reqType . ?sat rdf:type ?satType . ?ki kb:hasGraphPattern ?gp . ?gp rdf:type ?patternType . ?gp kb:hasPattern ?pattern .\"");
bindingSet.add(binding);

try {
Expand All @@ -115,7 +127,8 @@ public void testPostReact() throws InterruptedException {

LOG.info("After post!");
} catch (Exception e) {
LOG.error("Erorr", e);
LOG.error("Error", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void testPostReact() throws InterruptedException {
LOG.info("After post!");
} catch (ExecutionException e) {
LOG.error("Error", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void testPostReact() throws InterruptedException {

LOG.info("After post!");
} catch (Exception e) {
LOG.error("Erorr", e);
LOG.error("Error", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void testPostReact() throws InterruptedException {
assertEquals(expected, bs);
} catch (Exception e) {
LOG.error("Error", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public void testPostReact() throws InterruptedException {
// });

} catch (Exception e) {
LOG.error("Erorr", e);
LOG.error("Error", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testPostReact() throws InterruptedException {

LOG.info("After post!");
} catch (Exception e) {
LOG.error("Erorr", e);
LOG.error("Error", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ public void testRequestMetadata() throws InterruptedException {

kn.sync();

GraphPattern gp2 = new GraphPattern(prefixes,
"""
?kb rdf:type kb:KnowledgeBase .
?kb kb:hasName ?name .
?kb kb:hasDescription ?description .
?kb kb:hasKnowledgeInteraction ?ki .
?ki rdf:type ?kiType .
?ki kb:isMeta ?isMeta .
?ki kb:hasCommunicativeAct ?act .
?act rdf:type kb:CommunicativeAct .
?act kb:hasRequirement ?req .
?act kb:hasSatisfaction ?sat .
?req rdf:type ?reqType .
?sat rdf:type ?satType .
?ki kb:hasGraphPattern ?gp .
?gp rdf:type ?patternType .
?gp kb:hasPattern ?pattern .
"""
);
GraphPattern gp2 = new GraphPattern(prefixes, """
?kb rdf:type kb:KnowledgeBase .
?kb kb:hasName ?name .
?kb kb:hasDescription ?description .
?kb kb:hasKnowledgeInteraction ?ki .
?ki rdf:type ?kiType .
?ki kb:isMeta ?isMeta .
?ki kb:hasCommunicativeAct ?act .
?act rdf:type kb:CommunicativeAct .
?act kb:hasRequirement ?req .
?act kb:hasSatisfaction ?sat .
?req rdf:type ?reqType .
?sat rdf:type ?satType .
?ki kb:hasGraphPattern ?gp .
?gp rdf:type ?patternType .
?gp kb:hasPattern ?pattern .
""");

var ki2 = new ReactKnowledgeInteraction(new CommunicativeAct(), gp2, null);
kb2.register(ki2, new ReactHandler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.knowledge.engine.smartconnector.api;

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

import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -9,19 +11,9 @@

import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.sparql.graph.PrefixMappingMem;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.knowledge.engine.smartconnector.api.Binding;
import eu.knowledge.engine.smartconnector.api.BindingSet;
import eu.knowledge.engine.smartconnector.api.CommunicativeAct;
import eu.knowledge.engine.smartconnector.api.GraphPattern;
import eu.knowledge.engine.smartconnector.api.PostKnowledgeInteraction;
import eu.knowledge.engine.smartconnector.api.ReactExchangeInfo;
import eu.knowledge.engine.smartconnector.api.ReactHandler;
import eu.knowledge.engine.smartconnector.api.ReactKnowledgeInteraction;
import eu.knowledge.engine.smartconnector.runtime.KeRuntime;
import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork;
import eu.knowledge.engine.smartconnector.util.MockedKnowledgeBase;

Expand Down Expand Up @@ -172,6 +164,7 @@ public void run() {
}
} catch (InterruptedException e) {
LOG.debug("", e);
fail();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public BindingSet react(ReactKnowledgeInteraction anRKI, ReactExchangeInfo aReac
assertEquals("<https://www.tno.nl/example/object>", b2.get("o2"));
} catch (InterruptedException | ExecutionException e) {
LOG.error("{}", e);
fail();
}

}
Expand Down