SpringBoot整合Drools的實現(xiàn)步驟
更新時間:2021年05月25日 15:25:04 作者:youcong
Drools是一個易于訪問企業(yè)策略、易于調(diào)整以及易于管理的開源業(yè)務(wù)規(guī)則引擎,符合業(yè)內(nèi)標(biāo)準(zhǔn),速度快、效率高。業(yè)務(wù)分析師或?qū)徍巳藛T可以利用它輕松查看業(yè)務(wù)規(guī)則,從而檢驗是否已編碼的規(guī)則執(zhí)行所需的業(yè)務(wù)規(guī)則。本文將講述SpringBoot整合Drools的步驟
Drools有什么用
從我個人所待過的公司,其中做智能酒店這個項目時就用到規(guī)則引擎Drools,將它用于處理優(yōu)惠劵規(guī)則。
SpringBoot整合Drools初步實戰(zhàn)
1.導(dǎo)入Maven依賴
<properties>
<drools.version>7.14.0.Final</drools.version>
</properties>
<!-- drools -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools.version}</version>
</dependency>
2.編寫配置類
package com.springcloud.blog.admin.config;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.IOException;
@Configuration
public class KiaSessionConfig {
private static final String RULES_PATH = "rules/";
@Bean
public KieFileSystem kieFileSystem() throws IOException {
KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
for (Resource file : getRuleFiles()) {
kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_PATH + file.getFilename(), "UTF-8"));
}
return kieFileSystem;
}
private Resource[] getRuleFiles() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
final Resource[] resources = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
return resources;
}
@Bean
public KieContainer kieContainer() throws IOException {
final KieRepository kieRepository = getKieServices().getRepository();
kieRepository.addKieModule(new KieModule() {
public ReleaseId getReleaseId() {
return kieRepository.getDefaultReleaseId();
}
});
KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem());
kieBuilder.buildAll();
return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId());
}
private KieServices getKieServices() {
return KieServices.Factory.get();
}
@Bean
public KieBase kieBase() throws IOException {
return kieContainer().getKieBase();
}
@Bean
public KieSession kieSession() throws IOException {
return kieContainer().newKieSession();
}
}
3.resources目錄新建rules目錄
4.新建實體
package com.springcloud.blog.admin.drools;
public class People {
private int sex;
private String name;
private String drlType;
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDrlType() {
return drlType;
}
public void setDrlType(String drlType) {
this.drlType = drlType;
}
}
5.編寫規(guī)則文件
package com.springcloud.blog.admin.drools
import com.springcloud.blog.admin.drools.People
dialect "java"
rule "man"
when
$p : People(sex == 1 && drlType == "people")
then
System.out.println($p.getName() + "是男孩");
end
6.單元測試(只要正常輸出,表示整合是Ok的,接下來就可以任意應(yīng)用了)
package com.springcloud.blog.base.controller.test.task;
import com.springcloud.blog.admin.BlogAdminApplication;
import com.springcloud.blog.admin.drools.People;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BlogAdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DroolsJunitTest {
@Autowired
private KieSession session;
@Test
public void people() {
People people = new People();
people.setName("YC");
people.setSex(1);
people.setDrlType("people");
session.insert(people);//插入
session.fireAllRules();//執(zhí)行規(guī)則
}
}
7.輸出結(jié)果
YC是男孩
以上就是SpringBoot整合Drools的實現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Drools的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud集成MybatisPlus實現(xiàn)MySQL多數(shù)據(jù)源配置方法
本文詳細(xì)介紹了SpringCloud集成MybatisPlus實現(xiàn)MySQL多數(shù)據(jù)源配置的方法,包括在application.properties中配置多數(shù)據(jù)源,配置MybatisPlus,創(chuàng)建Mapper接口和使用多數(shù)據(jù)源等步驟,此外,還解釋了每一個配置項目的含義,以便讀者更好地理解和應(yīng)用2024-10-10
淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul
這篇文章主要介紹了淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測)
這篇文章主要介紹了IDEA 單元測試創(chuàng)建方法詳解(2020.03版本親測),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Java中l(wèi)ist集合為空或為null的區(qū)別說明
這篇文章主要介紹了Java中l(wèi)ist集合為空或為null的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

