初识Drools(二): Drools 之 SpringBoot

新启一个SpringBoot项目


项目目录如下:

初识Drools(二): Drools 之 SpringBoot_第1张图片


加入依赖:

6.3.0.Final

	
		org.springframework
		spring-tx
	
	
		org.springframework
		spring-beans
	
	
		org.springframework
		spring-core
	
	
		org.springframework
		spring-context
	


编写配置:

package com.example.myDroolsDemo.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.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;

/**
 * drools配置文件
 *
 * @author chenhao
 * @version 1.0.0
 * @since 1.0.0
 * 

* Created at 2018/12/18 */ @Configuration public class DroolsAutoConfiguration { private static final String RULES_PATH = "rules/"; @Bean @ConditionalOnMissingBean(KieFileSystem.class) 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(); return resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*"); } @Bean @ConditionalOnMissingBean(KieContainer.class) public KieContainer kieContainer() throws IOException { final KieRepository kieRepository = getKieServices().getRepository(); kieRepository.addKieModule(() -> 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(); } }

其中因为我将drl文件放在了 rules 文件夹下,所以给出了这样的路径,也可以直接放在 resources 文件下修改一下配置中的路径即可

初识Drools(二): Drools 之 SpringBoot_第2张图片


编写POJO:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private String name;
    private Integer age;
    private String address;
    private Integer high;
    private Integer householdIncome;
    private Integer family;
}

编写返回类:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentResult {
    private int fraction;
    private int worth;
}

编写简单规则:

package rules


import com.example.myDroolsDemo.pojo.Student;
import com.example.myDroolsDemo.fact.StudentResult
/**
 * 规则
 *
 * @author chenhao
 * @version 1.0.0
 * @since 1.0.0
 *
 * Created at 2018/12/18
 */

rule "checkAge"
    when
        student : Student(age < 20)
        studentResult: StudentResult();
    then
        studentResult.setFraction(1);
end

rule "checkHigh"
    when
        student : Student(high < 185)
        studentResult : StudentResult();
    then
        studentResult.setFraction(studentResult.getFraction()+1);
end

rule "checkFamily"
    when
        student : Student(family < 2)
        studentResult : StudentResult();
    then
         studentResult.setFraction(studentResult.getFraction()+1);
    end

rule "final"
    when
        studentResult : StudentResult(fraction>3);
    then
        studentResult.setWorth(100);
end

编写 Controller 层:

@RequestMapping("/test")
@Controller
public class CheckController {
    @Resource
    private KieSession kieSession;

    @RequestMapping("/check")
    @ResponseBody
    public void checkTest() {
        Student student = new Student();
        StudentResult sr = new StudentResult();
        student.setAge(10);
        student.setFamily(1);
        student.setHigh(100);
        kieSession.insert(student);
        kieSession.insert(sr);
        int i = kieSession.fireAllRules();
        System.out.println("触发了" + i + "条规则");

        System.out.println(sr.getWorth());

    }
}

运行后 访问 http://localhost:8080/test/check 

查看控制台

你可能感兴趣的:(其他,springBoot,drools)