Activiti中动态流程生成

  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.activiti.bpmn.BpmnAutoLayout;  
  7. import org.activiti.bpmn.model.BpmnModel;  
  8. import org.activiti.bpmn.model.EndEvent;  
  9. import org.activiti.bpmn.model.ExclusiveGateway;  
  10. import org.activiti.bpmn.model.Process;  
  11. import org.activiti.bpmn.model.SequenceFlow;  
  12. import org.activiti.bpmn.model.StartEvent;  
  13. import org.activiti.bpmn.model.UserTask;  
  14. import org.activiti.engine.ProcessEngine;  
  15. import org.activiti.engine.ProcessEngineConfiguration;  
  16. import org.activiti.engine.repository.Deployment;  
  17. import org.activiti.engine.runtime.ProcessInstance;  
  18. import org.activiti.engine.task.Task;  
  19. import org.apache.commons.io.FileUtils;  
  20. import org.apache.commons.lang.StringUtils;  
  21. import org.junit.Assert;  
  22.   
  23. /** 
  24.  *  
  25.  */  
  26.   
  27. /**   
  28.  * @ClassName: ActivitiTest01 
  29.  * @Description: TODO(activiti 工作流程图自动生成) 
  30.  * @author liang 
  31.  * @date 2016年4月20日 上午8:32:56 
  32.  * 
  33.  */  
  34. public class ActivitiTest01 {  
  35.   
  36.     public static void main(String[] args) {  
  37.         try {  
  38.             test01();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43.       
  44.     public static void test01() throws IOException {  
  45.         System.out.println(".........start...");  
  46.         ProcessEngine processEngine=getProcessEngine();  
  47.           
  48.         // 1. Build up the model from scratch  
  49.         BpmnModel model = new BpmnModel();    
  50.         Process process=new Process();  
  51.         model.addProcess(process);   
  52.         final String PROCESSID ="process01";  
  53.         final String PROCESSNAME ="测试01";  
  54.         process.setId(PROCESSID);   
  55.         process.setName(PROCESSNAME);  
  56.           
  57.         process.addFlowElement(createStartEvent());    
  58.         process.addFlowElement(createUserTask("task1", "节点01", "candidateGroup1"));   
  59.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway1"));   
  60.         process.addFlowElement(createUserTask("task2", "节点02", "candidateGroup2"));   
  61.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway2"));   
  62.         process.addFlowElement(createUserTask("task3", "节点03", "candidateGroup3"));   
  63.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway3"));   
  64.         process.addFlowElement(createUserTask("task4", "节点04", "candidateGroup4"));  
  65.         process.addFlowElement(createEndEvent());    
  66.           
  67.         process.addFlowElement(createSequenceFlow("startEvent", "task1", "", ""));   
  68.         process.addFlowElement(createSequenceFlow("task1", "task2", "", ""));   
  69.         process.addFlowElement(createSequenceFlow("task2", "createExclusiveGateway1", "", ""));  
  70.         process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task1", "不通过", "${pass=='2'}"));  
  71.         process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task3", "通过", "${pass=='1'}"));   
  72.         process.addFlowElement(createSequenceFlow("task3", "createExclusiveGateway2", "", ""));  
  73.         process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task2", "不通过", "${pass=='2'}"));  
  74.         process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task4", "通过", "${pass=='1'}"));  
  75.         process.addFlowElement(createSequenceFlow("task4", "createExclusiveGateway3", "", ""));  
  76.         process.addFlowElement(createSequenceFlow("createExclusiveGateway3", "task3", "不通过", "${pass=='2'}"));  
  77.         process.addFlowElement(createSequenceFlow("createExclusiveGateway3", "endEvent", "通过", "${pass=='1'}"));  
  78.           
  79.         // 2. Generate graphical information    
  80.         new BpmnAutoLayout(model).execute();  
  81.           
  82.         // 3. Deploy the process to the engine    
  83.         Deployment deployment = processEngine.getRepositoryService().createDeployment().addBpmnModel(PROCESSID+".bpmn", model).name(PROCESSID+"_deployment").deploy();    
  84.              
  85.         // 4. Start a process instance    
  86.         ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(PROCESSID);   
  87.           
  88.         // 5. Check if task is available    
  89.         List tasks = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).list();  
  90.         Assert.assertEquals(1, tasks.size());         
  91.           
  92.         // 6. Save process diagram to a file      
  93.         InputStream processDiagram = processEngine.getRepositoryService().getProcessDiagram(processInstance.getProcessDefinitionId());    
  94.         FileUtils.copyInputStreamToFile(processDiagram, new File("/deployments/"+PROCESSID+".png"));    
  95.              
  96.         // 7. Save resulting BPMN xml to a file    
  97.         InputStream processBpmn = processEngine.getRepositoryService().getResourceAsStream(deployment.getId(), PROCESSID+".bpmn");    
  98.         FileUtils.copyInputStreamToFile(processBpmn,new File("/deployments/"+PROCESSID+".bpmn"));  
  99.           
  100.         System.out.println(".........end...");  
  101.     }  
  102.       
  103.     protected static ProcessEngine getProcessEngine(){  
  104.         ProcessEngine processEngine=ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()  
  105.                 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)  
  106.                 .setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activitiDB")  
  107.                 .setJdbcDriver("com.mysql.jdbc.Driver")  
  108.                 .setJdbcUsername("root")  
  109.                 .setJdbcPassword("123456")  
  110.                 .setDatabaseSchemaUpdate("true")  
  111.                 .setJobExecutorActivate(false)  
  112.                 .buildProcessEngine();  
  113.         return processEngine;  
  114.                   
  115.     }  
  116.   
  117.     /*任务节点*/  
  118.     protected static UserTask createUserTask(String id, String name, String candidateGroup) {  
  119.         List candidateGroups=new ArrayList();  
  120.         candidateGroups.add(candidateGroup);  
  121.         UserTask userTask = new UserTask();  
  122.         userTask.setName(name);  
  123.         userTask.setId(id);  
  124.         userTask.setCandidateGroups(candidateGroups);  
  125.         return userTask;  
  126.     }  
  127.   
  128.     /*连线*/  
  129.     protected static SequenceFlow createSequenceFlow(String from, String to,String name,String conditionExpression) {  
  130.         SequenceFlow flow = new SequenceFlow();  
  131.         flow.setSourceRef(from);  
  132.         flow.setTargetRef(to);  
  133.         flow.setName(name);  
  134.         if(StringUtils.isNotEmpty(conditionExpression)){  
  135.             flow.setConditionExpression(conditionExpression);  
  136.         }         
  137.         return flow;  
  138.     }  
  139.       
  140.     /*排他网关*/  
  141.     protected static ExclusiveGateway createExclusiveGateway(String id) {  
  142.         ExclusiveGateway exclusiveGateway = new ExclusiveGateway();  
  143.         exclusiveGateway.setId(id);  
  144.         return exclusiveGateway;  
  145.     }  
  146.   
  147.     /*开始节点*/  
  148.     protected static StartEvent createStartEvent() {  
  149.         StartEvent startEvent = new StartEvent();  
  150.         startEvent.setId("startEvent");  
  151.         return startEvent;  
  152.     }  
  153.   
  154.     /*结束节点*/  
  155.     protected static EndEvent createEndEvent() {  
  156.         EndEvent endEvent = new EndEvent();  
  157.         endEvent.setId("endEvent");  
  158.         return endEvent;  
  159.     }  
  160.       
  161. }  

你可能感兴趣的:(activiti)