jbpm初歩



   :

package com.wlh.jbpm;
public class Document {

	private int id;
	private String title;
	private String content;
	private String creator;
	private Long processInstanceId;
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Long getProcessInstanceId() {
		return processInstanceId;
	}
	public void setProcessInstanceId(Long processInstanceId) {
		this.processInstanceId = processInstanceId;
	}
}

    :
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="T_Document" name="com.wlh.jbpm.Document">
	    <id name="id">
	      <generator class="native"/>
	    </id>
	    <property name="title"/>
	    <property name="content"/>
	    <property name="creator"/>
	    <property name="processInstanceId"/>
  </class>
</hibernate-mapping>


 hibernate.cfg.xml        
<mapping resource="com/wlh/jbpm/Document.hbm.xml"/>

HibernateUtils   :

package com.wlh.jbpm;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	private static SessionFactory sessionFactory;
	
	static{
		Configuration conf = new Configuration().configure();
		sessionFactory = conf.buildSessionFactory();
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
	}
	
	public static void  closeSession(Session session){
		if(session.isOpen()){
			session.close();
		}
	}
}


グラフィックインタフェースを使用して、src/main/jpdlの下にプロセス定義Process Definitionを作成します.
注:各タスクノードtask-node間にtaskラベルを追加するには(この手順はノードに参加者を割り当てることです)

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

<process-definition
  xmlns=""  name="jbpm01">
   <start-state name="start">
      <transition name="" to="    "></transition>
   </start-state>
   <task-node name="    ">
      <task>
        <assignment actor-id="  "/>
      </task>
      <transition name="" to="    "></transition>
   </task-node>
   <task-node name="    ">
       <task>
        <assignment actor-id="  "/>
      </task>
      <transition name="" to="    "></transition>
   </task-node>
   <task-node name="    ">
   	   <task>
        <assignment actor-id="  "/>
      </task>
      <transition name="" to="end1"></transition>
   </task-node>
   <end-state name="end1"></end-state>
</process-definition>

jbpmのAPIを使用してjbpm関連データベーステーブルを作成します.
a.データベースリンクの構成をMySQLの構成に変更する:
   com.mysql.jdbc.Driver
    jdbc:mysql://127.0.0.1/jbpm
    root
    bjsxt
    org.hibernate.dialect.MySQLDialect
b.src/main/configのhibernateを確保する.cfg.xmlのデータベース構成は現在のデータベースに対応しています.
データベースが作成されていない場合は、urlで構成するデータベース名に対応するデータベースを作成します.ここではjbpmです.
jdbc:hsqldb:mem:jbpm
package com.wlh.jbpm;

import org.jbpm.JbpmConfiguration;

import junit.framework.TestCase;

.
	
    //	  jbpm API,  JBPM      ,     hibernate.hbm2ddl.auto
	public void testCreateTable(){
		JbpmConfiguration.getInstance().createSchema();
	}
	
}

導入プロセスの定義:
package com.wlh.jbpm;

import junit.framework.TestCase;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;


public class Jbpm02_DeployProcessDefinition extends TestCase {

	static JbpmConfiguration jbpmConfiguration=JbpmConfiguration.getInstance();

	//            JBPM    
	public void testDeployDefinitionProcess(){
		//        ,  ProcessDefinition  
		ProcessDefinition processDefinition=ProcessDefinition.parseXmlResource("jbpm01/processdefinition.xml");
		//context     hibernate session     ,    hibernate session
		JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
		try{
			//            
			jbpmContext.deployProcessDefinition(processDefinition);
		}catch(Exception e){
			//       
			jbpmContext.setRollbackOnly();
		}finally{
			jbpmContext.close();
		}
		
		
		
	}
	
}

作成したばかりのプロセス定義に対応するプロセスインスタンスを作成します.
このプロシージャ・プロセス・インスタンスと文書は相互にバインドされます.
package com.wlh.jbpm;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;

public class Jbpm04_CreateProcessInstance extends TestCase {
	static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	
	public void testCreateProcessInstance(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		try{
		context.setSessionFactory(HibernateUtils.getSessionFactory());
		//       ProcessDefinition  
		ProcessDefinition processDefinition=context.getGraphSession().findLatestProcessDefinition("jbpm01");
		//              
		ProcessInstance processInstance=new ProcessInstance(processDefinition);
		//         
		context.save(processInstance);
		
		Session session=context.getSession();
		//           
		Document doc=(Document) session.load(Document.class, 4);
		doc.setProcessInstanceId(processInstance.getId());
		//           
		processInstance.getContextInstance().setVariable("documentId", doc.getId());
		processInstance.getContextInstance().setVariable("documentTitle", doc.getTitle());
		
		}catch(Exception e){
			context.setRollbackOnly();
		}finally{
			context.close();
		}
	}
}

