Spring Cloud 2.x之Activiti整合规则引擎Drools

  日常生活是由规则驱动的。红灯停绿灯行,这是我们的交通规则;我们站着往上跳,最终还是要落下来,这是地球的引力规则。规则在生活中无处不在。软件开发中我们也需要规则,满足什么规则应该进入什么分支。如果做过风控系统,就知道风控系统里存在非常多的规则(比如:age < 16 || age > 50 -> REJECT )。最便捷的实现就是用 if-else 来写,但是随着规则的增加以及需求的变动,代码将变得越来越难阅读和理解,如果再去修改这些代码,然后测试不够充分的话,将产生严重的生产事故。这时候就要引入Drools等规则引擎了。Drools就是为了解决业务代码和业务规则分离的引擎。

        要使用Drools规则引擎,需要先安装安装JBoss Drools Support插件,这里就不多说怎么安装安装JBoss Drools Support插件。下载地址如下:

https://download.jboss.org/drools/release/7.3.0.Final/droolsjbpm-tools-distribution-7.3.0.Final.zip

    

1、 新建项目sc-activiti-drools,对应的pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>

<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.sc</groupId>
    <artifactId>sc-activiti-drools</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>sc-activiti-drools</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>6.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>knowledge-api</artifactId>
            <version>6.5.0.Final</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>


2、新建spring配置文件application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/sc?characterEncoding=utf8&useSSL=true
    username: root
    password: root
  activiti:
    check-process-definitions: false #自动部署验证设置:true-开启(默认)、false-关闭
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
  show-sql: true

server:
  port: 8081
  context-path: /
  session:
    timeout: 10
  tomcat:
    uri-encoding: UTF-8


3、新建activiti对应的配置文件activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/act?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="customPostDeployers">
            <list> 
                  <bean class="org.activiti.engine.impl.rules.RulesDeployer" />
            </list> 
        </property>
        <!-- 
            <property name="deploymentResources" value="classpath*:/bpmn/*.bpmn" />  
         -->
    </bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>

    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>


4、新建请假流程对应的bpmn文件如下

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
    xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
    typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
    targetNamespace="http://www.activiti.org/test">

    <process id="leave" name="请假审批" isExecutable="true">
        <startEvent id="startevent1" name="Start"></startEvent>
        <endEvent id="endevent1" name="End"></endEvent>
        <userTask id="usertask1" name="部门经理审批"></userTask>
        <busine***uleTask id="busine***uletask1" name="天数判断" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></busine***uleTask>
        <serviceTask id="servicetask1" name="获取变量" activiti:class="sc.ad.service.DroolsService"></serviceTask>
        <userTask id="usertask2" name="HR审批"></userTask>
        <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
        <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="busine***uletask1"></sequenceFlow>
        <sequenceFlow id="flow3" sourceRef="busine***uletask1" targetRef="servicetask1"></sequenceFlow>
        <userTask id="usertask3" name="总经理审批"></userTask>
        <sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="usertask3">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total >= 10}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="flow5" sourceRef="servicetask1" targetRef="usertask2">
            <conditionExpression xsi:type="tFormalExpression"><![CDATA[${reason[0].total < 10}]]></conditionExpression>
        </sequenceFlow>
        <sequenceFlow id="flow6" sourceRef="usertask3" targetRef="usertask2"></sequenceFlow>
        <sequenceFlow id="flow7" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    </process>
</definitions>

bpmn文件对应的图如下


5、新建规则文件leave.drl

package sc.ad;

import sc.ad.model.Leave;

rule "leave1"
    when
        u : Leave(day < 3);
    then
        u.setTotal(u.getDay() + 2);
end

rule "leave2"
    when
        u : Leave(day >= 3);
    then
        u.setTotal(u.getDay() + 5);
end

对应的图如下

6、新建springboot启动类文件

package sc.ad;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("activiti.cfg.xml")
public class ActivitiDroolsApp {
    public static void main(String[] args)
    {
        SpringApplication.run(ActivitiDroolsApp.class, args);
    }

}


7、新建一个controller,用来发起请假请求

package sc.ad.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sc.ad.model.Leave;

@RestController
public class ADController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

//    @RequestMapping("/ad")
//    public void ad() {
//
//        // 根据bpmn文件部署流程
//        Deployment deployment = repositoryService.createDeployment()
//                .addClasspathResource("holiday.bpmn").deploy();
//        // 获取流程定义
//        ProcessDefinition processDefinition = repositoryService
//                .createProcessDefinitionQuery()
//                .deploymentId(deployment.getId()).singleResult();
//        // 启动流程定义,返回流程实例
//        ProcessInstance pi = runtimeService
//                .startProcessInstanceById(processDefinition.getId());
//        String processId = pi.getId();
//        System.out.println("流程创建成功,当前流程实例ID:" + processId);
//
//        Task task = taskService.createTaskQuery().processInstanceId(processId)
//                .singleResult();
//        System.out.println("第一次执行前,任务名称:" + task.getName());
//        taskService.complete(task.getId());
//
//        task = taskService.createTaskQuery().processInstanceId(processId)
//                .singleResult();
//        System.out.println("第二次执行前,任务名称:" + task.getName());
//        taskService.complete(task.getId());
//
//        task = taskService.createTaskQuery().processInstanceId(processId)
//                .singleResult();
//        System.out.println("task为null,任务执行完毕:" + task);
//    }

    @RequestMapping("/drl")
    public void drl() {
        /**
         * 注意这里:必须要把drl文件一起deploy
         */
        DeploymentBuilder deploy = repositoryService.createDeployment();
        deploy.addClasspathResource("leave.bpmn").addClasspathResource("leave.drl");
        deploy.deploy();
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("leave");
        Map<String, Object> vars = new HashMap<String, Object>();  
        vars.put("leave", new Leave("白展堂", 12));
        /**
         * 当前任务
         */
        List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
        for(Task task : tasks)
        {
            System.out.println(task.getId() + " , " + task.getName());
            taskService.complete(task.getId(), vars);
        }
        /**
         * 下一步任务
         */
        tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).list();
        for(Task task : tasks)
        {
            System.out.println(task.getId() + " , " + task.getName());
        }
    }
}


8、启动并验证规则是否生效

从日志中看启动成功

访问http://127.0.0.1:8081/drl后,再次查看日志:

把修改controller的如下代码

        vars.put("leave", new Leave("白展堂", 12));

改成

     vars.put("leave", new Leave("乔峰", 2));Spring Cloud 2.x之Activiti整合规则引擎Drools

可以看到规则文件的规则已经生效:

Spring Cloud 2.x之Activiti整合规则引擎Drools




上一篇:SpringBoot整合activiti5-任务分配


下一篇:applicationContext配置文件中的属性说明