package processing; import entity.Solution; import entity.Condition; import entity.UserInfo; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import javax.servlet.ServletContext; import java.io.*; import java.sql.SQLException; import java.text.ParseException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ProcessQuestion { static Map activatedMap = new HashMap(); static String POST_DATA_ERROR = "post数据有误"; static String POST_ERROR = "数据post时出错了"; static String API_ERROR = "api处理过程中出错了"; static String VARIABLE_REPLACE_PATTERN = "(\\{.*?\\-[12]\\})"; Pattern replacePattern = Pattern.compile(VARIABLE_REPLACE_PATTERN); /* public void updateDB(QuestionKeyWord qkw) throws SQLException { SqlHelper helper = new SqlHelper(); String result = helper.queryActionByKey(qkw.getChatKey()); if(result != "empty") //待添加 //qkw.setActionName(result+"+"+qkw.getActionName()); helper.updateAction(qkw); else helper.insertAction(qkw); helper.destroy(); } */ public boolean infoPointPreprocessing(Map curInfoPoint, Map historyInfoPoint, UserInfo userinfo, StringBuffer curInfoRecord){ String return_str = "", k ; boolean debugFlag = false; //输出所有actionName的值 Iterator iit = curInfoPoint.keySet().iterator(); while(iit.hasNext()){ k = iit.next(); // System.out.print(k+": "); if(k.equals("debug_mode")) debugFlag = true; String values[] = curInfoPoint.get(k); for(String v:values) { System.out.print(v + " "); } System.out.println(); if(values[0].equals(":")){ values[0] = ""; curInfoPoint.put(k, values); } } //如果引擎什么信息都没识别到,那就将上一轮的nextStep(也就是当前轮的actionName)设为unknown iit = curInfoPoint.keySet().iterator(); boolean unknown_flag = true; while(iit.hasNext()){ 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); } return debugFlag; } /** * 用迭代器迭代当前信息点中的非空关键字,由conditionName索引到状态表中保存包含conditionName这个条件的列表,并查询状态表中的条件是否满足及其优先级,满足条件且优先级更高就设为bestMatch * @param curInfoPoint 当前信息点,即引擎识别的conditionName和关键字 * @param historyInfoPoint 历史信息点,即引擎前几轮识别的conditionName和关键字 * @param conditionTable 满足什么条件,就回答什么,下一步跳什么 * @return bestMatch 状态表中满足条件最多的一条 */ public Condition getBestMatch(Map curInfoPoint,Map historyInfoPoint,HashMap> conditionTable, ServletContext context) throws IOException { Integer configId = 0; List conditionList = null; Condition bestMatch = new Condition(-1),iterCon; Iterator it = curInfoPoint.keySet().iterator(); Queue activatedQueue = new LinkedList(); Queue activatedCondition = new LinkedList(); while (it.hasNext()) { String conditionName = it.next(); String[] keywords = curInfoPoint.get(conditionName); String keyword = null; if (keywords != null && !keywords[0].equals("")) keyword = keywords[0]; if (keyword != null && conditionTable.get(conditionName) != null) { conditionList = conditionTable.get(conditionName); for (Condition condition : conditionList) { if(condition.status.equals("waiting") && conditionSatisfy(curInfoPoint, historyInfoPoint, condition)){ if(condition.featureMap.get("instantPost") != null){ } //Post后还是可以激活 if(condition.featureMap.get("activate") != null){ bestMatch = condition; activateCondition(activatedQueue, curInfoPoint.get("bot_name")[0], condition.priority, condition.nextStep, context); } else if((condition.priority > bestMatch.priority) || (condition.priority == bestMatch.priority && condition.auxiliaryPriority > bestMatch.auxiliaryPriority)) bestMatch = condition; } } } } bestMatch = activatedQueueMatching(bestMatch, activatedQueue, curInfoPoint, historyInfoPoint, context); return bestMatch; } public Condition activatedQueueMatching(Condition bestMatch,Queue activatedQueue, Map curInfoPoint,Map historyInfoPoint, ServletContext context){ Condition condition; //待激活配置条目被激活后的匹配 while(!activatedQueue.isEmpty()){ condition = activatedQueue.remove(); activatedMap.remove(condition.id); if(conditionSatisfy(curInfoPoint, historyInfoPoint, condition)){ if(condition.featureMap.containsKey("activate")){ bestMatch = condition; activateCondition(activatedQueue, curInfoPoint.get("bot_name")[0], condition.priority, condition.nextStep, context); } else if((condition.activePriority > bestMatch.activePriority) || (condition.activePriority == bestMatch.activePriority && condition.auxiliaryPriority > bestMatch.auxiliaryPriority)){ bestMatch = condition; } } } return bestMatch; } /** * 根据当前信息点,历史信息点,判断是否满足该condition的条件,match关键字指引擎识别的关键字包含在condition相应的关键词列表中,这里可以根据特征添加其他操作。 * currentPremise是当前信息点需要满足的条件,historyPremise是历史信息点需要满足的条件 * @param curInfoPoint 当前信息点,即引擎识别的conditionName和关键字 * @param historyInfoPoint 历史信息点,即引擎前几轮识别的conditionName和关键字 * @param condition 配置文件(状态表)的一行 * @return 满足条件就为True,不满足就为False */ public boolean conditionSatisfy(Map curInfoPoint,Map historyInfoPoint,Condition condition){ boolean flag = true; String actionName,keywords[],keyword,comparePair[],cmpType,premiseStr; HashSet keywordSet = null; if(condition.featureMap.containsKey("match")){ Iterator it = condition.currentPremise.keySet().iterator(); while (flag && it.hasNext()) { premiseStr = it.next(); comparePair = premiseStr.split("::"); actionName = comparePair[0]; keywords = curInfoPoint.get(actionName); if (keywords == null) { flag = false; break; } keyword = keywords[0]; if (comparePair.length == 1) cmpType = "match"; else cmpType = comparePair[1]; keywordSet = condition.currentPremise.get(premiseStr); if(!premiseSatisfy(keyword, cmpType, keywordSet)) flag = false; } it = condition.historyPremise.keySet().iterator(); while (flag && it.hasNext()) { premiseStr = it.next(); comparePair = premiseStr.split("::"); actionName = comparePair[0]; keyword = historyInfoPoint.get(actionName); if(keyword == null){ flag = false; break; } if (comparePair.length == 1) cmpType = "match"; else cmpType = comparePair[1]; keywordSet = condition.historyPremise.get(premiseStr); if(!premiseSatisfy(keyword, cmpType, keywordSet)) flag = false; } } return flag; } /** * 单个条件的匹配,判断是否满足条件 判断(keyword in keywordSet) * @param keyword * @param cmpType 比较类型 * @param keywordSet 候选关键字集合,满足其中任意一个都算满足条件 * @return 返回匹配或者不匹配 */ public boolean premiseSatisfy(String keyword, String cmpType, HashSet keywordSet){ boolean flag = true; String operator = ""; try { String splitPair[] = cmpType.split("-"); if (splitPair.length > 1) { operator = splitPair[1]; cmpType = splitPair[0]; } if (cmpType.equals("match")) { if (!keywordSet.contains(keyword)) { flag = false; } if (flag == false && keyword.contains(":")) { String ks[] = keyword.split(":"); for (String k : ks) { if (keywordSet.contains(k)) flag = true; } } } else if (cmpType.equals("number") && !keyword.equals("") && StringUtils.isNumeric(keyword)) { int candidateA = 0; int candidateB = 0; candidateA = Integer.parseInt(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = Integer.parseInt(k); break; } } if (operator.equals("Bigger")) flag = candidateA > candidateB; else if (operator.equals("BiggerOrEqual")) flag = candidateA >= candidateB; else if (operator.equals("Smaller")) flag = candidateA < candidateB; else if (operator.equals("SmallerOrEqual")) flag = candidateA <= candidateB; } else if (cmpType.equals("time")) { long candidateA, candidateB = 0; candidateA = MyFunc.transferToMillisecond(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = MyFunc.transferToMillisecond(k); break; } } if (operator.equals("Early")) flag = candidateA < candidateB; else if (operator.equals("Later")) flag = candidateA > candidateB; } //-------------------------------------------------------------------- //为兼容旧数据 else if (cmpType.equals("numberBigger") && StringUtils.isNumeric(keyword)) { int candidateA = 0; int candidateB = 0; candidateA = Integer.parseInt(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = Integer.parseInt(k); break; } } flag = candidateA > candidateB; } else if (cmpType.equals("numberBiggerOrEqual") && StringUtils.isNumeric(keyword)) { int candidateA = 0; int candidateB = 0; candidateA = Integer.parseInt(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = Integer.parseInt(k); break; } } flag = candidateA >= candidateB; } else if (cmpType.equals("numberSmaller") && StringUtils.isNumeric(keyword)) { int candidateA = 0; int candidateB = 0; candidateA = Integer.parseInt(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = Integer.parseInt(k); break; } } flag = candidateA < candidateB; } else if (cmpType.equals("numberSmallerOrEqual") && StringUtils.isNumeric(keyword)) { int candidateA = 0; int candidateB = 0; candidateA = Integer.parseInt(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = Integer.parseInt(k); break; } } flag = candidateA <= candidateB; } else if (cmpType.equals("timeEarly")) { long candidateA, candidateB = 0; candidateA = MyFunc.transferToMillisecond(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = MyFunc.transferToMillisecond(k); break; } } flag = candidateA < candidateB; } else if (cmpType.equals("timeLater")) { long candidateA, candidateB = 0; candidateA = MyFunc.transferToMillisecond(keyword); for (String k : keywordSet) { if (!k.equals("")) { candidateB = MyFunc.transferToMillisecond(k); break; } } flag = candidateA > candidateB; } else if (cmpType.equals("any")) { if (keyword.equals("")) //当前信息点中的关键词不为空即满足条件,为空则不满足 flag = false; } else { flag = false; } } catch (IndexOutOfBoundsException e){ System.out.println("配置文件格式有误"); e.printStackTrace(); }catch (Exception e){ System.out.println("操作符有误"); e.printStackTrace(); flag = false; } return flag; } /** * 替换答案中相应的变量{xxx-1},并根据当前信息点更新历史信息点(当前在下一轮变成历史)。 * @param curInfoPoint 当前信息点,即引擎识别的conditionName和关键字 * @param historyInfoPoint 历史信息点,即引擎前几轮识别的conditionName和关键字 * @return 返回给引擎的数据 */ public Solution getSolution(Condition ansCondition, Map curInfoPoint, Map historyInfoPoint, ServletContext context, UserInfo userinfo) throws IOException, ParseException { Iterator it = curInfoPoint.keySet().iterator(); String actionName, reply, variable; Solution ans = new Solution(0, "", ""); //如果一条配置文件有多个答案,则随 // 机选择一个答案 int randNum = (int) (Math.random() * ansCondition.answer.length); ans.setAnswer(ansCondition.answer[randNum]); //System.out.println(ans.getAnswer()); //post关键字 if(ansCondition.featureMap.containsKey("post")){ ans = postToServer(ans,ansCondition,curInfoPoint,historyInfoPoint); //nextNext关键字 }else if(ansCondition.featureMap.containsKey("newNext")){ ans = findNewNext(ans, ansCondition, curInfoPoint, historyInfoPoint); //上一步 }else if (ansCondition.featureMap.containsKey("return-1")){ String activeList[] = historyInfoPoint.get("activeList").split("\\|"); if (activeList.length == 1) ans.setNext("nextStep=" + activeList[activeList.length - 1]); else { ans.setNext("nextStep=" + activeList[activeList.length - 2]); } //停留 }else if (ansCondition.featureMap.containsKey("return-0")){ String activeList[] = historyInfoPoint.get("activeList").split("\\|"); ans.setNext("nextStep=" + activeList[activeList.length - 1]); //激活 }else if (ansCondition.featureMap.containsKey("activate")){ String activeList[] = historyInfoPoint.get("activeList").split("\\|"); if (activeList.length == 1) ans.setNext("nextStep=" + activeList[activeList.length - 1]); else { ans.setNext("nextStep=" + activeList[activeList.length - 2]); } } //updateKnowledge关键字 if (ansCondition.featureMap.containsKey("updateKnowledge")){ updateKnowledge(ansCondition, historyInfoPoint); } if(ans.getNext().equals("")){ ans.setNext("nextStep=" + ansCondition.nextStep); } try { /* it = historyInfoPoint.keySet().iterator(); String keyword1, keyword2; while (it.hasNext()) { actionName = it.next(); if(curInfoPoint.get(actionName) != null) { keyword1 = curInfoPoint.get(actionName)[0]; ans.setAnswer(ans.getAnswer().replace("{" + actionName + "-1}", keyword1));//这里 } keyword2 = historyInfoPoint.get(actionName); ans.setAnswer(ans.getAnswer().replace("{" + actionName + "-2}", keyword2)); } */ //用正则表达式将回答中的变量模板{conditionName-2}替换成后台存储的变量值 reply = ans.getAnswer(); Matcher m = replacePattern.matcher(reply); while(m.find()){ variable = m.group(1).substring(1, m.group(1).length()-3); if(m.group(1).endsWith("-1}")){ if(curInfoPoint.get(variable) != null && !curInfoPoint.get(variable)[0].equals("")) reply = reply.replace("{" + variable + "-1}", curInfoPoint.get(variable)[0]); }else if(m.group(1).endsWith("-2}")){ if(historyInfoPoint.get(variable) != null && !historyInfoPoint.get(variable).equals("")) reply = reply.replace("{" + variable + "-2}", historyInfoPoint.get(variable)); } } ans.setAnswer(reply); }catch(Exception e){ e.printStackTrace(); } return ans; } public void infoTransition(Map curInfoPoint, Map historyInfoPoint, Solution ans, ServletContext context, boolean debugFlag){ //将当前信息点更新至历史信息点 Iterator it = curInfoPoint.keySet().iterator(); String actionName, lastReply; String next = ans.getNext().split("=")[1]; //保存即将跳转的action名以供下一轮使用,nextStep的结构是nextStep=xxx historyInfoPoint.put("currentAction",next); historyInfoPoint.put("activeList", historyInfoPoint.get("activeList") + "|" + next); if(debugFlag) lastReply = ans.getAnswer().split(")")[2]; else lastReply = ans.getAnswer(); historyInfoPoint.put("lastReply", lastReply); try { String keyword1, keyword2; while (it.hasNext()) { actionName = it.next(); keyword1 = curInfoPoint.get(actionName)[0]; keyword2 = historyInfoPoint.get(actionName); if (keyword2 == null) historyInfoPoint.put(actionName, keyword1); else if (keyword2 != null && !keyword1.equals("")) historyInfoPoint.put(actionName, keyword1); } ChatCacheController.updateChatCache4Insert(curInfoPoint, historyInfoPoint, context); //context.setAttribute("chat_key+"+userinfo.userName,historyInfoPoint); }catch(Exception e){ e.printStackTrace(); } } /** * 在新平台添加debug_mode这个关键字, 后台会将存储的变量值返回给页面以作调试 * @param ans 最终返回的数据 * @param curInfoPoint 当前对话识别到的信息点 * @param historyInfoPoint 在当前对话之前采集到的所有用户信息点 * @param context */ public void appendDebugInfo(Solution ans, Map curInfoPoint, Map historyInfoPoint, ServletContext context) { Iterator it = null; String key, values[], value; HashSet skipSet = new HashSet(){ { add("activeList"); add("currentAction"); add("lastUpdateTime"); add("bot_name"); add("action_name"); add("chat_key"); add("lastReply"); } }; it = curInfoPoint.keySet().iterator(); String debugInfo = ""; debugInfo += "(识别:"; while(it.hasNext()) { key = it.next(); values = curInfoPoint.get(key); if (values != null && !values[0].equals(" ") && !values[0].equals("") && !skipSet.contains(key)) debugInfo += (" " + key + ":\'" + values[0] + "\' "); } debugInfo += ")(已知:"; it = historyInfoPoint.keySet().iterator(); while(it.hasNext()){ key = it.next(); value = historyInfoPoint.get(key); if(value != null && !value.equals(" ") && !value.equals("") && !skipSet.contains(key)) debugInfo += (" " + key + ":\'" + value + "\' "); } ans.setAnswer( debugInfo + ")" + ans.getAnswer()); } /** * 重新post到其他服务器读取数据,并根据这些数据修改返回给用户的信息,可能包括nextStep,和回答文本中的可变参数(用replace方法替换) * @param ans 返回给用户的数据 * @param ansCondition 满足条件最多的condition(配置文件的某一行) */ public Solution postToServer(Solution ans, Condition ansCondition, Map curInfoPoint, Map historyInfoPoint) throws IOException { String url = null,elem[], subElem[], key, postType = "all"; Map infoMap = new HashMap(); List pairList = new ArrayList(); Iterator k_iter = null; elem = ansCondition.featureMap.get("post").split("\\|"); url = elem[0]; postType = elem[1]; //提交类型为all时,会把当前信息点和历史信息点都作为post到另一个服务器 if(postType.equals("all")){ k_iter = curInfoPoint.keySet().iterator(); while(k_iter.hasNext()){ key = k_iter.next(); if(curInfoPoint.get(key)[0] != "") //infoMap.put(key + "_cur", curInfoPoint.get(key)[0]); pairList.add(new BasicNameValuePair(key , curInfoPoint.get(key)[0])); } k_iter = historyInfoPoint.keySet().iterator(); while(k_iter.hasNext()){ key = k_iter.next(); if(historyInfoPoint.get(key) != "") //infoMap.put(key + "_his", historyInfoPoint.get(key)); pairList.add(new BasicNameValuePair(key + "_his", historyInfoPoint.get(key))); } //提交类型为term时只会提交最佳匹配的配置条目中包含的条件和值 }else if(postType.equals("term")){ k_iter = ansCondition.currentPremise.keySet().iterator(); while(k_iter.hasNext()){ key = k_iter.next().split("::")[0]; if(curInfoPoint.get(key)[0] != "") pairList.add(new BasicNameValuePair(key , curInfoPoint.get(key)[0])); } k_iter = ansCondition.historyPremise.keySet().iterator(); while(k_iter.hasNext()){ key = k_iter.next().split("::")[0]; if(historyInfoPoint.get(key) != "") pairList.add(new BasicNameValuePair(key + "_his", historyInfoPoint.get(key))); //infoMap.put(key + "_his", historyInfoPoint.get(key)); } } HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); String respContent = null; String keyName = null; UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList, "utf-8"); entity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(entity); try { HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); JSONObject jobj = JSONObject.fromString(respContent); Iterator kit = jobj.keys(); while (kit.hasNext()) { keyName = kit.next(); if (keyName.equals("process")) { ans.setNext((String)jobj.get(keyName)); historyInfoPoint.put("process", (String)jobj.get(keyName)); } else if (keyName.equals("solution")) { ans.setAnswer((String) jobj.get(keyName)); } else { historyInfoPoint.put(keyName, jobj.getString(keyName)); } } } else { ans.setAnswer(API_ERROR); } }catch (Exception e){ e.printStackTrace(); ans.setNext(POST_ERROR + url); } return ans; } public Solution findNewNext(Solution ans, Condition ansCondition, Map curInfoPoint, Map historyInfoPoint){ String nextOptions[] = ansCondition.nextStep.split(","); String conditionName,premises[],cpPair[]; boolean flag = true; String activeList = historyInfoPoint.get("activeList"); for(String option: nextOptions){ flag = true; cpPair = option.split(":"); conditionName = cpPair[0]; if(cpPair.length == 1){ ans.setNext("nextStep="+conditionName); break; } premises = cpPair[1].split("-"); for(String premise:premises) { if(premise.equals("known")) { //如果当前/历史信息点中有option这个键,并且这个键的值不为空也不为unknown,那么证明这个option(即相应condition)已经被识别 boolean conditionExistInCurInfoPoint = curInfoPoint.containsKey(conditionName) && !curInfoPoint.get(conditionName)[0].equals("") && !curInfoPoint.get(conditionName)[0].equals("unknown"); boolean conditionExistInHistoryInfoPoint = historyInfoPoint.containsKey(conditionName) && !historyInfoPoint.get(conditionName).equals("") && !historyInfoPoint.get(conditionName).equals("unknown"); //只有前两者都为假时,conditionExist才为真 boolean conditionExist = (conditionExistInCurInfoPoint || conditionExistInHistoryInfoPoint); if (conditionExist) flag = false; } if(premise.equals("active")){ boolean isActive = activeList.contains(conditionName); if (isActive) flag = false; } } if(flag){ ans.setNext("nextStep="+conditionName); break; } } return ans; } public void updateKnowledge(Condition condition, Map historyInfoPoint) throws ParseException { String keyword, variableName, updateValue; Iterator iit = condition.updateKnowledge.keySet().iterator(); while(iit.hasNext()){ variableName = iit.next(); updateValue = condition.updateKnowledge.get(variableName); keyword = historyInfoPoint.get(variableName); try { //做数字加法 if (updateValue.startsWith("+")) { if(keyword == null || keyword.equals("")) keyword = "0"; if (keyword.matches("\\d{4}年\\d{1,2}月\\d{1,2}日.*")) { long time = MyFunc.transferToMillisecond(keyword); int offset = Integer.parseInt(updateValue.substring(1)); updateValue = MyFunc.transferToStandardTime(time + (1000 * offset)); }else if (StringUtils.isNumeric(keyword)){ int number = Integer.parseInt(keyword); int offset = Integer.parseInt(updateValue.substring(1)); updateValue = String.valueOf(number + offset); } //做数字减法 } else if (updateValue.startsWith("-")) { if(keyword == null || keyword.equals("")) keyword = "0"; if (keyword.matches("\\d{4}年\\d{1,2}月\\d{1,2}日.*")) { long time = MyFunc.transferToMillisecond(keyword); int offset = Integer.parseInt(updateValue.substring(1)); updateValue = MyFunc.transferToStandardTime(time - (1000 * offset)); }else if (StringUtils.isNumeric(keyword)){ int number = Integer.parseInt(keyword); int offset = Integer.parseInt(updateValue.substring(1)); updateValue = String.valueOf(number - offset); } //做字符串累加 } else if (updateValue.startsWith("str+")) { if(keyword == null || keyword.equals("")) keyword = ""; updateValue = keyword + updateValue.substring(4); //做字符串删除 } else if (updateValue.startsWith("str-")) { if(keyword == null || keyword.equals("")) keyword = ""; updateValue = keyword.replace(updateValue.substring(4), ""); } }catch (Exception e){ e.printStackTrace(); continue; } historyInfoPoint.put(variableName, updateValue); } } public void activateCondition(Queue activatedQueue, String userName, int basePriority, String activateList, ServletContext context){ UserInfo userinfo = (UserInfo) context.getAttribute(userName); String configStr = ""; Integer configId; Condition newCondition, originCondition; for(String configIdStr:activateList.split(",")){ if (!StringUtils.isNumeric(configIdStr)) continue; configId = Integer.parseInt(configIdStr); configStr = userinfo.id2config.get(configId); if(configStr != null) { originCondition = userinfo.id2condition.get(configId); newCondition = new Condition(originCondition, basePriority); //condition = ProcessQuestion.buildCondition(condition, configStr); if (activatedMap.containsKey(configId) && newCondition.activePriority > activatedMap.get(configId).activePriority) { activatedMap.get(configId).activePriority = newCondition.activePriority; } else if (!activatedMap.containsKey(configId)) { activatedMap.put(configId, newCondition); activatedQueue.add(newCondition); } } } } public Solution postActivate(Condition ansCondition, Map curInfoPoint, Map historyInfoPoint, ServletContext context, UserInfo userinfo) throws IOException, ParseException { Queue activatedQueue = new LinkedList(); Solution bestMatch = null; activateCondition(activatedQueue, userinfo.userName, ansCondition.priority, ansCondition.nextStep, context); ansCondition = activatedQueueMatching(ansCondition, activatedQueue, curInfoPoint, historyInfoPoint, context); bestMatch = getSolution(ansCondition, curInfoPoint, historyInfoPoint, context, userinfo); return bestMatch; } }