Camunda Conditional Events example

Posted by SoaringUSAEagle on Sat, 12 Feb 2022 05:38:53 +0100

Camunda Conditional Events: defines an event that is triggered when a given condition is evaluated to true. It can be used as the starting event, intermediate event and boundary event of the event sub process. Start and boundary events can be interrupted and uninterrupted. In camunda, Conditional Events are triggered with the help of process variables.

Camunda condition events include: Conditional Start Event, Intermediate Conditional Catch Event, Conditional Boundary Event, and Conditional Start Event for Event Sub Process.

This article focuses on the Conditional Start Event and T Conditional Boundary Event. For other events, please refer to camunda's official documents: https://docs.camunda.org/manual/7.15/reference/bpmn20/events/

1, Design flow chart

Test scenario: the condition event process is automatically initiated by simulating the reimbursement process. If the reimbursement amount is greater than 10000 yuan, the reimbursement approval sub process will be automatically initiated. In the financial approval phase, if the reimbursement amount changes, the boundary condition event will be automatically triggered and returned to the approver.

Main reimbursement process:

 

Reimbursement event node configuration:

 

The agent class code is as follows:

package com.example.demo1;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.runtime.ProcessInstance;

import java.util.List;


/**
 * Trigger condition event start sub process
 */
public class InstantiateProcessByConditionDelegate implements JavaDelegate {

    public void execute(DelegateExecution execution) {
        Integer money = (int)execution.getVariable("money");
        RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
        List<ProcessInstance> instances = runtimeService
                .createConditionEvaluation()
                .setVariable("money", money)
                .evaluateStartConditions();
    }
}

BPMN process model file:

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

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1ktuxzn" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

  <bpmn:process id="Process_10z1wy2" name="Reimbursement main process" isExecutable="true">

    <bpmn:startEvent id="StartEvent_1">

      <bpmn:outgoing>Flow_0f0u4dp</bpmn:outgoing>

    </bpmn:startEvent>

    <bpmn:sequenceFlow id="Flow_0f0u4dp" sourceRef="StartEvent_1" targetRef="Activity_02e4hhc" />

    <bpmn:sequenceFlow id="Flow_0ckn1r7" sourceRef="Activity_02e4hhc" targetRef="Activity_1lkdefp" />

    <bpmn:endEvent id="Event_1iic3x0">

      <bpmn:incoming>Flow_04z0quc</bpmn:incoming>

    </bpmn:endEvent>

    <bpmn:sequenceFlow id="Flow_04z0quc" sourceRef="Activity_1lkdefp" targetRef="Event_1iic3x0" />

    <bpmn:serviceTask id="Activity_02e4hhc" name="Reimbursement event" camunda:class="com.example.demo1.InstantiateProcessByConditionDelegate">

      <bpmn:incoming>Flow_0f0u4dp</bpmn:incoming>

      <bpmn:outgoing>Flow_0ckn1r7</bpmn:outgoing>

    </bpmn:serviceTask>

    <bpmn:userTask id="Activity_1lkdefp" name="Notify the reimbursement applicant" camunda:assignee="demo">

      <bpmn:incoming>Flow_0ckn1r7</bpmn:incoming>

      <bpmn:outgoing>Flow_04z0quc</bpmn:outgoing>

    </bpmn:userTask>

  </bpmn:process>

  <bpmndi:BPMNDiagram id="BPMNDiagram_1">

    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_10z1wy2">

      <bpmndi:BPMNEdge id="Flow_0f0u4dp_di" bpmnElement="Flow_0f0u4dp">

        <di:waypoint x="215" y="117" />

        <di:waypoint x="270" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_0ckn1r7_di" bpmnElement="Flow_0ckn1r7">

        <di:waypoint x="370" y="117" />

        <di:waypoint x="430" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_04z0quc_di" bpmnElement="Flow_04z0quc">

        <di:waypoint x="530" y="117" />

        <di:waypoint x="592" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">

        <dc:Bounds x="179" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_1iic3x0_di" bpmnElement="Event_1iic3x0">

        <dc:Bounds x="592" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_09rzn3u_di" bpmnElement="Activity_02e4hhc">

        <dc:Bounds x="270" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_1khoz85_di" bpmnElement="Activity_1lkdefp">

        <dc:Bounds x="430" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</bpmn:definitions>

