package com.qiwu.chatbotbackend.servlet; import com.qiwu.chatbotbackend.entity.ChatCache; import com.qiwu.chatbotbackend.entity.Condition; import com.qiwu.chatbotbackend.entity.Solution; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.processing.*; import com.qiwu.chatbotbackend.task.utils.BasicBotUtil; import com.qiwu.chatbotbackend.task.utils.GameBotUtil; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.sql.SQLException; import java.text.ParseException; import java.util.HashMap; import java.util.Map; @WebServlet(name = "GetAnswerServlet", urlPatterns = {"/test", "/test/", "/dialog"}) public class GetAnswerServlet extends HttpServlet { /** * GetAnswerServlet就是处理post请求并返回答案的入口 */ String NAME_MISSING_ERROR = "没找到bot名?"; String LOG_PATH = "/var/www/html/logs/"; //String LOG_PATH = "/home/hxw/source/logs/"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Map receiveParamters = new HashMap<>(request.getParameterMap()); Map curInfoPoint = new HashMap(); Map historyInfoPoint = null; Solution bestMatch = null; BasicBotUtil botUtil = null; Condition ansCondition = null; boolean debugFlag = false; String ansJ = "", return_str = "", state = "", chatKey = "", botType = "", userKey = "", randKey = ""; String botName = receiveParamters.get("bot_name")[0]; OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); //如果有client_id,会用@分隔 chatKey = receiveParamters.get("chat_key")[0]; String chatKeys[] = receiveParamters.get("chat_key")[0].split("@@@@"); if(chatKeys.length == 1){ userKey = chatKeys[0]; randKey = chatKeys[0]; }else{ userKey = chatKeys[0]; randKey = chatKeys[1]; } receiveParamters.put("userKey", new String[]{userKey}); receiveParamters.put("randKey", new String[]{randKey}); // if(!receiveParamters.get("chat_key")[0].startsWith("@")) // chatKey = receiveParamters.get("chat_key")[0].split("@")[0]; // else // chatKey = receiveParamters.get("chat_key")[0]; // receiveParamters.get("chat_key")[0] = chatKey; LogWriter logWriter = new LogWriter(LOG_PATH, chatKey); UserInfo userinfo = null; UserInfoController uiController = new UserInfoController(); try { //userinfo = process.userInfoQuery(chatKey,context); //从servlet根据用户名查询用户信息 userinfo = uiController.userInfoQuery(request.getParameter("bot_name"), context); } catch (SQLException e) { state = NAME_MISSING_ERROR; e.printStackTrace(); } if(userinfo != null) { //获得用户历史信息 ChatCache chatCache = null; Boolean loadCacheStatus = Boolean.valueOf(false); try { //chatCache = ChatCacheController.getChatCache2(userKey, randKey, context, userinfo, receiveParamters, loadCacheStatus); chatCache = ChatCacheController.getChatCache(userKey, randKey, context, userinfo, receiveParamters, loadCacheStatus); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } /** * 如果读档成功,则返回存档前的状态(包括最后一个condition和最后的回答) * 如果读档失败,则开始新的游戏,读档失败的判断标准是除了初始化数据外没有其他任何数据 */ historyInfoPoint = chatCache.getInfoPoint(); if(userinfo.varInitialMap.get("botType") != null) botType = userinfo.varInitialMap.get("botType"); switch(botType){ case "game": WorldController worldController = new WorldController(logWriter,userinfo); botUtil = new GameBotUtil(curInfoPoint, historyInfoPoint, userinfo, logWriter, worldController, chatCache.getGameEnv(), chatCache); break; default: botUtil = new BasicBotUtil(curInfoPoint, historyInfoPoint, userinfo, logWriter); break; } //针对不同游戏类型对信息点做预处理 debugFlag = botUtil.preprocessing(receiveParamters, context); //信息点预处理 QuestionMatcher matcher = new QuestionMatcher(curInfoPoint, historyInfoPoint, logWriter, userinfo, context, chatCache.getGameEnv(), botUtil); /*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/ //process.removeTimeoutChatkey(context); //System.out.println(userinfo.conditionTable); //测试 ansCondition = matcher.getBestMatch(userinfo.conditionTable, context, userinfo); if (state.equals("") && ansCondition.priority == -1) state = "我不理解,"; if (state.equals("")) { try { bestMatch = matcher.getSolution(ansCondition, context, userinfo); if ((ansCondition.featureMap.containsKey("activate") && ansCondition.featureMap.containsKey("post")) || ansCondition.featureMap.containsKey("activate:after")) { //如果当前condition包含激活和提交表单两个操作,那么在所有操作操作完成之后会再激活一次待激活列表,简称后激活阶段 bestMatch = matcher.postActivate(ansCondition, context, userinfo); } } catch (ParseException e) { e.printStackTrace(); } } else { bestMatch = new Solution(0, " \n" + return_str + "\n " + state, "nextStep=" + historyInfoPoint.get("currentAction")); } if (debugFlag) { matcher.appendDebugInfo(bestMatch, context); } //if(userinfo.varInitialMap.get("infoTransferTime") != null && userinfo.varInitialMap.get("infoTransferTime").equals("after")) matcher.infoTransfer(bestMatch, context, debugFlag); ChatCacheController.updateChatCache4Insert(curInfoPoint, chatCache.getInfoPoint(), chatCache.getGameEnv(), context, userinfo); //清理超时的用户数据 if (context.getAttribute("cacheUpdateTime") == null) context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController.TIME_OUT) { ChatCacheController.updateChatCache4Delete(chatKey, context, userinfo); context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); } //针对不同游戏类型做后处理 botUtil.postprocessing(chatKey, context, bestMatch); ansJ = MyFunc.toJson(bestMatch); } else { ansJ = "找不到配置文件"; } logWriter.writeln(); logWriter.writeln(); logWriter.record("-----------------------------------------对话处理结束", 0); logWriter.writeln(); logWriter.writeln(); logWriter.close(); System.out.println(ansJ); out.write(ansJ.getBytes()); out.flush(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("do get method"); } }