2

是否可以在 camunda 定义 xml 中为用户任务设置变量?

我想设置一个变量(可删除),它会因任务而异。

任务 1 实例:deletable = true
任务 2 实例:deletable = true
任务 3 实例:deletable = false
任务 4 实例:deletable = false

这是实际的任务配置:

<bpmn2:userTask id="createtrunkdoc" camunda:candidateUsers="${candidateUser}" camunda:candidateGroups="provisioning" camunda:assignee="${candidateUser}" name="Create New&#xD;&#xA;Trunk Request">
  <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
  <bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:userTask>

测试用例(camunda:7.1.0-Final)

@RunWith(MockitoJUnitRunner.class)
public class testTest {

    @Rule
    public ProcessEngineRule processEngineRule = new ProcessEngineRule();

    @Test
    @Deployment(resources = { "test.bpmn20.xml" })
    public void testHappyPath() {
        ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("nipa-createsipinterconnect");

        assertThat(processInstance).isStarted().isNotEnded().task().hasDefinitionKey("reviewnewtrunk");

        Task task = taskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        assertEquals(true, taskService().getVariable(task.getId(), "deletable"));
    }

}

完整的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:camunda="http://activiti.org/bpmn" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd" id="_1RW-0F22EeOq-_ehbw1S0Q" exporter="camunda modeler" exporterVersion="2.5.0" targetNamespace="http://activiti.org/bpmn">
  <bpmn2:collaboration id="_Collaboration_8">
    <bpmn2:participant id="nipacreatesipinterconnect" name="Create new SIP Interconnect" processRef="nipa-createsipinterconnect"/>
  </bpmn2:collaboration>
  <bpmn2:process id="nipa-createsipinterconnect" name="Create new SIP Interconnect" isExecutable="true">
    <bpmn2:laneSet id="LaneSet_1" name="Lane Set 1">
      <bpmn2:lane id="Lane_1" name="Lane 1">
        <bpmn2:flowNodeRef>reviewnewtrunk</bpmn2:flowNodeRef>
        <bpmn2:flowNodeRef>StartCreateSIPInterconnect</bpmn2:flowNodeRef>
      </bpmn2:lane>
    </bpmn2:laneSet>
    <bpmn2:userTask id="reviewnewtrunk" camunda:candidateGroups="management" camunda:candidateUsers="" name="Review New &#xD;&#xA;Trunk Request">
      <bpmn2:extensionElements>
        <camunda:inputOutput>
            <camunda:inputParameter name="deletable">${true}</camunda:inputParameter>
         </camunda:inputOutput>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_5</bpmn2:incoming>
    </bpmn2:userTask>
    <bpmn2:startEvent id="StartCreateSIPInterconnect" name="Describe New &#xD;&#xA;SIP Interconnect &#xD;&#xA;Request">
      <bpmn2:outgoing>SequenceFlow_5</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_5" name="" sourceRef="StartCreateSIPInterconnect" targetRef="reviewnewtrunk"/>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="_Collaboration_8">
      <bpmndi:BPMNShape id="_BPMNShape_Participant_12" bpmnElement="nipacreatesipinterconnect" isHorizontal="true">
        <dc:Bounds height="333.0" width="1381.0" x="48.0" y="16.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_9" bpmnElement="StartCreateSIPInterconnect">
        <dc:Bounds height="36.0" width="36.0" x="228.0" y="154.0"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds height="54.0" width="116.0" x="188.0" y="195.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="_BPMNShape_UserTask_22" bpmnElement="reviewnewtrunk">
        <dc:Bounds height="80.0" width="157.0" x="516.0" y="132.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="_BPMNShape_Lane_7" bpmnElement="Lane_1" isHorizontal="true">
        <dc:Bounds height="333.0" width="1351.0" x="78.0" y="16.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="BPMNEdge_SequenceFlow_5" bpmnElement="SequenceFlow_5" sourceElement="_BPMNShape_StartEvent_9" targetElement="_BPMNShape_UserTask_22">
        <di:waypoint xsi:type="dc:Point" x="264.0" y="172.0"/>
        <di:waypoint xsi:type="dc:Point" x="516.0" y="172.0"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds height="6.0" width="6.0" x="291.0" y="172.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>
4

1 回答 1

6

在版本 >= 7.2.0-alpha2

您可以使用inputOutput 扩展元素

所以像

<bpmn2:userTask id="createtrunkdoc" camunda:candidateUsers="${candidateUser}"  camunda:candidateGroups="provisioning" camunda:assignee="${candidateUser}" name="Create New&#xD;&#xA;Trunk Request">
  <extensionElements>
    <camunda:inputOutput>
      <camunda:inputParameter name="deletable">${true}</camunda:inputParameter>
    </camunda:inputOutput>
  </extensionElements>
  <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
  <bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>      
</bpmn2:userTask>

编辑:

假设您有一个过程,这是唯一的任务,以下对我有用:

ProcessInstance pi = runtimeService.startProcessInstanceByKey("processKey");
Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
assertEquals(true, taskService.getVariable(task.getId(), "deletable"));

只需确保任务 ID 正确,并注意这将设置一个布尔值而不是字符串。

在版本 < 7.2.0-alpha2

对此没有开箱即用的支持,但您可以通过以下方式实现它:

创建一个自定义扩展元素,定义要设置的变量和值(或重复使用上面的 camunda 扩展元素)。然后创建一个解析监听器,为每个任务解析这些扩展元素,并为每个监听“开始”事件的任务添加一个执行监听器。然后,执行侦听器可以设置解析侦听器为其提供的变量。

解析和执行监听器结合的机制示例,参见https://github.com/camunda/camunda-bpm-examples/tree/master/process-engine-plugin/bpmn-parse-listener

于 2014-08-28T12:02:54.837 回答