Merge branch 'test-training2' of https://git.code.tencent.com/lian-cbtc/rtss-server into test-training2-zhouyin

 Conflicts:
	src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java
	src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java
This commit is contained in:
tiger_zhou 2023-04-11 13:20:37 +08:00
commit cdd12fd1b8
5 changed files with 255 additions and 51 deletions

View File

@ -41,7 +41,7 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService {
// 参数处理出错 // 参数处理出错
return; return;
} }
paramArr[index] = paramExtractResults.get(paramRule.getIndexArr()[index]); paramArr[index] = paramExtractResults.get(paramRule.getIndexArr()[index] - 1); // 这里因为配置是从1开始需要减1做对应
} }
replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramArr)); replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramArr));
} }

View File

@ -124,25 +124,53 @@ public class VoiceParseServiceImpl implements VoiceParseService {
private void paramExtract(Simulation simulation, VoiceDiscriminateResult result) { private void paramExtract(Simulation simulation, VoiceDiscriminateResult result) {
List<ParamExtractRule> paramsRules = result.getRule().getParamsRules(); List<ParamExtractRule> paramsRules = result.getRule().getParamsRules();
List<ParamExtractResult> paramExtractResults = new ArrayList<>(paramsRules.size()); List<ParamExtractResult> paramExtractResults = new ArrayList<>(paramsRules.size());
ParamExtractResult extractResult = null;
String[] groupStrArr = null;
int groupSize = result.getGroupCount(); int groupSize = result.getGroupCount();
ParamExtractResult extractResult = null;
String[] originGroupArr = null, correctGroupArr = null;
for (ParamExtractRule rule : paramsRules) { for (ParamExtractRule rule : paramsRules) {
if (rule.getIndexArr() != null) { // 如果定位信息不为空 if (rule.getIndexArr() != null) { // 如果定位信息不为空
groupStrArr = new String[rule.getIndexArr().length]; originGroupArr = new String[rule.getIndexArr().length];
for (int index = 0, len = groupStrArr.length; index < len; index ++) { correctGroupArr = new String[rule.getIndexArr().length];
for (int index = 0, len = originGroupArr.length; index < len; index ++) {
if (groupSize < rule.getIndexArr()[index]) { if (groupSize < rule.getIndexArr()[index]) {
result.setSuccess(false); result.setSuccess(false);
result.setMsg("提取参数出错"); result.setMsg("提取参数出错");
return; return;
} }
groupStrArr[index] = result.getGroup(rule.getIndexArr()[index]); originGroupArr[index] = result.getGroup(rule.getIndexArr()[index]);
correctGroupArr[index] = result.getCorrectGroup(rule.getIndexArr()[index]);
} }
} else { // 如果定位信息为空则将参数置空
originGroupArr = null;
correctGroupArr = null;
} }
extractResult = new ParamExtractResult(); extractResult = rule.getParseRule().matchParam(simulation, originGroupArr, correctGroupArr);
extractResult.setValue(rule.getParseRule().matchParam(simulation, groupStrArr));
paramExtractResults.add(extractResult); paramExtractResults.add(extractResult);
// 对已纠正过的数据进行记录 TODO
handleCorrectGroup(result, extractResult, originGroupArr, rule.getIndexArr());
} }
result.setParamExtractResultList(paramExtractResults); result.setParamExtractResultList(paramExtractResults);
} }
/**
* 处理纠正过的Group信息
* @param result 最终结果新
* @param extractResult 处理过的参数信息
* @param originArr 原始group数组
* @param indexArr group对应索引
*/
private void handleCorrectGroup(VoiceDiscriminateResult result, ParamExtractResult extractResult, String[] originArr, Integer[] indexArr) {
if (indexArr == null || extractResult.getCorrectGroupMap() == null) {
return;
}
String origin = null, correct = null;
for (int index = 0, len = indexArr.length; index < len; index++) {
origin = originArr[index];
correct = extractResult.getCorrectGroupMap().get(origin);
if (StringUtils.hasText(correct)) {
result.setCorrectGroup(indexArr[index], correct);
}
}
}
} }

View File

