文章

【若依】32、绘制流程图

DrawFlowChartDemo01.bpmn20.xml 可以实时绘制流程目前走到哪一步了

1 绘制普通流程图

根据流程定义,绘制流程图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Test
void generateDiagram01() throws IOException {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("DrawFlowChartDemo01").latestVersion().singleResult();
    // 绘制流程图的对象
    BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
    // 绘制图片的生成器
    DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
    // 所有已经执行过的活动 ID,将之保存在这个集合中
    List<String> highLightedActivities = new ArrayList<>();
    // 所有已经执行过的线条 ID,将之保存在这个集合中
    List<String> highLightedFlows  = new ArrayList<>();
    // 缩放因子
    double scaleFactor = 1.0;
    // 绘制连接线时,是否绘制对应的文本
    boolean drawSequenceFlowNameWithNoLabelDI = true;
    ProcessEngineConfiguration processEngineConfig = processEngine.getProcessEngineConfiguration();
    InputStream is = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "PNG", highLightedActivities, highLightedFlows, processEngineConfig.getActivityFontName(), processEngineConfig.getLabelFontName(), processEngineConfig.getAnnotationFontName(), processEngineConfig.getClassLoader(), scaleFactor, drawSequenceFlowNameWithNoLabelDI);
    // 将 IO 流解析为一张图片
    FileUtils.copyInputStreamToFile(is, new File("C:\\Users\\15235\\Downloads\\DrawFlowChartDemo01.png"));
    is.close();
}

DrawFlowChartDemo01.png

2 绘制正在执行的流程进度图

在绘制的时候,将已经执行完的活动高亮表示出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Test
void generateDiagram02() throws IOException {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("DrawFlowChartDemo01").latestVersion().singleResult();
    BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
    // 查询流程实例
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey("DrawFlowChartDemo01").singleResult();
    if (ObjectUtil.isNull(processInstance)) {
        // 说明目前没有正在执行的流程,直接返回即可
        return;
    }
    // 绘制图片的生成器
    DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
    // 所有已经执行过的活动 ID,将之保存在这个集合中
    List<String> highLightedActivities = new ArrayList<>();
    // 所有已经执行过的线条 ID,将之保存在这个集合中
    List<String> highLightedFlows  = new ArrayList<>();
    // 缩放因子
    double scaleFactor = 1.0;
    // 绘制连接线时,是否绘制对应的文本
    boolean drawSequenceFlowNameWithNoLabelDI = true;
    // 查询流程实例的所有活动信息
    List<ActivityInstance> activityInstanceList = runtimeService.createActivityInstanceQuery().processInstanceId(processInstance.getId()).list();
    for (ActivityInstance activityInstance : activityInstanceList) {
        if (activityInstance.getActivityType().equals("sequenceFlow")) {
            // 如果活动类型是 sequenceFlow,那么就加入到线条的 ID 中
            highLightedFlows.add(activityInstance.getActivityId());
        } else {
            // 否则就加入到高亮活动的 ID 中
            highLightedActivities.add(activityInstance.getActivityId());
        }
    }
    ProcessEngineConfiguration processEngineConfig = processEngine.getProcessEngineConfiguration();
    InputStream is = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "PNG", highLightedActivities, highLightedFlows, processEngineConfig.getActivityFontName(), processEngineConfig.getLabelFontName(), processEngineConfig.getAnnotationFontName(), processEngineConfig.getClassLoader(), scaleFactor, drawSequenceFlowNameWithNoLabelDI);
    FileUtils.copyInputStreamToFile(is, new File("C:\\Users\\15235\\Downloads\\DrawFlowChartDemo01.png"));
    is.close();
}

DrawFlowChartDemo01.png

3 绘制执行完毕的流程图

绘制历史流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Test
void generateDiagram03() throws IOException {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("DrawFlowChartDemo01").latestVersion().singleResult();
    BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
    // 查询流程实例
    HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("DrawFlowChartDemo01").singleResult();
    if (ObjectUtil.isNull(historicProcessInstance)) {
        // 说明目前没有执行完的流程,直接返回即可
        return;
    }
    // 绘制图片的生成器
    DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
    // 所有已经执行过的活动 ID,将之保存在这个集合中
    List<String> highLightedActivities = new ArrayList<>();
    // 所有已经执行过的线条 ID,将之保存在这个集合中
    List<String> highLightedFlows  = new ArrayList<>();
    // 缩放因子
    double scaleFactor = 1.0;
    // 绘制连接线时,是否绘制对应的文本
    boolean drawSequenceFlowNameWithNoLabelDI = true;
    // 查询流程实例的历史活动信息
    List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(historicProcessInstance.getId()).list();
    for (HistoricActivityInstance historicActivityInstance : historicActivityInstanceList) {
        if (historicActivityInstance.getActivityType().equals("sequenceFlow")) {
            // 如果活动类型是 sequenceFlow,那么就加入到线条的 ID 中
            highLightedFlows.add(historicActivityInstance.getActivityId());
        } else {
            // 否则就加入到高亮活动的 ID 中
            highLightedActivities.add(historicActivityInstance.getActivityId());
        }
    }
    ProcessEngineConfiguration processEngineConfig = processEngine.getProcessEngineConfiguration();
    InputStream is = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "PNG", highLightedActivities, highLightedFlows, processEngineConfig.getActivityFontName(), processEngineConfig.getLabelFontName(), processEngineConfig.getAnnotationFontName(), processEngineConfig.getClassLoader(), scaleFactor, drawSequenceFlowNameWithNoLabelDI);
    FileUtils.copyInputStreamToFile(is, new File("C:\\Users\\15235\\Downloads\\DrawFlowChartDemo01.png"));
    is.close();
}

DrawFlowChartDemo01.png

本文由作者按照 CC BY 4.0 进行授权