Drools+springBoot使用IDEA搭建Hello World demo

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



4.0.0


com.javainuse
boot-drools
1.0.0-SNAPSHOT
jar

boot-drools
Demo project for Spring Boot Drools


	UTF-8
	1.8



	org.springframework.boot
	spring-boot-starter-parent
	1.5.2.RELEASE


	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		org.drools
		drools-core
		7.5.0.Final
	
	
		org.drools
		drools-compiler
		7.5.0.Final
	

	
		org.kie
		kie-api
		7.5.0.Final
	
	
		org.kie
		kie-internal
		7.5.0.Final
	


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

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



    
    
    	
    


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就可以了

 

 

你可能感兴趣的:(Drools+springBoot使用IDEA搭建Hello World demo)