Merge pull request #13493 from timis1/JAVA-18133

JAVA-18133 Upgrade drools module to JDK 11
This commit is contained in:
Loredana Crusoveanu 2023-02-24 16:16:20 +02:00 committed by GitHub
commit 70d2e9431c
11 changed files with 51 additions and 56 deletions

View File

@ -8,9 +8,8 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-spring-4</artifactId> <artifactId>parent-modules</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../parent-spring-4</relativePath>
</parent> </parent>
<dependencies> <dependencies>
@ -57,10 +56,10 @@
</dependencies> </dependencies>
<properties> <properties>
<http-component-version>4.4.6</http-component-version> <http-component-version>4.4.16</http-component-version>
<drools-version>7.4.1.Final</drools-version> <drools-version>8.32.0.Final</drools-version>
<apache-poi-version>3.13</apache-poi-version> <apache-poi-version>5.2.3</apache-poi-version>
<opta-planner-version>7.10.0.Final</opta-planner-version> <opta-planner-version>8.32.0.Final</opta-planner-version>
</properties> </properties>
</project> </project>

View File

@ -11,7 +11,6 @@ public class BackwardChaining {
Result result = new BackwardChaining().backwardChaining(); Result result = new BackwardChaining().backwardChaining();
System.out.println(result.getValue()); System.out.println(result.getValue());
result.getFacts() result.getFacts()
.stream()
.forEach(System.out::println); .forEach(System.out::println);
} }

View File

