SpringBoot整合Drools的實(shí)現(xiàn)步驟
Drools有什么用
從我個(gè)人所待過的公司,其中做智能酒店這個(gè)項(xiàng)目時(shí)就用到規(guī)則引擎Drools,將它用于處理優(yōu)惠劵規(guī)則。
SpringBoot整合Drools初步實(shí)戰(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.新建實(shí)體
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.單元測(cè)試(只要正常輸出,表示整合是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的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Drools的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法
本文詳細(xì)介紹了SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置的方法,包括在application.properties中配置多數(shù)據(jù)源,配置MybatisPlus,創(chuàng)建Mapper接口和使用多數(shù)據(jù)源等步驟,此外,還解釋了每一個(gè)配置項(xiàng)目的含義,以便讀者更好地理解和應(yīng)用2024-10-10淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul
這篇文章主要介紹了淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10IDEA 單元測(cè)試創(chuàng)建方法詳解(2020.03版本親測(cè))
這篇文章主要介紹了IDEA 單元測(cè)試創(chuàng)建方法詳解(2020.03版本親測(cè)),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Java中l(wèi)ist集合為空或?yàn)閚ull的區(qū)別說明
這篇文章主要介紹了Java中l(wèi)ist集合為空或?yàn)閚ull的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11