Reimbursement approval sub process:

 

Conditional Start Event node configuration:

 

${money > = 10000} means that the reimbursement amount is greater than or equal to 10000 yuan, and the process starts automatically.

Conditional Boundary Event node configuration:

 

${money > = 20000} indicates that the reimbursement amount is greater than or equal to 20000 yuan, and the boundary condition event is automatically triggered.

BPMN process model file:

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

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1n4m8dk" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.8.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

  <bpmn:process id="Process_0uf5zc5" name="Conditional event flow" isExecutable="true">

    <bpmn:sequenceFlow id="Flow_1f7cdrv" sourceRef="StartEvent_1" targetRef="Activity_15fulz2" />

    <bpmn:sequenceFlow id="Flow_0p7c5lt" sourceRef="Activity_15fulz2" targetRef="Activity_0xikne3" />

    <bpmn:endEvent id="Event_152527w">

      <bpmn:incoming>Flow_1l03vwj</bpmn:incoming>

    </bpmn:endEvent>

    <bpmn:sequenceFlow id="Flow_1l03vwj" sourceRef="Activity_0xikne3" targetRef="Event_152527w" />

    <bpmn:startEvent id="StartEvent_1">

      <bpmn:outgoing>Flow_1f7cdrv</bpmn:outgoing>

      <bpmn:conditionalEventDefinition id="ConditionalEventDefinition_0lzdf7y" camunda:variableName="money">

        <bpmn:condition xsi:type="bpmn:tFormalExpression">${money&gt;=10000}</bpmn:condition>

      </bpmn:conditionalEventDefinition>

    </bpmn:startEvent>

    <bpmn:userTask id="Activity_15fulz2" name="Manager approval" camunda:assignee="demo">

      <bpmn:incoming>Flow_1f7cdrv</bpmn:incoming>

      <bpmn:incoming>Flow_08he9tn</bpmn:incoming>

      <bpmn:outgoing>Flow_0p7c5lt</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:userTask id="Activity_0xikne3" name="Financial approval" camunda:assignee="demo">

      <bpmn:incoming>Flow_0p7c5lt</bpmn:incoming>

      <bpmn:outgoing>Flow_1l03vwj</bpmn:outgoing>

    </bpmn:userTask>

    <bpmn:boundaryEvent id="Event_0mnbhru" name="Reimbursement expense modification event" attachedToRef="Activity_0xikne3">

      <bpmn:outgoing>Flow_08he9tn</bpmn:outgoing>

      <bpmn:conditionalEventDefinition id="ConditionalEventDefinition_19eu007" camunda:variableName="money">

        <bpmn:condition xsi:type="bpmn:tFormalExpression">${money&gt;=20000}</bpmn:condition>

      </bpmn:conditionalEventDefinition>

    </bpmn:boundaryEvent>

    <bpmn:sequenceFlow id="Flow_08he9tn" sourceRef="Event_0mnbhru" targetRef="Activity_15fulz2" />

  </bpmn:process>

  <bpmndi:BPMNDiagram id="BPMNDiagram_1">

    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0uf5zc5">

      <bpmndi:BPMNEdge id="Flow_1f7cdrv_di" bpmnElement="Flow_1f7cdrv">

        <di:waypoint x="215" y="117" />

        <di:waypoint x="270" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_0p7c5lt_di" bpmnElement="Flow_0p7c5lt">

        <di:waypoint x="370" y="117" />

        <di:waypoint x="500" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_1l03vwj_di" bpmnElement="Flow_1l03vwj">

        <di:waypoint x="600" y="117" />

        <di:waypoint x="662" y="117" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge id="Flow_08he9tn_di" bpmnElement="Flow_08he9tn">

        <di:waypoint x="550" y="175" />

        <di:waypoint x="550" y="195" />

        <di:waypoint x="320" y="195" />

        <di:waypoint x="320" y="157" />

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNShape id="Event_1d290h0_di" bpmnElement="StartEvent_1">

        <dc:Bounds x="179" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_1hwmimx_di" bpmnElement="Activity_15fulz2">

        <dc:Bounds x="270" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_152527w_di" bpmnElement="Event_152527w">

        <dc:Bounds x="662" y="99" width="36" height="36" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Activity_10pf0x5_di" bpmnElement="Activity_0xikne3">

        <dc:Bounds x="500" y="77" width="100" height="80" />

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape id="Event_0bjat0x_di" bpmnElement="Event_0mnbhru">

        <dc:Bounds x="532" y="139" width="36" height="36" />

        <bpmndi:BPMNLabel>

          <dc:Bounds x="451" y="166" width="77" height="14" />

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</bpmn:definitions>

