Drools+springBoot使用IDEA搭建Hello World demo

首先我们先进入idea创建maven项目,然后选择spring项目然后点击完成,在resources目录下创建META-INF文件夹和rules文件夹,然后点击pom文件进行输入下面内容


<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">
<modelVersion>4.0.0</modelVersion>

<!--项目组织,包名,版本,包类型,-->
<groupId>com.javainuse</groupId>
<artifactId>boot-drools</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!--项目名字,项目描述-->
<name>boot-drools</name>
<description>Demo project for Spring Boot Drools</description>
<!--项目格式,jdk版本-->
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<java.version>1.8</java.version>
</properties>
<!--父依赖关系-->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
	<!-- springBoot lib -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<!-- drools lib -->
	<dependency>
		<groupId>org.drools</groupId>
		<artifactId>drools-core</artifactId>
		<version>7.5.0.Final</version>
	</dependency>
	<dependency>
		<groupId>org.drools</groupId>
		<artifactId>drools-compiler</artifactId>
		<version>7.5.0.Final</version>
	</dependency>

	<dependency>
		<groupId>org.kie</groupId>
		<artifactId>kie-api</artifactId>
		<version>7.5.0.Final</version>
	</dependency>
	<dependency>
		<groupId>org.kie</groupId>
		<artifactId>kie-internal</artifactId>
		<version>7.5.0.Final</version>
	</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
		</plugin>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
</project>

然后对项目进行构建,建立model,controller,service包分别放实体,API,加载规则引擎的服务操作;在META-INF文件夹下面建立kmodule.xml,同时在rules文件夹下面建立rules.drl文件分别在两个文件里面写入下面的内容;

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!--定位规则,同时以rulesSession作为该规则入口(目前是这样理解的,理解有错误请留言互相讨论一下,后面再改)-->
    <kbase name="rules" packages="rules">
    	<ksession name="rulesSession" />
    </kbase>

</kmodule>
package rules
 
import com.javainuse.model.Product
/*规则名字*/
rule "Offer for Diamond"
	when
	/*规则条件,里面出来的结果只能是ture或者false*/
		productObject: Product(type=="diamond")
	then
		productObject.setDiscount(15);
	end
rule "Offer for Gold"
	when 
	/*对productObject进行参数绑定,当条件满足时*/
		productObject: Product(type=="gold")
	then
	/*对productObject进行操作*/
		productObject.setDiscount(25);
	end

分别在 model,controller,service包下面建立Product,JewelleryShopController,JewelleryShopService类,代码在下面

package com.javainuse.model;

/*Product实体,有类型与折扣两个类型,有代码洁癖的可以使用lombok进行简化*/
public class Product {

	private String type;
	private int discount;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getDiscount() {
		return discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

}
package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.javainuse.model.Product;
import com.javainuse.service.JewelleryShopService;

@RestController
public class JewelleryShopController {

	private final JewelleryShopService jewelleryShopService;

	@Autowired
	public JewelleryShopController(JewelleryShopService jewelleryShopService) {
		this.jewelleryShopService = jewelleryShopService;
	}
	/*暴露出来的api接口,通过捕获type=进行后续规则*/
	@RequestMapping(value = "/getDiscount", method = RequestMethod.GET, produces = "application/json")
	public Product getQuestions(@RequestParam(required = true) String type) {

		Product product = new Product();
		product.setType(type);

		jewelleryShopService.getProductDiscount(product);

		return product;
	}

}

 

package com.javainuse.service;

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

import com.javainuse.model.Product;

@Service
public class JewelleryShopService {

	/*这个类好像是配合Drools使用的加载kmodul.xml规则*/
	private final KieContainer kieContainer;

	@Autowired
	public JewelleryShopService(KieContainer kieContainer) {
		this.kieContainer = kieContainer;
	}

	public Product getProductDiscount(Product product) {
		KieSession kieSession = kieContainer.newKieSession("rulesSession");
		kieSession.insert(product);
		kieSession.fireAllRules();
		kieSession.dispose();
		return product;
	}
}

除了这个以外,还要对SpringBootDroolsHelloWorldApp进行添加KieContainer的注入,代码如下

package com.javainuse;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootDroolsHelloWorldApp {

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

	}
	/*需要注入这个类到bean工厂*/
	@Bean
	public KieContainer kieContainer() {
		return KieServices.Factory.get().getKieClasspathContainer();
	}

}

 启动maven,下载相对应的包,然后启动项目就可以了,然后访问http://localhost:8080/getDiscount?type=gold就可以了

 

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值