springboot 2.x 集成 drools 7.x

简介:springboot 2.0.4.RELEASE 集成 drools 7.11.0.Final

1.项目结构

springboot 2.x 集成 drools 7.x_第1张图片

2. pom.xml文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>AllTestartifactId>
        <groupId>com.lccgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>drools_springbootartifactId>

    <properties>
        <drools.version>7.11.0.Finaldrools.version>
        <spring.boot.version>2.0.4.RELEASEspring.boot.version>
    properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>${spring.boot.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
            <version>${spring.boot.version}version>
        dependency>


        <dependency>
            <groupId>org.kiegroupId>
            <artifactId>kie-springartifactId>
            <version>${drools.version}version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>spring-txartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>spring-beansartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>spring-coreartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>spring-contextartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.droolsgroupId>
            <artifactId>drools-compilerartifactId>
            <version>${drools.version}version>
        dependency>


        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.12.6version>
        dependency>

    dependencies>


project>

3.配置类

基于springboot的初始化配置:

package com.drools.spring;

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.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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * Created by lcc on 2018/9/27.
 */
@Configuration
public class DroolsAutoConfiguration {

    /** 特别要注意 rule的存放位置  */
    private static final String RULES_PATH = "rules/";

    @Bean
    @ConditionalOnMissingBean(KieFileSystem.class)
    public KieFileSystem kieFileSystem() throws IOException {
        KieFileSystem kieFileSystem = getKieServices().newKieFileSystem();
        for (Resource file : getRuleFiles()) {
            String path = RULES_PATH + file.getFilename();
            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
        }
        return kieFileSystem;
    }

    /**
     * 这里要引入 org.springframework.core.io.Resource  包
     *
     * @return
     * @throws IOException
     */
    private Resource[] getRuleFiles() throws IOException {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "**/*.*");
    }

    @Bean
    @ConditionalOnMissingBean(KieContainer.class)
    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
    @ConditionalOnMissingBean(KieBase.class)
    public KieBase kieBase() throws IOException {
        return kieContainer().getKieBase();
    }

    @Bean
    @ConditionalOnMissingBean(KieSession.class)
    public KieSession kieSession() throws IOException {
        return kieContainer().newKieSession();
    }

    @Bean
    @ConditionalOnMissingBean(KModuleBeanFactoryPostProcessor.class)
    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
        return new KModuleBeanFactoryPostProcessor();
    }
}

4.Springboot启动类

package com.drools.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by lcc on 2018/9/27.
 */
@SpringBootApplication
public class SpringBootDroolsApplication {

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

5.实体类

package com.drools.spring.entity;

import lombok.Data;

/**
 * Created by lcc on 2018/9/27.
 */
@Data
public class Address {

    private String postcode;

    private String street;

    private String state;

}

6.规则返回结果类

package com.drools.spring.entity;

import lombok.Data;

/**
 * Created by lcc on 2018/9/27.
 */
@Data
public class AddressCheckResult {

    private boolean postCodeResult = false; // true:通过校验;false:未通过校验
}

7.规则文件

注意存放的位置

package com.rules

import com.drools.spring.entity.Address;
import com.drools.spring.entity.AddressCheckResult;

rule "Postcode should be filled with exactly 5 numbers"

    when
        address : Address(postcode != null, postcode matches "([0-9]{5})")
        checkResult : AddressCheckResult();
    then
        checkResult.setPostCodeResult(true);
        System.out.println("规则中打印日志:校验通过!");
end

8.规则文件kmodule


<kmodule xmlns="http://www.drools.org/xsd/kmodule">
    <kbase name="rules" packages="com.rules">
        <ksession name="ksession-rule"/>
    kbase>
kmodule>

9.测试Controller

package com.drools.spring.controller;

import com.drools.spring.entity.Address;
import com.drools.spring.entity.AddressCheckResult;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by lcc on 2018/9/27.
 */
@RequestMapping("/test")
@Controller
public class TestController {

    @Resource
    private KieSession kieSession;

    @ResponseBody
    @RequestMapping("/address")
    public void test(){
        Address address = new Address();
        address.setPostcode("99425");

        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert(address);
        kieSession.insert(result);
        int ruleFiredCount = kieSession.fireAllRules();
        System.out.println("触发了" + ruleFiredCount + "条规则");

        if(result.isPostCodeResult()){
            System.out.println("规则校验通过");
        }

    }
}

启动Springboot项目之后,默认访问http://localhost:8080/test/address 即可。
运行结果

规则中打印日志:校验通过!
触发了1条规则
规则校验通过

你可能感兴趣的:(规则引擎-CEP,规则引擎)