`

JBPM-Decision的用法

    博客分类:
  • JBPM
阅读更多

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());
    }
}

 

文章出处:DIY部落(http://www.diybl.com/course/3_program/java/javajs/2007927/74805.html)

分享到:
评论
2 楼 nneverwei 2011-08-24  
xl6861989 写道
晕,怎么都是这个例子啊,这个是jbpm几啊?没有那个token的包包啊,我的是jbpm4.3

兄弟,我上面注明了是转载的啦,而且是那么多年前的了。现在的话,上JBPM官网找官方资料看看会更清晰吧。。。
1 楼 xl6861989 2011-08-23  
晕,怎么都是这个例子啊,这个是jbpm几啊?没有那个token的包包啊,我的是jbpm4.3

相关推荐

Global site tag (gtag.js) - Google Analytics