天天看点

activiti流程图动态创建

具体代码:

[java]  view plain  copy  print ?

  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. public class ActivitiTest01 {  
  23.     public static void main(String[] args) {  
  24.         try {  
  25.             test01();  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.     public static void test01() throws IOException {  
  31.         System.out.println(".........start...");  
  32.         ProcessEngine processEngine=getProcessEngine();  
  33.         // 1. Build up the model from scratch  
  34.         BpmnModel model = new BpmnModel();    
  35.         Process process=new Process();  
  36.         model.addProcess(process);   
  37.         final String PROCESSID ="process01";  
  38.         final String PROCESSNAME ="测试01";  
  39.         process.setId(PROCESSID);   
  40.         process.setName(PROCESSNAME);  
  41.         process.addFlowElement(createStartEvent());    
  42.         process.addFlowElement(createUserTask("task1", "节点01", "candidateGroup1"));   
  43.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway1"));   
  44.         process.addFlowElement(createUserTask("task2", "节点02", "candidateGroup2"));   
  45.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway2"));   
  46.         process.addFlowElement(createUserTask("task3", "节点03", "candidateGroup3"));   
  47.         process.addFlowElement(createExclusiveGateway("createExclusiveGateway3"));   
  48.         process.addFlowElement(createUserTask("task4", "节点04", "candidateGroup4"));  
  49.         process.addFlowElement(createEndEvent());    
  50.         process.addFlowElement(createSequenceFlow("startEvent", "task1", "", ""));   
  51.         process.addFlowElement(createSequenceFlow("task1", "task2", "", ""));   
  52.         process.addFlowElement(createSequenceFlow("task2", "createExclusiveGateway1", "", ""));  
  53.         process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task1", "不通过", "${pass=='2'}"));  
  54.         process.addFlowElement(createSequenceFlow("createExclusiveGateway1", "task3", "通过", "${pass=='1'}"));   
  55.         process.addFlowElement(createSequenceFlow("task3", "createExclusiveGateway2", "", ""));  
  56.         process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task2", "不通过", "${pass=='2'}"));  
  57.         process.addFlowElement(createSequenceFlow("createExclusiveGateway2", "task4", "通过", "${pass=='1'}"));  
  58.         process.addFlowElement(createSequenceFlow("task4", "createExclusiveGateway3", "", ""));  
  59.         process.addFlowElement(createSequenceFlow("createExclusiveGateway3", "task3", "不通过", "${pass=='2'}"));  
  60.         process.addFlowElement(createSequenceFlow("createExclusiveGateway3", "endEvent", "通过", "${pass=='1'}"));  
  61.         // 2. Generate graphical information    
  62.         new BpmnAutoLayout(model).execute();  
  63.         // 3. Deploy the process to the engine    
  64.         Deployment deployment = processEngine.getRepositoryService().createDeployment().addBpmnModel(PROCESSID+".bpmn", model).name(PROCESSID+"_deployment").deploy();    
  65.         // 4. Start a process instance    
  66.         ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(PROCESSID);   
  67.         // 5. Check if task is available    
  68.         List<Task> tasks = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).list();  
  69.         Assert.assertEquals(1, tasks.size());         
  70.         // 6. Save process diagram to a file      
  71.         InputStream processDiagram = processEngine.getRepositoryService().getProcessDiagram(processInstance.getProcessDefinitionId());    
  72.         FileUtils.copyInputStreamToFile(processDiagram, new File("/deployments/"+PROCESSID+".png"));    
  73.         // 7. Save resulting BPMN xml to a file    
  74.         InputStream processBpmn = processEngine.getRepositoryService().getResourceAsStream(deployment.getId(), PROCESSID+".bpmn");    
  75.         FileUtils.copyInputStreamToFile(processBpmn,new File("/deployments/"+PROCESSID+".bpmn"));  
  76.         System.out.println(".........end...");  
  77.     }  
  78.     protected static ProcessEngine getProcessEngine(){  
  79.         ProcessEngine processEngine=ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()  
  80.                 .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)  
  81.                 .setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activitiDB")  
  82.                 .setJdbcDriver("com.mysql.jdbc.Driver")  
  83.                 .setJdbcUsername("root")  
  84.                 .setJdbcPassword("123456")  
  85.                 .setDatabaseSchemaUpdate("true")  
  86.                 .setJobExecutorActivate(false)  
  87.                 .buildProcessEngine();  
  88.         return processEngine;  
  89.     }  
  90.     protected static UserTask createUserTask(String id, String name, String candidateGroup) {  
  91.         List<String> candidateGroups=new ArrayList<String>();  
  92.         candidateGroups.add(candidateGroup);  
  93.         UserTask userTask = new UserTask();  
  94.         userTask.setName(name);  
  95.         userTask.setId(id);  
  96.         userTask.setCandidateGroups(candidateGroups);  
  97.         return userTask;  
  98.     }  
  99.     protected static SequenceFlow createSequenceFlow(String from, String to,String name,String conditionExpression) {  
  100.         SequenceFlow flow = new SequenceFlow();  
  101.         flow.setSourceRef(from);  
  102.         flow.setTargetRef(to);  
  103.         flow.setName(name);  
  104.         if(StringUtils.isNotEmpty(conditionExpression)){  
  105.             flow.setConditionExpression(conditionExpression);  
  106.         }         
  107.         return flow;  
  108.     }  
  109.     protected static ExclusiveGateway createExclusiveGateway(String id) {  
  110.         ExclusiveGateway exclusiveGateway = new ExclusiveGateway();  
  111.         exclusiveGateway.setId(id);  
  112.         return exclusiveGateway;  
  113.     }  
  114.     protected static StartEvent createStartEvent() {  
  115.         StartEvent startEvent = new StartEvent();  
  116.         startEvent.setId("startEvent");  
  117.         return startEvent;  
  118.     }  
  119.     protected static EndEvent createEndEvent() {  
  120.         EndEvent endEvent = new EndEvent();  
  121.         endEvent.setId("endEvent");  
  122.         return endEvent;  
  123.     }  
  124. }