可选择的状态节点state例子

流程定义文件:

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

<process key="forever" name="StateChoice" xmlns="http://jbpm.org/4.3/jpdl">
   <start g="137,74,48,48" name="start1">
      <transition g="-125,-17" name="to wait for response" to="wait for response"/>
   </start>
   <state g="85,219,157,52" name="wait for response">
      <transition g="-56,-14" name="accept" to="submit document"/>
      <transition g="9,-6" name="reject" to="try again"/>
   </state>
   <state g="26,400,121,52" name="submit document">
      <transition name="to end1" to="end1" g="-47,-17"/>
   </state>
   <state g="185,394,126,52" name="try again">
      <transition name="to end1" to="end1" g="-47,-17"/>
   </state>
   <end name="end1" g="145,508,48,48"/>
   
</process>

 图:

 

 测试类StateChoice:

 

package org.forever.jbpm;

import java.util.List;
import java.util.UUID;

import org.jbpm.api.Configuration;
import org.jbpm.api.Deployment;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class StateChoice {

	ProcessEngine processEngine = null;
	RepositoryService repositoryService = null;
	ExecutionService executionService = null;
	TaskService taskService = null;
	HistoryService historyService = null;
	ManagementService managementService = null;
	String pid = null;

	@Before
	public void initialize() {
		// 1.获取所有的服务
		processEngine = new Configuration().buildProcessEngine();
		repositoryService = processEngine.getRepositoryService();
		executionService = processEngine.getExecutionService();
		taskService = processEngine.getTaskService();
		historyService = processEngine.getHistoryService();
		managementService = processEngine.getManagementService();
	}

	@Test
	// 部署流程
	public void testDeploy() {
		// 2.部署流程,获取流程定义id(格式:key-version)
		String deployId = repositoryService.createDeployment()
				.addResourceFromClasspath(
						"org/forever/jbpm/jpdl/StateChoice.jpdl.xml").deploy();
		System.out.println("流程定义id:" + deployId);
	}

	// 启动流程实例
	@Test
	public void testStart() {
		// 3.启动流程实例,绑定业务key,key最好是唯一的
		// starts a new process instance in the latest version of the given
		// process definition
		UUID uuid = UUID.randomUUID();
		ProcessInstance processInstance = executionService
				.startProcessInstanceByKey("forever", uuid.toString());
		pid = processInstance.getId();
		System.out.println(pid);
	}

	// 获取流程实例
	public ProcessInstance getProcessInstance() {
		//对于用户给的key一般是某种唯一的key,可以这样设计,给每个表的数据生成一个uuid作为唯一标识
		//获取指定的流程实例
		ProcessInstance processInstance = executionService
				.createProcessInstanceQuery().processInstanceKey("8d0e225c-d9b6-488f-abe3-c899ea59c864")
				.uniqueResult();
		Assert.assertNotNull(processInstance);
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getName());
		System.out.println(processInstance.getKey());
		System.out.println(processInstance.getPriority());
		System.out.println(processInstance.getState());
		
		return processInstance;
	
	
	}

	//让流程实例向下流转
	@Test
	public void testSignal() {
		ProcessInstance processInstance = getProcessInstance();
		Assert.assertNotNull(processInstance);
		//根据具体的业务进行流转
		 processInstance = executionService.signalExecutionById(processInstance.getId(),"accept"); 
	}
	
	//让流程实例继续向下流转
	@Test
	public void testSignalNext() {
		ProcessInstance processInstance = getProcessInstance();
		Assert.assertNotNull(processInstance);
		//根据具体的业务进行流转
		//当流程实例的任务完成之后,数据库会自动删除该实例
		 processInstance = executionService.signalExecutionById(processInstance.getId());
		 getProcessInstance();
	}
	
	//删除流程实例
	@Test
	public void testDelDeployment(){
		List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
		for (Deployment deployment : deployments) {
			System.out.println(deployment.getId());
			//侧地删除与之相关的内容
			repositoryService.deleteDeploymentCascade(deployment.getId());
		}
	}

	//从头执行到尾
	public void main(String[] args) {

		// 1.获取所有的服务
		ProcessEngine processEngine = new Configuration().buildProcessEngine();
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		ExecutionService executionService = processEngine.getExecutionService();
		// TaskService taskService = processEngine.getTaskService();
		// HistoryService historyService = processEngine.getHistoryService();
		// ManagementService managementService =
		// processEngine.getManagementService();

		// 2.部署流程,获取流程定义id(格式:key-version)
		String deployId = repositoryService.createDeployment()
				.addResourceFromClasspath("StateChoice.jpdl.xml").deploy();

		System.out.println("流程定义id:" + deployId);

		// 3.启动流程实例,绑定业务key
		// starts a new process instance in the latest version of the given
		// process definition
		UUID uuid = UUID.randomUUID();
		ProcessInstance processInstance = executionService
				.startProcessInstanceByKey("forever", uuid.toString());
		//String pid = processInstance.getId();// 获取绑定的key
		// 流向到达了wait for response,等待状态
		System.out.println(processInstance.isActive("wait for response"));
		// 触发到下一个分支状态
		String executionId = processInstance.findActiveExecutionIn(
				"wait for response").getId();
		System.out.println(executionId);
		// 向指定的名字外向转移
		processInstance = executionService.signalExecutionById(executionId,
				"accept");
		System.out.println(processInstance.isActive("submit document"));
		System.out.println("是否结束:" + processInstance.isEnded());
		processInstance = executionService.signalExecutionById(processInstance
				.findActiveExecutionIn("submit document").getId());
		System.out.println("是否结束:" + processInstance.isEnded());
		// 删除流程实例
		repositoryService.deleteDeploymentCascade(deployId);
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值