flowable中引入决策任务节点
概述
决策任务和普通的任务节点一样,是配置的工作流内容的一个环节,并不是脱离flowable工作流单独存在的。由于项目要求需要在flowable流程中增加决策任务,于是网上查了很久,整理出了使用的大致流程。决策任务的例子就是找的最普通的input>10,<10,=10的例子,就不赘述了。
1.pom依赖
<!--flowable流程引擎-->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.4.2</version>
</dependency>
<!--mysql数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- 日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!--决策引擎dmn-->
<!-- https://mvnrepository.com/artifact/org.flowable/flowable-dmn-engine-configurator -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-dmn-engine-configurator</artifactId>
<version>6.4.2</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-dmn-engine</artifactId>
<version>6.4.2</version>
</dependency>
<!--junit4-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
2.dmn文件定义
文件名:dmnsample.dmn.xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101" id="simple" name="Simple" namespace="http://flowable.org/dmn">
<decision id="decision" name="Simple decision">
<decisionTable id="decisionTable">
<input>
<inputExpression id="input1" typeRef="number">
<text>input1</text>
</inputExpression>
</input>
<output id="output1" label="Output 1" name="output1" typeRef="string" />
<output id="output2" label="Output 2" name="output2" typeRef="number" />
<rule>
<inputEntry id="inputEntry1">
<text><![CDATA[> 10]]></text>
</inputEntry>
<outputEntry id="outputEntry1_1">
<text>'test'</text>
</outputEntry>
<outputEntry id="outputEntry2_1">
<text>1</text>
</outputEntry>
</rule>
<rule>
<inputEntry id="inputEntry2">
<text><![CDATA[< 10]]></text>
</inputEntry>
<outputEntry id="outputEntry1_2">
<text>'test2'</text>
</outputEntry>
<outputEntry id="outputEntry2_2">
<text>2</text>
</outputEntry>
</rule>
<rule>
<inputEntry id="inputEntry3">
<text><![CDATA[== 10]]></text>
</inputEntry>
<outputEntry id="outputEntry1_3">
<text>'test3'</text>
</outputEntry>
<outputEntry id="outputEntry2_3">
<text>3</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
</definitions>
3.dmn文件部署
没有使用xml方式初始化引擎配置,而是使用构造函数初始化。
DmnEngineConfiguration dmnEngineConfiguration = DmnEngineConfiguration.createStandaloneDmnEngineConfiguration()
.setJdbcPassword("")
.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowable?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8")
.setJdbcUsername("root")
.setJdbcPassword("root")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
// 如果数据表不存在的时候,自动创建数据表
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
DmnEngine dmnEngine = dmnEngineConfiguration.buildDmnEngine();
DmnRepositoryService dmnRepositoryService = dmnEngine.getDmnRepositoryService();
dmnRepositoryService.createDeployment().name("dmnsample.dmn.xml")
.addClasspathResource("dmnsample.dmn.xml")
.deploy();
4.执行dmn规则验证结果
/**
* 执行定义的规则文件
*/
@Test
public void executeDecisionByKey() {
DmnEngineConfiguration dmnEngineConfiguration = DmnEngineConfiguration.createStandaloneDmnEngineConfiguration()
.setJdbcPassword("")
.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowable?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8")
.setJdbcUsername("root")
.setJdbcPassword("root")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
// 如果数据表不存在的时候,自动创建数据表
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
DmnEngine dmnEngine = dmnEngineConfiguration.buildDmnEngine();
DmnRuleService dmnRuleService = dmnEngine.getDmnRuleService();
Map<String, Object> inputVariables = new HashMap<String, Object>();
inputVariables.put("input1", 11);
List<Map<String, Object>> result = dmnRuleService.executeDecisionByKey("decision", inputVariables);
System.out.println(result);
}
5.决策任务使用
写一个简单的流程,中间加入决策任务的节点即可。
xml样例:
<serviceTask id="sid-64DC19A4-1CBA-45C6-859F-83F3EE44C7F4" name="决策表" flowable:type="dmn">
<extensionElements>
<flowable:field name="decisionTableReferenceKey">
<flowable:string><![CDATA[decision]]></flowable:string>
</flowable:field>
<flowable:field name="decisionTaskThrowErrorOnNoHits">
<flowable:string><![CDATA[false]]></flowable:string>
</flowable:field>
</extensionElements>
</serviceTask>
6.部署流程,启动流程实例
这一步按照flowable正常的启动即可