栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Drools(6):Spring Boot整合Drools

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Drools(6):Spring Boot整合Drools

下面我们演示如何将Drools整合到springboot中

目录结构如下:

第⼀步:创建maven⼯程drools_springboot并配置pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.7.2
		 
	
	com.example
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
		
			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
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.boot
			spring-boot-starter-web
			RELEASE
			compile
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


第⼆步:创建/resources/application.yml⽂件

server:
  port: 8088
spring:
  application:
    name: spring-boot-drools

第三步:创建规则⽂件/resources/rules/score-rules.drl

package rules
import com.example.demo.entity.Order

//规则1:100元以下, 不加分
rule "score_1"
when
    $s : Order(amout <= 100)
then
    $s.setScore(0);
    System.out.println("成功匹配到规则1:100元以下, 不加分 ");
end

//规则2:100元-500元 加100分
rule "score_2"
when
    $s : Order(amout > 100 && amout <= 500)
then
    $s.setScore(100);
    System.out.println("成功匹配到规则2:100元-500元 加100分 ");
end

//规则3:500元-1000元 加500分
rule "score_3"
when
    $s : Order(amout > 500 && amout <= 1000)
then
    $s.setScore(500);
    System.out.println("成功匹配到规则3:500元-1000元 加500分 ");
end

//规则4:1000元 以上 加1000分
rule "score_4"
when
    $s : Order(amout > 1000)
then
    $s.setScore(1000);
    System.out.println("成功匹配到规则4:1000元 以上 加1000分 ");
end

第四步:编写配置类DroolsConfig

package com.example.demo.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.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();
    }
}

第五步:创建RuleService类

package com.example.demo.service;

import com.example.demo.entity.Order;
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 Order executeOrderRule(Order order) {
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(order);
        kieSession.fireAllRules();
        kieSession.dispose();
        return order;
    }

}

第六步:创建OrderController类

package com.example.demo.controller;

import com.example.demo.entity.Order;
import com.example.demo.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    private RuleService ruleService;

    @GetMapping("/saveOrder")
    public Order saveOrder(Order order) {
        System.out.println("hello,world");
        return ruleService.executeOrderRule(order);
    }
}

第七步:新建order.html页面,发送ajax请求(将⻚⾯放在src/main/resources/static下,可以在浏 览器直接访问,这是springboot默认的静态资源⽂件夹)




    
    订单⻚⾯
    
    



    
订单⾦额:
该订单可以累计的积分:

第⼋步:启动服务,访问http://localhost:8088/order.html

输入对应的数字

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031334.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号