|  |  |  | 
|---|
|  |  |  | import com.zy.acs.common.utils.RedisSupport; | 
|---|
|  |  |  | import com.zy.acs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.acs.framework.exception.CoolException; | 
|---|
|  |  |  | import com.zy.acs.manager.core.domain.SortCodeDto; | 
|---|
|  |  |  | import com.zy.acs.manager.common.utils.MapDataUtils; | 
|---|
|  |  |  | import com.zy.acs.manager.core.service.astart.domain.DynamicNode; | 
|---|
|  |  |  | import com.zy.acs.manager.core.utils.RouteGenerator; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.Code; | 
|---|
|  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.ConcurrentLinkedQueue; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  | import java.util.stream.IntStream; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 6/6/2024 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private int[][] turnMatrix; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private String[][] cdaMatrix; | 
|---|
|  |  |  | private Double[][][] cdaMatrix; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Map<String, Boolean> routeCdaMap = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | redis.setValue(RedisConstant.AGV_MAP_ASTAR_DYNAMIC_FLAG, String.valueOf(lev), JSON.toJSONString(dynamicMatrix)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String[][] getCdaMatrix(Integer lev) { | 
|---|
|  |  |  | public Double[][][] getCdaMatrix(Integer lev) { | 
|---|
|  |  |  | lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); | 
|---|
|  |  |  | // redis | 
|---|
|  |  |  | if (null == this.cdaMatrix) { | 
|---|
|  |  |  | String cdaMatrixStr = redis.getValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev)); | 
|---|
|  |  |  | if (!Cools.isEmpty(cdaMatrixStr)) { | 
|---|
|  |  |  | this.cdaMatrix = JSON.parseObject(cdaMatrixStr, String[][].class); | 
|---|
|  |  |  | String[][] cdaStrMatrix = JSON.parseObject(cdaMatrixStr, String[][].class); | 
|---|
|  |  |  | this.cdaMatrix = MapDataUtils.preComputeCdaMatrix(cdaStrMatrix); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // init | 
|---|
|  |  |  | 
|---|
|  |  |  | return this.cdaMatrix; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void setCdaMatrix(Integer lev, String[][] cdaMatrix) { | 
|---|
|  |  |  | redis.setValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev), JSON.toJSONString(cdaMatrix)); | 
|---|
|  |  |  | this.cdaMatrix = cdaMatrix; | 
|---|
|  |  |  | public void setCdaMatrix(Integer lev, String[][] cdaStrMatrix) { | 
|---|
|  |  |  | redis.setValue(RedisConstant.AGV_MAP_ASTAR_CDA_FLAG, String.valueOf(lev), JSON.toJSONString(cdaStrMatrix)); | 
|---|
|  |  |  | this.cdaMatrix = MapDataUtils.preComputeCdaMatrix(cdaStrMatrix); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public int[][] getTurnMatrix(Integer lev) { | 
|---|
|  |  |  | 
|---|
|  |  |  | lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String[][] codeMatrix = getCodeMatrix(lev); | 
|---|
|  |  |  | if (null == codeMatrix || codeMatrix.length == 0) { | 
|---|
|  |  |  | return new DynamicNode[0][0]; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | DynamicNode[][] dynamicMatrix = new DynamicNode[codeMatrix.length][codeMatrix[0].length]; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (int i = 0; i < codeMatrix.length; i++) { | 
|---|
|  |  |  | 
|---|
|  |  |  | cdaArr.add(yIdxList.get(i)); | 
|---|
|  |  |  | cdaMatrix[i][j] = JSON.toJSONString(cdaArr); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Code currCode = codeService.selectByData(codeMatrix[i][j]); | 
|---|
|  |  |  | Code currCode = codeService.getCacheByData(codeMatrix[i][j]); | 
|---|
|  |  |  | if (null != currCode) { | 
|---|
|  |  |  | List<Double> cdaArr = new ArrayList<>(); | 
|---|
|  |  |  | cdaArr.add(currCode.getX()); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (CodeNodeType.NONE.val.equals(codeMatrix[i][j])) { | 
|---|
|  |  |  | turnMatrix[i][j] = TurnNodeType.NONE.val; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Code currCode = codeService.selectByData(codeMatrix[i][j]); | 
|---|
|  |  |  | Code currCode = codeService.getCacheByData(codeMatrix[i][j]); | 
|---|
|  |  |  | List<String> neighborCodeList = routeService.findCodeDataOfSingle(currCode.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | switch (neighborCodeList.size()) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Route route : routeList) { | 
|---|
|  |  |  | Code startCode = codeService.getById(route.getStartCode()); | 
|---|
|  |  |  | Code startCode = codeService.getCacheById(route.getStartCode()); | 
|---|
|  |  |  | int[] startCodeIdx = getCodeMatrixIdx(lev, startCode.getData()); | 
|---|
|  |  |  | Code endCode = codeService.getById(route.getEndCode()); | 
|---|
|  |  |  | Code endCode = codeService.getCacheById(route.getEndCode()); | 
|---|
|  |  |  | int[] codeMatrixIdx = getCodeMatrixIdx(lev, endCode.getData()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String routeKey = RouteGenerator.generateRouteKey(startCode.getData(), endCode.getData()); | 
|---|
|  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<String> queryCodeListFromDynamicNode(Integer lev, String nodeType) { | 
|---|
|  |  |  | if (Cools.isEmpty(nodeType)) { | 
|---|
|  |  |  | public List<int[]> getCodeMatrixIdxList(Integer lev, List<String> codeDataList) { | 
|---|
|  |  |  | if (Cools.isEmpty(codeDataList)) { | 
|---|
|  |  |  | return new ArrayList<>(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | lev = Optional.ofNullable(lev).orElse(MAP_DEFAULT_LEV); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DynamicNode[][] dynamicMatrix = getDynamicMatrix(lev); | 
|---|
|  |  |  | String[][] codeMatrix = this.getCodeMatrix(lev); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ConcurrentLinkedQueue<SortCodeDto> codeList = new ConcurrentLinkedQueue<>(); | 
|---|
|  |  |  | List<int[]> codeMatrixIdxList = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | IntStream.range(0, codeMatrix.length).parallel().forEach(i -> { | 
|---|
|  |  |  | Map<String, int[]> map = new HashMap<>(); | 
|---|
|  |  |  | Set<String> codeDataSet = new HashSet<>(codeDataList); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (int i = 0; i < codeMatrix.length; i++) { | 
|---|
|  |  |  | for (int j = 0; j < codeMatrix[i].length; j++) { | 
|---|
|  |  |  | DynamicNode dynamicNode = dynamicMatrix[i][j]; | 
|---|
|  |  |  | if (nodeType.equals(dynamicNode.getVehicle())) { | 
|---|
|  |  |  | codeList.add(new SortCodeDto(codeMatrix[i][j], dynamicNode.getSerial())); | 
|---|
|  |  |  | String codeData = codeMatrix[i][j]; | 
|---|
|  |  |  | if (codeDataSet.contains(codeData)) { | 
|---|
|  |  |  | map.put(codeData, new int[]{i, j}); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return codeList.stream() | 
|---|
|  |  |  | .sorted(Comparator.comparingInt(SortCodeDto::getSerial)) | 
|---|
|  |  |  | .map(SortCodeDto::getCode) | 
|---|
|  |  |  | .collect(Collectors.toList()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        List<SortCodeDto> codeList = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        for (int i = 0; i < codeMatrix.length; i++) { | 
|---|
|  |  |  | //            for (int j = 0; j < codeMatrix[i].length; j++) { | 
|---|
|  |  |  | //                DynamicNode dynamicNode = dynamicMatrix[i][j]; | 
|---|
|  |  |  | //                if (nodeType.equals(dynamicNode.getVehicle())) { | 
|---|
|  |  |  | //                    codeList.add(new SortCodeDto(codeMatrix[i][j], dynamicNode.getSerial())); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        codeList.sort(Comparator.comparingInt(SortCodeDto::getSerial)); | 
|---|
|  |  |  | //        return codeList.stream().map(SortCodeDto::getCode).collect(Collectors.toList()); | 
|---|
|  |  |  | for (String codeData : codeDataList) { | 
|---|
|  |  |  | int[] codeMatrixIdx = map.get(codeData); | 
|---|
|  |  |  | if (codeMatrixIdx != null) { | 
|---|
|  |  |  | codeMatrixIdxList.add(codeMatrixIdx); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return codeMatrixIdxList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void modifyDynamicMatrix(Integer lev, List<String> codeDataList, String vehicle) { | 
|---|
|  |  |  | this.modifyDynamicMatrix(lev, codeDataList, vehicle, false); | 
|---|
|  |  |  | public void modifyDynamicMatrix(Integer lev, List<int[]> codeIdxList, String vehicle) { | 
|---|
|  |  |  | this.modifyDynamicMatrix(lev, codeIdxList, vehicle, false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public synchronized void modifyDynamicMatrix(Integer lev, List<String> codeDataList, String vehicle, boolean reset) { | 
|---|
|  |  |  | public synchronized void modifyDynamicMatrix(Integer lev, List<int[]> codeIdxList, String vehicle, boolean reset) { | 
|---|
|  |  |  | if (Cools.isEmpty(vehicle)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!reset) { | 
|---|
|  |  |  | //            long time = System.currentTimeMillis() / 1000; | 
|---|
|  |  |  | int serial = 1; | 
|---|
|  |  |  | for (String codeData : codeDataList) { | 
|---|
|  |  |  | int[] codeMatrixIdx = getCodeMatrixIdx(lev, codeData); | 
|---|
|  |  |  | for (int[] codeMatrixIdx : codeIdxList) { | 
|---|
|  |  |  | dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]] = new DynamicNode(vehicle, serial); | 
|---|
|  |  |  | serial++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | setDynamicMatrix(lev, dynamicMatrix); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void clearDynamicMatrixByCodeList(Integer lev, List<String> codeDataList) { | 
|---|
|  |  |  | this.modifyDynamicMatrix(lev, codeDataList, DynamicNodeType.ACCESS.val); | 
|---|
|  |  |  | public void clearDynamicMatrixByCodeList(Integer lev, List<int[]> codeIdxList) { | 
|---|
|  |  |  | this.modifyDynamicMatrix(lev, codeIdxList, DynamicNodeType.ACCESS.val); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public int[][] filterMapData(int[][] mapMatrix, Integer lev, List<String> lockNodes) { | 
|---|