欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 22:49:46
目的 官方的drools范例大都是基于纯java项目或maven项目,而基于spring boot项目的很少。 本文介绍如何在spring boot项目上加上drool...

目的

官方的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();
 }
}

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