| | |
| | | package com.zy.acs.manager.core.service.floyd; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zy.acs.common.constant.RedisConstant; |
| | | import com.zy.acs.common.utils.RedisSupport; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.exception.BusinessException; |
| | | import com.zy.acs.manager.core.service.astart.MapDataDispatcher; |
| | | import com.zy.acs.manager.manager.entity.Code; |
| | | import com.zy.acs.manager.manager.entity.CodeGap; |
| | | import com.zy.acs.manager.manager.entity.Route; |
| | |
| | | import com.zy.acs.manager.system.service.ConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | |
| | | |
| | | public static final double INF = 999999.0; |
| | | |
| | | private final RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | |
| | | @Value("${floyd.enable}") |
| | | private Boolean floydEnable; |
| | | @Autowired |
| | | private CodeService codeService; |
| | | @Autowired |
| | |
| | | return this.matrixHeader.get(idx); |
| | | } |
| | | |
| | | @PostConstruct |
| | | @SuppressWarnings("all") |
| | | @PostConstruct |
| | | public void generateMatrix() { |
| | | log.info("【FLOYD】正在计算矩阵数据......"); |
| | | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1).eq(Code::getDeleted, false)); |
| | | |
| | | int size = codeList.size(); |
| | | |
| | | matrixHeader = new ArrayList<>(size); |
| | | adjacencyMatrix = new Double[size][size]; |
| | | pathMatrix = new int[size][size]; |
| | | |
| | | for (int i = 0; i < size; i++) { |
| | | Code startCode = codeList.get(i); |
| | | matrixHeader.add(startCode.getId()); |
| | | for (int j = 0; j < size; j++) { |
| | | Code endCode = codeList.get(j); |
| | | Route route = routeService.findByCodeOfBoth(startCode.getId(), endCode.getId()); |
| | | CodeGap codeGap = codeGapService.findByCodeOfBoth(startCode.getId(), endCode.getId()); |
| | | |
| | | double distance = INF; |
| | | int path = -1; |
| | | |
| | | if (null != route && null != codeGap && codeGap.getDistance() > 0) { |
| | | if (i != j) { |
| | | distance = codeGap.getDistance(); |
| | | } |
| | | path = i; |
| | | } |
| | | |
| | | |
| | | adjacencyMatrix[i][j] = distance; |
| | | |
| | | pathMatrix[i][j] = path; |
| | | } |
| | | if (!floydEnable) { |
| | | return; |
| | | } |
| | | Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; |
| | | |
| | | floydMatrix = this.floydAlgorithm(adjacencyMatrix); |
| | | String floydHeaderMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_HEADER_FLAG, String.valueOf(lev)); |
| | | String floydPathMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_PATH_FLAG, String.valueOf(lev)); |
| | | String floydMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_FLAG, String.valueOf(lev)); |
| | | |
| | | if (!Cools.isEmpty(floydMatrixStr) && !Cools.isEmpty(floydPathMatrixStr) && !Cools.isEmpty(floydHeaderMatrixStr)) { |
| | | this.matrixHeader = JSON.parseObject(floydHeaderMatrixStr, new TypeReference<ArrayList<Long>>() {}); |
| | | this.pathMatrix = JSON.parseObject(floydPathMatrixStr, int[][].class); |
| | | this.floydMatrix = JSON.parseObject(floydMatrixStr, Double[][].class); |
| | | } else { |
| | | |
| | | log.info("【FLOYD】正在计算矩阵数据......"); |
| | | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1).eq(Code::getDeleted, false)); |
| | | |
| | | int size = codeList.size(); |
| | | |
| | | matrixHeader = new ArrayList<>(size); |
| | | adjacencyMatrix = new Double[size][size]; |
| | | pathMatrix = new int[size][size]; |
| | | |
| | | for (int i = 0; i < size; i++) { |
| | | Code startCode = codeList.get(i); |
| | | matrixHeader.add(startCode.getId()); |
| | | for (int j = 0; j < size; j++) { |
| | | Code endCode = codeList.get(j); |
| | | Route route = routeService.findByCodeOfBoth(startCode.getId(), endCode.getId()); |
| | | CodeGap codeGap = codeGapService.findByCodeOfBoth(startCode.getId(), endCode.getId()); |
| | | |
| | | double distance = INF; |
| | | int path = -1; |
| | | |
| | | if (null != route && null != codeGap && codeGap.getDistance() > 0) { |
| | | if (i != j) { |
| | | distance = codeGap.getDistance(); |
| | | } |
| | | path = i; |
| | | } |
| | | |
| | | |
| | | adjacencyMatrix[i][j] = distance; |
| | | |
| | | pathMatrix[i][j] = path; |
| | | } |
| | | } |
| | | |
| | | floydMatrix = this.floydAlgorithm(adjacencyMatrix); |
| | | |
| | | redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_HEADER_FLAG, String.valueOf(lev), JSON.toJSONString(matrixHeader)); |
| | | redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_PATH_FLAG, String.valueOf(lev), JSON.toJSONString(pathMatrix)); |
| | | redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_FLAG, String.valueOf(lev), JSON.toJSONString(floydMatrix)); |
| | | } |
| | | |
| | | } |
| | | |