@ -10,26 +10,25 @@ import org.kie.internal.builder.DecisionTableConfiguration;
import org.kie.internal.builder.DecisionTableInputType; import org.kie.internal.builder.DecisionTableInputType;
import org.kie.internal.builder.KnowledgeBuilderFactory; import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory; import org.kie.internal.io.ResourceFactory;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
public class DroolsBeanFactory { public class DroolsBeanFactory {
private static final String RULES_PATH = "com/baeldung/drools/rules/"; 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(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
List<String> rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls"); List<String> rules = Arrays.asList("BackwardChaining.drl", "SuggestApplicant.drl", "Product_rules.xls");
for(String rule:rules){ for(String rule:rules) {
kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); kieFileSystem.write(ResourceFactory.newClassPathResource(rule));
} }
return kieFileSystem; return kieFileSystem;
} }
public KieContainer getKieContainer() throws IOException { public KieContainer getKieContainer() {
getKieRepository(); getKieRepository();
KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem()); KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
@ -44,14 +43,10 @@ public class DroolsBeanFactory {
private void getKieRepository() { private void getKieRepository() {
final KieRepository kieRepository = kieServices.getRepository(); final KieRepository kieRepository = kieServices.getRepository();
kieRepository.addKieModule(new KieModule() { kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
} }
public KieSession getKieSession(){ public KieSession getKieSession() {
getKieRepository(); getKieRepository();
KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); 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/SuggestApplicant.drl"));
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll(); kb.buildAll();
KieModule kieModule = kb.getKieModule(); KieModule kieModule = kb.getKieModule();
@ -67,7 +61,6 @@ public class DroolsBeanFactory {
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer.newKieSession(); return kContainer.newKieSession();
} }
public KieSession getKieSession(Resource dt) { public KieSession getKieSession(Resource dt) {

View File

@ -6,7 +6,7 @@ import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty; import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore; import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution; 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.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -2,14 +2,13 @@ package com.baeldung.drools.optaplanner;
import java.util.HashSet; import java.util.HashSet;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore; 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<CourseSchedule> { public class ScoreCalculator implements EasyScoreCalculator<CourseSchedule, HardSoftScore> {
@Override @Override
public Score calculateScore(CourseSchedule courseSchedule) { public HardSoftScore calculateScore(CourseSchedule courseSchedule) {
int hardScore = 0; int hardScore = 0;
int softScore = 0; int softScore = 0;
@ -27,6 +26,6 @@ public class ScoreCalculator implements EasyScoreCalculator<CourseSchedule> {
} }
} }
return HardSoftScore.valueOf(hardScore, softScore); return HardSoftScore.of(hardScore, softScore);
} }
} }

View File

@ -3,19 +3,20 @@ package com.baeldung.drools.service;
import com.baeldung.drools.config.DroolsBeanFactory; import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Applicant; import com.baeldung.drools.model.Applicant;
import com.baeldung.drools.model.SuggestedRole; import com.baeldung.drools.model.SuggestedRole;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
import java.io.IOException;
public class ApplicantService { public class ApplicantService {
KieSession kieSession=new DroolsBeanFactory().getKieSession(); KieSession kieSession = new DroolsBeanFactory().getKieSession();
public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) throws IOException { public SuggestedRole suggestARoleForApplicant(Applicant applicant,SuggestedRole suggestedRole) {
try {
kieSession.insert(applicant); kieSession.insert(applicant);
kieSession.setGlobal("suggestedRole",suggestedRole); kieSession.setGlobal("suggestedRole", suggestedRole);
kieSession.fireAllRules(); kieSession.fireAllRules();
} finally {
kieSession.dispose();
}
System.out.println(suggestedRole.getRole()); System.out.println(suggestedRole.getRole());
return suggestedRole; return suggestedRole;

View File

@ -2,16 +2,19 @@ package com.baeldung.drools.service;
import com.baeldung.drools.config.DroolsBeanFactory; import com.baeldung.drools.config.DroolsBeanFactory;
import com.baeldung.drools.model.Product; import com.baeldung.drools.model.Product;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
public class ProductService { public class ProductService {
private KieSession kieSession=new DroolsBeanFactory().getKieSession(); private KieSession kieSession = new DroolsBeanFactory().getKieSession();
public Product applyLabelToProduct(Product product){ public Product applyLabelToProduct(Product product){
try {
kieSession.insert(product); kieSession.insert(product);
kieSession.fireAllRules(); kieSession.fireAllRules();
} finally {
kieSession.dispose();
}
System.out.println(product.getLabel()); System.out.println(product.getLabel());
return product; return product;

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<solver> <solver>
<scanAnnotatedClasses/>
<solutionClass>com.baeldung.drools.optaplanner.CourseSchedule</solutionClass>
<entityClass>com.baeldung.drools.optaplanner.Lecture</entityClass>
<scoreDirectorFactory> <scoreDirectorFactory>
<scoreDrl>courseSchedule.drl</scoreDrl> <scoreDrl>courseSchedule.drl</scoreDrl>

View File

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<solver> <solver>
<scanAnnotatedClasses/>
<solutionClass>com.baeldung.drools.optaplanner.CourseSchedule</solutionClass>
<entityClass>com.baeldung.drools.optaplanner.Lecture</entityClass>
<scoreDirectorFactory> <scoreDirectorFactory>
<easyScoreCalculatorClass>com.baeldung.drools.optaplanner.ScoreCalculator</easyScoreCalculatorClass> <easyScoreCalculatorClass>com.baeldung.drools.optaplanner.ScoreCalculator</easyScoreCalculatorClass>

View File

@ -21,13 +21,12 @@ public class OptaPlannerUnitTest {
unsolvedCourseSchedule.getLectureList().add(new Lecture()); unsolvedCourseSchedule.getLectureList().add(new Lecture());
} }
unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(new Integer[] { 1, 2, 3 })); unsolvedCourseSchedule.getPeriodList().addAll(Arrays.asList(1, 2, 3));
unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(new Integer[] { 1, 2 })); unsolvedCourseSchedule.getRoomList().addAll(Arrays.asList(1, 2));
} }
@Test @Test
public void test_whenCustomJavaSolver() { public void test_whenCustomJavaSolver() {
SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml"); SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml");
Solver<CourseSchedule> solver = solverFactory.buildSolver(); Solver<CourseSchedule> solver = solverFactory.buildSolver();
CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule);
@ -38,7 +37,6 @@ public class OptaPlannerUnitTest {
@Test @Test
public void test_whenDroolsSolver() { public void test_whenDroolsSolver() {
SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml"); SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml");
Solver<CourseSchedule> solver = solverFactory.buildSolver(); Solver<CourseSchedule> solver = solverFactory.buildSolver();
CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule); CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule);

View File

@ -340,7 +340,6 @@
<module>couchbase</module> <module>couchbase</module>
<module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 --> <!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<!-- <module>gradle-modules</module> --> <!-- Not a maven project --> <!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
<module>gradle-modules/gradle/maven-to-gradle</module> <module>gradle-modules/gradle/maven-to-gradle</module>
@ -594,7 +593,6 @@
<module>core-java-modules</module> <module>core-java-modules</module>
<module>couchbase</module> <module>couchbase</module>
<module>drools</module> <module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 --> <!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<!-- <module>gradle-modules</module> --> <!-- Not a maven project --> <!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
@ -906,6 +904,7 @@
<module>ddd-contexts</module> <module>ddd-contexts</module>
<module>deeplearning4j</module> <module>deeplearning4j</module>
<module>docker-modules</module> <module>docker-modules</module>
<module>drools</module>
<module>guava-modules</module> <module>guava-modules</module>
<module>apache-httpclient-2</module> <module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module> <module>kubernetes-modules/kubernetes-spring</module>
@ -1125,7 +1124,6 @@
<module>core-java-modules/core-java-httpclient</module> <module>core-java-modules/core-java-httpclient</module>
<module>spring-aop</module> <module>spring-aop</module>
<module>spring-aop-2</module> <module>spring-aop-2</module>
<module>custom-pmd</module> <module>custom-pmd</module>
<module>spring-core-6</module> <module>spring-core-6</module>
<module>data-structures</module> <module>data-structures</module>
@ -1133,6 +1131,7 @@
<module>deeplearning4j</module> <module>deeplearning4j</module>
<module>jmeter</module> <module>jmeter</module>
<module>docker-modules</module> <module>docker-modules</module>
<module>drools</module>
<module>guava-modules</module> <module>guava-modules</module>
<module>apache-httpclient-2</module> <module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module> <module>kubernetes-modules/kubernetes-spring</module>