Skip to content

Commit

Permalink
Merge pull request #482 from TNO/480-nullpointerexception-can-occur-w…
Browse files Browse the repository at this point in the history
…hen-receiving-a-message-with-unknown-knowledge-interaction-id

Prevent NullPointerException by checking whether Knowledge Interaction ID is known by the receiver
  • Loading branch information
Sophietje authored Feb 22, 2024
2 parents da6fb91 + 070f6e7 commit e71e844
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void run() {

public void cleanUp() {
thread.interrupt();
try {
thread.join();
} catch (InterruptedException e) {
LOG.info("Failed to join thread.");
}
}

private static boolean portAvailable(int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.jena.vocabulary.RDF;
import org.slf4j.Logger;

import eu.knowledge.engine.reasoner.BaseRule;
import eu.knowledge.engine.reasoner.Rule;
import eu.knowledge.engine.smartconnector.api.AnswerExchangeInfo;
import eu.knowledge.engine.smartconnector.api.AnswerKnowledgeInteraction;
Expand Down Expand Up @@ -203,6 +202,16 @@ public CompletableFuture<AnswerMessage> processAskFromMessageRouter(AskMessage a
KnowledgeInteractionInfo knowledgeInteractionById = this.myKnowledgeBaseStore
.getKnowledgeInteractionById(answerKnowledgeInteractionId);

if (knowledgeInteractionById == null) {
AnswerMessage m = new AnswerMessage(anAskMsg.getToKnowledgeBase(), anAskMsg.getToKnowledgeInteraction(),
anAskMsg.getFromKnowledgeBase(), anAskMsg.getFromKnowledgeInteraction(), anAskMsg.getMessageId(),
"Received AskMessage wth unknown ToKnowledgeInteractionId");
LOG.debug("Received AskMessage with unknown ToKnowledgeInteractionId: "+anAskMsg.getToKnowledgeInteraction().toString());
CompletableFuture<AnswerMessage> f = new CompletableFuture<>();
f.complete(m);
return f;
}

AnswerKnowledgeInteraction answerKnowledgeInteraction;
answerKnowledgeInteraction = (AnswerKnowledgeInteraction) knowledgeInteractionById.getKnowledgeInteraction();

Expand Down Expand Up @@ -305,6 +314,17 @@ public CompletableFuture<ReactMessage> processPostFromMessageRouter(PostMessage
URI reactKnowledgeInteractionId = aPostMsg.getToKnowledgeInteraction();
KnowledgeInteractionInfo knowledgeInteractionById = this.myKnowledgeBaseStore
.getKnowledgeInteractionById(reactKnowledgeInteractionId);

if (knowledgeInteractionById == null) {
ReactMessage m = new ReactMessage(aPostMsg.getToKnowledgeBase(), aPostMsg.getToKnowledgeInteraction(),
aPostMsg.getFromKnowledgeBase(), aPostMsg.getFromKnowledgeInteraction(), aPostMsg.getMessageId(),
"Received PostMessage with unknown ToKnowledgeInteractionId");
LOG.debug("Received PostMessage with unknown ToKnowledgeInteractionId: "+aPostMsg.getToKnowledgeInteraction().toString());
CompletableFuture<ReactMessage> f = new CompletableFuture<>();
f.complete(m);
return f;
}

ReactKnowledgeInteraction reactKnowledgeInteraction;
reactKnowledgeInteraction = (ReactKnowledgeInteraction) knowledgeInteractionById.getKnowledgeInteraction();

Expand Down

0 comments on commit e71e844

Please sign in to comment.