新聞中心
本文小編為大家詳細(xì)介紹“SpringBoot2中如何整合Drools規(guī)則引擎”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringBoot2中如何整合Drools規(guī)則引擎”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),建水企業(yè)網(wǎng)站建設(shè),建水品牌網(wǎng)站建設(shè),網(wǎng)站定制,建水網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,建水網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。一、Drools引擎簡(jiǎn)介
1、基礎(chǔ)簡(jiǎn)介
Drools是一個(gè)基于java的規(guī)則引擎,開(kāi)源的,可以將復(fù)雜多變的規(guī)則從硬編碼中解放出來(lái),以規(guī)則腳本的形式存放在文件中,使得規(guī)則的變更不需要修正代碼重啟機(jī)器就可以立即在線上環(huán)境生效。具有易于訪問(wèn)企業(yè)策略、易于調(diào)整以及易于管理的特點(diǎn),作為開(kāi)源業(yè)務(wù)規(guī)則引擎,符合業(yè)內(nèi)標(biāo)準(zhǔn),速度快、效率高。
2、規(guī)則語(yǔ)法
(1)、演示drl文件格式
package droolRule ; import org.slf4j.Logger import org.slf4j.LoggerFactory ; dialect "java" rule "paramcheck1" when then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 規(guī)則引擎") ; LOGGER.info("參數(shù)"); end
(2)、語(yǔ)法說(shuō)明
· 文件格式
可以 .drl、xml文件,也可以Java代碼塊硬編碼;
· package
規(guī)則文件中,package是必須定義的,必須放在規(guī)則文件第一行;
· import
規(guī)則文件使用到的外部變量,可以是一個(gè)類(lèi),也可以是類(lèi)中的可訪問(wèn)的靜態(tài)方法;
· rule
定義一個(gè)規(guī)則。paramcheck1規(guī)則名。規(guī)則通常包含三個(gè)部分:屬性、條件、結(jié)果;
二、整合SpringBoot框架
1、項(xiàng)目結(jié)構(gòu)
SpringBoot2 整合 Drools規(guī)則引擎,實(shí)現(xiàn)高效的業(yè)務(wù)規(guī)則
2、核心依賴
org.drools drools-core 7.6.0.Final org.drools drools-compiler 7.6.0.Final org.drools drools-templates 7.6.0.Final org.kie kie-api 7.6.0.Final org.kie kie-spring 7.6.0.Final
3、配置文件
@Configuration public class RuleEngineConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineConfig.class) ; private static final String RULES_PATH = "droolRule/"; private final KieServices kieServices = KieServices.Factory.get(); @Bean public KieFileSystem kieFileSystem() throws IOException { KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*"); String path = null; for (Resource file : files) { path = RULES_PATH + file.getFilename(); LOGGER.info("path="+path); kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8")); } return kieFileSystem; } @Bean public KieContainer kieContainer() throws IOException { KieRepository kieRepository = kieServices.getRepository(); kieRepository.addKieModule(kieRepository::getDefaultReleaseId); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return kieServices.newKieContainer(kieRepository.getDefaultReleaseId()); } @Bean public KieBase kieBase() throws IOException { return kieContainer().getKieBase(); } @Bean public KieSession kieSession() throws IOException { return kieContainer().newKieSession(); } @Bean public KModuleBeanFactoryPostProcessor kiePostProcessor() { return new KModuleBeanFactoryPostProcessor(); } }
這樣環(huán)境整合就完成了。
三、演示案例
1、規(guī)則文件
規(guī)則一
dialect "java" rule "paramcheck1" salience 99 when queryParam : QueryParam(paramId != null && paramSign.equals("+")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-one 規(guī)則引擎") ; LOGGER.info("參數(shù):getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeAddRule(queryParam); resultParam.setPostCodeResult(true); end
規(guī)則二
dialect "java" rule "paramcheck2" salience 88 when queryParam : QueryParam(paramId != null && paramSign.equals("-")) resultParam : RuleResult() then final Logger LOGGER = LoggerFactory.getLogger("param-check-two 規(guī)則引擎") ; LOGGER.info("參數(shù):getParamId="+queryParam.getParamId()+";getParamSign="+queryParam.getParamSign()); RuleEngineServiceImpl ruleEngineService = new RuleEngineServiceImpl() ; ruleEngineService.executeRemoveRule(queryParam); resultParam.setPostCodeResult(true); end
規(guī)則說(shuō)明:
A、salience 的值越大,越優(yōu)先執(zhí)行;
B、規(guī)則流程:如果paramId不為null,參數(shù)標(biāo)識(shí)是+號(hào),執(zhí)行添加規(guī)則,-號(hào),執(zhí)行移除規(guī)則操作。
2、規(guī)則執(zhí)行代碼
@Service public class RuleEngineServiceImpl implements RuleEngineService { private static final Logger LOGGER = LoggerFactory.getLogger(RuleEngineServiceImpl.class) ; @Override public void executeAddRule(QueryParam param) { LOGGER.info("參數(shù)數(shù)據(jù):"+param.getParamId()+";"+param.getParamSign()); ParamInfo paramInfo = new ParamInfo() ; paramInfo.setId(param.getParamId()); paramInfo.setParamSign(param.getParamSign()); paramInfo.setCreateTime(new Date()); paramInfo.setUpdateTime(new Date()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; paramInfoService.insertParam(paramInfo); } @Override public void executeRemoveRule(QueryParam param) { LOGGER.info("參數(shù)數(shù)據(jù):"+param.getParamId()+";"+param.getParamSign()); ParamInfoService paramInfoService = (ParamInfoService)SpringContextUtil.getBean("paramInfoService") ; ParamInfo paramInfo = paramInfoService.selectById(param.getParamId()); if (paramInfo != null){ paramInfoService.removeById(param.getParamId()) ; } } }
3、規(guī)則調(diào)用接口
@RestController @RequestMapping("/rule") public class RuleController { @Resource private KieSession kieSession; @Resource private RuleEngineService ruleEngineService ; @RequestMapping("/param") public void param (){ QueryParam queryParam1 = new QueryParam() ; queryParam1.setParamId("1"); queryParam1.setParamSign("+"); QueryParam queryParam2 = new QueryParam() ; queryParam2.setParamId("2"); queryParam2.setParamSign("-"); // 入?yún)? kieSession.insert(queryParam1) ; kieSession.insert(queryParam2) ; kieSession.insert(this.ruleEngineService) ; // 返參 RuleResult resultParam = new RuleResult() ; kieSession.insert(resultParam) ; kieSession.fireAllRules() ; } }
讀到這里,這篇“SpringBoot2中如何整合Drools規(guī)則引擎”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:SpringBoot2中如何整合Drools規(guī)則引擎-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://fisionsoft.com.cn/article/ceggjj.html