@ -9,7 +9,6 @@ import club.joylink.rtss.simulation.cbtc.data.map.Route;
import club.joylink.rtss.simulation.cbtc.data.map.Station; import club.joylink.rtss.simulation.cbtc.data.map.Station;
import club.joylink.rtss.util.PinYinUtil; import club.joylink.rtss.util.PinYinUtil;
import lombok.Getter; import lombok.Getter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.similarity.JaroWinklerSimilarity; import org.apache.commons.text.similarity.JaroWinklerSimilarity;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -29,83 +28,172 @@ public enum ExtractRule {
STATION_NAME_EXTRACT("根据车站名称匹配车站") { STATION_NAME_EXTRACT("根据车站名称匹配车站") {
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (ArrayUtils.isEmpty(sourceStr)) { if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数不缺失"); throw new IllegalArgumentException("定位参数不缺失");
} }
ParamExtractResult result = new ParamExtractResult();
MapNamedElement mapNamedElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
mapNamedElement = findDeviceByCorrect(simulation, MapElement.DeviceType.STATION, correctArr[0]);
} else {
// 这里可能需要做对入参做一些处理 // 这里可能需要做对入参做一些处理
String stationName = sourceStr[0]; String stationName = originArr[0];
return findDevice(simulation, MapElement.DeviceType.STATION,Station.class,stationName); mapNamedElement = findDevice(simulation, MapElement.DeviceType.STATION, Station.class, stationName);
if (mapNamedElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(stationName, mapNamedElement.getName());
}
}
if (mapNamedElement != null) {
result.setValue(mapNamedElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到车站[%s]", originArr[0]));
} }
}, },
ROUTE_NAME_EXTRACT("根据进路名称匹配进路") { ROUTE_NAME_EXTRACT("根据进路名称匹配进路") {
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (ArrayUtils.isEmpty(sourceStr)) { if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数不缺失"); throw new IllegalArgumentException("定位参数不缺失");
} }
ParamExtractResult result = new ParamExtractResult();
MapNamedElement mapNamedElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
mapNamedElement = findDeviceByCorrect(simulation, MapElement.DeviceType.ROUTE, correctArr[0]);
} else {
// 这里可能对XS做处理 // 这里可能对XS做处理
String routeName = sourceStr[0]; String routeName = originArr[0];
return findDevice(simulation, MapElement.DeviceType.ROUTE,Route.class,routeName); mapNamedElement = findDevice(simulation, MapElement.DeviceType.ROUTE, Route.class, routeName);
if (mapNamedElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(routeName, mapNamedElement.getName());
}
}
if (mapNamedElement != null) {
result.setValue(mapNamedElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到进路[%s]", originArr[0]));
} }
}, },
ROUTE_SIGNAL_EXTRACT("根据起始、终点信号机匹配进路") { ROUTE_SIGNAL_EXTRACT("根据起始、终点信号机匹配进路") {
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (sourceStr == null || sourceStr.length < 2) { if (originArr == null || originArr.length < 2) {
throw new IllegalArgumentException("定位参数缺失"); throw new IllegalArgumentException("定位参数缺失");
} }
String startSignalName = sourceStr[0], endSignalName = sourceStr[1]; ParamExtractResult result = new ParamExtractResult();
MapNamedElement startSignal = ExtractRule.findDevice(simulation, MapElement.DeviceType.SIGNAL,Signal.class,startSignalName); // 查找起始信号机
MapNamedElement endSinal = ExtractRule.findDevice(simulation, MapElement.DeviceType.SIGNAL,Signal.class,endSignalName); MapNamedElement startSignal = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
startSignal = findDeviceByCorrect(simulation, MapElement.DeviceType.SIGNAL, correctArr[0]);
} else {
String signalName = originArr[0];
startSignal = findDevice(simulation, MapElement.DeviceType.SIGNAL, Signal.class, signalName);
if (startSignal != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(signalName, startSignal.getName());
}
}
// 查找终端信号机
MapNamedElement endSignal = null;
if (correctArr != null && correctArr[1] != null) { // 有正确值直接匹配
endSignal = findDeviceByCorrect(simulation, MapElement.DeviceType.SIGNAL, correctArr[1]);
} else {
String signalName = originArr[1];
endSignal = findDevice(simulation, MapElement.DeviceType.SIGNAL, Signal.class, signalName);
if (endSignal != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(signalName, endSignal.getName());
}
}
if (startSignal == null || endSignal == null) {
return result;
}
String startCode = startSignal.getCode(), endCode = endSignal.getCode();
// 查找进路
Optional<Route> routeOptional = simulation.getRepository().getRouteList().stream() Optional<Route> routeOptional = simulation.getRepository().getRouteList().stream()
.filter(route -> { .filter(route -> {
if (route.getStart() == null || route.getDestination() == null) { if (route.getStart() == null || route.getDestination() == null) {
return false; return false;
} }
return Objects.equals(startSignal.getCode(), route.getStart().getCode()) return Objects.equals(startCode, route.getStart().getCode()) || Objects.equals(endCode, route.getDestination().getCode());
|| Objects.equals(endSinal, route.getDestination().getCode());
}).findFirst(); }).findFirst();
if (routeOptional.isPresent()) { if (routeOptional.isPresent()) {
return routeOptional.get(); result.setValue(routeOptional.get());
return result;
} }
throw new IllegalArgumentException(String.format("未找到信号机始端[%s-%s],终端[%s-%s]",startSignalName, startSignal.getName(), endSignalName, endSinal.getName())); throw new IllegalArgumentException(String.format("未找到信号机始端[%s],终端[%s]", originArr[0], originArr[1]));
} }
}, },
STAND_STATION_UP_DOWN_EXTRACT("根据车站、上下行匹配站台") { STAND_STATION_UP_DOWN_EXTRACT("根据车站、上下行匹配站台") {
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (sourceStr == null || sourceStr.length < 2) { if (originArr == null || originArr.length < 2) {
throw new IllegalArgumentException("定位参数缺失"); throw new IllegalArgumentException("定位参数缺失");
} }
String stationName = sourceStr[0], upDown = sourceStr[1]; ParamExtractResult result = new ParamExtractResult();
Station station = (Station) STATION_NAME_EXTRACT.matchParam(simulation, new String[] {stationName}); // 车站匹配结果
Boolean right = (Boolean) UP_DOWN_WAY.matchParam(simulation,new String[]{upDown}); ParamExtractResult stationResult = STATION_NAME_EXTRACT.matchParam(simulation, new String[] { originArr[0] }, new String[] { correctArr[0] });
List<Stand> stands = station.getStandOf(right); result.setCorrectGroupMap(stationResult.getCorrectGroupMap());
// 上行匹配结果
ParamExtractResult upDownResult = UP_DOWN_WAY.matchParam(simulation, new String[] { originArr[1] }, new String[] { correctArr[1] });
result.setCorrectGroupMap(upDownResult.getCorrectGroupMap());
// 获取站台列表
List<Stand> stands = ((Station) stationResult.getValue()).getStandOf((Boolean) upDownResult.getValue());
if(CollectionUtils.isEmpty(stands)){ if(CollectionUtils.isEmpty(stands)){
throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s-%s],上下行[%s-%s]",stationName,station.getCode(),upDown,right)); throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s],上下行[%s]",originArr[0], originArr[1]));
} }
return station.getStandOf(right).get(0); result.setValue(stands.get(0));
return result;
} }
},UP_DOWN_WAY("匹配上下行"){ },
UP_DOWN_WAY("车辆上下行"){
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
String way = sourceStr[0]; if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数缺失");
}
ParamExtractResult result = new ParamExtractResult();
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
result.setValue(correctArr[0]);
} else {
String way = originArr[0];
if(StringUtils.containsIgnoreCase(way,"shang")) { if(StringUtils.containsIgnoreCase(way,"shang")) {
return true; result.setValue(true);
result.setCorrectGroupMap(way, "");
}else if(StringUtils.containsIgnoreCase(way,"xia")){ }else if(StringUtils.containsIgnoreCase(way,"xia")){
return false; result.setValue(false);
result.setCorrectGroupMap(way, "");
} }
throw new IllegalArgumentException(String.format("不能解析上下行[%s]",way));
} }
},SWITCH_NAME("道岔名称"){ throw new IllegalArgumentException(String.format("不能解析上下行[%s]", originArr[0]));
}
},
SWITCH_NAME("道岔名称"){
@Override @Override
public Object matchParam(Simulation simulation, String[] sourceStr) { public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
String swtichName = sourceStr[0]; if (originArr == null || originArr.length == 0) {
return ExtractRule.findDevice(simulation, MapElement.DeviceType.SWITCH,Switch.class,swtichName); throw new IllegalArgumentException("定位参数缺失");
}
ParamExtractResult result = new ParamExtractResult();
// 查找起始信号机
MapNamedElement switchElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
switchElement = findDeviceByCorrect(simulation, MapElement.DeviceType.SWITCH, correctArr[0]);
} else {
String switchName = originArr[0];
switchElement = findDevice(simulation, MapElement.DeviceType.SWITCH, Switch.class, switchName);
if (switchElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(switchName, switchElement.getName());
}
}
if (switchElement != null) {
result.setValue(switchElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到道岔[%s]", originArr[0]));
} }
}, },
; ;
@ -119,7 +207,15 @@ public enum ExtractRule {
this.description = description; this.description = description;
} }
public abstract Object matchParam(Simulation simulation, String[] sourceStr); /**
* 解析参数
*
* @param simulation 仿真信息
* @param originArr 原始的group信息
* @param correctArr 已经纠正过的group信息
* @return 解析结果
*/
public abstract ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr);
private static MapNamedElement findDevice(Simulation simulation, MapElement.DeviceType dt, Class<? extends MapNamedElement> eleClass,String matchVal){ private static MapNamedElement findDevice(Simulation simulation, MapElement.DeviceType dt, Class<? extends MapNamedElement> eleClass,String matchVal){
List<? extends MapNamedElement> eleList = simulation.getRepository().getListByType(dt,eleClass); List<? extends MapNamedElement> eleList = simulation.getRepository().getListByType(dt,eleClass);
@ -136,4 +232,21 @@ public enum ExtractRule {
} }
return nameEle; return nameEle;
} }
/**
* 根据正确的名称信息获取设备信息
*
* @param simulation 仿真
* @param type 设备类型
* @param matchVal 匹配信息
* @return 设备信息
*/
private static MapNamedElement findDeviceByCorrect(Simulation simulation, MapElement.DeviceType type, String matchVal) {
return simulation.getRepository().getDeviceMap().values().stream()
.filter(mapElement -> Objects.equals(mapElement.getDeviceType(), type))
.map(mapElement -> (MapNamedElement) mapElement)
.filter(mapElement -> Objects.equals(mapElement.getName(), matchVal))
.findFirst().orElse(null);
}
} }

