Spring Boot+Drools规则引擎整合详解

 更新时间:2017年09月30日 09:11:14   作者:Dem  
本篇文章主要介绍了Spring Boot+Drools规则引擎整合,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

目的

官方的Drools范例大都是基于纯Java项目或Maven项目,而基于Spring Boot项目的很少。

本文介绍如何在Spring Boot项目上加上Drools规则引擎。

POM依赖

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.galaxyyao</groupId>
 <artifactId>springbootdroolstest1</artifactId>
 <version>1.0.0</version>
 <packaging>jar</packaging>

 <name>springbootdroolstest1</name>
 <url>http://maven.apache.org</url>

 <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>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.drools</groupId>
   <artifactId>drools-core</artifactId>
   <version>7.0.0.Final</version>
  </dependency>
  <dependency>
   <groupId>org.drools</groupId>
   <artifactId>drools-compiler</artifactId>
   <version>7.0.0.Final</version>
  </dependency>
  <dependency>
   <groupId>org.drools</groupId>
   <artifactId>drools-decisiontables</artifactId>
   <version>7.0.0.Final</version>
  </dependency>
  <dependency>
   <groupId>org.drools</groupId>
   <artifactId>drools-templates</artifactId>
   <version>7.0.0.Final</version>
  </dependency>

  <dependency>
   <groupId>org.kie</groupId>
   <artifactId>kie-api</artifactId>
   <version>7.0.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>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <configuration>
     <executable>java</executable>
     <arguments>
      <argument>com.galaxyyao.springbootdroolstest1.SpringBootDroolsTest1Application</argument>
     </arguments>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

其中比较需要注意的是使用了exec-maven-plugin插件,不然无法启动

引用的Drools包的版本可以自行调整。

Resources

在src/main/resources下添加两个目录:

META-INF和rules

META-INF下添加kmodule.xml,内容如下:

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

rules目录下添加一个简单的Hello World规则

package com.galaxyyao.springbootdroolstest1
import com.galaxyyao.springbootdroolstest1.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

Domain层

Domain层定义一个Drools中需要使用到的Model:

package com.galaxyyao.springbootdroolstest1.domain;

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;
 }
}

Service层

Service层负责实际运行规则:

package com.galaxyyao.springbootdroolstest1.service;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

import com.galaxyyao.springbootdroolstest1.domain.Message;

@Service
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);
  kSession.insert(message);//插入
  kSession.fireAllRules();//执行规则
  kSession.dispose();
  return message.getMessage();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论