Flowable 流程连线多扭转条件设置

1.扭转条件常用表达式:

${var:get(myparam) == "1"}
${myparam  !="123"}
${var:eq(myparam,"same")} -- 两个值是否相等 
${var:containsAny(myparam, 1, 2)} -- 判断list中是否存在该1,2变量
${var:contains(myparam, 1)} -- 判断参数是否包含该1变量
${var:gt(myparam, 10)    
${var:gte(myparam, 10)}--变量值是否大于或大于等于
${var:empty(myparam)} -- 变量是否为空
${var:isNotEmpty(myparam) - - 变量不为空
${var:lt(myparam, 10) --变量是否小于
${var:lte(myparam, 10) --变量是否小于等于
${var:ne(myparam, "123")  --不等于
${var:getOrDefault(myparam, 123) > 100}变量是否大于给定的值,不大于取默认值然后与给定的值比较;判断myVar是否大于123 大于则取myVar 否则取默认值 与100比较

2.Flowable支持条件表达式转化方式(Flowable版本是6.4.2,调试是发现支持13中条件表达,按照下图中的规则写条件表达式,否则转化时会报错)

3.设置多条件demo:

4.bumn.xml样式:



5.bumn.xml and test code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1599529605337" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
    <startEvent id="_2" name="StartEvent"/>
    <userTask activiti:assignee="张三" activiti:exclusive="true" id="_3" name="UserTask"/>
    <userTask activiti:assignee="李四" activiti:exclusive="true" id="_4" name="UserTask"/>
    <sequenceFlow id="_5" sourceRef="_2" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${var:equals(name,"张三") && var:gt(money, 100)}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_6" sourceRef="_2" targetRef="_4">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${var:equals(name,"李四") && var:lt(money, 100)}]]></conditionExpression>
    </sequenceFlow>
    <endEvent id="_7" name="EndEvent"/>
    <sequenceFlow id="_8" sourceRef="_3" targetRef="_7"/>
    <sequenceFlow id="_9" sourceRef="_4" targetRef="_7"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="myProcess_1">
      <bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">
        <omgdc:Bounds height="32.0" width="32.0" x="25.0" y="225.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" id="Shape-_3">
        <omgdc:Bounds height="55.0" width="85.0" x="115.0" y="120.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_4" id="Shape-_4">
        <omgdc:Bounds height="55.0" width="85.0" x="105.0" y="300.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">
        <omgdc:Bounds height="32.0" width="32.0" x="330.0" y="230.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_5" id="BPMNEdge__5" sourceElement="_2" targetElement="_3">
        <omgdi:waypoint x="57.0" y="241.0"/>
        <omgdi:waypoint x="115.0" y="147.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="_2" targetElement="_4">
        <omgdi:waypoint x="57.0" y="241.0"/>
        <omgdi:waypoint x="105.0" y="327.5"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_3" targetElement="_7">
        <omgdi:waypoint x="200.0" y="147.5"/>
        <omgdi:waypoint x="330.0" y="246.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_4" targetElement="_7">
        <omgdi:waypoint x="190.0" y="327.5"/>
        <omgdi:waypoint x="330.0" y="246.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
@Test
    public void deploy() throws Exception {
        InputStream in = this.getClass().getResourceAsStream("/processes/multi-condition.bpmn");
        Deployment deployment = repositoryService.createDeployment()
                .name("多条件测试")
                .addInputStream("multi-condition.bpmn", in)
                .deploy();
        System.out.println(deployment.getId());
    }


    @Test
    public void start() {
        Map<String, Object> var = new HashMap<>();
        //${var.eq(name,"李四") && var:lt('money', 100)}
        var.put("name", "张三");
        var.put("money", 101);
        var.put("test", "test");
        ProcessInstance processInstance = runtimeService.
                startProcessInstanceById("myProcess_1:8:fd010351-f193-11ea-9686-e0d55e5900cd", var);
        System.out.println(processInstance.getId() + "====" + processInstance.getName());
    }

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flowable 是一个流程引擎框架,条件判断是其中非常重要的一部分。Flowable 中的条件判断主要分为两类:表达式条件判断和脚本条件判断。下面分别介绍这两种条件判断的源码实现。 1. 表达式条件判断 表达式条件判断是通过表达式来判断条件是否成立。Flowable 中支持的表达式语言有 EL 表达式、Juel 表达式、Mvel 表达式等。这里以 EL 表达式为例。 首先我们看到 Flowable 中的表达式条件判断是通过 org.flowable.bpmn.model.SequenceFlow 类中的 conditionExpression 属性来实现的。该属性的类型为 String,表示一个表达式字符串。 在执行条件判断时,Flowable 会将这个表达式字符串解析成一个 EL 表达式对象,并将当前执行上下文中的变量传递给该表达式对象进行计算,最终得出判断结果。下面是相关源码实现: ```java public class SequenceFlow extends FlowElement { protected String conditionExpression; // ... public boolean hasCondition() { return StringUtils.isNotEmpty(conditionExpression); } public Expression getConditionExpression() { if (hasCondition()) { ExpressionManager expressionManager = Context.getProcessEngineConfiguration().getExpressionManager(); return expressionManager.createExpression(conditionExpression); } return null; } // ... } ``` 可以看到,当 conditionExpression 属性不为空时,调用 getConditionExpression 方法会将该属性解析成一个 EL 表达式对象返回。 接下来看一下条件判断的执行过程。在 Flowable 中,条件判断是通过 org.flowable.engine.impl.bpmn.behavior.ConditionalEventBehavior 类的 execute 方法实现的。该方法中会先获取 SequenceFlow 对象的 conditionExpression 属性,然后调用 getConditionExpression 方法解析成一个 EL 表达式对象。最后将当前执行上下文中的变量传递给该表达式对象进行计算,得出判断结果。下面是相关源码实现: ```java public class ConditionalEventBehavior extends FlowNodeActivityBehavior { // ... @Override public void execute(ActivityExecution execution) throws Exception { // ... SequenceFlow outgoingSequenceFlow = (SequenceFlow) conditionalEvent.getOutgoingFlows().get(0); if (outgoingSequenceFlow.hasCondition()) { Expression conditionExpression = outgoingSequenceFlow.getConditionExpression(); Object value = conditionExpression.getValue(execution); if (value instanceof Boolean && (Boolean) value) { leave(execution); } } else { leave(execution); } // ... } // ... } ``` 可以看到,当 SequenceFlow 对象的 conditionExpression 属性不为空时,会调用 getConditionExpression 方法获取一个 EL 表达式对象,并将当前执行上下文中的变量传递给该表达式对象进行计算。最终得出的判断结果为 true 时,会继续执行下一步任务,否则不会执行。 2. 脚本条件判断 脚本条件判断是通过脚本来判断条件是否成立。Flowable 中支持的脚本语言有 Groovy、JavaScript、Python 等。这里以 Groovy 为例。 在 Flowable 中,脚本条件判断是通过 org.flowable.bpmn.model.ScriptTask 类中的 script 属性来实现的。该属性的类型为 String,表示一个 Groovy 脚本字符串。 执行脚本条件判断的过程与表达式条件判断类似,在这里不再赘述。需要注意的是,在使用脚本条件判断时,需要在流程引擎配置中添加对应的脚本引擎。下面是相关源码实现: ```java public class ScriptTask extends TaskWithFieldExtensions { protected String scriptFormat; protected String script; // ... public boolean hasScript() { return StringUtils.isNotEmpty(script); } public Object executeScript(VariableScope variableScope) { ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines(); ScriptEngine scriptEngine = scriptingEngines.getScriptEngine(scriptFormat); return scriptingEngines.evaluate(script, scriptEngine, variableScope); } // ... } ``` 可以看到,当 script 属性不为空时,调用 executeScript 方法会将该属性解析成一个 Groovy 脚本对象,并将当前执行上下文中的变量传递给该脚本对象进行计算,最终得出判断结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值