DMN 决策表 初探
Camunda DMN 决策
Camunda 中使用DMN决策
Camunda 支持DMN1.3版本,在BPMN业务活动流程中,可通过业务规则任务调用DMN决策。DMN决策目的是想把业务代码和决策进行解耦,使决策分析人员只需关心决策即可。
Camunda DMN案例
通过幼儿园学生年龄age和身高height分配不同的班级,决策表可随时修改。校长审核时向流程中传递决策表所需要的流程变量age、height。流程径流决策表后自动提交到相关班级老师审批。
创建学生分配班级审核流程
<bpmn:process id="Process_DMN2" name="幼儿园班级选择-决策表" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_1kmfo50</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1kmfo50" sourceRef="StartEvent_1" targetRef="Activity_09yea1p" />
<bpmn:userTask id="Activity_09yea1p" name="校长审核" camunda:assignee="admin">
<bpmn:incoming>Flow_1kmfo50</bpmn:incoming>
<bpmn:outgoing>Flow_1ohj7ez</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="Flow_1ohj7ez" sourceRef="Activity_09yea1p" targetRef="Activity_0lqrqvr" />
<bpmn:businessRuleTask id="Activity_0lqrqvr" name="调用决策表,判断哪个班" camunda:resultVariable="dmnResult" camunda:decisionRef="MyDMN02" camunda:mapDecisionResult="singleEntry">
<bpmn:incoming>Flow_1ohj7ez</bpmn:incoming>
<bpmn:outgoing>Flow_0jaf2y3</bpmn:outgoing>
</bpmn:businessRuleTask>
<bpmn:exclusiveGateway id="Gateway_0c6380i">
<bpmn:incoming>Flow_0jaf2y3</bpmn:incoming>
<bpmn:outgoing>Flow_0x76o56</bpmn:outgoing>
<bpmn:outgoing>Flow_03o01yz</bpmn:outgoing>
<bpmn:outgoing>Flow_16wz3jj</bpmn:outgoing>
</bpmn:exclusiveGateway>
<bpmn:sequenceFlow id="Flow_0jaf2y3" sourceRef="Activity_0lqrqvr" targetRef="Gateway_0c6380i" />
<bpmn:sequenceFlow id="Flow_0x76o56" sourceRef="Gateway_0c6380i" targetRef="Activity_1rh5001">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${dmnResult==2}</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:sequenceFlow id="Flow_03o01yz" sourceRef="Gateway_0c6380i" targetRef="Activity_1c2dxw0">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${dmnResult==3}</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:sequenceFlow id="Flow_16wz3jj" sourceRef="Gateway_0c6380i" targetRef="Activity_0ol6tq7">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${dmnResult==1}</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:userTask id="Activity_0ol6tq7" name="幼儿园小班" camunda:assignee="admin">
<bpmn:incoming>Flow_16wz3jj</bpmn:incoming>
<bpmn:outgoing>Flow_13fe5iy</bpmn:outgoing>
</bpmn:userTask>
<bpmn:userTask id="Activity_1rh5001" name="幼儿园中班" camunda:assignee="admin">
<bpmn:incoming>Flow_0x76o56</bpmn:incoming>
<bpmn:outgoing>Flow_0pero7b</bpmn:outgoing>
</bpmn:userTask>
<bpmn:userTask id="Activity_1c2dxw0" name="幼儿园大班" camunda:assignee="admin">
<bpmn:incoming>Flow_03o01yz</bpmn:incoming>
<bpmn:outgoing>Flow_1k33ruz</bpmn:outgoing>
</bpmn:userTask>
<bpmn:endEvent id="Event_09q2lsr">
<bpmn:incoming>Flow_13fe5iy</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_13fe5iy" sourceRef="Activity_0ol6tq7" targetRef="Event_09q2lsr" />
<bpmn:endEvent id="Event_0r6p8f2">
<bpmn:incoming>Flow_0pero7b</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0pero7b" sourceRef="Activity_1rh5001" targetRef="Event_0r6p8f2" />
<bpmn:endEvent id="Event_1icnobe">
<bpmn:incoming>Flow_1k33ruz</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1k33ruz" sourceRef="Activity_1c2dxw0" targetRef="Event_1icnobe" />
</bpmn:process>
创建决策
<decision id="MyDMN02" name="幼儿园班级判断">
<decisionTable id="DecisionTable_1hc3c2q" hitPolicy="COLLECT" aggregation="MAX">
<input id="InputClause_1vbdtew" label="年龄" biodi:width="192" camunda:inputVariable="age">
<inputExpression id="LiteralExpression_1hfef7m" typeRef="integer" />
</input>
<input id="InputClause_13b3rh9" label="身高" camunda:inputVariable="height">
<inputExpression id="LiteralExpression_1hr6wum" typeRef="double">
<text></text>
</inputExpression>
</input>
<output id="OutputClause_1poncf1" label="选择班级" name="clsName" typeRef="integer" />
<rule id="DecisionRule_1029rws">
<description>大于3岁并且身高高于70cm,可上幼儿园小班</description>
<inputEntry id="UnaryTests_1e4z08i">
<text>>3</text>
</inputEntry>
<inputEntry id="UnaryTests_0t3xek6">
<text>>70</text>
</inputEntry>
<outputEntry id="LiteralExpression_0l1lpg2">
<text>1</text>
</outputEntry>
</rule>
<rule id="DecisionRule_023jo7u">
<description>大于等于5岁并且身高高于95cm,可上幼儿园中班</description>
<inputEntry id="UnaryTests_0jeku2j">
<text>>=5</text>
</inputEntry>
<inputEntry id="UnaryTests_1w0xn25">
<text>>=96</text>
</inputEntry>
<outputEntry id="LiteralExpression_0qkdpew">
<text>2</text>
</outputEntry>
</rule>
<rule id="DecisionRule_09zimc1">
<description>大于7岁并且身高高于120cm,可上幼儿园大班</description>
<inputEntry id="UnaryTests_1852h8n">
<text>>7</text>
</inputEntry>
<inputEntry id="UnaryTests_1e7igfg">
<text>>120</text>
</inputEntry>
<outputEntry id="LiteralExpression_1o3xlmi">
<text>3</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
开启流程,校长审核时,指定决策表中所需要的变量
通过决策表后,当前学生到幼儿园中班老师审批
注意
- 决策表中的决策逻辑完全可由流程变量+流程网关实现,用决策可把流程业务活动和业务决策信息解耦,业务决策人员只需关系决策即可;决策信息可随时进行调整,不需发布流程和代码;
- BPMN 业务流程活动可通过 “业务规则任务” (Business Rule Task) 调用DMN决策表逻辑;调用的DMN决策表策略结果可输出到流程变量中,流程中通过此变量进行流程流转;
- 决策DMN文件中只有决策表、文本表达式参与到决策引擎运算,其它输入源、知识源和知识模型等符号会被camunda DMN引擎忽略;
- 决策DMN文件中可有多个决策表、其他符号组成决策需求图(DRG)。决策需求图(DRG)为决策领域建模,展示相关的最重要的元素和它们的依赖关系。建模的元素是决策(decisions),输入数据(input data),和知识源(knowledge sources)这些只是告诉决策人员,当前决策表需要的输入条件信息,不与DMN引擎计算。
- 决策表里面可以有多个输入源,输入源可以选择表达式或计算流程变量,作为输入值,条件对这个表达式进行判断,满足条件时,当前决策符合。
参考资料
Camunda DMN 决策配置:
http://camunda-cn.shaochenfeng.com/user-guide/process-engine/decisions/
DMN 决策使用
http://camunda-cn.shaochenfeng.com/reference/dmn/
BPMN 中调用DMN决策表
http://camunda-cn.shaochenfeng.com/reference/bpmn20/tasks/business-rule-task/
DMN决策使用案例
https://www.jianshu.com/p/8661da33135e