服务热线
135-6963-3175
activiti如何集成规则引擎drools呢
三个步骤:
1、pom加入依赖,activiti引擎集成部署器
1、首先进行drools规则编写、然后挂到流程图节点上
2、进行规则文件和bpmn流程文件的部署
例以下:
规则引擎部署器集成activiti
pom加入依赖:
<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>
2、activiti引擎配置集成
List<> list=new ArrayList(); list.add(org.activiti.engine.impl.rules.RulesDeployer); processEngineConfiguration.setCustomPostDeployers(list);
3、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> <businessRuleTask id="businessruletask1" name="天数判断" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></businessRuleTask> <serviceTask id="servicetask1" name="获取变量" activiti:class="com.1json.service.DroolsService"></serviceTask> <userTask id="usertask2" name="HR审批"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="businessruletask1"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="businessruletask1" 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>
其中
<businessRuleTask id="businessruletask1" name="天数判断" activiti:ruleVariablesInput="${leave}" activiti:rules="leave1,leave2" activiti:resultVariable="reason"></businessRuleTask>
配置了规则引擎节点的参数:输入参数变量${leave},规则集合activiti:rules="leave1,leave2". 规则计算结果值保存到reason变量里
4、drl规则文件代码
package com.1json; import com.1json.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
5.部署并访问
@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("1json", 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()); } }