JBPM-Decision的用法

本文介绍JBPM工作流引擎中的决策状态使用方法,通过一个考试成绩评判的例子展示了如何定义决策状态及其对应的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Decision决定状态。Decision和Node一样,都是即时状态,而不是等待状态。Decision的多个离开转向transition中可以设置条件。业务程序实例执行到Decision状态时,会依次计算所有的离开转向的条件,遇到条件为真的转向时,就会立即执行这个转向。如果没有为转向设置条件,那么这个转向的条件值就是true。Decision是功能最强大的即时状态。

processdefinition.xml

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

<process-definition
  
xmlns="urn:jbpm.org:jpdl-3.1"  name="example_8">
   
<start-state name="考试">
      
<transition name="" to="评判"/>
   
</start-state>
   
<decision name="评判">
         
<handler class="com.wide.example8.JudgeDecisionHandler"/>
      
<transition name="升学" to="进大学深造"/>
      
<transition name="复习" to="复习备考"/>
      
<transition name="辍学" to="闯荡江湖"/>
   
</decision>
   
<state name="进大学深造">
      
<transition name="" to="End"/>
   
</state>
   
<state name="复习备考">
      
<transition name="" to="End"/>
   
</state>
   
<state name="闯荡江湖">
      
<transition name="" to="End"/>
   
</state>
   
<end-state name="End"></end-state>
</process-definition>
JudgeDecisionHandler.java
package com.wide.example8;

import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.node.DecisionHandler;

public class JudgeDecisionHandler implements DecisionHandler {

    
public String decide(ExecutionContext executionContext) throws Exception {
        
int score = (Integer)executionContext.getContextInstance().getVariable("score");
        
        
if(score < 30)
            
return "辍学";
        
else if(score < 60)
            
return "复习";
        
else
            
return "升学";
    }


}

测试(JUnit4)

 

package com.sample;

import static org.junit.Assert.assertEquals;

import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.junit.Before;
import org.junit.Test;

public class Example8 {
    
private ProcessDefinition processDefinition = null;

    @Before
    
public void init() {
        processDefinition 
= ProcessDefinition
                .parseXmlResource(
"example_8/processdefinition.xml");
    }


    @Test
    
public void process1() {
        ProcessInstance processInstance 
= new ProcessInstance(processDefinition);

        Token token 
= processInstance.getRootToken();
        processInstance.getContextInstance().setVariable(
"score"80);
        token.signal();
        assertEquals(
"进大学深造", token.getNode().getName());
        token.signal();
        assertEquals(
"End", token.getNode().getName());
    }


    @Test
    
public void process2() {
        ProcessInstance processInstance 
= new ProcessInstance(processDefinition);

        Token token 
= processInstance.getRootToken();
        processInstance.getContextInstance().setVariable(
"score"50);
        token.signal();
        assertEquals(
"复习备考", token.getNode().getName());
        token.signal();
        assertEquals(
"End", token.getNode().getName());
    }


    @Test
    
public void process3() {
        ProcessInstance processInstance 
= new ProcessInstance(processDefinition);

        Token token 
= processInstance.getRootToken();
        processInstance.getContextInstance().setVariable(
"score"10);
        token.signal();
        assertEquals(
"闯荡江湖", token.getNode().getName());
        token.signal();
        assertEquals(
"End", token.getNode().getName());
    }

}

 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值