diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java index bf877ca11..0d9d35a26 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java @@ -11,6 +11,7 @@ import club.joylink.rtss.util.PinYinUtil; import lombok.Getter; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.similarity.JaroWinklerSimilarity; +import org.springframework.util.CollectionUtils; import java.util.List; import java.util.Map; @@ -31,16 +32,18 @@ public enum ExtractRule { if (sourceStr == null || sourceStr.length == 0) { throw new IllegalArgumentException("定位参数不缺失"); } + // 这里可能需要做对入参做一些处理 String stationName = sourceStr[0]; + return findDevice(simulation, MapElement.DeviceType.STATION,Station.class,stationName); - Optional stationOptional = simulation.getRepository().getStationList().stream() + /* Optional stationOptional = simulation.getRepository().getStationList().stream() .filter(station -> Objects.equals(stationName, PinYinUtil.toPinYin(station.getName()))) .findFirst(); if (stationOptional.isPresent()) { return stationOptional.get(); } - throw new IllegalArgumentException("未找到【" + stationName + "】车站"); + throw new IllegalArgumentException("未找到【" + stationName + "】车站");*/ } }, ROUTE_NAME_EXTRACT("根据进路名称匹配进路") { @@ -51,14 +54,14 @@ public enum ExtractRule { } // 这里可能对X、S做处理 String routeName = sourceStr[0]; - - Optional routeOptional = simulation.getRepository().getRouteList().stream() + return findDevice(simulation, MapElement.DeviceType.ROUTE,Route.class,routeName); +/* Optional routeOptional = simulation.getRepository().getRouteList().stream() .filter(route -> Objects.equals(route.getName(), routeName)) .findFirst(); if (routeOptional.isPresent()) { return routeOptional.get(); } - throw new IllegalArgumentException("未找到【" + routeName + "】进路"); + throw new IllegalArgumentException("未找到【" + routeName + "】进路");*/ } }, ROUTE_SIGNAL_EXTRACT("根据起始、终点信号机匹配进路") { @@ -91,8 +94,24 @@ public enum ExtractRule { } String stationName = sourceStr[0], upDown = sourceStr[1]; Station station = (Station) STATION_NAME_EXTRACT.matchParam(simulation, stationName); - boolean right = "SHANG".equals(upDown); - return station.getStandOf(right); + Boolean right = (Boolean) UP_DOWN_WAY.matchParam(simulation,upDown); + List stands = station.getStandOf(right); + if(CollectionUtils.isEmpty(stands)){ + throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s-%s],上下行[%s-%s]",stationName,station.getCode(),upDown,right)); + } + return station.getStandOf(right).get(0); + + } + },UP_DOWN_WAY("车辆上下行"){ + @Override + public Object matchParam(Simulation simulation, String... sourceStr) { + String way = sourceStr[0]; + if(StringUtils.containsAnyIgnoreCase(way,"shang")){ + return true; + }else if(StringUtils.containsAnyIgnoreCase(way,"xia")){ + return false; + } + throw new IllegalArgumentException(String.format("不能解析上下行[%s]",way)); } }, ; @@ -107,4 +126,20 @@ public enum ExtractRule { } public abstract Object matchParam(Simulation simulation, String... sourceStr); + + private static MapNamedElement findDevice(Simulation simulation, MapElement.DeviceType dt, Class eleClass,String matchVal){ + List eleList = simulation.getRepository().getListByType(dt,eleClass); + Map elementMap = Maps.newHashMap(); + for (MapNamedElement ele : eleList) { + String namePinYin = PinYinUtil.toPinYin(ele.getName()); + double ratio = StrUtils.getJaroWinklerSimilarityRatio(namePinYin,matchVal); + elementMap.put(ratio,ele); + } + Double d = elementMap.keySet().stream().mapToDouble(k->k).max().orElse(0D); + MapNamedElement nameEle = elementMap.get(d); + if(Objects.isNull(nameEle)){ + return null; + } + return nameEle; + } }