Merge pull request #14783 from Bipinkumar27/JAVA-18763

JAVA-18763 : Changes made for Update Spring Drools article
This commit is contained in:
Kasra Madadipouya 2023-09-24 23:33:43 +02:00 committed by GitHub
commit b172cbf69b
1 changed files with 16 additions and 31 deletions

View File

@ -1,8 +1,15 @@
package com.baeldung.drools.config; package com.baeldung.drools.config;
import java.util.Arrays;
import java.util.List;
import org.drools.decisiontable.DecisionTableProviderImpl; import org.drools.decisiontable.DecisionTableProviderImpl;
import org.kie.api.KieServices; import org.kie.api.KieServices;
import org.kie.api.builder.*; import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.io.Resource; import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
@ -10,35 +17,19 @@ 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.util.Arrays;
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() { private KieFileSystem getKieFileSystem() {
KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
List<String> rules = Arrays.asList("BackwardChaining.drl", "SuggestApplicant.drl", "Product_rules.drl.xls"); List<String> rules = Arrays.asList("com/baeldung/drools/rules/BackwardChaining.drl", "com/baeldung/drools/rules/SuggestApplicant.drl", "com/baeldung/drools/rules/Product_rules.drl.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() {
getKieRepository();
KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
kb.buildAll();
KieModule kieModule = kb.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId());
return kContainer;
} }
private void getKieRepository() { private void getKieRepository() {
@ -47,20 +38,14 @@ public class DroolsBeanFactory {
} }
public KieSession getKieSession() { public KieSession getKieSession() {
getKieRepository(); KieBuilder kb = kieServices.newKieBuilder(getKieFileSystem());
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/BackwardChaining.drl"));
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl"));
kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.drl.xls"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll(); kb.buildAll();
KieModule kieModule = kb.getKieModule();
KieContainer kContainer = kieServices.newKieContainer(kieModule.getReleaseId()); KieRepository kieRepository = kieServices.getRepository();
ReleaseId krDefaultReleaseId = kieRepository.getDefaultReleaseId();
KieContainer kieContainer = kieServices.newKieContainer(krDefaultReleaseId);
return kContainer.newKieSession(); return kieContainer.newKieSession();
} }
public KieSession getKieSession(Resource dt) { public KieSession getKieSession(Resource dt) {