Drools与Spring boot集成简单DEMO-初步

本文主要介绍如何将Drools集成到Spring boot。


1、修改pom文件

增加drools maven依赖:


   org.drools
   drools-core
   7.0.0.Final


   org.drools
   drools-compiler
   7.0.0.Final


   org.drools
   drools-decisiontables
   7.0.0.Final


   org.drools
   drools-templates
   7.0.0.Final



   org.kie
   kie-api
   7.0.0.Final

2、添加Resources

创建META-INF和rules目录

在META-INF目录下创建文件kmodule.xml,内容如下:

xmlns="http://jboss.org/kie/6.0.0/kmodule">
    name="rules" packages="rules">
        name="ksession-rules"/>
    

在rules目录下创建文件helloworld.drl文件,该文件为简单的规则定义,内容如下:

package com.yanli
import com.com.yanli.domain.Message
dialect  "mvel"

rule "Hello World"
    dialect "mvel"
    when
        m : Message(status.equals(Message.HELLO), message : message )
    then
        System.out.println( message);
    modify ( m ) { message = "Goodbye cruel world",status = Message.GOODBYE };
end

rule "Good Bye"
    dialect "java"
    when
       Message( status == Message.GOODBYE, message : message )
    then
        System.out.println( message );
end

3、domain层创建一个规则的model

package com.yanli.domain;

/**
 * Created with IntelliJ IDEA
 * User: chenyanli
 * Date: 2018/5/30
 * Time: 下午6:29
 * Description:
 */
public class Message {

    public static final Integer HELLO = 0;
    public static final Integer GOODBYE = 1;

    private String message;

    private Integer status;

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

}

4、Service层负责实际运行规则

package com.yanli.service;

import com.yanli.domain.Message;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA
 * User: chenyanli
 * Date: 2018/5/30
 * Time: 下午6:33
 * Description:
 */

@Service
@Transactional
public class DroolsService {

    public String fireRule() {
        // load up the knowledge base
        KieServices ks = KieServices.Factory.get();
        KieContainer kContainer = ks.getKieClasspathContainer();
        KieSession kSession = kContainer.newKieSession("ksession-rules");

        // go !
        Message message = new Message();
//        message.setMessage("Hello World");
//        message.setStatus(Message.HELLO);
        message.setMessage("Goodbye");
        message.setStatus(Message.GOODBYE);
        kSession.insert(message);//插入
        kSession.fireAllRules();//执行规则
        kSession.dispose();
        return message.getMessage();
    }

}

5、Conroller层将其封装为一个Restful API

package com.yanli.controller;

import com.yanli.service.DroolsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA
 * User: chenyanli
 * Date: 2018/5/30
 * Time: 下午6:36
 * Description:
 */

@RestController
@RequestMapping("/drools")
public class DroolsController {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DroolsService droolsService;

    @RequestMapping(value="",method= {RequestMethod.GET})
    public String verifyToken(HttpServletRequest request) {
        String message = droolsService.fireRule();
        logger.info(message);
        return message;
    }
}
启动Spring boot就可以通过接口方式执行我们定义的Drools规则了。

你可能感兴趣的:(Drools与Spring boot集成简单DEMO-初步)