From 4e005e4936039fca16bfd1679942daa416a21986 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 20 十一月 2025 14:27:47 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/common/utils/NavigateUtils.java | 121 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 121 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/zy/common/utils/NavigateUtils.java b/src/main/java/com/zy/common/utils/NavigateUtils.java
new file mode 100644
index 0000000..e324def
--- /dev/null
+++ b/src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -0,0 +1,121 @@
+package com.zy.common.utils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import org.springframework.stereotype.Component;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.core.exception.CoolException;
+import com.zy.common.model.NavigateNode;
+
+@Component
+public class NavigateUtils {
+
+ public synchronized List<NavigateNode> calcByStationId(Integer startStationId, Integer endStationId) {
+ NavigateSolution navigateSolution = new NavigateSolution();
+ List<List<NavigateNode>> stationMap = navigateSolution.getStationMap();
+
+ NavigateNode startNode = navigateSolution.findStationNavigateNode(stationMap, startStationId);
+ if (startNode == null){
+ throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勮妭鐐�");
+ }
+
+ NavigateNode endNode = navigateSolution.findStationNavigateNode(stationMap, endStationId);
+ if (endNode == null){
+ throw new CoolException("鏈壘鍒拌 缁堢偣 瀵瑰簲鐨勮妭鐐�");
+ }
+
+ NavigateNode res_node = navigateSolution.astarSearchJava(stationMap, startNode, endNode);
+ if (res_node == null) {
+ throw new CoolException("鏈壘鍒拌璺緞");
+ }
+
+ ArrayList<NavigateNode> list = new ArrayList<>();
+ // 浣跨敤 visited 闆嗗悎闃叉鐖堕摼鍑虹幇鐜鑷存寰幆锛屽悓鏃惰缃畨鍏ㄦ鏁颁笂闄�
+ HashSet<NavigateNode> visited = new HashSet<>();
+ int maxSteps = stationMap.size() * stationMap.get(0).size() + 5; // 瀹夊叏涓婇檺
+ int steps = 0;
+ while (res_node != null && visited.add(res_node) && steps++ < maxSteps) {
+ list.add(res_node);
+ res_node = res_node.getFather();//杩唬鎿嶄綔
+ }
+ if (steps >= maxSteps) {
+ throw new CoolException("璺緞鍥炴函瓒呭嚭瀹夊叏涓婇檺锛岀枒浼煎瓨鍦ㄧ埗閾惧惊鐜�");
+ }
+ Collections.reverse(list);
+ //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�)
+ for (NavigateNode navigateNode : list) {
+ //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆�
+ //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤��
+ navigateNode.setFather(null);
+ }
+
+ //鍘婚噸
+ HashSet<Integer> set = new HashSet<>();
+ List<NavigateNode> fitlerList = new ArrayList<>();
+ for(NavigateNode navigateNode : list){
+ JSONObject valuObject = JSON.parseObject(navigateNode.getNodeValue());
+ if(set.add(valuObject.getInteger("stationId"))){
+ fitlerList.add(navigateNode);
+ }
+ }
+
+ return fitlerList;
+ }
+
+ public synchronized List<NavigateNode> calcByTrackSiteNo(Integer startTrackSiteNo, Integer endTrackSiteNo) {
+ NavigateSolution navigateSolution = new NavigateSolution();
+ List<List<NavigateNode>> rgvTrackMap = navigateSolution.getRgvTrackMap();
+
+ NavigateNode startNode = navigateSolution.findTrackSiteNoNavigateNode(rgvTrackMap, startTrackSiteNo);
+ if (startNode == null){
+ throw new CoolException("鏈壘鍒拌 璧风偣 瀵瑰簲鐨勮妭鐐�");
+ }
+
+ NavigateNode endNode = navigateSolution.findTrackSiteNoNavigateNode(rgvTrackMap, endTrackSiteNo);
+ if (endNode == null){
+ throw new CoolException("鏈壘鍒拌 缁堢偣 瀵瑰簲鐨勮妭鐐�");
+ }
+
+ NavigateNode res_node = navigateSolution.astarSearchJava(rgvTrackMap, startNode, endNode);
+ if (res_node == null) {
+ throw new CoolException("鏈壘鍒拌璺緞");
+ }
+
+ ArrayList<NavigateNode> list = new ArrayList<>();
+ // 浣跨敤 visited 闆嗗悎闃叉鐖堕摼鍑虹幇鐜鑷存寰幆锛屽悓鏃惰缃畨鍏ㄦ鏁颁笂闄�
+ HashSet<NavigateNode> visited = new HashSet<>();
+ int maxSteps = rgvTrackMap.size() * rgvTrackMap.get(0).size() + 5; // 瀹夊叏涓婇檺
+ int steps = 0;
+ while (res_node != null && visited.add(res_node) && steps++ < maxSteps) {
+ list.add(res_node);
+ res_node = res_node.getFather();//杩唬鎿嶄綔
+ }
+ if (steps >= maxSteps) {
+ throw new CoolException("璺緞鍥炴函瓒呭嚭瀹夊叏涓婇檺锛岀枒浼煎瓨鍦ㄧ埗閾惧惊鐜�");
+ }
+ Collections.reverse(list);
+ //灏嗘瘡涓妭鐐归噷闈㈢殑fatherNode鑷充负null(鏂逛究鍚庣画璁$畻鏃剁埗鑺傜偣杩囧瀵艰嚧鏄剧ず鐨勮妭鐐瑰お澶�)
+ for (NavigateNode navigateNode : list) {
+ //鐖惰妭鐐硅缃负null锛屼笉褰卞搷璁$畻缁撴灉锛屼笉褰卞搷鍚庣画鎿嶄綔銆�
+ //姝ゆ搷浣滀粎涓哄悗缁帓鏌ュ鐞嗘彁渚涜瑙夋柟渚裤��
+ navigateNode.setFather(null);
+ }
+
+ //鍘婚噸
+ HashSet<Integer> set = new HashSet<>();
+ List<NavigateNode> fitlerList = new ArrayList<>();
+ for(NavigateNode navigateNode : list){
+ JSONObject valuObject = JSON.parseObject(navigateNode.getNodeValue());
+ if(set.add(valuObject.getInteger("trackSiteNo"))){
+ fitlerList.add(navigateNode);
+ }
+ }
+
+ return fitlerList;
+ }
+
+}
--
Gitblit v1.9.1