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

背景:后置流程。例如: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;
    }
}
相关推荐
夏日米米茶3 小时前
Windows系统下npm报错node-gyp configure got “gyp ERR“解决方法
前端·windows·npm
虾球xz5 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
码上库利南5 小时前
Windows开机自动启动中间件
windows
nenchoumi311912 小时前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
love530love13 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
黄交大彭于晏14 小时前
发送文件脚本源码版本
java·linux·windows
vfvfb1 天前
bat批量去掉本文件夹中的文件扩展名
服务器·windows·批处理·删除扩展名·bat技巧
我命由我123451 天前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
PT_silver1 天前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft
爱炸薯条的小朋友1 天前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf