From fccb9cd29e2f16f240f6d20d6c3706cb3954389d Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 3 Oct 2025 15:32:53 +0200 Subject: [PATCH 1/5] First version of fixing this problem. --- ...SmartConnectorLifeCycleApiServiceImpl.java | 1 - .../impl/KnowledgeBaseStore.java | 5 +- .../impl/KnowledgeBaseStoreImpl.java | 14 +++- .../impl/KnowledgeBaseStoreListener.java | 11 ++- .../impl/MetaKnowledgeBaseImpl.java | 29 ++++--- .../impl/SmartConnectorImpl.java | 19 +++-- .../api/TestSmartConnectorStop.java | 75 +++++++++++++++++++ 7 files changed, 125 insertions(+), 29 deletions(-) create mode 100644 smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java diff --git a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/SmartConnectorLifeCycleApiServiceImpl.java b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/SmartConnectorLifeCycleApiServiceImpl.java index c97fbe99c..13f1747ca 100644 --- a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/SmartConnectorLifeCycleApiServiceImpl.java +++ b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/SmartConnectorLifeCycleApiServiceImpl.java @@ -17,7 +17,6 @@ import eu.knowledge.engine.rest.api.NotFoundException; import eu.knowledge.engine.rest.model.ResponseMessage; import eu.knowledge.engine.rest.model.SmartConnector; -import eu.knowledge.engine.smartconnector.api.KnowledgeEngineException; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Content; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStore.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStore.java index 6fca5b168..d0122a973 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStore.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStore.java @@ -2,6 +2,7 @@ import java.net.URI; import java.util.Set; +import java.util.concurrent.CompletableFuture; import org.apache.jena.rdf.model.Resource; @@ -77,6 +78,6 @@ public interface KnowledgeBaseStore { URI getMetaId(URI knowledgeBaseId, KnowledgeInteractionInfo.Type kiType, Resource purpose); Resource getPurpose(URI knowledgeBaseId, URI knowledgeInteractionId); - - void stop(); + + CompletableFuture stop(); } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreImpl.java index 222dde8f0..820ff030c 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreImpl.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; @@ -20,6 +21,7 @@ import eu.knowledge.engine.smartconnector.api.KnowledgeBase; import eu.knowledge.engine.smartconnector.api.KnowledgeInteraction; import eu.knowledge.engine.smartconnector.api.PostKnowledgeInteraction; +import eu.knowledge.engine.smartconnector.api.PostResult; import eu.knowledge.engine.smartconnector.api.ReactHandler; import eu.knowledge.engine.smartconnector.api.ReactKnowledgeInteraction; import eu.knowledge.engine.smartconnector.api.Vocab; @@ -322,8 +324,16 @@ public Resource getPurpose(URI knowledgeBaseId, URI knowledgeInteractionId) { } @Override - public void stop() { + public CompletableFuture stop() { this.LOG.trace("Stopping KnowledgeBaseStoreImpl"); - this.listeners.forEach(l -> l.smartConnectorStopping()); + + Set> futures = new HashSet<>(); + this.listeners.forEach(l -> { + + CompletableFuture future = l.smartConnectorStopping(); + futures.add(future); + + }); + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); } } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreListener.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreListener.java index e5986e1ac..d8da3fa18 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreListener.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/KnowledgeBaseStoreListener.java @@ -1,14 +1,21 @@ package eu.knowledge.engine.smartconnector.impl; +import java.util.concurrent.CompletableFuture; + +import eu.knowledge.engine.smartconnector.api.PostResult; + public interface KnowledgeBaseStoreListener { void knowledgeInteractionRegistered(KnowledgeInteractionInfo ki); void knowledgeInteractionUnregistered(KnowledgeInteractionInfo ki); - + /** * The Knowledge Base has requested the smart connector to stop. + * + * @return a future that will complete once all other knowledge bases have been + * notified of our termination. */ - void smartConnectorStopping(); + CompletableFuture smartConnectorStopping(); } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java index 3f1890461..c52f8ba8e 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java @@ -585,21 +585,20 @@ public void knowledgeInteractionUnregistered(KnowledgeInteractionInfo ki) { } @Override - public void smartConnectorStopping() { - try { - - var kiInfo = this.knowledgeBaseStore.getKnowledgeInteractionByObject(this.metaPostRemovedKI); - Set otherKnowledgeBases = this.otherKnowledgeBaseStore.getOtherKnowledgeBases(); - // Block on the future, but wait no longer than the timeout. - long timeout = POST_REMOVED_TIMEOUT_MILLIS_PER_OTHERKB - + otherKnowledgeBases.size() * POST_REMOVED_TIMEOUT_MILLIS_PER_OTHERKB; - LOG.debug("Waiting for max {}ms for other KBs to ack my termination message.", timeout); - this.interactionProcessor.planPostFromKnowledgeBase(kiInfo, new RecipientSelector()) - .execute(this.fillMetaBindings(null)).get(timeout, TimeUnit.MILLISECONDS); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - LOG.error("An error occured while informing other KBs about our " - + "termination. Proceeding to stop the smart connector regardless.", e); - } + public CompletableFuture smartConnectorStopping() { + var kiInfo = this.knowledgeBaseStore.getKnowledgeInteractionByObject(this.metaPostRemovedKI); + Set otherKnowledgeBases = this.otherKnowledgeBaseStore.getOtherKnowledgeBases(); + // Block on the future, but wait no longer than the timeout. + long timeout = POST_REMOVED_TIMEOUT_MILLIS_PER_OTHERKB + + otherKnowledgeBases.size() * POST_REMOVED_TIMEOUT_MILLIS_PER_OTHERKB; + LOG.debug("Waiting for max {}ms for other KBs to ack my termination message.", timeout); + return this.interactionProcessor.planPostFromKnowledgeBase(kiInfo, new RecipientSelector()) + .execute(this.fillMetaBindings(null)).orTimeout(timeout, TimeUnit.MILLISECONDS) + .exceptionally((Throwable e) -> { + LOG.error("An error occured while informing other KBs about our " + + "termination. Proceeding to stop the smart connector regardless.", e); + return (PostResult) null; + }); } @Override diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java index 0de0aaddd..36dfd5032 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java @@ -5,6 +5,7 @@ import java.time.Instant; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import org.slf4j.Logger; @@ -459,13 +460,17 @@ public void stop() { this.isStopped = true; // this will trigger notifications to other Smart Connectors. - this.knowledgeBaseStore.stop(); - - this.otherKnowledgeBaseStore.stop(); - - KeRuntime.localSmartConnectorRegistry().unregister(this); - - this.knowledgeBaseExecutorService.execute(() -> this.myKnowledgeBase.smartConnectorStopped(this)); + CompletableFuture future = this.knowledgeBaseStore.stop(); + + try { + future.whenComplete((Void v, Throwable t) -> { + this.otherKnowledgeBaseStore.stop(); + KeRuntime.localSmartConnectorRegistry().unregister(this); + this.knowledgeBaseExecutorService.execute(() -> this.myKnowledgeBase.smartConnectorStopped(this)); + }).get(); + } catch (InterruptedException | ExecutionException e) { + LOG.error("No problems should occur when stopping a SmartConnector.", e); + } } private void checkStopped() { diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java new file mode 100644 index 000000000..7f59143f7 --- /dev/null +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java @@ -0,0 +1,75 @@ +package eu.knowledge.engine.smartconnector.api; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.net.URI; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import eu.knowledge.engine.smartconnector.impl.SmartConnectorBuilder; + +public class TestSmartConnectorStop { + + private static final Logger LOG = LoggerFactory.getLogger(TestSmartConnectorStop.class); + + private static SmartConnector sc1; + private static SmartConnector sc2; + private static AtomicBoolean stopped = new AtomicBoolean(false); + + @Test + public void test() throws InterruptedException { + + sc1 = SmartConnectorBuilder.newSmartConnector(new MyKnowledgeBase("kb1")).create(); + sc2 = SmartConnectorBuilder.newSmartConnector(new MyKnowledgeBase("kb2")).create(); + + sc1.register(new AskKnowledgeInteraction(new CommunicativeAct(), new GraphPattern("?t1 ?t2 ."))); + + sc2.register(new AnswerKnowledgeInteraction(new CommunicativeAct(), new GraphPattern("?r1 ?r2 .")), + (AnswerKnowledgeInteraction var1, AnswerExchangeInfo var2) -> { + + return new BindingSet(); + }); + + Thread.sleep(2000); + + sc1.stop(); + + assertTrue(stopped.get()); + + Thread.sleep(2000); + } + + class MyKnowledgeBase implements KnowledgeBase { + + private String name; + + //@formatter:off + public MyKnowledgeBase(String aName) { this.name = aName; } + + @Override + public URI getKnowledgeBaseId() { return URI.create("http://example.org/" + this.name); } + + @Override + public String getKnowledgeBaseName() { return this.name + " name"; } + + @Override + public String getKnowledgeBaseDescription() { return this.name + " desc"; } + + @Override + public void smartConnectorReady(SmartConnector aSC) { LOG.info("{} ready!", this.name); } + + @Override + public void smartConnectorConnectionLost(SmartConnector aSC) { LOG.info("{} lost!", this.name); } + + @Override + public void smartConnectorConnectionRestored(SmartConnector aSC) { LOG.info("{} restored!", this.name); } + + @Override + public void smartConnectorStopped(SmartConnector aSC) { TestSmartConnectorStop.stopped.set(true); LOG.info("{} stopped!", this.name); } + //@formatter:on + } + +} From 20e1c71f9a71655e018720dbf12f773c23669f89 Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 3 Oct 2025 16:26:58 +0200 Subject: [PATCH 2/5] Improved the original test. --- .../smartconnector/api/SmartConnector.java | 6 +++++- .../smartconnector/impl/SmartConnectorImpl.java | 16 ++++++---------- .../api/TestSmartConnectorStop.java | 15 +++++++++------ .../runtime/messaging/MockSmartConnector.java | 6 ++++-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/smart-connector-api/src/main/java/eu/knowledge/engine/smartconnector/api/SmartConnector.java b/smart-connector-api/src/main/java/eu/knowledge/engine/smartconnector/api/SmartConnector.java index f4eb9c4b9..73569a540 100644 --- a/smart-connector-api/src/main/java/eu/knowledge/engine/smartconnector/api/SmartConnector.java +++ b/smart-connector-api/src/main/java/eu/knowledge/engine/smartconnector/api/SmartConnector.java @@ -305,7 +305,11 @@ CompletableFuture post(PostKnowledgeInteraction aPKI, RecipientSelec * {@link SmartConnector} is unpredictable. * * Note that a stopped {@link SmartConnector} can no longer be used. + * + * @return a completable future that completes after + * {@link KnowledgeBase#smartConnectorStopped(SmartConnector)} is + * called. */ - void stop(); + CompletableFuture stop(); } \ No newline at end of file diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java index 36dfd5032..8c783d78c 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java @@ -454,7 +454,7 @@ public PostPlan planPost(PostKnowledgeInteraction aPKI, RecipientSelector aSelec * Note that a stopped {@link SmartConnectorImpl} can no longer be used. */ @Override - public void stop() { + public CompletableFuture stop() { this.checkStopped(); this.isStopped = true; @@ -462,15 +462,11 @@ public void stop() { // this will trigger notifications to other Smart Connectors. CompletableFuture future = this.knowledgeBaseStore.stop(); - try { - future.whenComplete((Void v, Throwable t) -> { - this.otherKnowledgeBaseStore.stop(); - KeRuntime.localSmartConnectorRegistry().unregister(this); - this.knowledgeBaseExecutorService.execute(() -> this.myKnowledgeBase.smartConnectorStopped(this)); - }).get(); - } catch (InterruptedException | ExecutionException e) { - LOG.error("No problems should occur when stopping a SmartConnector.", e); - } + return future.whenComplete((v, t) -> { + this.otherKnowledgeBaseStore.stop(); + KeRuntime.localSmartConnectorRegistry().unregister(this); + this.myKnowledgeBase.smartConnectorStopped(this); + }); } private void checkStopped() { diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java index 7f59143f7..5286b2777 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestSmartConnectorStop.java @@ -17,7 +17,7 @@ public class TestSmartConnectorStop { private static SmartConnector sc1; private static SmartConnector sc2; - private static AtomicBoolean stopped = new AtomicBoolean(false); + private static AtomicBoolean scStoppedCalled = new AtomicBoolean(false); @Test public void test() throws InterruptedException { @@ -33,13 +33,16 @@ public void test() throws InterruptedException { return new BindingSet(); }); - Thread.sleep(2000); + Thread.sleep(1000); - sc1.stop(); + sc1.stop().whenComplete((v, t) -> { + assertTrue(scStoppedCalled.get(), + "The returned future should always complete after smartConnectorStopped(...) is called."); + }); - assertTrue(stopped.get()); + Thread.sleep(500); - Thread.sleep(2000); + sc2.stop(); } class MyKnowledgeBase implements KnowledgeBase { @@ -68,7 +71,7 @@ class MyKnowledgeBase implements KnowledgeBase { public void smartConnectorConnectionRestored(SmartConnector aSC) { LOG.info("{} restored!", this.name); } @Override - public void smartConnectorStopped(SmartConnector aSC) { TestSmartConnectorStop.stopped.set(true); LOG.info("{} stopped!", this.name); } + public void smartConnectorStopped(SmartConnector aSC) { TestSmartConnectorStop.scStoppedCalled.set(true); LOG.info("{} stopped!", this.name); } //@formatter:on } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/MockSmartConnector.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/MockSmartConnector.java index 16a99d566..f8c5db492 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/MockSmartConnector.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/MockSmartConnector.java @@ -118,9 +118,11 @@ public CompletableFuture post(PostKnowledgeInteraction ki, BindingSe } @Override - public void stop() { + public CompletableFuture stop() { // TODO Auto-generated method stub - + var future = new CompletableFuture(); + future.complete((Void) null); + return future; } @Override From dd0609789f218a443af7dc459075a89b84af6f77 Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 10 Oct 2025 10:02:20 +0200 Subject: [PATCH 3/5] Improve handling of stops with Knowledge Network. Also: - make sure more tests use Knowledge Network stop method. - Make ReasonerProcessor failed post/ask Message less verbose. --- .../impl/InteractionProcessor.java | 1 - .../impl/MetaKnowledgeBaseImpl.java | 1 - .../impl/ReasonerProcessor.java | 21 +++++-- .../impl/SmartConnectorImpl.java | 1 - .../messaging/RemoteKerConnectionManager.java | 11 ++-- .../util/KnowledgeBaseImpl.java | 12 +--- .../smartconnector/util/KnowledgeNetwork.java | 6 +- .../api/AdditionForAudienceTest.java | 3 +- .../api/GeneralizeIfNecessaryTest.java | 8 +-- .../api/NotDesignedToWorkTogetherTest.java | 8 +-- .../api/NotDesignedToWorkTogetherTest2.java | 8 +-- .../smartconnector/api/TestAskAnswer.java | 25 +++----- .../smartconnector/api/TestAskAnswer2.java | 18 ++---- .../smartconnector/api/TestAskAnswer3.java | 31 ++-------- .../smartconnector/api/TestAskAnswer4.java | 18 ++---- .../smartconnector/api/TestAskAnswer5.java | 4 +- .../api/TestAskAnswerBindingValidity.java | 19 ++---- .../api/TestAskAnswerLargeBindingSets.java | 24 ++------ .../api/TestAskAnswerManyKIs.java | 31 ++-------- ...tAskAnswerSingleKBMultipleMatchingKIs.java | 19 ++---- .../api/TestAskAnswerUnregister.java | 19 ++---- .../api/TestAskAnswerWithPartialBindings.java | 18 ++---- .../api/TestAskPartialMetadata.java | 25 ++------ .../api/TestAskRecipientSelector.java | 58 +++++-------------- .../api/TestComplexGraphPatternMatching.java | 25 ++------ .../api/TestDuplicateBindings.java | 21 +------ ...etadataFromNormalKnowledgeInteraction.java | 18 ++---- ...tMetadataKnowledgeInteractionMatching.java | 21 ++----- .../smartconnector/api/TestPostReact.java | 18 ++---- .../smartconnector/api/TestPostReact2.java | 26 ++------- .../smartconnector/api/TestPostReact3.java | 26 ++------- .../smartconnector/api/TestPostReact4.java | 18 ++---- .../api/TestPostReactDatatype.java | 20 ++----- .../api/TestPostReactLanguageTag.java | 18 ++---- .../api/TestPostReactPerformance.java | 20 ++----- .../api/TestPostRecipientSelector.java | 26 ++------- .../smartconnector/api/TestReactMetadata.java | 17 +++--- .../api/TestRequestMetadata.java | 15 ++--- .../api/VariableBindingNameTest.java | 20 ++----- .../misc/ConfigurationTest.java | 22 ++++--- .../SmartConnectorRegistrationStressTest.java | 7 ++- .../TestRegisterSmartConnectorWithSameId.java | 44 +++++++------- 42 files changed, 216 insertions(+), 555 deletions(-) diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/InteractionProcessor.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/InteractionProcessor.java index 959e52fe9..6a34938a7 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/InteractionProcessor.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/InteractionProcessor.java @@ -10,7 +10,6 @@ import eu.knowledge.engine.smartconnector.api.AskPlan; import eu.knowledge.engine.smartconnector.api.AskResult; import eu.knowledge.engine.smartconnector.api.BindingSet; -import eu.knowledge.engine.smartconnector.api.GraphPattern; import eu.knowledge.engine.smartconnector.api.KnowledgeBase; import eu.knowledge.engine.smartconnector.api.KnowledgeInteraction; import eu.knowledge.engine.smartconnector.api.PostKnowledgeInteraction; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java index c52f8ba8e..8ee436d3a 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MetaKnowledgeBaseImpl.java @@ -11,7 +11,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.apache.jena.query.Query; import org.apache.jena.query.QueryExecution; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java index 5f29d239c..70df8afc3 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java @@ -295,7 +295,7 @@ private void continueReasoningForward(eu.knowledge.engine.reasoner.api.BindingSe if (aBindingSetHandler != null && aBindingSetHandler.getBindingSet() != null) { resultBS = aBindingSetHandler.getBindingSet(); } - + this.finalBindingSetFuture.complete(resultBS); } else { continueReasoningForward(incomingBS, aBindingSetHandler); @@ -468,9 +468,11 @@ public CompletableFuture handle( }); } catch (IOException e) { - LOG.warn("Error '{}' occurred while sending {}", + LOG.warn( + "Error '{}' should not occur while sending an AskMessage from KB '{}' to KB '{}'. The latter might have been stopped.", e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), - askMessage.getClass().getSimpleName()); + ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), + this.kii.getKnowledgeBaseId()); LOG.debug("", e); bsFuture = new CompletableFuture(); bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet()); @@ -558,9 +560,11 @@ public CompletableFuture handle( }); } catch (IOException e) { - LOG.warn("Error '{}' occurred while sending {}", + LOG.warn( + "Error '{}' should not occur while sending an PostMessage from KB '{}' to KB '{}'. The latter might have been stopped.", e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), - postMessage.getClass().getSimpleName()); + ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), + this.kii.getKnowledgeBaseId()); LOG.debug("", e); bsFuture = new CompletableFuture(); bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet()); @@ -624,7 +628,12 @@ public CompletableFuture handle(eu.knowledge.engine.reasoner.api.BindingSe }); } catch (IOException e) { - LOG.error("No errors should occur while sending an PostMessage.", e); + LOG.warn( + "Error '{}' should not occur while sending an PostMessage from KB '{}' to KB '{}'. The latter might have been stopped.", + e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), + ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), + this.kii.getKnowledgeBaseId()); + LOG.debug("{}", e); bsFuture = new CompletableFuture(); bsFuture.complete((Void) null); } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java index 8c783d78c..f042d6389 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java @@ -5,7 +5,6 @@ import java.time.Instant; import java.util.Set; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import org.slf4j.Logger; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java index 53fb0c63b..5189fcade 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/runtime/messaging/RemoteKerConnectionManager.java @@ -1,7 +1,5 @@ package eu.knowledge.engine.smartconnector.runtime.messaging; -import java.io.FileInputStream; -import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -10,7 +8,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -24,15 +21,15 @@ import eu.knowledge.engine.smartconnector.api.SmartConnector; import eu.knowledge.engine.smartconnector.api.SmartConnectorConfig; +import eu.knowledge.engine.smartconnector.edc.EdcConnectorProperties; +import eu.knowledge.engine.smartconnector.edc.EdcConnectorService; +import eu.knowledge.engine.smartconnector.edc.InMemoryTokenManager; +import eu.knowledge.engine.smartconnector.edc.Token; import eu.knowledge.engine.smartconnector.runtime.KeRuntime; import eu.knowledge.engine.smartconnector.runtime.messaging.inter_ker.api.NotFoundException; import eu.knowledge.engine.smartconnector.runtime.messaging.inter_ker.api.SmartConnectorManagementApiService; import eu.knowledge.engine.smartconnector.runtime.messaging.inter_ker.model.KnowledgeEngineRuntimeDetails; import eu.knowledge.engine.smartconnector.runtime.messaging.kd.model.KnowledgeEngineRuntimeConnectionDetails; -import eu.knowledge.engine.smartconnector.edc.EdcConnectorProperties; -import eu.knowledge.engine.smartconnector.edc.EdcConnectorService; -import eu.knowledge.engine.smartconnector.edc.InMemoryTokenManager; -import eu.knowledge.engine.smartconnector.edc.Token; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.SecurityContext; diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeBaseImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeBaseImpl.java index 291c5da94..c6bd02293 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeBaseImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeBaseImpl.java @@ -76,8 +76,6 @@ public class KnowledgeBaseImpl implements KnowledgeBase { protected String description; private Phaser readyPhaser; - private CompletableFuture stoppedFuture = new CompletableFuture(); - /** * Using the default reasoner level from the configuration. */ @@ -175,7 +173,6 @@ public void smartConnectorConnectionRestored(SmartConnector aSC) { @Override public void smartConnectorStopped(SmartConnector aSC) { LOG.info(this.name + " smartconnnector stopped"); - this.stoppedFuture.complete(null); } @Override @@ -187,17 +184,14 @@ protected SmartConnector getSC() { return this.sc; } - public void stop() { - this.sc.stop(); + public CompletableFuture stop() { + CompletableFuture future = this.sc.stop(); // remove all KIs this.unregisteredAskKIs.clear(); this.unregisteredAnswerKIs.clear(); this.currentPostKIs.clear(); this.currentReactKIs.clear(); - } - - public CompletableFuture getStopFuture() { - return this.stoppedFuture; + return future; } public void register(AskKnowledgeInteraction anAskKI) { diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeNetwork.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeNetwork.java index 9326f5e1a..d6367526d 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeNetwork.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/util/KnowledgeNetwork.java @@ -78,7 +78,7 @@ private void startAndWaitForReady() { // register our state check Knowledge Interaction on each Smart Connecotr GraphPattern gp = new GraphPattern(this.prefixMapping, - // @formatter:off + // @formatter:off "?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 .", @@ -151,8 +151,8 @@ private String getUpToDateInfo(Map upToDate) { public CompletableFuture stop() { var kbStoppedFutures = new ArrayList>(); this.knowledgeBases.forEach(kb -> { - kb.stop(); - kbStoppedFutures.add(kb.getStopFuture()); + LOG.info("Stopping {}", kb.getKnowledgeBaseName()); + kbStoppedFutures.add(kb.stop()); }); this.knowledgeBases.clear(); return CompletableFuture.allOf(kbStoppedFutures.toArray(new CompletableFuture[kbStoppedFutures.size()])); diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/AdditionForAudienceTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/AdditionForAudienceTest.java index 06449f6df..af568520e 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/AdditionForAudienceTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/AdditionForAudienceTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.jupiter.api.AfterAll; @@ -252,7 +251,7 @@ public void test() throws InterruptedException, ExecutionException { @AfterAll public static void afterAll() throws InterruptedException, ExecutionException, TimeoutException { - kn.stop().get(10, TimeUnit.SECONDS); + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/GeneralizeIfNecessaryTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/GeneralizeIfNecessaryTest.java index d06b54ab2..b440b7442 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/GeneralizeIfNecessaryTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/GeneralizeIfNecessaryTest.java @@ -158,13 +158,9 @@ private void configureFloorplanKb() { } @AfterAll - public static void close() { + public static void close() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", GeneralizeIfNecessaryTest.class.getSimpleName()); - try { - kn.stop().get(); - } catch (InterruptedException | ExecutionException e) { - LOG.error("Stopping the Knowledge Network should succeed: {}", e); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest.java index 682cb1e87..61d4d5843 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest.java @@ -134,13 +134,9 @@ public void test() throws InterruptedException { } @AfterAll - public static void close() { + public static void close() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", NotDesignedToWorkTogetherTest.class.getSimpleName()); - try { - kn.stop().get(); - } catch (InterruptedException | ExecutionException e) { - LOG.error("Stopping the Knowledge Network should succeed: {}", e); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest2.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest2.java index a235f5dd3..eb5657c4d 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest2.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/NotDesignedToWorkTogetherTest2.java @@ -142,13 +142,9 @@ public void test() throws InterruptedException { } @AfterAll - public static void close() { + public static void close() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", NotDesignedToWorkTogetherTest2.class.getSimpleName()); - try { - kn.stop().get(); - } catch (InterruptedException | ExecutionException e) { - LOG.error("Stopping the Knowledge Network should succeed: {}", e); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer.java index 6f7b429cb..638a94b4a 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer.java @@ -32,6 +32,8 @@ public class TestAskAnswer { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -43,7 +45,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -51,7 +53,8 @@ public void testAskAnswer() throws InterruptedException { LOG.info("Waiting for ready..."); - GraphPattern gp1 = new GraphPattern(prefixes, "?a ."); + GraphPattern gp1 = new GraphPattern(prefixes, + "?a ."); CommunicativeAct act1 = new CommunicativeAct(new HashSet<>(Arrays.asList(Vocab.INFORM_PURPOSE)), new HashSet<>(Arrays.asList(Vocab.RETRIEVE_KNOWLEDGE_PURPOSE))); @@ -71,7 +74,6 @@ public void testAskAnswer() throws InterruptedException { }); LOG.info("Registered first AKI"); - GraphPattern gp2 = new GraphPattern(prefixes, "?x ?y."); CommunicativeAct act2 = new CommunicativeAct(new HashSet<>(Arrays.asList(Vocab.INFORM_PURPOSE)), new HashSet<>(Arrays.asList(Vocab.RETRIEVE_KNOWLEDGE_PURPOSE))); @@ -102,7 +104,7 @@ public void testAskAnswer() throws InterruptedException { assertTrue(!b.containsKey("a") && !b.containsKey("c"), "The variable names should follow the graph pattern of the requesting KB."); - + assertEquals("", b.get("x"), "Binding of 'x' is incorrect."); assertEquals("", b.get("y"), "Binding of 'y' is incorrect."); @@ -110,19 +112,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer2.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer2.java index ac16d322f..e8dda3237 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer2.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer2.java @@ -27,6 +27,7 @@ public class TestAskAnswer2 { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { @@ -39,7 +40,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -96,19 +97,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer2.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer3.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer3.java index 064cd7fa6..00cf1358d 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer3.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer3.java @@ -33,6 +33,8 @@ public class TestAskAnswer3 { private static KnowledgeBaseImpl kb3; private static KnowledgeBaseImpl kb4; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -44,7 +46,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -142,31 +144,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer3.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } - - if (kb4 != null) { - kb4.stop(); - } else { - fail("KB4 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer4.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer4.java index 32e4482a8..d6b89e854 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer4.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer4.java @@ -27,6 +27,7 @@ public class TestAskAnswer4 { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { @@ -39,7 +40,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -98,19 +99,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer4.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer5.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer5.java index 9ca5e76cc..c1f166822 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer5.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswer5.java @@ -165,8 +165,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer5.class.getSimpleName()); - kn.stop(); + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerBindingValidity.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerBindingValidity.java index f2eb1e887..c66208305 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerBindingValidity.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerBindingValidity.java @@ -27,6 +27,8 @@ public class TestAskAnswerBindingValidity { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; + @Test public void testAskAnswerInvalidOutgoingBindings() throws InterruptedException { @@ -34,7 +36,7 @@ public void testAskAnswerInvalidOutgoingBindings() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://example.org/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -89,19 +91,8 @@ public void testAskAnswerInvalidOutgoingBindings() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerBindingValidity.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerLargeBindingSets.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerLargeBindingSets.java index e4d543b22..e6621f139 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerLargeBindingSets.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerLargeBindingSets.java @@ -35,6 +35,7 @@ public class TestAskAnswerLargeBindingSets { private static BindingSet kb1BS; private static BindingSet kb2BS; + private static KnowledgeNetwork kn; private static int NR_OF_BINDINGS = 1500; @@ -71,7 +72,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -143,25 +144,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerLargeBindingSets.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerManyKIs.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerManyKIs.java index af2218479..77d100d57 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerManyKIs.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerManyKIs.java @@ -38,6 +38,8 @@ public class TestAskAnswerManyKIs { private String pattern = "?{} ?{}."; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -49,7 +51,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -164,31 +166,8 @@ private String format(Object... objects) { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerManyKIs.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } - - if (kb4 != null) { - kb4.stop(); - } else { - fail("KB4 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerSingleKBMultipleMatchingKIs.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerSingleKBMultipleMatchingKIs.java index 4030376ce..663c854f3 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerSingleKBMultipleMatchingKIs.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerSingleKBMultipleMatchingKIs.java @@ -29,6 +29,8 @@ public class TestAskAnswerSingleKBMultipleMatchingKIs { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -40,7 +42,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -114,20 +116,9 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerSingleKBMultipleMatchingKIs.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerUnregister.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerUnregister.java index 9399e1422..b9a412125 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerUnregister.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerUnregister.java @@ -28,6 +28,8 @@ public class TestAskAnswerUnregister { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -39,7 +41,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -155,19 +157,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerUnregister.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerWithPartialBindings.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerWithPartialBindings.java index 9d7028efd..42c18dc52 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerWithPartialBindings.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskAnswerWithPartialBindings.java @@ -24,6 +24,7 @@ public class TestAskAnswerWithPartialBindings { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; @Test public void testAskAnswer() throws InterruptedException { @@ -32,7 +33,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -124,19 +125,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswerWithPartialBindings.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskPartialMetadata.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskPartialMetadata.java index 22fa80c9e..e5d8da9ce 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskPartialMetadata.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskPartialMetadata.java @@ -23,6 +23,7 @@ public class TestAskPartialMetadata { private static final Logger LOG = LoggerFactory.getLogger(TestAskPartialMetadata.class); + private static KnowledgeNetwork kn; private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; private static KnowledgeBaseImpl kb3; @@ -38,7 +39,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -75,26 +76,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskPartialMetadata.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskRecipientSelector.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskRecipientSelector.java index e4b780355..07481ba64 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskRecipientSelector.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestAskRecipientSelector.java @@ -34,6 +34,8 @@ public class TestAskRecipientSelector { private static KnowledgeBaseImpl kb3; private static KnowledgeBaseImpl kb4; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -45,7 +47,7 @@ public void testAskAnswer() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -143,11 +145,9 @@ public void testAskAnswer() throws InterruptedException { LOG.info("Binding: {}", bind); // Recipient Selector is multiple KBs (kb1 & kb3). - result = kb2.ask( - askKI, - new RecipientSelector(Arrays.asList(kb1.getKnowledgeBaseId(), kb3.getKnowledgeBaseId())), - new BindingSet() - ).get(); + result = kb2.ask(askKI, + new RecipientSelector(Arrays.asList(kb1.getKnowledgeBaseId(), kb3.getKnowledgeBaseId())), + new BindingSet()).get(); bindings = result.getBindings(); kbIds = result.getExchangeInfoPerKnowledgeBase().stream().map(AskExchangeInfo::getKnowledgeBaseId) .collect(Collectors.toSet()); @@ -158,17 +158,14 @@ public void testAskAnswer() throws InterruptedException { assertEquals(2, bindings.size()); assertTrue(bindings.stream() - .allMatch(b -> - (b.get("x").equals("") || b.get("x").equals("")) - && (b.get("y").equals("") || b.get("y").equals("")) - ) - ); - assertTrue(bindings.stream() - .anyMatch(b -> b.get("x").equals("") && b.get("y").equals("")) - ); - assertTrue(bindings.stream() - .anyMatch(b -> b.get("x").equals("") && b.get("y").equals("")) - ); + .allMatch(b -> (b.get("x").equals("") + || b.get("x").equals("")) + && (b.get("y").equals("") + || b.get("y").equals("")))); + assertTrue(bindings.stream().anyMatch(b -> b.get("x").equals("") + && b.get("y").equals(""))); + assertTrue(bindings.stream().anyMatch(b -> b.get("x").equals("") + && b.get("y").equals(""))); // Recipient Selector asks all Knowledge Bases (kb1, kb3, kb4). @@ -197,31 +194,8 @@ public void testAskAnswer() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskRecipientSelector.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } - - if (kb4 != null) { - kb4.stop(); - } else { - fail("KB4 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestComplexGraphPatternMatching.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestComplexGraphPatternMatching.java index 64b48e87c..d7b14cedc 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestComplexGraphPatternMatching.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestComplexGraphPatternMatching.java @@ -43,6 +43,8 @@ public class TestComplexGraphPatternMatching { private static KnowledgeBaseImpl dashboardKB; private static KnowledgeBaseImpl observationsKB; + private static KnowledgeNetwork kn; + @BeforeAll public static void setup() throws InterruptedException, BrokenBarrierException, TimeoutException { } @@ -55,7 +57,7 @@ public void testComplexGraphPattern() throws InterruptedException { prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); prefixes.setNsPrefix("saref", "https://saref.etsi.org/core/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); createDevicesKB(prefixes, kn); createObservationsKB(prefixes, kn); @@ -422,26 +424,9 @@ private void createObservationsKB(PrefixMappingMem prefixes, KnowledgeNetwork kn } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestComplexGraphPatternMatching.class.getSimpleName()); - if (devicesKB != null) { - devicesKB.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (dashboardKB != null) { - - dashboardKB.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (observationsKB != null) { - observationsKB.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestDuplicateBindings.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestDuplicateBindings.java index 2dcd9d94a..ebef2d5fc 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestDuplicateBindings.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestDuplicateBindings.java @@ -202,25 +202,8 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestAskAnswer3.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataFromNormalKnowledgeInteraction.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataFromNormalKnowledgeInteraction.java index a7d3c1fe3..30c18f47f 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataFromNormalKnowledgeInteraction.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataFromNormalKnowledgeInteraction.java @@ -8,6 +8,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -28,11 +29,12 @@ public class TestMetadataFromNormalKnowledgeInteraction { private static final Logger LOG = LoggerFactory.getLogger(TestMetadataFromNormalKnowledgeInteraction.class); public boolean kb2Received = false; + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kb1.setReasonerLevel(1); kn.addKB(kb1); @@ -125,18 +127,8 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReact2.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataKnowledgeInteractionMatching.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataKnowledgeInteractionMatching.java index a4f6dd1e5..0202779d5 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataKnowledgeInteractionMatching.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestMetadataKnowledgeInteractionMatching.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -26,7 +27,7 @@ * the other. */ public class TestMetadataKnowledgeInteractionMatching { - private static final Logger LOG = LoggerFactory.getLogger(TestDynamicSemanticComposition.class); + private static final Logger LOG = LoggerFactory.getLogger(TestMetadataKnowledgeInteractionMatching.class); private static KnowledgeNetwork kn; private static KnowledgeBaseImpl kb1, kb2; @@ -87,7 +88,7 @@ public void testChangedKB() { } @Test - public void testRemovedKB() { + public void testRemovedKB() throws InterruptedException, ExecutionException { ReactKnowledgeInteraction rKI = new ReactKnowledgeInteraction( new CommunicativeAct(new HashSet<>(Arrays.asList(Vocab.INFORM_PURPOSE)), new HashSet<>(Arrays.asList(Vocab.REMOVED_KNOWLEDGE_PURPOSE))), @@ -102,21 +103,11 @@ public void testRemovedKB() { kb2 = new KnowledgeBaseImpl("Kb2"); kn.addKB(kb2); kn.sync(); - - kb2.stop(); } @AfterEach - public void cleanup() { - try { - kb1.stop(); - } catch (IllegalStateException e) { - LOG.error("Stopping a knowledge base should succeed: {}", e.getMessage()); - } - try { - kb2.stop(); - } catch (IllegalStateException e) { - LOG.error("Stopping a knowledge base should succeed. {}", e.getMessage()); - } + public void cleanup() throws InterruptedException, ExecutionException { + LOG.info("Clean up: {}", TestMetadataKnowledgeInteractionMatching.class.getSimpleName()); + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact.java index c6e13f054..7d630127c 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact.java @@ -25,6 +25,7 @@ public class TestPostReact { public boolean kb2Received = false; private static final Logger LOG = LoggerFactory.getLogger(TestPostReact.class); + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -32,7 +33,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -86,20 +87,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReact.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact2.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact2.java index 7159984fb..0c51ec08c 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact2.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact2.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -25,6 +26,7 @@ public class TestPostReact2 { private static final Logger LOG = LoggerFactory.getLogger(TestPostReact2.class); public boolean kb2Received = false, kb3Received = false; + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -32,7 +34,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -110,27 +112,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReact2.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact3.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact3.java index 7187202a8..b3fb23718 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact3.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact3.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -25,6 +26,7 @@ public class TestPostReact3 { private static final Logger LOG = LoggerFactory.getLogger(TestPostReact3.class); public boolean kb2Received = false, kb3Received = false; + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -32,7 +34,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://example.org/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -138,27 +140,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReact3.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact4.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact4.java index cf26c62ab..758288292 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact4.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact4.java @@ -25,6 +25,7 @@ public class TestPostReact4 { public boolean kb2Received = false; private static final Logger LOG = LoggerFactory.getLogger(TestPostReact4.class); + private static KnowledgeNetwork kn; @Test public void testPostReactTimeout() throws InterruptedException { @@ -33,7 +34,7 @@ public void testPostReactTimeout() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -95,21 +96,10 @@ public void testPostReactTimeout() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT); LOG.info("Clean up: {}", TestPostReact4.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactDatatype.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactDatatype.java index e9227adf9..d2ee8d30a 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactDatatype.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactDatatype.java @@ -15,9 +15,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork; import eu.knowledge.engine.smartconnector.api.ExchangeInfo.Status; import eu.knowledge.engine.smartconnector.util.KnowledgeBaseImpl; +import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork; public class TestPostReactDatatype { private static KnowledgeBaseImpl kb1; @@ -26,6 +26,7 @@ public class TestPostReactDatatype { public boolean kb2Received = false; private static final Logger LOG = LoggerFactory.getLogger(TestPostReactDatatype.class); + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -33,7 +34,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -90,20 +91,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReactDatatype.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactLanguageTag.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactLanguageTag.java index a6e3bb490..f7382494e 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactLanguageTag.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactLanguageTag.java @@ -22,6 +22,7 @@ public class TestPostReactLanguageTag { private static KnowledgeBaseImpl kb1; private static KnowledgeBaseImpl kb2; + private static KnowledgeNetwork kn; public boolean kb2Received = false; @@ -33,7 +34,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -90,20 +91,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReactLanguageTag.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactPerformance.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactPerformance.java index 4804f8b38..eebb795fd 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactPerformance.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReactPerformance.java @@ -6,6 +6,7 @@ import java.util.Iterator; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -22,6 +23,7 @@ public class TestPostReactPerformance { private static KnowledgeBaseImpl kb2; private static final Logger LOG = LoggerFactory.getLogger(TestPostReactPerformance.class); + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -29,8 +31,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - // create the network - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -102,20 +103,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostReactPerformance.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostRecipientSelector.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostRecipientSelector.java index 32de69ee5..f7a02aecd 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostRecipientSelector.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostRecipientSelector.java @@ -7,6 +7,7 @@ import static org.junit.jupiter.api.Assertions.fail; import java.util.Iterator; +import java.util.concurrent.ExecutionException; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -24,6 +25,7 @@ public class TestPostRecipientSelector { private static KnowledgeBaseImpl kb3; private static final Logger LOG = LoggerFactory.getLogger(TestPostRecipientSelector.class); + private static KnowledgeNetwork kn; @Test public void testPostReact() throws InterruptedException { @@ -31,7 +33,7 @@ public void testPostReact() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -99,27 +101,9 @@ public void testPostReact() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", TestPostRecipientSelector.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } - - if (kb3 != null) { - - kb3.stop(); - } else { - fail("KB3 should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestReactMetadata.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestReactMetadata.java index fbc52a7df..826e3e465 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestReactMetadata.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestReactMetadata.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.concurrent.ExecutionException; + import org.apache.jena.rdf.model.Model; import org.apache.jena.shared.PrefixMapping; import org.apache.jena.sparql.graph.PrefixMappingMem; @@ -21,6 +23,7 @@ public class TestReactMetadata { private static KnowledgeBaseImpl kb2; private static final Logger LOG = LoggerFactory.getLogger(TestReactMetadata.class); + private static KnowledgeNetwork kn; @Test public void testRequestMetadata() throws InterruptedException { @@ -30,7 +33,7 @@ public void testRequestMetadata() throws InterruptedException { prefixes.setNsPrefix("kb", Vocab.ONTO_URI); prefixes.setNsPrefix("saref", "https://saref.etsi.org/core/"); - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -123,14 +126,8 @@ public BindingSet react(ReactKnowledgeInteraction anRKI, ReactExchangeInfo aReac } @AfterAll - public static void cleanup() { - - if (kb2 != null) { - kb2.stop(); - } - - if (kb1 != null) { - kb1.stop(); - } + public static void cleanup() throws InterruptedException, ExecutionException { + LOG.info("Clean up: {}", TestReactMetadata.class.getSimpleName()); + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestRequestMetadata.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestRequestMetadata.java index ae60b1541..5814dd1a7 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestRequestMetadata.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestRequestMetadata.java @@ -27,6 +27,7 @@ public class TestRequestMetadata { private static KnowledgeBaseImpl kb2; private static final Logger LOG = LoggerFactory.getLogger(TestRequestMetadata.class); + private static KnowledgeNetwork kn; @Test public void testRequestMetadata() throws InterruptedException, ExecutionException, ParseException { @@ -36,7 +37,7 @@ public void testRequestMetadata() throws InterruptedException, ExecutionExceptio prefixes.setNsPrefix("kb", Vocab.ONTO_URI); prefixes.setNsPrefix("saref", "https://saref.etsi.org/core/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); @@ -78,14 +79,8 @@ public void testRequestMetadata() throws InterruptedException, ExecutionExceptio } @AfterAll - public static void cleanup() { - - if (kb1 != null) { - kb1.stop(); - } - - if (kb2 != null) { - kb2.stop(); - } + public static void cleanup() throws InterruptedException, ExecutionException { + LOG.info("Clean up: {}", TestRequestMetadata.class.getSimpleName()); + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/VariableBindingNameTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/VariableBindingNameTest.java index 80fd311a4..b52e869c9 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/VariableBindingNameTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/VariableBindingNameTest.java @@ -26,6 +26,8 @@ class VariableBindingNameTest { private static KnowledgeBaseImpl sensor; private static KnowledgeBaseImpl thermostat; + private static KnowledgeNetwork kn; + @Test void test() { PrefixMappingMem prefixes = new PrefixMappingMem(); @@ -34,8 +36,7 @@ void test() { prefixes.setNsPrefix("ic", "https://w3id.org/knowledge-engine/"); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - // first add the relevant knowledge bases - var kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); sensor = new KnowledgeBaseImpl("temperatureSensor"); kn.addKB(sensor); thermostat = new KnowledgeBaseImpl("thermostat"); @@ -156,19 +157,8 @@ public BindingSet react(ReactKnowledgeInteraction anRKI, ReactExchangeInfo aReac } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { LOG.info("Clean up: {}", VariableBindingNameTest.class.getSimpleName()); - if (sensor != null) { - sensor.stop(); - } else { - fail("Sensor should not be null!"); - } - - if (thermostat != null) { - - thermostat.stop(); - } else { - fail("Thermostat should not be null!"); - } + kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/ConfigurationTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/ConfigurationTest.java index f741c63c4..f4558c679 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/ConfigurationTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/ConfigurationTest.java @@ -169,25 +169,31 @@ public void testConfigWaitForKnowledgeBasePositive() { @Test public void testConfigReasonerLevelOutOfRange() { - var kb = new KnowledgeBaseImpl("kb11"); this.kn.addKB(kb); kb.setReasonerLevel(0); assertThrowsExactly(IllegalArgumentException.class, () -> kb.start()); + LOG.info("after first"); var kbb = new KnowledgeBaseImpl("kb22"); this.kn.addKB(kbb); kbb.setReasonerLevel(6); assertThrowsExactly(IllegalArgumentException.class, () -> kbb.start()); - } - @AfterEach - public void afterTest() { + // less elegant solution to wait for SCs to be ready. KnowledgeNetwork solution + // did not work due to illegal argument exception try { - kn.stop().get(); - } catch (InterruptedException | ExecutionException e) { - fail(); + Thread.sleep(500); + } catch (InterruptedException e) { + LOG.info("{}", e); } + + } + + @AfterEach + public void afterTest() throws InterruptedException, ExecutionException { + LOG.info("Clean up: {}", ConfigurationTest.class.getSimpleName()); + kn.stop().get(); } private void intializeKB1() { @@ -211,7 +217,7 @@ private void intializeKB2() { var bs = new BindingSet(); var b = new Binding(); b.put("p", ""); - b.put("name", "\"Barry Nouwt\""); + b.put("name", "\"Barry NL\""); bs.add(b); try { diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/SmartConnectorRegistrationStressTest.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/SmartConnectorRegistrationStressTest.java index 79a34c199..2e360cf8e 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/SmartConnectorRegistrationStressTest.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/misc/SmartConnectorRegistrationStressTest.java @@ -20,8 +20,8 @@ import eu.knowledge.engine.smartconnector.api.KnowledgeBase; import eu.knowledge.engine.smartconnector.api.SmartConnector; import eu.knowledge.engine.smartconnector.impl.SmartConnectorBuilder; -import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork; import eu.knowledge.engine.smartconnector.util.KnowledgeBaseImpl; +import eu.knowledge.engine.smartconnector.util.KnowledgeNetwork; @Tag("Long") @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -111,7 +111,8 @@ public void smartConnectorStopped(SmartConnector aSC) { } @AfterAll - void cleanup() { - this.kn.stop().join(); + void cleanup() throws InterruptedException, ExecutionException { + LOG.info("Clean up: {}", SmartConnectorRegistrationStressTest.class.getSimpleName()); + this.kn.stop().get(); } } diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/TestRegisterSmartConnectorWithSameId.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/TestRegisterSmartConnectorWithSameId.java index 4fed65a85..425900bf9 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/TestRegisterSmartConnectorWithSameId.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/runtime/messaging/TestRegisterSmartConnectorWithSameId.java @@ -14,27 +14,29 @@ import eu.knowledge.engine.smartconnector.util.KnowledgeBaseImpl; public class TestRegisterSmartConnectorWithSameId { - private static final Logger LOG = LoggerFactory.getLogger(TestRegisterSmartConnectorWithSameId.class); - private Phaser readyPhaser = new Phaser(1); - - @Test + private static final Logger LOG = LoggerFactory.getLogger(TestRegisterSmartConnectorWithSameId.class); + private Phaser readyPhaser = new Phaser(1); + + @Test public void testRegisterSmartConnectorWithSameIdInSameRuntimeThrows() throws InterruptedException { - var kb1 = new KnowledgeBaseImpl("http://example.org/kb1"); - kb1.setPhaser(this.readyPhaser); - kb1.start(); - - var kb1AsWell = new KnowledgeBaseImpl("http://example.org/kb1"); - kb1.setPhaser(this.readyPhaser); - - assertThrows(IllegalArgumentException.class, () -> { - kb1AsWell.start(); - }); - - kb1.stop(); - } - - @Test + LOG.info("Starting testRegisterSameRuntime"); + var kb1 = new KnowledgeBaseImpl("http://example.org/kb1"); + kb1.setPhaser(this.readyPhaser); + kb1.start(); + + var kb1AsWell = new KnowledgeBaseImpl("http://example.org/kb1"); + kb1.setPhaser(this.readyPhaser); + + assertThrows(IllegalArgumentException.class, () -> { + kb1AsWell.start(); + }); + + kb1.stop(); + } + + @Test void testRegisterSmartConnectorWithSameIdInDifferentRuntimeThrows() throws Exception { + LOG.info("Starting testRegisterDifferentRuntime"); assertTrue(NetUtils.portAvailable(8080)); KnowledgeDirectory kd = new KnowledgeDirectory(8080); MessageDispatcher md1 = new MessageDispatcher(8081, new URI("http://localhost:8081"), @@ -48,11 +50,11 @@ void testRegisterSmartConnectorWithSameIdInDifferentRuntimeThrows() throws Excep // this ID is REUSED in both smart connectors! URI kb1Id = new URI("http://example.org/kb1"); - + md1.start(); MockSmartConnector sc1 = new MockSmartConnector(kb1Id); md1.getLocalSmartConnectorConnectionManager().smartConnectorAdded(sc1); - + md2.start(); Thread.sleep(5000); MockSmartConnector sc2 = new MockSmartConnector(kb1Id); From 387d8b7956f0fc57960ddd437a6ee8fd11cc3d4d Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 10 Oct 2025 13:20:10 +0200 Subject: [PATCH 4/5] Merge and improve message handling. Merge branch 'master' into 452-the-delete-sc-operation-of-the-rest-api-should-wait-until-the-kb-is-fully-removed-before-returning --- .../impl/MessageRouterImpl.java | 79 ++++++++++++++----- .../impl/ReasonerProcessor.java | 6 +- .../impl/SmartConnectorImpl.java | 6 ++ .../smartconnector/api/TestPostReact5.java | 18 +---- 4 files changed, 73 insertions(+), 36 deletions(-) diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MessageRouterImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MessageRouterImpl.java index 47ce4b927..f13bfef8f 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MessageRouterImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/MessageRouterImpl.java @@ -3,9 +3,11 @@ import java.io.IOException; import java.net.URI; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -92,29 +94,41 @@ public CompletableFuture sendAskMessage(AskMessage askMessage) th } CompletableFuture future = new CompletableFuture<>(); - // wait maximally WAIT_TIMEOUT for a return message. - int waitInSeconds = this.getWaitTimeout(); - if (waitInSeconds > 0) { - future = future.orTimeout(waitInSeconds, TimeUnit.SECONDS); - } - future.whenComplete((m, e) -> { + this.openAskMessages.remove(askMessage.getMessageId()); if (m == null) if (e != null) if (e instanceof TimeoutException) LOG.error("KB '{}' did not respond within {}s to AskMessage '{}'.", - askMessage.getToKnowledgeBase(), this.getWaitTimeout(), askMessage.getMessageId(), e); + askMessage.getToKnowledgeBase(), this.getWaitTimeout(), askMessage.getMessageId()); + else if (e instanceof CancellationException) + LOG.debug("Waiting for AnswerMessage to AskMessage '{}' was cancelled due to a stopping SC.", + askMessage.getMessageId()); else LOG.error("A {} occurred while sending an AskMessage.", e.getClass().getSimpleName(), e); else LOG.error( "The AnswerMessage future should complete either exceptionally or normally. Not with both AnswerMessage and Exception null."); - this.openAskMessages.remove(askMessage.getMessageId()); }); + // wait maximally WAIT_TIMEOUT for a return message. + int waitInSeconds = this.getWaitTimeout(); + if (waitInSeconds > 0) { + future = future.orTimeout(waitInSeconds, TimeUnit.SECONDS); + } + + // first send and if success add to open messages this.openAskMessages.put(askMessage.getMessageId(), future); - messageDispatcher.send(askMessage); + + try { + messageDispatcher.send(askMessage); + } catch (IOException ioe) { + // cancel future and remove from open messages + this.openAskMessages.remove(askMessage.getMessageId()).cancel(true); + // and re throw + throw ioe; + } LOG.debug("Sent AskMessage: {}", askMessage); @@ -129,28 +143,40 @@ public CompletableFuture sendPostMessage(PostMessage postMessage) } CompletableFuture future = new CompletableFuture<>(); - // wait maximally WAIT_TIMEOUT for a return message. - int waitInSeconds = this.getWaitTimeout(); - if (waitInSeconds > 0) { - future = future.orTimeout(waitInSeconds, TimeUnit.SECONDS); - } - future.whenComplete((m, e) -> { + this.openPostMessages.remove(postMessage.getMessageId()); if (m == null) if (e != null) if (e instanceof TimeoutException) - LOG.error("KB '{}' did not respond within {}s to PostMessage '{}'.", - postMessage.getToKnowledgeBase(), this.getWaitTimeout(), postMessage.getMessageId(), e); + LOG.warn("KB '{}' did not respond within {}s to PostMessage '{}'.", + postMessage.getToKnowledgeBase(), this.getWaitTimeout(), postMessage.getMessageId()); + else if (e instanceof CancellationException) + LOG.debug("Waiting for ReactMessage to PostMessage '{}' was cancelled due to a stopping SC.", + postMessage.getMessageId()); else LOG.error("A {} occurred while sending an PostMessage.", e.getClass().getSimpleName(), e); else LOG.error( "The ReactMessage future should complete either exceptionally or normally. Not with both ReactMessage and Exception null."); - this.openAskMessages.remove(postMessage.getMessageId()); }); + // wait maximally WAIT_TIMEOUT for a return message. + int waitInSeconds = this.getWaitTimeout(); + if (waitInSeconds > 0) { + future = future.orTimeout(waitInSeconds, TimeUnit.SECONDS); + } + + // first send and if success add to open messages this.openPostMessages.put(postMessage.getMessageId(), future); - messageDispatcher.send(postMessage); + + try { + messageDispatcher.send(postMessage); + } catch (IOException ioe) { + // cancel future and remove from open messages + this.openPostMessages.remove(postMessage.getMessageId()).cancel(true); + // and re throw + throw ioe; + } LOG.debug("Sent PostMessage: {}", postMessage); return future; @@ -303,4 +329,19 @@ public void unsetMessageDispatcher() { this.smartConnector.communicationInterrupted(); } + public void stop() { + int i = 0; + + for (CompletableFuture future : new HashSet<>(this.openAskMessages.values())) { + if (future.cancel(true)) + i++; + } + + for (CompletableFuture future : new HashSet<>(this.openPostMessages.values())) { + if (future.cancel(true)) + i++; + } + LOG.debug("MessageRouterImpl stopped. Cancelled {} message(s).", i); + } + } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java index 70df8afc3..117dd2514 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/ReasonerProcessor.java @@ -473,7 +473,7 @@ public CompletableFuture handle( e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), this.kii.getKnowledgeBaseId()); - LOG.debug("", e); + LOG.trace("Detailed error information.", e); bsFuture = new CompletableFuture(); bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet()); } @@ -565,7 +565,7 @@ public CompletableFuture handle( e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), this.kii.getKnowledgeBaseId()); - LOG.debug("", e); + LOG.trace("Detailed error information.", e); bsFuture = new CompletableFuture(); bsFuture.complete(new eu.knowledge.engine.reasoner.api.BindingSet()); } @@ -633,7 +633,7 @@ public CompletableFuture handle(eu.knowledge.engine.reasoner.api.BindingSe e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(), ReasonerProcessor.this.myKnowledgeInteraction.getKnowledgeBaseId(), this.kii.getKnowledgeBaseId()); - LOG.debug("{}", e); + LOG.trace("Detailed error information.", e); bsFuture = new CompletableFuture(); bsFuture.complete((Void) null); } diff --git a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java index f042d6389..e810b08e7 100644 --- a/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java +++ b/smart-connector/src/main/java/eu/knowledge/engine/smartconnector/impl/SmartConnectorImpl.java @@ -462,6 +462,12 @@ public CompletableFuture stop() { CompletableFuture future = this.knowledgeBaseStore.stop(); return future.whenComplete((v, t) -> { + + if (t != null) + LOG.debug("An error occurred while notifying other SCs.", t); + + // stop message router and cancel waiting for response to messages. + this.messageRouter.stop(); this.otherKnowledgeBaseStore.stop(); KeRuntime.localSmartConnectorRegistry().unregister(this); this.myKnowledgeBase.smartConnectorStopped(this); diff --git a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact5.java b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact5.java index 80acffffb..ae0d3434c 100644 --- a/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact5.java +++ b/smart-connector/src/test/java/eu/knowledge/engine/smartconnector/api/TestPostReact5.java @@ -25,6 +25,7 @@ public class TestPostReact5 { public boolean kb2Received = false; private static final Logger LOG = LoggerFactory.getLogger(TestPostReact5.class); + private static KnowledgeNetwork kn; @Test public void testPostReactTimeout() throws InterruptedException { @@ -33,7 +34,7 @@ public void testPostReactTimeout() throws InterruptedException { prefixes.setNsPrefixes(PrefixMapping.Standard); prefixes.setNsPrefix("ex", "https://www.tno.nl/example/"); - KnowledgeNetwork kn = new KnowledgeNetwork(); + kn = new KnowledgeNetwork(); kb1 = new KnowledgeBaseImpl("kb1"); kn.addKB(kb1); kb2 = new KnowledgeBaseImpl("kb2"); @@ -103,21 +104,10 @@ public void testPostReactTimeout() throws InterruptedException { } @AfterAll - public static void cleanup() { + public static void cleanup() throws InterruptedException, ExecutionException { System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT); LOG.info("Clean up: {}", TestPostReact5.class.getSimpleName()); - if (kb1 != null) { - kb1.stop(); - } else { - fail("KB1 should not be null!"); - } - - if (kb2 != null) { - - kb2.stop(); - } else { - fail("KB2 should not be null!"); - } + kn.stop().get(); } } From 000c4408fc05f318ca1ed757a8270c2028e636ee Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 10 Oct 2025 14:06:32 +0200 Subject: [PATCH 5/5] Improve REST API stopping of KB/SC. --- .../engine/rest/api/impl/RestKnowledgeBase.java | 13 ++++++++++--- .../rest/api/impl/RestKnowledgeBaseManager.java | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBase.java b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBase.java index ec501fe0d..df7a9a9fd 100644 --- a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBase.java +++ b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBase.java @@ -724,6 +724,7 @@ public void smartConnectorStopped(SmartConnector aSC) { LOG.info("canceled inactivity timer for {} because its smart connector stopped.", this.getKnowledgeBaseId()); } + LOG.debug("SC of KB '{}' has stopped.", this.knowledgeBaseId); } private void cancelAsyncResponse() { @@ -743,15 +744,21 @@ public void stop() { if (this.hasAsyncResponse()) this.cancelAsyncResponse(); this.cancelInactivityTimeout(); - this.sc.stop(); + try { + this.sc.stop().get(); + } catch (InterruptedException | ExecutionException e) { + LOG.error("An error occurred while stopping SC of KB '{}'.", this.knowledgeBaseId); + } this.cancelAndClearAllHandleRequests(); } private void cancelAndClearAllHandleRequests() { List cancelledRequests = this.toBeProcessedHandleRequests.stream() .map(HandleRequest::getHandleRequestId).toList(); - LOG.warn("KB with id " + this.knowledgeBaseId - + " has stopped. The following handle requests will be cancelled: " + cancelledRequests); + + if (cancelledRequests.size() > 0) + LOG.warn("KB with id " + this.knowledgeBaseId + + " has stopped. The following handle requests will be cancelled: " + cancelledRequests); String cancelMessage = "KB with id " + this.knowledgeBaseId + " will no longer respond, because it stopped."; this.toBeProcessedHandleRequests.forEach(hr -> { diff --git a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBaseManager.java b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBaseManager.java index 5b4f70065..563d8e100 100644 --- a/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBaseManager.java +++ b/smart-connector-rest-server/src/main/java/eu/knowledge/engine/rest/api/impl/RestKnowledgeBaseManager.java @@ -122,7 +122,7 @@ public boolean deleteKB(String knowledgeBaseId) { rkb.stop(); success = true; this.restKnowledgeBases.remove(knowledgeBaseId); - LOG.info("Removed KB {}", knowledgeBaseId); + LOG.debug("Removed KB {}", knowledgeBaseId); } catch (RuntimeException e) { LOG.error("Could not stop knowledge base {}, so it stays in the list of knowledge bases", knowledgeBaseId); LOG.error("Encountered exception while stopping knowledge base", e);