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();
        }
    }
相关文章
相关标签/搜索