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

背景:后置流程。例如: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;
    }
}
相关推荐
CC__xy6 小时前
demo 通讯录 + 城市选择器 (字母索引左右联动 ListItemGroup+AlphabetIndexer)笔记
windows
LZQqqqqo13 小时前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
季春二九13 小时前
Windows 11 首次开机引导(OOBE 阶段)跳过登录微软账户,创建本地账户
windows·microsoft
芥子沫14 小时前
Jenkins常见问题及解决方法
windows·https·jenkins
cpsvps_net1 天前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋1 天前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps1 天前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
网硕互联的小客服2 天前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix2 天前
implement copy file content to clipboard on Windows
windows·stm32·单片机
许泽宇的技术分享2 天前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net