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

View File

@ -11,7 +11,6 @@ public class BackwardChaining {
Result result = new BackwardChaining().backwardChaining();
System.out.println(result.getValue());
result.getFacts()
.stream()
.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.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<String> rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls");
for(String rule:rules){
List<String> 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) {

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.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;

View File

@ -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<CourseSchedule> {
public class ScoreCalculator implements EasyScoreCalculator<CourseSchedule, HardSoftScore> {
@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<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.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;

View File

@ -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;
}

View File

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

View File

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

View File

@ -21,13 +21,12 @@ 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<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfiguration.xml");
Solver<CourseSchedule> solver = solverFactory.buildSolver();
CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule);
@ -38,7 +37,6 @@ public class OptaPlannerUnitTest {
@Test
public void test_whenDroolsSolver() {
SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("courseScheduleSolverConfigDrools.xml");
Solver<CourseSchedule> solver = solverFactory.buildSolver();
CourseSchedule solvedCourseSchedule = solver.solve(unsolvedCourseSchedule);

11
pom.xml
View File

@ -340,7 +340,6 @@
<module>couchbase</module>
<module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
<module>gradle-modules/gradle/maven-to-gradle</module>
@ -594,7 +593,6 @@
<module>core-java-modules</module>
<module>couchbase</module>
<module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
@ -900,12 +898,13 @@
<module>core-java-modules/core-java-networking-3</module>
<module>core-java-modules/core-java-strings</module>
<module>core-java-modules/core-java-httpclient</module>
<module>custom-pmd</module>
<module>custom-pmd</module>
<module>spring-core-6</module>
<module>data-structures</module>
<module>ddd-contexts</module>
<module>deeplearning4j</module>
<module>docker-modules</module>
<module>drools</module>
<module>guava-modules</module>
<module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module>
@ -956,7 +955,7 @@
<module>asm</module>
<module>atomikos</module>
<module>atomix</module>
<!-- <module>axon</module>--><!-- JAVA-18408 -->
<!-- <module>axon</module>--><!-- JAVA-18408 -->
<module>bazel</module>
<module>code-generation</module>
@ -1125,14 +1124,14 @@
<module>core-java-modules/core-java-httpclient</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
<module>custom-pmd</module>
<module>custom-pmd</module>
<module>spring-core-6</module>
<module>data-structures</module>
<module>ddd-contexts</module>
<module>deeplearning4j</module>
<module>jmeter</module>
<module>docker-modules</module>
<module>drools</module>
<module>guava-modules</module>
<module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module>