From 433877fd7084a6f469922cbfd9433e5dc76a3f6a Mon Sep 17 00:00:00 2001 From: timis1 Date: Fri, 17 Feb 2023 22:09:14 +0200 Subject: [PATCH 1/4] JAVA-18133 Upgrade drools module to JDK 11 --- drools/pom.xml | 8 +++---- .../backward_chaining/BackwardChaining.java | 1 - .../drools/config/DroolsBeanFactory.java | 21 +++++++------------ .../drools/optaplanner/CourseSchedule.java | 2 +- .../drools/optaplanner/ScoreCalculator.java | 9 ++++---- .../drools/service/ApplicantService.java | 17 ++++++++------- .../drools/service/ProductService.java | 13 +++++++----- .../optaplanner/OptaPlannerUnitTest.java | 9 ++++---- pom.xml | 4 ++-- 9 files changed, 40 insertions(+), 44 deletions(-) diff --git a/drools/pom.xml b/drools/pom.xml index daaf1fa8a7..ff121d7838 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -57,10 +57,10 @@ - 4.4.6 - 7.4.1.Final - 3.13 - 7.10.0.Final + 4.4.16 + 8.32.0.Final + 5.2.3 + 8.32.0.Final \ No newline at end of file diff --git a/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java b/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java index 6f15ee510b..3baabbeb0f 100644 --- a/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java +++ b/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java @@ -11,7 +11,6 @@ public class BackwardChaining { Result result = new BackwardChaining().backwardChaining(); System.out.println(result.getValue()); result.getFacts() - .stream() .forEach(System.out::println); } diff --git a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java index cf5d56f246..bbc1f3170a 100644 --- a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java +++ b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java @@ -10,26 +10,25 @@ import org.kie.internal.builder.DecisionTableConfiguration; import org.kie.internal.builder.DecisionTableInputType; import org.kie.internal.builder.KnowledgeBuilderFactory; import org.kie.internal.io.ResourceFactory; -import java.io.IOException; import java.util.Arrays; import java.util.List; public class DroolsBeanFactory { private static final String RULES_PATH = "com/baeldung/drools/rules/"; - private KieServices kieServices=KieServices.Factory.get(); + private KieServices kieServices = KieServices.Factory.get(); - private KieFileSystem getKieFileSystem() throws IOException{ + private KieFileSystem getKieFileSystem() { KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); - List rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls"); - for(String rule:rules){ + List rules = Arrays.asList("BackwardChaining.drl", "SuggestApplicant.drl", "Product_rules.xls"); + for(String rule:rules) { kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); } return kieFileSystem; } - public KieContainer getKieContainer() throws IOException { + public KieContainer getKieContainer() { getKieRepository(); KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem()); @@ -44,14 +43,10 @@ public class DroolsBeanFactory { private void getKieRepository() { final KieRepository kieRepository = kieServices.getRepository(); - kieRepository.addKieModule(new KieModule() { - public ReleaseId getReleaseId() { - return kieRepository.getDefaultReleaseId(); - } - }); + kieRepository.addKieModule(kieRepository::getDefaultReleaseId); } - public KieSession getKieSession(){ + public KieSession getKieSession() { getKieRepository(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); @@ -59,7 +54,6 @@ public class DroolsBeanFactory { kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl")); kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); - KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); kb.buildAll(); KieModule kieModule = kb.getKieModule(); @@ -67,7 +61,6 @@ public class DroolsBeanFactory { KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); return kContainer.newKieSession(); - } public KieSession getKieSession(Resource dt) { diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java index 7b2ba117a1..d0ae5318b9 100644 --- a/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/CourseSchedule.java @@ -6,7 +6,7 @@ import java.util.List; import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; import org.optaplanner.core.api.domain.solution.PlanningScore; import org.optaplanner.core.api.domain.solution.PlanningSolution; -import org.optaplanner.core.api.domain.solution.drools.ProblemFactCollectionProperty; +import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty; import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider; import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; import org.slf4j.Logger; diff --git a/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java index 86501cdccd..c9d2b4df99 100644 --- a/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java +++ b/drools/src/main/java/com/baeldung/drools/optaplanner/ScoreCalculator.java @@ -2,14 +2,13 @@ package com.baeldung.drools.optaplanner; import java.util.HashSet; -import org.optaplanner.core.api.score.Score; import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; -import org.optaplanner.core.impl.score.director.easy.EasyScoreCalculator; +import org.optaplanner.core.api.score.calculator.EasyScoreCalculator; -public class ScoreCalculator implements EasyScoreCalculator { +public class ScoreCalculator implements EasyScoreCalculator { @Override - public Score calculateScore(CourseSchedule courseSchedule) { + public HardSoftScore calculateScore(CourseSchedule courseSchedule) { int hardScore = 0; int softScore = 0; @@ -27,6 +26,6 @@ public class ScoreCalculator implements EasyScoreCalculator { } } - return HardSoftScore.valueOf(hardScore, softScore); + return HardSoftScore.of(hardScore, softScore); } } diff --git a/drools/src/main/java/com/baeldung/drools/service/ApplicantService.java b/drools/src/main/java/com/baeldung/drools/service/ApplicantService.java index f74298ef05..06f84358b0 100644 --- a/drools/src/main/java/com/baeldung/drools/service/ApplicantService.java +++ b/drools/src/main/java/com/baeldung/drools/service/ApplicantService.java @@ -3,19 +3,20 @@ package com.baeldung.drools.service; import com.baeldung.drools.config.DroolsBeanFactory; import com.baeldung.drools.model.Applicant; import com.baeldung.drools.model.SuggestedRole; -import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; -import java.io.IOException; - public class ApplicantService { - KieSession kieSession=new DroolsBeanFactory().getKieSession(); + KieSession kieSession = new DroolsBeanFactory().getKieSession(); - public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) throws IOException { - kieSession.insert(applicant); - kieSession.setGlobal("suggestedRole",suggestedRole); - kieSession.fireAllRules(); + public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) { + try { + kieSession.insert(applicant); + kieSession.setGlobal("suggestedRole", suggestedRole); + kieSession.fireAllRules(); + } finally { + kieSession.dispose(); + } System.out.println(suggestedRole.getRole()); return suggestedRole; diff --git a/drools/src/main/java/com/baeldung/drools/service/ProductService.java b/drools/src/main/java/com/baeldung/drools/service/ProductService.java index be5e43ed7b..401f5a600f 100644 --- a/drools/src/main/java/com/baeldung/drools/service/ProductService.java +++ b/drools/src/main/java/com/baeldung/drools/service/ProductService.java @@ -2,18 +2,21 @@ package com.baeldung.drools.service; import com.baeldung.drools.config.DroolsBeanFactory; import com.baeldung.drools.model.Product; -import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; public class ProductService { - private KieSession kieSession=new DroolsBeanFactory().getKieSession(); + private KieSession kieSession = new DroolsBeanFactory().getKieSession(); public Product applyLabelToProduct(Product product){ - kieSession.insert(product); - kieSession.fireAllRules(); + try { + kieSession.insert(product); + kieSession.fireAllRules(); + } finally { + kieSession.dispose(); + } System.out.println(product.getLabel()); - return product; + return product; } diff --git a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java index 1880e30b86..2165cdda1f 100644 --- a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java +++ b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.optaplanner.core.api.solver.Solver; import org.optaplanner.core.api.solver.SolverFactory; +import org.optaplanner.core.config.solver.SolverConfig; public class OptaPlannerUnitTest { @@ -21,14 +22,14 @@ public class OptaPlannerUnitTest { unsolvedCourseSchedule.getLectureList().add(new Lecture()); } - unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(new Integer[] { 1, 2, 3 })); - unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(new Integer[] { 1, 2 })); + unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(1, 2, 3)); + unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(1, 2)); } @Test public void test_whenCustomJavaSolver() { - - SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml"); + SolverConfig solverConfig = SolverConfig.createFromXmlResource("courseScheduleSolverConfiguration.xml"); + SolverFactory solverFactory = SolverFactory.create(solverConfig); Solver solver = solverFactory.buildSolver(); CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); diff --git a/pom.xml b/pom.xml index 8b395d44f1..62c9a36a3c 100644 --- a/pom.xml +++ b/pom.xml @@ -342,7 +342,6 @@ couchbase custom-pmd - drools gradle-modules/gradle/maven-to-gradle @@ -610,7 +609,6 @@ core-java-modules couchbase custom-pmd - drools gradle-modules/gradle/maven-to-gradle @@ -927,6 +925,7 @@ ddd-contexts deeplearning4j docker-modules + drools apache-httpclient-2 kubernetes-modules/kubernetes-spring libraries-concurrency @@ -1136,6 +1135,7 @@ ddd-contexts deeplearning4j docker-modules + drools apache-httpclient-2 kubernetes-modules/kubernetes-spring libraries-concurrency From 1bba7257a4508bf682a1d54dc94019d0462cdbea Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 22 Feb 2023 17:26:33 +0200 Subject: [PATCH 2/4] Update pom.xml --- drools/pom.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drools/pom.xml b/drools/pom.xml index ff121d7838..12aaf1318a 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -8,9 +8,8 @@ com.baeldung - parent-spring-4 - 0.0.1-SNAPSHOT - ../parent-spring-4 + parent-modules + 1.0.0-SNAPSHOT @@ -63,4 +62,4 @@ 8.32.0.Final - \ No newline at end of file + From 583bde3f6ada73872b4e3653b756fd25f7c858bf Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 23 Feb 2023 14:27:31 +0200 Subject: [PATCH 3/4] JAVA-18133 fix optaplanner test --- .../main/resources/courseScheduleSolverConfigDrools.xml | 6 ++++-- .../main/resources/courseScheduleSolverConfiguration.xml | 4 +++- .../baeldung/drools/optaplanner/OptaPlannerUnitTest.java | 5 +---- pom.xml | 8 ++++---- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/drools/src/main/resources/courseScheduleSolverConfigDrools.xml b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml index 7cf95fdcd3..74112d9c7d 100644 --- a/drools/src/main/resources/courseScheduleSolverConfigDrools.xml +++ b/drools/src/main/resources/courseScheduleSolverConfigDrools.xml @@ -1,7 +1,9 @@ - - + + com.baeldung.drools.optaplanner.CourseSchedule + com.baeldung.drools.optaplanner.Lecture + courseSchedule.drl diff --git a/drools/src/main/resources/courseScheduleSolverConfiguration.xml b/drools/src/main/resources/courseScheduleSolverConfiguration.xml index dfedb8f2fd..4214b0943a 100644 --- a/drools/src/main/resources/courseScheduleSolverConfiguration.xml +++ b/drools/src/main/resources/courseScheduleSolverConfiguration.xml @@ -1,7 +1,9 @@ - + com.baeldung.drools.optaplanner.CourseSchedule + com.baeldung.drools.optaplanner.Lecture + com.baeldung.drools.optaplanner.ScoreCalculator diff --git a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java index 2165cdda1f..36b24349c7 100644 --- a/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java +++ b/drools/src/test/java/com/baeldung/drools/optaplanner/OptaPlannerUnitTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.optaplanner.core.api.solver.Solver; import org.optaplanner.core.api.solver.SolverFactory; -import org.optaplanner.core.config.solver.SolverConfig; public class OptaPlannerUnitTest { @@ -28,8 +27,7 @@ public class OptaPlannerUnitTest { @Test public void test_whenCustomJavaSolver() { - SolverConfig solverConfig = SolverConfig.createFromXmlResource("courseScheduleSolverConfiguration.xml"); - SolverFactory solverFactory = SolverFactory.create(solverConfig); + SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml"); Solver solver = solverFactory.buildSolver(); CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); @@ -39,7 +37,6 @@ public class OptaPlannerUnitTest { @Test public void test_whenDroolsSolver() { - SolverFactory solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml"); Solver solver = solverFactory.buildSolver(); CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); diff --git a/pom.xml b/pom.xml index e330fd81c1..bd6c43ad3d 100644 --- a/pom.xml +++ b/pom.xml @@ -904,13 +904,13 @@ core-java-modules/core-java-networking-3 core-java-modules/core-java-strings core-java-modules/core-java-httpclient - custom-pmd + custom-pmd spring-core-6 data-structures ddd-contexts deeplearning4j docker-modules - drools + drools guava-modules apache-httpclient-2 kubernetes-modules/kubernetes-spring @@ -1118,13 +1118,13 @@ core-java-modules/core-java-networking-3 core-java-modules/core-java-strings core-java-modules/core-java-httpclient - custom-pmd + custom-pmd spring-core-6 data-structures ddd-contexts deeplearning4j docker-modules - drools + drools guava-modules apache-httpclient-2 kubernetes-modules/kubernetes-spring From 373f969aa521f5b444af30dc6c12ad0d2cb93db1 Mon Sep 17 00:00:00 2001 From: timis1 Date: Thu, 23 Feb 2023 19:01:37 +0200 Subject: [PATCH 4/4] JAVA-18133 Fix wrong indentation --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c8b6d2833e..49b862a723 100644 --- a/pom.xml +++ b/pom.xml @@ -900,7 +900,7 @@ core-java-modules/core-java-networking-3 core-java-modules/core-java-strings core-java-modules/core-java-httpclient - custom-pmd + custom-pmd spring-core-6 data-structures ddd-contexts @@ -1125,7 +1125,7 @@ core-java-modules/core-java-httpclient spring-aop spring-aop-2 - custom-pmd + custom-pmd spring-core-6 data-structures ddd-contexts