From c02fa14d318cffb82a7aa948cd004a3af5cfb61d Mon Sep 17 00:00:00 2001 From: weizhihong Date: Tue, 18 Apr 2023 11:31:47 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E6=88=90=E5=91=98=E8=A7=A3=E6=9E=90=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discriminate/OperateParseServiceImpl.java | 65 ++++++++++++------- .../discriminate/ReplyParseServiceImpl.java | 42 +++++++----- .../VoiceDiscriminateService.java | 29 ++++++++- .../cbtc/discriminate/OperateResult.java | 10 --- 4 files changed, 91 insertions(+), 55 deletions(-) diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java index 37848c5a3..45f73f99d 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java @@ -44,11 +44,16 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { if (operateRule == null) { return; } - // 参数解析 List paramExtractResults = result.getParamExtractResultList(); Map paramExtractResultMap = IntStream.range(0,paramExtractResults.size()).boxed().collect(Collectors.toMap(i->++i,paramExtractResults::get)); OperateResult operateResult = new OperateResult(operateRule.getType()); int paramSize = paramExtractResults.size(); + // 成员解析 + List simulationMemberList = filterSimulationMember(simulation, result, paramExtractResultMap, operateRule.getMemberRule()); + if (CollectionUtils.isEmpty(simulationMemberList)) { + return; + } + // 参数解析 for (CommandParamRule paramRule : operateRule.getParamRules()) { ParamExtractResult[] paramArr = new ParamExtractResult[paramRule.getIndexArr().length]; for (int index = 0, len = paramArr.length; index < len; index ++) { @@ -67,33 +72,18 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { operateResult.putParam(getMethodParamName(paramRule.getParamIndex(), operateRule.getType().name()), val); } else { // 参数位置未指定 - return; } } - // 成员解析 - MemberRule memberRule = operateRule.getMemberRule(); - if (memberRule != null) { - List simulationMemberList = null; - if (memberRule.getIndex() == null) { - simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); - } else if (memberRule.getIndex() < paramExtractResults.size()) { - simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex())); - } else { - return; - } - if (CollectionUtils.isEmpty(simulationMemberList)) { - return; - } - operateResult.setMember(simulationMemberList.get(0)); - } // 执行操作 - try { - atsOperationDispatcher.execute(simulation, operateResult.getMember(), operateResult.getType().name(), operateResult.getParams()); - log.info("执行语音指令操作成功,操作[{}] 源指令[{}]",operateResult.getType().name(),result.getOriginContent()); - } catch (Exception e) { - log.error("执行操作失败[{}] 输入源指令[{}] msg[{}]",operateResult.getType().name(),result.getOriginContent(),e.getMessage(),e); - } + simulationMemberList.forEach(member -> { + try { + atsOperationDispatcher.execute(simulation, member, operateResult.getType().name(), operateResult.getParams()); + log.info("[{}]执行语音指令操作成功,操作[{}] 源指令[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent()); + } catch (Exception e) { + log.error("[{}]执行操作失败[{}] 输入源指令[{}] msg[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent(),e.getMessage(),e); + } + }); } /** @@ -115,7 +105,7 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { * @param operateName 操作名称 * @return 参数名称 */ - public String getMethodParamName(int index, String operateName) { + private String getMethodParamName(int index, String operateName) { OperateMethod handlerMethod = this.atsManager.getHandlerMethod(operateName); Parameter[] parameters = handlerMethod.getMethod().getParameters(); if (parameters.length < index) { @@ -123,4 +113,29 @@ public class OperateParseServiceImpl implements VoiceTransactionalService { } return parameters[index].getName(); } + + /** + * 获取执行操作的人员 + * @return 人员列表 + */ + private List filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result + , Map paramExtractResultMap, MemberRule memberRule) { + int paramSize = result.getParamExtractResultList().size(); + List simulationMemberList = null; + if (memberRule != null) { // 如果有指定成员解析规则,使用此规则解析 + if (memberRule.getIndex() == null) { + simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); + } else if (memberRule.getIndex() < paramSize) { + simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex())); + } + return simulationMemberList; // 返回解析结果 + } else { + simulationMemberList = result.getSimulationMemberList(); // 获取群组成员对象 + if (CollectionUtils.isEmpty(simulationMemberList) || simulationMemberList.size() > 1) { + return List.of(); + } else { + return simulationMemberList; + } + } + } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java index fe9f124b7..6d5d1765c 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java @@ -30,10 +30,16 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService { if (replyRule == null) { return; } + // 回复成员 + List simulationMemberList = filterSimulationMember(simulation, result, replyRule.getMemberRule()); + if (CollectionUtils.isEmpty(simulationMemberList)) { + return; + } + // 格式化回复语句 List paramExtractResults = result.getParamExtractResultList(); ReplyResult replyResult = new ReplyResult(replyRule.getParamRules().size()); - ParamExtractResult[] paramArr = null; int paramSize = paramExtractResults.size(); + ParamExtractResult[] paramArr = null; for (CommandParamRule paramRule : replyRule.getParamRules()) { paramArr = new ParamExtractResult[paramRule.getIndexArr().length]; for (int index = 0, len = paramArr.length; index < len; index ++) { @@ -45,31 +51,33 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService { } replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramArr)); } - - // 格式化回复语句 Object[] objArr = new Object[replyResult.getParamList().size()]; replyResult.getParamList().toArray(objArr); replyResult.setContent(String.format(replyRule.getMessageFormat(), objArr)); - // 匹配回复人员 - MemberRule memberRule = replyRule.getMemberRule(); + // 执行回复 + simulationMemberList.forEach(member -> conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null)); + } + + /** + * 获取回复成员列表 + * + * @param simulation 仿真 + * @param result 封装的结果信息 + * @param memberRule 成员解析规则 + * @return 回复成员列表 + */ + private List filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result, MemberRule memberRule) { + List paramExtractResults = result.getParamExtractResultList(); + List simulationMemberList = null; if (memberRule != null) { - List simulationMemberList = null; if (memberRule.getIndex() == null) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, null); } else if (memberRule.getIndex() < paramExtractResults.size()) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResults.get(memberRule.getIndex() - 1)); - } else { - return; } - if (CollectionUtils.isEmpty(simulationMemberList)) { - return; - } - replyResult.setSimulationMemberList(simulationMemberList); + return simulationMemberList; + } else { + return result.getSimulationMemberList(); } - - // 执行回复 - replyResult.getSimulationMemberList().forEach(member -> { - conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null); - }); } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java index 8899a227f..587d4c173 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java @@ -1,6 +1,7 @@ package club.joylink.rtss.services.voice.discriminate; import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationMember; import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.event.conversation.SimulationConversationGroupDissolveEvent; @@ -11,8 +12,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + /** * 语音识别服务类 @@ -56,6 +62,17 @@ public class VoiceDiscriminateService { correctSourceService.doExec(result, simulation);// 原信息纠错 } + /** + * 接受语音文件、资源等必要条件,解析开始 + */ + public void doAnalysis(Simulation simulation, String content, List memberList) { + VoiceDiscriminateResult result = voiceParseService.doParse(simulation, content); + result.setSimulationMemberList(memberList); // 匹配到人员集合 + operateParseService.doExec(result, simulation); // 指令执行 + replyParseService.doExec(result, simulation); // 回复执行 + correctSourceService.doExec(result, simulation);// 原信息纠错 + } + /** * 处理聊天信息 * @param event 聊天事件信息 @@ -63,8 +80,14 @@ public class VoiceDiscriminateService { @EventListener public void handleMessage(SimulationConversationGroupMessageEvent event) { ConversationGroup conversationGroup = event.getConversationGroup(); - - - + SimulationMember member = event.getMember(); + List memberList = conversationGroup.getMemberList().stream() + .filter(cm -> cm.isRobot() && !Objects.equals(cm.getMember(), member)) + .map(ConversationMember::getMember).collect(Collectors.toList()); + if (CollectionUtils.isEmpty(memberList)) { // 人员全部为真实人员时直接返回 + return; + } + // 执行语音识别 + doAnalysis(event.getSimulation(), event.getContent(), memberList); } } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java index 279566222..5cf59b699 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/OperateResult.java @@ -1,7 +1,6 @@ package club.joylink.rtss.simulation.cbtc.discriminate; import club.joylink.rtss.simulation.cbtc.ATS.operation.Operation; -import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Data; import java.util.HashMap; @@ -18,24 +17,15 @@ public class OperateResult { */ private Operation.Type type; -// private Boolean operationResult; -// private String errResultMsg; /** * 指令参数 */ private Map params = new HashMap<>(); - /** - * 角色ID - */ - private SimulationMember member; - public OperateResult(Operation.Type type) { this.type = type; } - - /** * 参数放入集合 * @param key 集合key