Activiti的流程定義文件解析

在實現流程的模擬運行時,需獲取流程定義中的相關活動節點信息,故需對流程定義文件進行解析。 java

此處對流程定義文件的解析,主要是經過參考Activiti流程部署的源碼來實現。 數據庫

代碼以下: ui

/**
     * <p>
     * 解析流程定義文件;對於Context設置引擎配置能夠將流程定義解析操做設置爲命令,這樣執行時Context將會自動注入引擎配置
     * <p>
     * @author zhangping
     * @param file 流程定義資源文件 zip壓縮文件
     * @param filePath 流程定義文件(.bpmn)的類路徑
     * @return
     */
    public void bpmnParse(File file, String fileClassPath) {
        try {
            Context.setProcessEngineConfiguration(getProcessEngineConfiguration());
            /* 加載流程定義文件 獲取 DeploymentBuilderImpl 因爲該步未執行 deploy方法 故不會執行流程定義與數據庫的交互
             * 能夠採用類路徑下的流程定義文件進行部署
             * 也能夠 經過zip文件部署 ,以下代碼
             */
            DeploymentBuilderImpl deploymentBuilder = (DeploymentBuilderImpl) getRepositoryService()
                    .createDeployment();
            if(file != null){
                InputStream in = new FileInputStream(file);
                ZipInputStream zis = new ZipInputStream(in);
                deploymentBuilder.addZipInputStream(zis);
            }
            if(fileClassPath != null){
                deploymentBuilder.addClasspathResource(fileClassPath);
            }
            
            // 獲取DeploymentEntity
            DeploymentEntity deploymentEntity = deploymentBuilder.getDeployment();
            // 設置部署實體的 信息 包括部署時間 是否爲新版本 這兩步能夠省略
            deploymentEntity.setDeploymentTime(ClockUtil.getCurrentTime());
            deploymentEntity.setNew(true);
            // 獲取部署實體的流程定義資源文件 因爲上文中調用了 addClasspathResource("process/Demo.bpmn") 會將資源注入給DeploymentEntity 故能夠取到
            Map<String, ResourceEntity> resources = deploymentEntity.getResources();
            // 獲取BpmnDeployer 都是由構建流程引擎時初始設置的
            BpmnDeployer bpmnDeployer = (BpmnDeployer) getProcessEngineConfiguration().
                    getDeploymentManager().getDeployers().get(0);
            // 流程bpmn解析類
            BpmnParser bpmnParser = bpmnDeployer.getBpmnParser();
            for (String resourceName : resources.keySet()) {
                //判斷資源文件是否屬於BPMN資源文件
                if (isBpmnResource(resourceName)) {
                    ResourceEntity resource = resources.get(resourceName);
                    byte[] bytes = resource.getBytes();
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
                    //執行解析
                    BpmnParse bpmnParse = bpmnParser.createParse().sourceInputStream(inputStream)
                            .deployment(deploymentEntity).name(resourceName);
                    bpmnParse.execute();
                    //解析後便可經過bpmnParse.getBpmnModel() 獲取流程模型信息 bpmnParse.getSequenceFlows() 獲取順序流信息
                    
                    //因爲流程定義文件中通常只定義一個流程,故get(0)
                    Collection<FlowElement> fes = bpmnParse.getBpmnModel()
                            .getProcesses().get(0).getFlowElements();
                    //fes即爲當前流程定義中的全部元素集合,能夠按需求進行相應處理
                    getFlowElement(fes, true, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
相關文章
相關標籤/搜索