Skip to content

Commit

Permalink
[kie-issues#1154] Make KieSession auto-closeable (apache#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
baldimir authored and rgdoliveira committed May 9, 2024
1 parent 2c952f3 commit 31a3538
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ public void testLargeCompiledAlphaNetwork() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> 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<Object> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,18 @@ public void testMixedConstraints() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> 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<Object> 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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ public void testMultipleIndexedConstraintTest() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> 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<Object> 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();
}
}

Expand Down
11 changes: 10 additions & 1 deletion kie-api/src/main/java/org/kie/api/runtime/KieSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public interface KieSession
StatefulRuleSession,
StatefulProcessSession,
CommandExecutor,
KieRuntime {
KieRuntime,
AutoCloseable {

/**
* Deprecated. use {@link #getIdentifier()} instead
Expand All @@ -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
Expand Down

0 comments on commit 31a3538

Please sign in to comment.