Activiti学习之根据条件判断流程走向

在流程设计当中经常会遇到根据条件来判断流程走向的问题,Activiti中也给出了相应的实现方法:网关
网关(Gateway)用于控制流程走向(在BPMN2.0规范中称为“执行令牌”)。根据功能不同可以划分为以下四种网关:
- 排他网关
- 并行网关
- 包容网关
- 事件网关
其中,排他网关(Exclusive Gateway)用来对流程中的走向进行建模。流程执行到该网关时,Activiti 根据设置的条件进行计算,当条件计算结果为true时,执行当前网关的输出流。

小提示:如果多个线路计算结果都为true,那么只会执行第一个,如果没有为true的值,会抛出异常。

排他网关在流程图当中的形象如下图所示:

排他网关

下面介绍一下排他网关的使用方法。

1、绘制流程图

ExclusiveProcess.bpmn如下所示:
Activiti学习之根据条件判断流程走向_第1张图片
其中排他网关的三个输出线分别设置Condition值为:

${type == 1}
${type == 2}
${type == 3}

当表达式判断结果为true时会执行后面的系统任务。
三个系统任务分别设置成如图所示:
Activiti学习之根据条件判断流程走向_第2张图片
三个系统任务的Task type 设为Expression,Expression选项中分别填入

${resumeService.showTask1()}
${resumeService.showTask2()}
${resumeService.showTask3()}

流程图的XML代码如下:


<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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="exclusiveProcess" name="ExclusiveProcess.bpmn" isExecutable="true">
    <startEvent id="startevent1" name="Start">startEvent>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway">exclusiveGateway>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="exclusivegateway1">sequenceFlow>
    <serviceTask id="servicetask1" name="Service Task1" activiti:expression="${resumeService.showTask1()}">serviceTask>
    <sequenceFlow id="flow2" sourceRef="exclusivegateway1" targetRef="servicetask1">
      <conditionExpression xsi:type="tFormalExpression">conditionExpression>
    sequenceFlow>
    <serviceTask id="servicetask3" name="Service Task3" activiti:expression="${resumeService.showTask3()}">serviceTask>
    <sequenceFlow id="flow3" sourceRef="exclusivegateway1" targetRef="servicetask3">
      <conditionExpression xsi:type="tFormalExpression">conditionExpression>
    sequenceFlow>
    <serviceTask id="servicetask2" name="Service Task2" activiti:expression="${resumeService.showTask2()}">serviceTask>
    <sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="servicetask2">
      <conditionExpression xsi:type="tFormalExpression">conditionExpression>
    sequenceFlow>
    <endEvent id="endevent1" name="End">endEvent>
    <sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="endevent1">sequenceFlow>
    <sequenceFlow id="flow6" sourceRef="servicetask1" targetRef="endevent1">sequenceFlow>
    <sequenceFlow id="flow7" sourceRef="servicetask3" targetRef="endevent1">sequenceFlow>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_exclusiveProcess">
    <bpmndi:BPMNPlane bpmnElement="exclusiveProcess" id="BPMNPlane_exclusiveProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="60.0" y="140.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="140.0" y="138.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="260.0" y="40.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
        <omgdc:Bounds height="55.0" width="105.0" x="269.0" y="220.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
        <omgdc:Bounds height="55.0" width="105.0" x="260.0" y="131.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="480.0" y="141.0">omgdc:Bounds>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="95.0" y="157.0">omgdi:waypoint>
        <omgdi:waypoint x="140.0" y="158.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="160.0" y="138.0">omgdi:waypoint>
        <omgdi:waypoint x="160.0" y="67.0">omgdi:waypoint>
        <omgdi:waypoint x="260.0" y="67.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="160.0" y="178.0">omgdi:waypoint>
        <omgdi:waypoint x="160.0" y="247.0">omgdi:waypoint>
        <omgdi:waypoint x="269.0" y="247.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="180.0" y="158.0">omgdi:waypoint>
        <omgdi:waypoint x="260.0" y="158.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="365.0" y="158.0">omgdi:waypoint>
        <omgdi:waypoint x="480.0" y="158.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="365.0" y="67.0">omgdi:waypoint>
        <omgdi:waypoint x="497.0" y="67.0">omgdi:waypoint>
        <omgdi:waypoint x="497.0" y="141.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="374.0" y="247.0">omgdi:waypoint>
        <omgdi:waypoint x="497.0" y="247.0">omgdi:waypoint>
        <omgdi:waypoint x="497.0" y="176.0">omgdi:waypoint>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

2、部署流程

将流程图文件放到项目当中的resources下的processes文件夹,运行程序 。

3、编写代码

新建spring boot 项目,在项目中实现一个service,并且在service中实现三个方法,代码如下:
ResumeService接口

public interface ResumeService {

    void showTask1();

    void showTask2();

    void showTask3();
}

ResumeServiceImpl实现类

@Service("resumeService")
public class ResumeServiceImpl implements ResumeService {

    @Override
    public void showTask1() {
        System.out.println("#############################执行任务1");
    }

    @Override
    public void showTask2() {
        System.out.println("#############################执行任务2");
    }

    @Override
    public void showTask3() {
        System.out.println("#############################执行任务3");
    }
}

利用spring boot 单元测试来实现流程:
ExclusiveProcessTest测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootActivitiApplication.class)
public class ExclusiveProcessTest {

    @Autowired
    RuntimeService runtimeService;

    /**
     * 启动流程,并给流程传入参数type,排他网关根据参数type来判断流程走向
     */
    @Test
    public void testStartProcess() {
        Map map = new HashMap<>();
        map.put("type", 3);
        runtimeService.startProcessInstanceByKey("exclusiveProcess", map);

    }
}

4、执行结果

运行单元测试,执行结果如下:
Activiti学习之根据条件判断流程走向_第3张图片

小知识点:排他网关中有一个属性为default,当所有条件都不满足时,排他网关默认执行default属性制定的流程。

<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway" default="flow1">exclusiveGateway>

你可能感兴趣的:(activiti)