公文書を提出する
package com.wlh.jbpm;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;

public class Jbpm05_SubmitDocument extends TestCase {

static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	
	//           
	public void testSubmitDocument(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		try{
		context.setSessionFactory(HibernateUtils.getSessionFactory());
		Document doc=(Document) context.getSession().load(Document.class, 4);
		long processInstanceId=doc.getProcessInstanceId();
		ProcessInstance instance=context.getProcessInstance(processInstanceId);
		
		//           
		instance.signal();
		}catch(Exception e){
			e.printStackTrace();
			context.setRollbackOnly();
		}finally{
			context.close();
		}
		
	}
}

プロセス内の文書が現在存在するノードを取得します.
package com.wlh.jbpm;

import junit.framework.TestCase;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.exe.ProcessInstance;

public class Jbpm06_CurrentNode extends TestCase{
	
	static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	//           
	public void testCurrentNode(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		try{
			context.setSessionFactory(HibernateUtils.getSessionFactory());
			Document doc = (Document)context.getSession().load(Document.class, 4);
			long processInstanceId = doc.getProcessInstanceId();
			ProcessInstance instance = context.getProcessInstance(processInstanceId);
			System.out.println("      ="+instance.getRootToken().getNode().getName());
		   //     ?        ,  instance.getRootToken().getNode().getName()      "    ",      ;
		   //this.assertEquals(instance.getRootToken().getNode().getName(),"    " );
		}catch(Exception e){
			e.printStackTrace();
			context.setRollbackOnly();
		}finally{
			context.close();
		}
		
	}
}

クエリーは、参加者が所有する文書のリストに移動します.
package com.wlh.jbpm;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class Jbpm07_SearchMyTaskList extends TestCase {

	static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	
	//                 【       】
	public void testSearchMyTaskList(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		try{
			context.setSessionFactory(HibernateUtils.getSessionFactory());
			
			printTask(context,"  ");
			printTask(context,"  ");
			printTask(context,"  ");
			
		}catch(Exception e){
			e.printStackTrace();
			context.setRollbackOnly();
		}finally{
			context.close();
		}
	}
	
	
	private void printTask(JbpmContext context,String actorId){
		
		//       (       )       ,               (       )    
		List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
		for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
			TaskInstance taskInstance = (TaskInstance) iter.next();
			//               ,               (      ,         id title)
			//            ?    Jbpm04_CreateProcessInstance.java               
			Integer docId = (Integer)taskInstance.getProcessInstance().getContextInstance().getVariable("documentId");
			String title = (String)taskInstance.getProcessInstance().getContextInstance().getVariable("documentTitle");
			System.out.println("    【"+actorId+"】      :[title="+title+",id="+docId+"]");
		}
		
	}
}

プロセスフローを次のノードにトリガーします.
package com.wlh.jbpm;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;

import junit.framework.TestCase;

public class Jbpm08_NextNode extends TestCase {
	
	static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
	
	//            
	public void testFlowToNextNode(){
		JbpmContext context = jbpmConfiguration.createJbpmContext();
		try{
			context.setSessionFactory(HibernateUtils.getSessionFactory());
			
			
			nextStep(context,"  ",4);//               
		}catch(Exception e){
			e.printStackTrace();
			context.setRollbackOnly();
		}finally{
			context.close();
		}
	}
	
	
	/**
	 *                      
	 * @param context
	 * @param actorId    id
	 * @param docId    id
	 */
	private void nextStep(JbpmContext context,String actorId,Integer docId){
		
		//       (       )       ,               (       )    
		List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
		for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
			TaskInstance taskInstance = (TaskInstance) iter.next();
//			
			//               ,               (      ,         id title)
			//            ?    Jbpm04_CreateProcessInstance.java               
			Integer oldDocId = (Integer)taskInstance.getProcessInstance().getContextInstance().getVariable("documentId");
			String title = (String)taskInstance.getProcessInstance().getContextInstance().getVariable("documentTitle");
			if(oldDocId.equals(docId)){
				//            ,        
				//       ,              
				taskInstance.end();//      
				System.err.println("  [title="+title+",id="+docId+"]   【"+actorId+"】    ,         ");
			}
		}
		
	}
}