From 31a3538f738659595e178b37ae004bb588611215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tibor=20Zim=C3=A1nyi?= Date: Tue, 7 May 2024 12:36:07 +0200 Subject: [PATCH] [kie-issues#1154] Make KieSession auto-closeable (#5890) --- .../ancompiler/AlphaNetworkCompilerTest.java | 5 +---- .../ancompiler/LargeAlphaNetworkTest.java | 21 ++++++++----------- .../ancompiler/MixedConstraintsTest.java | 21 ++++++++----------- .../MultipleIndexableConstraintsTest.java | 21 ++++++++----------- .../java/org/kie/api/runtime/KieSession.java | 11 +++++++++- 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/AlphaNetworkCompilerTest.java b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/AlphaNetworkCompilerTest.java index 2a7b1c483a9..4491f39eace 100644 --- a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/AlphaNetworkCompilerTest.java +++ b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/AlphaNetworkCompilerTest.java @@ -453,12 +453,9 @@ public void testAlphaConstraintNagate() { "then\n" + "end"; - KieSession ksession = getKieSession(str); - try { + try (KieSession ksession = getKieSession(str)) { ksession.insert(new Person("Mario", 45)); assertThat(ksession.fireAllRules()).isEqualTo(0); - } finally { - ksession.dispose(); } } diff --git a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/LargeAlphaNetworkTest.java b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/LargeAlphaNetworkTest.java index 39cf059f7f4..ce1bb10992a 100644 --- a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/LargeAlphaNetworkTest.java +++ b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/LargeAlphaNetworkTest.java @@ -43,21 +43,18 @@ public void testLargeCompiledAlphaNetwork() { rule.append(ruleWithIndex(i)); } - KieSession ksession = getKieSession(rule.toString()); - ArrayList results = new ArrayList<>(); - ksession.setGlobal("results", results); - Person a = new Person("a", 1); - Person b = new Person("b", 0); - Person c = new Person("a", 7); - ksession.insert(a); - ksession.insert(b); - ksession.insert(c); + try (KieSession ksession = getKieSession(rule.toString())) { + ArrayList results = new ArrayList<>(); + ksession.setGlobal("results", results); + Person a = new Person("a", 1); + Person b = new Person("b", 0); + Person c = new Person("a", 7); + ksession.insert(a); + ksession.insert(b); + ksession.insert(c); - try { ksession.fireAllRules(); assertThat(results).contains(a, b, c); - } finally { - ksession.dispose(); } } diff --git a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MixedConstraintsTest.java b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MixedConstraintsTest.java index 86a5691de79..ac6321acca5 100644 --- a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MixedConstraintsTest.java +++ b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MixedConstraintsTest.java @@ -46,21 +46,18 @@ public void testMixedConstraints() { rule.append(ruleWithIndex(i)); } - KieSession ksession = getKieSession(rule.toString()); - ArrayList results = new ArrayList<>(); - ksession.setGlobal("results", results); - Person a = new Person("a", 1); - Person b = new Person("b", 0); - Person c = new Person("a", 7); - ksession.insert(a); - ksession.insert(b); - ksession.insert(c); + try (KieSession ksession = getKieSession(rule.toString())) { + ArrayList results = new ArrayList<>(); + ksession.setGlobal("results", results); + Person a = new Person("a", 1); + Person b = new Person("b", 0); + Person c = new Person("a", 7); + ksession.insert(a); + ksession.insert(b); + ksession.insert(c); - try { ksession.fireAllRules(); assertThat(results).contains(a, b, c); - } finally { - ksession.dispose(); } } diff --git a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MultipleIndexableConstraintsTest.java b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MultipleIndexableConstraintsTest.java index 9f5765d3b5e..431c4bce881 100644 --- a/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MultipleIndexableConstraintsTest.java +++ b/drools-alphanetwork-compiler/src/test/java/org/drools/ancompiler/MultipleIndexableConstraintsTest.java @@ -45,21 +45,18 @@ public void testMultipleIndexedConstraintTest() { rule.append(ruleWithIndex(i)); } - KieSession ksession = getKieSession(rule.toString()); - ArrayList results = new ArrayList<>(); - ksession.setGlobal("results", results); - Person a = new Person("a", 1); - Person b = new Person("b", 0); - Person c = new Person("a", 7); - ksession.insert(a); - ksession.insert(b); - ksession.insert(c); + try (KieSession ksession = getKieSession(rule.toString())) { + ArrayList results = new ArrayList<>(); + ksession.setGlobal("results", results); + Person a = new Person("a", 1); + Person b = new Person("b", 0); + Person c = new Person("a", 7); + ksession.insert(a); + ksession.insert(b); + ksession.insert(c); - try { ksession.fireAllRules(); assertThat(results).contains(a, b, c); - } finally { - ksession.dispose(); } } diff --git a/kie-api/src/main/java/org/kie/api/runtime/KieSession.java b/kie-api/src/main/java/org/kie/api/runtime/KieSession.java index 7c6267f377e..13d6d4ad10f 100644 --- a/kie-api/src/main/java/org/kie/api/runtime/KieSession.java +++ b/kie-api/src/main/java/org/kie/api/runtime/KieSession.java @@ -94,7 +94,8 @@ public interface KieSession StatefulRuleSession, StatefulProcessSession, CommandExecutor, - KieRuntime { + KieRuntime, + AutoCloseable { /** * Deprecated. use {@link #getIdentifier()} instead @@ -114,6 +115,14 @@ public interface KieSession */ void dispose(); + /** + * Disposes the KieSession when used as AutoClosable. Wrapper method that calls {@link #dispose()}. + * To see more details, please see documentation on the method {@link #dispose()}. + */ + @Override + default void close() { + dispose(); + } /** * Destroys session permanently. In case of session state being persisted in data store