2, Deploy process and test validation

After process deployment, view the database process subscription event table act_ru_event_subscr, it is found that the Conditional Start Event has been persisted to the database.

 

Log in to camunda platform through demo user http://localhost:8080/camunda/app/tasklist/default/#/login , start the main flow simulation test.

 

Because the reimbursement amount meets the conditions, the sub process is automatically triggered:

 

Submit the manual sub process to the next node:

 

At this point, the process flows to the Conditional Boundary Event node, and view the database table records:

 

To simulate triggering of Conditional Boundary Event, modify the value of money variable in camunda background:

 

Since the process variable money is changed to 20000, which meets the trigger condition of Conditional Boundary Event, the process automatically flows to the corresponding node.

 

3, Condition event instructions

  1. Conditional Start Event

Conditional Start Event can start a process by calculating some conditions. A process can have one or more conditional start events.

If multiple conditions are met, a corresponding number of processes will be triggered.

When deploying a process definition with conditional start events, you need to consider the following:

In a given process definition, the condition of a conditional start event must be unique, that is, a process definition cannot have multiple conditional start events with the same condition. If two or more conditional start events contain the same condition, the engine will throw an exception when deploying the process definition.

Process version control: when deploying a new version of a process definition, the conditional subscription of the previous version will be cancelled. The same is true for conditional events that do not appear in the new version.

When starting a process instance, you can use the following methods on the RuntimeService to trigger a conditional start event:

List<ProcessInstance> instances = runtimeService

    .createConditionEvaluation()

    .setVariable("temperature", 24)

    .evaluateStartConditions();

// or

List<ProcessInstance> instances = runtimeService

    .createConditionEvaluation()

    .setVariables(variableMap)

.evaluateStartConditions();

The variables provided are used to calculate the conditions. They are also passed as variables to the newly created process instance. The XML representation of a conditional start event is a normal start event declaration with a child element of conditionalEventDefinition.

Optional: adding the variableName attribute to the conditionalEventDefinition allows you to specify a variable name on which the condition of the conditional event should be calculated specifically.

<startEvent id="conditionalStartEvent">

  <conditionalEventDefinition camunda:variableName="temperature">

    <condition type="tFormalExpression">${temperature > 20}</condition>

  </conditionalEventDefinition>

</startEvent>

2. Conditional Boundary Event

A Conditional Boundary Event is like an observer. If a specific condition is met, it will be triggered. Interrupt condition events are different from non interrupt condition events. By default, it is an interrupt event. A non disruptive event causes the original activity not to be interrupted and the instance is still active. Instead, an additional execution path is created using the outgoing transformation of events. An uninterrupted conditional event can be triggered multiple times as long as its attached activity is active.

In the XML representation of a non breaking conditional event, the cancelActivity property is set to false:

<boundaryEvent id="conditionalEvent" attachedToRef="taskWithCondition" cancelActivity="false">

  <conditionalEventDefinition>

    <condition type="tFormalExpression">${var1 == 1}</condition>

  </conditionalEventDefinition>

</boundaryEvent>

reference resources:

Cloud process | cloud BPM, cloud process BPM, low code platform, low code development platform, open source process engine, Camunda,flowable, business process management, activiti, intelligent form, electronic form, visual development, zero code development, basic platform, process PaaS, process SaaS

https://docs.camunda.org/manual/7.15/reference/bpmn20/events/conditional-events/

Topics: Activiti Flowable bpmn camunda