欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot2整合Drools規(guī)則引擎及案例詳解

 更新時(shí)間:2019年10月23日 08:31:28   作者:知了一笑  
這篇文章主要介紹了SpringBoot2整合Drools規(guī)則引擎及案例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、Drools引擎簡介

1、基礎(chǔ)簡介

Drools是一個(gè)基于java的規(guī)則引擎,開源的,可以將復(fù)雜多變的規(guī)則從硬編碼中解放出來,以規(guī)則腳本的形式存放在文件中,使得規(guī)則的變更不需要修正代碼重啟機(jī)器就可以立即在線上環(huán)境生效。具有易于訪問企業(yè)策略、易于調(diào)整以及易于管理的特點(diǎn),作為開源業(yè)務(wù)規(guī)則引擎,符合業(yè)內(nèi)標(biāo)準(zhǔn),速度快、效率高。

2、規(guī)則語法

(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)、語法說明

· 文件格式

可以 .drl、xml文件,也可以Java代碼塊硬編碼;

· package

規(guī)則文件中,package是必須定義的,必須放在規(guī)則文件第一行;

· import

規(guī)則文件使用到的外部變量,可以是一個(gè)類,也可以是類中的可訪問的靜態(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、核心依賴

<!--drools規(guī)則引擎-->
<dependency>
  <groupId>org.drools</groupId>
  <artifactId>drools-core</artifactId>
  <version>7.6.0.Final</version>
</dependency>
<dependency>
  <groupId>org.drools</groupId>
  <artifactId>drools-compiler</artifactId>
  <version>7.6.0.Final</version>
</dependency>
<dependency>
  <groupId>org.drools</groupId>
  <artifactId>drools-templates</artifactId>
  <version>7.6.0.Final</version>
</dependency>
<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-api</artifactId>
  <version>7.6.0.Final</version>
</dependency>
<dependency>
  <groupId>org.kie</groupId>
  <artifactId>kie-spring</artifactId>
  <version>7.6.0.Final</version>
</dependency>

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ī)則說明:

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() ;
  }
}

這樣,完整的案例就結(jié)束了。

四、源代碼地址

GitHub·地址

https://github.com/cicadasmile/middle-ware-parent

GitEE·地址

https://gitee.com/cicadasmile/middle-ware-parent

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring開發(fā)中自定義注解的使用詳解

    Spring開發(fā)中自定義注解的使用詳解

    這篇文章主要介紹了Spring開發(fā)中自定義注解的使用詳解,在Java項(xiàng)目中,可以自定義注解,方便進(jìn)行某些處理操作,提供開發(fā)效率,需要的朋友可以參考下
    2024-01-01
  • Minio與SpringBoot使用okhttp3問題解決

    Minio與SpringBoot使用okhttp3問題解決

    這篇文章主要介紹了Minio與SpringBoot使用okhttp3問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Jmeter測(cè)試必知的名詞及環(huán)境搭建

    Jmeter測(cè)試必知的名詞及環(huán)境搭建

    我們本章開始學(xué)習(xí)Jmeter,后續(xù)還會(huì)有RF以及LoadRunner 的介紹,為什么要學(xué)習(xí)Jmeter,它主要是用來做性能測(cè)試的,其中它也需要間接或直接的需要用到抓包工具
    2021-09-09
  • JDK8新特性-java.util.function-Function接口使用

    JDK8新特性-java.util.function-Function接口使用

    這篇文章主要介紹了JDK8新特性-java.util.function-Function接口使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot如何使用TestEntityManager進(jìn)行JPA集成測(cè)試

    SpringBoot如何使用TestEntityManager進(jìn)行JPA集成測(cè)試

    TestEntityManager是Spring Framework提供的一個(gè)測(cè)試框架,它可以幫助我們進(jìn)行 JPA 集成測(cè)試,在本文中,我們將介紹如何使用 TestEntityManager 進(jìn)行 JPA 集成測(cè)試,感興趣的跟著小編一起來學(xué)習(xí)吧
    2023-06-06
  • springboot項(xiàng)目配置context path失效的問題解決

    springboot項(xiàng)目配置context path失效的問題解決

    本文主要介紹了springboot項(xiàng)目配置context path失效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 淺談Mybatis二級(jí)緩存的缺陷

    淺談Mybatis二級(jí)緩存的缺陷

    本文主要介紹了淺談Mybatis二級(jí)緩存的缺陷,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 如何通過RabbitMq實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)詳解

    如何通過RabbitMq實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)詳解

    工作中經(jīng)常會(huì)有定時(shí)任務(wù)的需求,常見的做法可以使用Timer、Quartz、Hangfire等組件,這次想嘗試下新的思路,使用RabbitMQ死信隊(duì)列的機(jī)制來實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于如何通過RabbitMq實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java對(duì)象的內(nèi)存布局全流程

    Java對(duì)象的內(nèi)存布局全流程

    這篇文章主要介紹了Java對(duì)象的內(nèi)存布局全流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 關(guān)于Java中static關(guān)鍵字的用法

    關(guān)于Java中static關(guān)鍵字的用法

    這篇文章主要介紹了關(guān)于Java中static關(guān)鍵字的用法,static:意為靜態(tài)的,在?Java?里面作為靜態(tài)修飾符,可以理解為全局的意思,static?不僅可以修飾成員變量,成員方法,還可以修飾代碼塊,需要的朋友可以參考下
    2023-08-08

最新評(píng)論