是否无法通过JBPM中的脚本任务设置过程变量?

时间:2019-03-06 11:05:51

标签: jbpm bpmn

尝试从Script任务添加新的流程变量时遇到错误,如下所示:

kcontext.setVariable("newProcessVariable","value");

我遇到以下错误:

NodeInstanceImpl - Could not find variable newProcessVariable

在执行任务期间是否不可能添加新的过程变量,还是必须在启动过程时定义所有过程变量? 如果我根本上缺少某些东西,请纠正我。

2 个答案:

答案 0 :(得分:0)

在设计时定义过程变量(名称和数据类型),并在过程开始时进行初始化。您可以在脚本任务中为过程变量设置值,但不能创建在设计时未定义的新变量。

答案 1 :(得分:0)

自您的初始帖子以来,JBPM已添加了动态过程变量支持。使用JBPM 7.23进行测试,我可以使用以下命令从脚本任务中动态添加过程变量:

kcontext.setVariable("Test", "test var");

很好(非常感谢),因为我们的解决方案之一就是依赖于此,因为我们在设计时还不知道许多过程变量!

包含一个可以导入和测试的非常简单的业务流程资产,可以在JBPM 7.23中使用!

<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.omg.org/bpmn20" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:bpsim="http://www.bpsim.org/schemas/1.0" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:drools="http://www.jboss.org/drools" id="_6LXvcA00EeuO-Jf6v3VhIw" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd http://www.jboss.org/drools drools.xsd http://www.bpsim.org/schemas/1.0 bpsim.xsd http://www.omg.org/spec/DD/20100524/DC DC.xsd http://www.omg.org/spec/DD/20100524/DI DI.xsd " exporter="jBPM Process Modeler" exporterVersion="2.0" targetNamespace="http://www.omg.org/bpmn20">
  <bpmn2:process id="CGGWorkItemUtils.Test" drools:packageName="org.jbpm" drools:version="1.0" drools:adHoc="false" name="Test" isExecutable="true">
    <bpmn2:sequenceFlow id="_F0541B12-E0CB-4679-80E7-BDE4903067A1" sourceRef="_BB6923A3-4BBC-4841-A28A-53988CEFF8A1" targetRef="_BE0F66D3-AB5F-456F-9F37-BD85D0EB1A65">
      <bpmn2:extensionElements>
        <drools:metaData name="isAutoConnection.source">
          <drools:metaValue><![CDATA[true]]></drools:metaValue>
        </drools:metaData>
        <drools:metaData name="isAutoConnection.target">
          <drools:metaValue><![CDATA[true]]></drools:metaValue>
        </drools:metaData>
      </bpmn2:extensionElements>
    </bpmn2:sequenceFlow>
    <bpmn2:sequenceFlow id="_0AD95BD7-2F41-4C28-BA86-4C05258E32C8" sourceRef="_ADD9704B-537B-402F-ADB2-EE47AB09A489" targetRef="_BB6923A3-4BBC-4841-A28A-53988CEFF8A1">
      <bpmn2:extensionElements>
        <drools:metaData name="isAutoConnection.source">
          <drools:metaValue><![CDATA[true]]></drools:metaValue>
        </drools:metaData>
        <drools:metaData name="isAutoConnection.target">
          <drools:metaValue><![CDATA[true]]></drools:metaValue>
        </drools:metaData>
      </bpmn2:extensionElements>
    </bpmn2:sequenceFlow>
    <bpmn2:scriptTask id="_BB6923A3-4BBC-4841-A28A-53988CEFF8A1" name="Task" scriptFormat="http://www.java.com/java">
      <bpmn2:extensionElements>
        <drools:metaData name="elementname">
          <drools:metaValue><![CDATA[Task]]></drools:metaValue>
        </drools:metaData>
      </bpmn2:extensionElements>
      <bpmn2:incoming>_0AD95BD7-2F41-4C28-BA86-4C05258E32C8</bpmn2:incoming>
      <bpmn2:outgoing>_F0541B12-E0CB-4679-80E7-BDE4903067A1</bpmn2:outgoing>
      <bpmn2:script><![CDATA[kcontext.setVariable("Test", "test");]]></bpmn2:script>
    </bpmn2:scriptTask>
    <bpmn2:endEvent id="_BE0F66D3-AB5F-456F-9F37-BD85D0EB1A65">
      <bpmn2:incoming>_F0541B12-E0CB-4679-80E7-BDE4903067A1</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:startEvent id="_ADD9704B-537B-402F-ADB2-EE47AB09A489">
      <bpmn2:outgoing>_0AD95BD7-2F41-4C28-BA86-4C05258E32C8</bpmn2:outgoing>
    </bpmn2:startEvent>
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="_6LYWgA00EeuO-Jf6v3VhIw">
    <bpmndi:BPMNPlane id="_6LYWgQ00EeuO-Jf6v3VhIw" bpmnElement="CGGWorkItemUtils.Test">
      <bpmndi:BPMNShape id="shape__ADD9704B-537B-402F-ADB2-EE47AB09A489" bpmnElement="_ADD9704B-537B-402F-ADB2-EE47AB09A489">
        <dc:Bounds height="56.0" width="56.0" x="100.0" y="90.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape__BE0F66D3-AB5F-456F-9F37-BD85D0EB1A65" bpmnElement="_BE0F66D3-AB5F-456F-9F37-BD85D0EB1A65">
        <dc:Bounds height="56.0" width="56.0" x="691.0" y="92.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape__BB6923A3-4BBC-4841-A28A-53988CEFF8A1" bpmnElement="_BB6923A3-4BBC-4841-A28A-53988CEFF8A1">
        <dc:Bounds height="102.0" width="154.0" x="381.0" y="69.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge_shape__ADD9704B-537B-402F-ADB2-EE47AB09A489_to_shape__BB6923A3-4BBC-4841-A28A-53988CEFF8A1" bpmnElement="_0AD95BD7-2F41-4C28-BA86-4C05258E32C8">
        <di:waypoint xsi:type="dc:Point" x="156.0" y="118.0"/>
        <di:waypoint xsi:type="dc:Point" x="381.0" y="120.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge_shape__BB6923A3-4BBC-4841-A28A-53988CEFF8A1_to_shape__BE0F66D3-AB5F-456F-9F37-BD85D0EB1A65" bpmnElement="_F0541B12-E0CB-4679-80E7-BDE4903067A1">
        <di:waypoint xsi:type="dc:Point" x="535.0" y="120.0"/>
        <di:waypoint xsi:type="dc:Point" x="691.0" y="120.0"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
  <bpmn2:relationship id="_6LYWgg00EeuO-Jf6v3VhIw" type="BPSimData">
    <bpmn2:extensionElements>
      <bpsim:BPSimData>
        <bpsim:Scenario xsi:type="bpsim:Scenario" id="default" name="Simulationscenario">
          <bpsim:ScenarioParameters xsi:type="bpsim:ScenarioParameters"/>
          <bpsim:ElementParameters xsi:type="bpsim:ElementParameters" elementRef="_ADD9704B-537B-402F-ADB2-EE47AB09A489" id="_6LYWgw00EeuO-Jf6v3VhIw">
            <bpsim:TimeParameters xsi:type="bpsim:TimeParameters">
              <bpsim:ProcessingTime xsi:type="bpsim:Parameter">
                <bpsim:NormalDistribution mean="0.0" standardDeviation="0.0"/>
              </bpsim:ProcessingTime>
            </bpsim:TimeParameters>
          </bpsim:ElementParameters>
          <bpsim:ElementParameters xsi:type="bpsim:ElementParameters" elementRef="_BB6923A3-4BBC-4841-A28A-53988CEFF8A1" id="_6LYWhA00EeuO-Jf6v3VhIw">
            <bpsim:TimeParameters xsi:type="bpsim:TimeParameters">
              <bpsim:ProcessingTime xsi:type="bpsim:Parameter">
                <bpsim:NormalDistribution mean="0.0" standardDeviation="0.0"/>
              </bpsim:ProcessingTime>
            </bpsim:TimeParameters>
            <bpsim:ResourceParameters xsi:type="bpsim:ResourceParameters">
              <bpsim:Availability xsi:type="bpsim:Parameter">
                <bpsim:FloatingParameter value="0.0"/>
              </bpsim:Availability>
              <bpsim:Quantity xsi:type="bpsim:Parameter">
                <bpsim:FloatingParameter value="0.0"/>
              </bpsim:Quantity>
            </bpsim:ResourceParameters>
            <bpsim:CostParameters xsi:type="bpsim:CostParameters">
              <bpsim:UnitCost xsi:type="bpsim:Parameter">
                <bpsim:FloatingParameter value="0.0"/>
              </bpsim:UnitCost>
            </bpsim:CostParameters>
          </bpsim:ElementParameters>
        </bpsim:Scenario>
      </bpsim:BPSimData>
    </bpmn2:extensionElements>
    <bpmn2:source>_6LXvcA00EeuO-Jf6v3VhIw</bpmn2:source>
    <bpmn2:target>_6LXvcA00EeuO-Jf6v3VhIw</bpmn2:target>
  </bpmn2:relationship>
</bpmn2:definitions>