在camunda中设置任务上的变量

时间:2014-08-28 11:13:42

标签: bpm camunda

是否可以在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>

1 个答案:

答案 0 :(得分:6)

版本&gt; = 7.2.0-alpha2

您可以使用inputOuput extension element

类似

<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正确,并注意这会设置布尔值而不是字符串。

版本&lt; 7.2.0-α-2

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

创建一个自定义扩展元素,用于定义要设置的变量和值(或重用上面的camunda扩展元素)。然后创建一个解析侦听器,为每个任务解析这些扩展元素,并为每个侦听&#34; start&#34;的任务添加执行侦听器。事件。然后,执行侦听器可以设置parse侦听器为其提供的变量。

有关组合解析和执行侦听器的机制的示例,请参阅https://github.com/camunda/camunda-bpm-examples/tree/master/process-engine-plugin/bpmn-parse-listener