【递归方式,流程回路检测】

背景:后置流程。例如:task1配置后置流程task2,task3。task3配置后置流程task4

问题:需要解决的问题配置时候防止回路,造成死循环:task1配置后置流程task2,task2配置后置流程task1

bash 复制代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoopDetection {

    public static void main(String[] args) {

        List<MyProcess> flowList = new ArrayList<>();
        Integer processId = 1001;
        List<Integer> checkList = new ArrayList<>();
        Map<Integer,MyProcess> postMap = new HashMap<>();
        getAllPostProcess(flowList,processId,checkList,postMap);
        System.out.println("is ok");
    }

    private static MyProcess getProcess(Integer processId){
        // 模拟数据库的数据
        Map<Integer,MyProcess> mockDataBaseMap = new HashMap<>();
        mockDataBaseMap.put(1001,new MyProcess(1001,"process01","1002,1003,1004"));
        mockDataBaseMap.put(1002,new MyProcess(1002,"process02","1003"));
        mockDataBaseMap.put(1003,new MyProcess(1003,"process03","1004"));
        mockDataBaseMap.put(1004,new MyProcess(1004,"process04",null));
        return mockDataBaseMap.get(processId);
    }
    private static void getAllPostProcess(List<MyProcess> flowList, Integer processId, List<Integer> checkList, Map<Integer, MyProcess> postMap){
        MyProcess processById = null;
        if(postMap!=null){
            if(!postMap.containsKey(processId)){
                processById = getProcess(processId);
                postMap.put(processById.getId(),processById);
            }else {
                processById = postMap.get(processId);
            }
        }

        if (processById!=null && processById.getId()!=null){
            flowList.add(processById);
            if (checkList.contains(processId)){
                throw new RuntimeException("0,后置流程中存在死循环");
            }
            checkList.add(processId);
            if (!"".equals(processById.getPostProcess()) && processById.getPostProcess()!=null){
                for (String flowIdstr : processById.getPostProcess().split(",")) {
                    getAllPostProcess(flowList,Integer.parseInt(flowIdstr),checkList,postMap);
                }
            }
            checkList.remove(processId);
        }
    }
}

class MyProcess{
    private Integer id;
    private String name;
    private String postProcess;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPostProcess() {
        return postProcess;
    }

    public void setPostProcess(String postProcess) {
        this.postProcess = postProcess;
    }

    public MyProcess(Integer id, String name, String postProcess) {
        this.id = id;
        this.name = name;
        this.postProcess = postProcess;
    }
}
相关推荐
JAY看世界11 分钟前
Photoshop启动错误:找不到MSVCP140.dll的多步骤解决指南
windows·photoshop
van叶~2 小时前
仓颉语言实战——1. 类型
开发语言·windows·python·仓颉
小奥超人3 小时前
pdf有密码,如何实现pdf转换word?
windows·经验分享·pdf·办公技巧·pdf加密解密
IT管理圈6 小时前
50个JAVA常见代码大全:学完这篇从Java小白到架构师
java·windows·python
dntktop15 小时前
华为浏览器(HuaweiBrowser),简约高效上网更轻松
windows
LKID体17 小时前
pathlib:面向对象的文件系统路径
linux·网络·windows
Beekeeper&&P...18 小时前
java中list和map区别
java·windows·list
唐宋元明清218818 小时前
.NET Bioss相关数据读写
windows·外设
竹影卿心19 小时前
Java连接HANA数据库
java·数据库·windows
.m20 小时前
Windows配置IE浏览器不自动跳转到Edge
windows