package servlet; import entity.AnswerNextStep; import entity.Condition; import entity.UserInfo; import processing.ChatCacheController; import processing.MyFunc; import processing.ProcessQuestion; 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.util.*; @WebServlet(name = "GetAnswerServlet",urlPatterns = {"/test","/test/"}) public class GetAnswerServlet extends HttpServlet { /** * GetAnswerServlet就是处理post请求并返回答案的入口 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Map curInfoPoint = new HashMap<>(request.getParameterMap()); Map historyInfoPoint = null; AnswerNextStep bestMatch = null; Condition ansCondition = null; String ansJ,return_str="",state = ""; String chatKey = curInfoPoint.get("chat_key")[0]; ProcessQuestion process = new ProcessQuestion(); OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); //获得用户历史信息 historyInfoPoint = ChatCacheController.getChatCache(chatKey,context); UserInfo userinfo = null; try { //userinfo = process.userInfoQuery(chatKey,context); //从servlet根据用户名查询用户信息 userinfo = process.userInfoQuery(request.getParameter("bot_name"),context); } catch (SQLException e) { state = "没找到bot名?"; e.printStackTrace(); } //如果historyInfoPoint不存在,就新建一个 if(historyInfoPoint == null) { String k; historyInfoPoint = new HashMap(); Iterator iit = curInfoPoint.keySet().iterator(); while (iit.hasNext()){ k = iit.next(); historyInfoPoint.put(k,""); } } //输出所有actionName的值 Iterator iit = curInfoPoint.keySet().iterator(); while(iit.hasNext()){ String k = iit.next(); // System.out.print(k+": "); String values[] = curInfoPoint.get(k); if(values != null && !values[0].equals(" ") && !values[0].equals("") && !k.equals("chat_key") && !k.equals("action_name") && !k.equals("bot_name")) //return_str = return_str + " " + k + ":" + values[0] + " "; for(String v:values) { System.out.print(v + " "); } System.out.println(); } //如果引擎什么信息都没识别到,那就将上一轮的nextStep(也就是当前轮的actionName)设为unknown iit = curInfoPoint.keySet().iterator(); boolean unknown_flag = true; while(iit.hasNext()){ String k = iit.next(); String values[] = curInfoPoint.get(k); if(k.equals("chat_key") || k.equals("action_name") || k.equals("bot_name") || k.equals("emptySet")) continue; if(values != null && !values[0].equals("")) { unknown_flag = false; break; } } String currentAction = ""; if(historyInfoPoint.containsKey("currentAction")) currentAction = historyInfoPoint.get("currentAction"); else if(userinfo.extraInfo.containsKey("activeAction")) { currentAction = userinfo.extraInfo.get("activeAction"); historyInfoPoint.put("currentAction", currentAction); }else currentAction = ""; curInfoPoint.put("emptySet", new String[]{" ",}); if(historyInfoPoint.get("activeList") == null){ historyInfoPoint.put("activeList", currentAction); } if(unknown_flag && !currentAction.equals("")){ String i[] = {"unknown",}; curInfoPoint.put(currentAction,i); } /*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/ //process.removeTimeoutChatkey(context); //System.out.println(userinfo.conditionTable); //测试 ansCondition = process.getAnswer(curInfoPoint,historyInfoPoint,userinfo.conditionTable, context); if (state.equals("") && ansCondition.priority == -1) state = "我不理解,"; if (state.equals("")) { bestMatch = process.updateInfoGetBestMatch(ansCondition, curInfoPoint, historyInfoPoint, context, userinfo); //process.updateDB(kw); //System.out.println("Current time:" + System.currentTimeMillis()); //清理超时的用户数据 if (context.getAttribute("cacheUpdateTime") == null) context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > 300000) { ChatCacheController.updateChatCache4Delete(context); context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); } bestMatch.setAnswer(" \n" + return_str + "\n " + bestMatch.getAnswer()); }else{ bestMatch = new AnswerNextStep(0,state,"nextStep=" + historyInfoPoint.get("currentAction")); } 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 m ethod"); } }