package servlet; import entity.*; import processing.ChatCacheController; import processing.MyFunc; import processing.QuestionMatcher; import processing.UserInfoController; 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/"}) public class GetAnswerServlet extends HttpServlet { /** * GetAnswerServlet就是处理post请求并返回答案的入口 */ String NAME_MISSING_ERROR = "没找到bot名?"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Map curInfoPoint = new HashMap<>(request.getParameterMap()); Map historyInfoPoint = null; Solution bestMatch = null; Condition ansCondition = null; String ansJ, return_str = "", state = ""; String chatKey = curInfoPoint.get("chat_key")[0]; OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); boolean debugFlag = false; String fileName = "/var/www/html/logs/" + chatKey + ".html"; File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } file.setReadable(true,false); FileWriter fw = new FileWriter(file, true); //BufferedWriter bw = new BufferedWriter(fos, "UTF-8"); //context.setAttribute("logWriter", bw); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); pw.println("\n" + "\n" + "\n" + " \n" + " log\n" + ""); 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(); } //获得用户历史信息 ChatCache chatCache = ChatCacheController.getChatCache2(chatKey, context, curInfoPoint, userinfo); historyInfoPoint = chatCache.getInfoPoint(); //信息点预处理 QuestionMatcher matcher = new QuestionMatcher(pw,userinfo, context, chatCache.getWorld()); debugFlag = matcher.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo, context); /*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/ //process.removeTimeoutChatkey(context); //System.out.println(userinfo.conditionTable); //测试 ansCondition = matcher.getBestMatch(curInfoPoint, historyInfoPoint, userinfo.conditionTable, context, userinfo); if (state.equals("") && ansCondition.priority == -1) state = "我不理解,"; if (state.equals("")) { try { bestMatch = matcher.getSolution(ansCondition, curInfoPoint, historyInfoPoint, context, userinfo); if (ansCondition.featureMap.containsKey("activate") && ansCondition.featureMap.containsKey("post")) { //如果当前condition包含激活和提交表单两个操作,那么在提交操作完成之后会再激活一次待激活列表,简称后激活阶段 bestMatch = matcher.postActivate(ansCondition, curInfoPoint, historyInfoPoint, context, userinfo); } } catch (ParseException e) { e.printStackTrace(); } //清理超时的用户数据 if (context.getAttribute("cacheUpdateTime") == null) context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > 300000) { ChatCacheController.updateChatCache4Delete2(chatKey, context); context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); } //bestMatch.setAnswer(" \n" + return_str + "\n " + bestMatch.getAnswer()); } else { bestMatch = new Solution(0, " \n" + return_str + "\n " + state, "nextStep=" + historyInfoPoint.get("currentAction")); } if (debugFlag) { matcher.appendDebugInfo(bestMatch, curInfoPoint, historyInfoPoint, context); } matcher.infoTransfer(curInfoPoint, historyInfoPoint, bestMatch, context, debugFlag); bw.write("

" + "-----------------------------------------对话处理结束-----------------------------------


"); bw.close(); fw.close(); ansJ = MyFunc.toJson(bestMatch); 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"); } }