View File

@ -1,6 +1,11 @@
package club.joylink.rtss.simulation.cbtc.discriminate; package club.joylink.rtss.simulation.cbtc.discriminate;
import lombok.Data; import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
/** /**
* 参数提取结果 * 参数提取结果
@ -8,9 +13,45 @@ import lombok.Data;
@Data @Data
public class ParamExtractResult { public class ParamExtractResult {
/**
* 矫正后的group信息
* key为传入信息
*/
private Map<String, String> correctGroupMap;
/** /**
* 参数解析出的相关数据 * 参数解析出的相关数据
*/ */
private Object value; private Object value;
/**
* 放入纠正结果
*
* @param origin 原始信息
* @param correct 纠错信息
*/
public void setCorrectGroupMap(String origin, String correct) {
if (StringUtils.isEmpty(origin) || StringUtils.isEmpty(correct)) {
return;
}
if (correctGroupMap == null) {
correctGroupMap = new HashMap<>();
}
correctGroupMap.put(origin, correct);
}
/**
* 放入纠正结果
*
* @param correctGroupMap 纠正过的map信息
*/
public void setCorrectGroupMap(Map<String, String> correctGroupMap) {
if (CollectionUtils.isEmpty(correctGroupMap)) {
return;
}
if (this.correctGroupMap == null) {
this.correctGroupMap = new HashMap<>();
}
this.correctGroupMap.putAll(correctGroupMap);
}
} }

View File

@ -3,6 +3,7 @@ package club.joylink.rtss.simulation.cbtc.discriminate;
import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import club.joylink.rtss.simulation.cbtc.member.SimulationMember;
import lombok.Data; import lombok.Data;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -81,4 +82,25 @@ public class VoiceDiscriminateResult {
} }
return matcher.group(index); return matcher.group(index);
} }
/**
* 根据索引获取正确的group
* @return
*/
public String getCorrectGroup(int index) {
return this.correctGroupStr == null ? null : this.correctGroupStr.get(index);
}
/**
* 给正确MAP中赋值
*
* @param index GROUP 索引
* @param correct 正确值
*/
public void setCorrectGroup(Integer index, String correct) {
if (this.correctGroupStr == null) {
this.correctGroupStr = new HashMap<>();
}
this.correctGroupStr.put(index, correct);
}
} }