package com.qiwu.chatbotbackend.servlet; import com.qiwu.chatbotbackend.entity.*; import com.qiwu.chatbotbackend.processing.*; 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.*; import java.sql.SQLException; import java.text.ParseException; import java.util.*; @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/"; 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; Condition ansCondition = null; String ansJ = "", return_str = "", state = "", chatKey = ""; String botName = receiveParamters.get("bot_name")[0]; chatKey = receiveParamters.get("chat_key")[0].split("-")[0]; receiveParamters.get("chat_key")[0] = chatKey; OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); boolean debugFlag = false; 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.getChatCache(chatKey, context, userinfo, receiveParamters, loadCacheStatus); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } /** * 如果读档成功,则返回存档前的状态(包括最后一个condition和最后的回答) * 如果读档失败,则开始新的游戏,读档失败的判断标准是除了初始化数据外没有其他任何数据 */ if(!chatCache.isReloading()) { historyInfoPoint = chatCache.getInfoPoint(); //信息点预处理 QuestionMatcher matcher = new QuestionMatcher(curInfoPoint, historyInfoPoint, logWriter, userinfo, context, chatCache.getGameEnv()); debugFlag = matcher.infoPointPreprocessing(receiveParamters, curInfoPoint, historyInfoPoint, userinfo, context); /*得到答案更新历史信息点&生成答案,,清楚超时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")) { //如果当前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); } matcher.infoTransfer(bestMatch, context, debugFlag); ChatCacheController.updateChatCache4Insert(curInfoPoint, historyInfoPoint, chatCache.getGameEnv(), context); //清理超时的用户数据 if (context.getAttribute("cacheUpdateTime") == null) context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > ChatCacheController.TIME_OUT) { ChatCacheController.updateChatCache4Delete(chatKey, context); context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); } } else { System.out.println("----------读档中------------"); String nextStep = chatCache.getInfoPoint().get("currentAction"); String reply = chatCache.getInfoPoint().get("lastReply"); bestMatch = new Solution(1, "--读取存档--" + reply, "nextStep=" + nextStep); } 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"); } }