drools整合springboot(十)

1,创建项目drools-springboot

2, 引入依赖 pom依赖


    4.0.0
    
        org.springframework.boot
        spring-boot-starters
        2.0.6.RELEASE
    
    com.dream21th.drools.study
    drools-springboot
    1.0-SNAPSHOT
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            commons-lang
            commons-lang
            2.6
        
        
        
            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
            
                
                    org.springframework
                    spring-tx
                
                
                    org.springframework
                    spring-beans
                
                
                    org.springframework
                    spring-core
                
                
                    org.springframework
                    spring-context
                
            
            7.6.0.Final
        
    
    
        ${project.artifactId}
        
            
                src/main/java
                
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.*
                
                false
            
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.8
                    1.8
                
            
        
    

3,创建配置文件application.yml

server:
  port: 8080
spring:
  application:
    name: drools_springboot

4,创建DroolConfig.java

package com.dream.drools.config;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.Resource;
import java.io.IOException;
/**
 * 规则引擎配置类
 */
@Configuration
public class DroolsConfig {
    //指定规则文件存放的目录
    private static final String RULES_PATH = "rules/";
    private final KieServices kieServices = KieServices.Factory.get();
    @Bean
    @ConditionalOnMissingBean
    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();
            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
        }
        return kieFileSystem;
    }
    @Bean
    @ConditionalOnMissingBean
    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
    @ConditionalOnMissingBean
    public KieBase kieBase() throws IOException {
        return kieContainer().getKieBase();
    }
    @Bean
    @ConditionalOnMissingBean
    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
        return new KModuleBeanFactoryPostProcessor();
    }
}

5,创建RuleService.java

package com.dream.drools.service;

import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RuleService {
    @Autowired
    private KieBase kieBase;
    public void rule(){
        KieSession kieSession = kieBase.newKieSession();
        kieSession.fireAllRules();
        kieSession.dispose();
    }
}

6,创建HelloController

package com.dream.drools.controller;

import com.dream.drools.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private RuleService ruleService;
    @RequestMapping("/rule")
    public String rule(){
        ruleService.rule();
        return "OK";
    }
}

7,创建启动类

package com.dream.drools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
 * @Author dream21th
 **/

@SpringBootApplication
public class DroolsApplication {
    public static void main(String[] args) {
        SpringApplication.run(DroolsApplication.class,args);
    }
}

8,创建规则文件

package helloworld
rule "rule_helloworld"
    when
        eval(true)
    then
        System.out.println("规则:rule_helloworld触发...");
end

9,启动应用,并在浏览器中输入http://localhost:8080/hello/rule

控制台可以观察到规则输出

 

代码地址: https://gitee.com/dream21th/drools-study/tree/master/drools-springboot

你可能感兴趣的:(drools规则引擎)