Commit 2555aef0 by hxw

删除全局变量分类,程序中断时自动保存对话数据

parent ab892f2f
...@@ -30,9 +30,13 @@ public class ChatCache implements DisposableBean { ...@@ -30,9 +30,13 @@ public class ChatCache implements DisposableBean {
this.reloading = reloading; this.reloading = reloading;
} }
public String getChatKey() { return chatKey; } public String getChatKey() {
return chatKey;
}
public void setChatKey(String chatKey) { this.chatKey = chatKey; } public void setChatKey(String chatKey) {
this.chatKey = chatKey;
}
public Map<String, String> getInfoPoint() { public Map<String, String> getInfoPoint() {
return infoPoint; return infoPoint;
...@@ -50,14 +54,13 @@ public class ChatCache implements DisposableBean { ...@@ -50,14 +54,13 @@ public class ChatCache implements DisposableBean {
this.gameEnv = gameEnv; this.gameEnv = gameEnv;
} }
public ChatCache(String chatKey, GameEnv gameEnv, Map<String, String> infoPoint){ public ChatCache(String chatKey, GameEnv gameEnv, Map<String, String> infoPoint) {
this.chatKey = chatKey; this.chatKey = chatKey;
this.gameEnv = gameEnv; this.gameEnv = gameEnv;
this.infoPoint = infoPoint; this.infoPoint = infoPoint;
} }
@Override @Override
public void destroy() throws IOException { public void destroy() throws IOException {
//System.out.println("销毁:" + chatKey); //System.out.println("销毁:" + chatKey);
...@@ -67,7 +70,7 @@ public class ChatCache implements DisposableBean { ...@@ -67,7 +70,7 @@ public class ChatCache implements DisposableBean {
public ChatCache(UserInfo userInfo) throws Exception { public ChatCache(UserInfo userInfo) throws Exception {
try { try {
gameEnv = new GameEnv(userInfo.botAccount); gameEnv = new GameEnv(userInfo.botAccount);
}catch (Exception e){ } catch (Exception e) {
System.out.println("游戏平台初始化失败"); System.out.println("游戏平台初始化失败");
e.printStackTrace(); e.printStackTrace();
} }
...@@ -82,4 +85,11 @@ public class ChatCache implements DisposableBean { ...@@ -82,4 +85,11 @@ public class ChatCache implements DisposableBean {
infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis())); infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis()));
} }
@PreDestroy
public void dostory() {
System.out.println("I'm destory method using @PreDestroy.....");
}
} }
...@@ -213,6 +213,18 @@ public class JConfig implements Cloneable{ ...@@ -213,6 +213,18 @@ public class JConfig implements Cloneable{
this.nextStep = nextStep; this.nextStep = nextStep;
} }
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge, Map<String, String> autoJumpUpdateKnowledge) {
this.curPremise = curPremise;
this.hisPremise = hisPremise;
this.feature = feature;
this.updateKnowledge = appendKnowledgeStr(autoJumpUpdateKnowledge, updateKnowledge);
//if (!updateKnowledge.equals(""))
// this.updateKnowledge += "updateKnowledge-<-" + updateKnowledge;
this.reply = reply;
this.nextStep = nextStep;
}
public JConfig(List<String> curPremise, List<String> hisPremise, Map<String, String> updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority) { public JConfig(List<String> curPremise, List<String> hisPremise, Map<String, String> updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority) {
this.curPremise = appendPremiseStr(curPremise, extraCP); this.curPremise = appendPremiseStr(curPremise, extraCP);
......
...@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject; ...@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.JConfig; import com.qiwu.chatbotbackend.entity.JConfig;
import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.entity.UserInfo;
import org.apache.commons.lang.StringUtils;
import java.util.*; import java.util.*;
......
...@@ -123,8 +123,48 @@ public class ElementConstructor { ...@@ -123,8 +123,48 @@ public class ElementConstructor {
} }
} }
for(Map premise:JSONArray.parseArray(config.getJSONArray("historyPremiseList").toJSONString(), Map.class)){
conditionName = (String) premise.get("actionName");
if(conditionName.startsWith("npc_") || conditionName.startsWith("goods_"))
continue;
if(!exceptRelation.contains(conditionName)) {
if(!relationKeywords.containsKey(conditionName + "_head")) {
relationKeywords.put(conditionName + "_head", new HashSet<String>());
relationKeywords.put(conditionName + "_tail", new HashSet<String>());
}
premise.put("keywords", ((String)premise.get("keywords")).replace("qiwu", "#visitorname").replace(botName, "#botname"));
for (String keyword : ((String) premise.get("keywords")).split("\\+")) {
if(keyword.equals("")){
continue;
} else if(keyword.contains(":")){
pair = keyword.split(":",2);
if(pair[1].equals(""))
continue;
relationKeywords.get(conditionName + "_head").add(pair[0]);
relationKeywords.get(conditionName + "_tail").add(pair[1]);
} else {
relationKeywords.get(conditionName + "_tail").add(keyword);
}
}
}
}
} }
/** /**
* 根据插入的数据做后处理 * 根据插入的数据做后处理
* @param idList * @param idList
......
...@@ -37,7 +37,7 @@ public class GlobalVariableConstructor extends ElementConstructor { ...@@ -37,7 +37,7 @@ public class GlobalVariableConstructor extends ElementConstructor {
for(String attrName:attribute.keySet()) { for(String attrName:attribute.keySet()) {
attrValue = attribute.get(attrName); attrValue = attribute.get(attrName);
varInitialMap.put("globalVariable_" + category + "_" + attrName, attrValue); varInitialMap.put("globalVariable_" + attrName, attrValue);
} }
} }
......
...@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject; ...@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.JConfig; import com.qiwu.chatbotbackend.entity.JConfig;
import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.entity.UserInfo;
import org.apache.commons.lang.StringUtils;
import java.util.*; import java.util.*;
......
...@@ -181,8 +181,6 @@ public class MapConstructor extends ElementConstructor { ...@@ -181,8 +181,6 @@ public class MapConstructor extends ElementConstructor {
operationStatus = 0; operationStatus = 0;
e.printStackTrace(); e.printStackTrace();
message = "地图处理出现异常:"; message = "地图处理出现异常:";
e.printStackTrace();
message += e.getMessage();
return; return;
} }
...@@ -396,6 +394,10 @@ public class MapConstructor extends ElementConstructor { ...@@ -396,6 +394,10 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘周围有什么人’所需要的配置文件
*/
public void generateSurroundingPeople(Map<String, StringBuffer> keywordMap){ public void generateSurroundingPeople(Map<String, StringBuffer> keywordMap){
String conditionName = "know_exist_what"; String conditionName = "know_exist_what";
String conditionKeywords = keywordMap.get("people_" + conditionName).toString(); String conditionKeywords = keywordMap.get("people_" + conditionName).toString();
...@@ -408,6 +410,10 @@ public class MapConstructor extends ElementConstructor { ...@@ -408,6 +410,10 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘周围有什么物品’所需要的配置文件
*/
public void generateSurroundingItem(Map<String, StringBuffer> keywordMap){ public void generateSurroundingItem(Map<String, StringBuffer> keywordMap){
String conditionName = "know_exist_what"; String conditionName = "know_exist_what";
String conditionKeywords = keywordMap.get("item_" + conditionName).toString(); String conditionKeywords = keywordMap.get("item_" + conditionName).toString();
...@@ -421,6 +427,10 @@ public class MapConstructor extends ElementConstructor { ...@@ -421,6 +427,10 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘周围有什么(包含人和物品)’所需要的配置文件
*/
public void generateSurroundThings(Map<String, StringBuffer> keywordMap){ public void generateSurroundThings(Map<String, StringBuffer> keywordMap){
String conditionName = "know_exist_what_mohu"; String conditionName = "know_exist_what_mohu";
String conditionKeywords = keywordMap.get("things_" + conditionName).toString(); String conditionKeywords = keywordMap.get("things_" + conditionName).toString();
...@@ -433,6 +443,10 @@ public class MapConstructor extends ElementConstructor { ...@@ -433,6 +443,10 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘周围有什么地点’所需要的配置文件
*/
public void generateSurroundPlace(Map<String, StringBuffer> keywordMap){ public void generateSurroundPlace(Map<String, StringBuffer> keywordMap){
String conditionName = "know_exist_what"; String conditionName = "know_exist_what";
String conditionKeywords = keywordMap.get("place_" + conditionName).toString(); String conditionKeywords = keywordMap.get("place_" + conditionName).toString();
...@@ -446,6 +460,11 @@ public class MapConstructor extends ElementConstructor { ...@@ -446,6 +460,11 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘我们有什么’所需要的配置文件
*/
public void generateWeHave(Map<String, StringBuffer> keywordMap){ public void generateWeHave(Map<String, StringBuffer> keywordMap){
String conditionName = "know_exist_what_mohu"; String conditionName = "know_exist_what_mohu";
String conditionKeywords = keywordMap.get("we_have_" + conditionName).toString(); String conditionKeywords = keywordMap.get("we_have_" + conditionName).toString();
...@@ -459,6 +478,11 @@ public class MapConstructor extends ElementConstructor { ...@@ -459,6 +478,11 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘里面有什么人’所需要的配置文件
*/
public void generateInnerPeople(Map<String, StringBuffer> keywordMap){ public void generateInnerPeople(Map<String, StringBuffer> keywordMap){
...@@ -479,6 +503,11 @@ public class MapConstructor extends ElementConstructor { ...@@ -479,6 +503,11 @@ public class MapConstructor extends ElementConstructor {
} }
/**
* 生成‘我们的任务是什么’所需要的配置文件
*/
public void generateTaskDescription(Map<String, StringBuffer> keywordMap){ public void generateTaskDescription(Map<String, StringBuffer> keywordMap){
......
...@@ -152,6 +152,7 @@ public class ChatCacheController { ...@@ -152,6 +152,7 @@ public class ChatCacheController {
chatCache.setReloading(true); chatCache.setReloading(true);
br.close(); br.close();
fr.close();
loadCacheStatus = true; loadCacheStatus = true;
...@@ -262,6 +263,7 @@ public class ChatCacheController { ...@@ -262,6 +263,7 @@ public class ChatCacheController {
if (now - lastChatTime > ChatCacheController.TIME_OUT) { if (now - lastChatTime > ChatCacheController.TIME_OUT) {
chatCache = chatCacheMap.get(chatKey); chatCache = chatCacheMap.get(chatKey);
saveChatCache(chatKey, chatCache); saveChatCache(chatKey, chatCache);
System.out.println("移除" + chatKey);
it.remove(); it.remove();
} }
} }
......
...@@ -2,7 +2,6 @@ package com.qiwu.chatbotbackend.processing; ...@@ -2,7 +2,6 @@ package com.qiwu.chatbotbackend.processing;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.entity.Condition; import com.qiwu.chatbotbackend.entity.Condition;
import com.qiwu.chatbotbackend.entity.Solution;
import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.entity.UserInfo;
import com.qiwu.chatbotbackend.gameDesign.PlotConstructor; import com.qiwu.chatbotbackend.gameDesign.PlotConstructor;
...@@ -35,12 +34,18 @@ public class ResponseFilter { ...@@ -35,12 +34,18 @@ public class ResponseFilter {
return reply; return reply;
} }
if(historyInfoPoint.get("plot_jump_text") != null && !historyInfoPoint.get("plot_jump_text").equals("")){
return reply;
}
if(reply.startsWith("【" + historyInfoPoint.get(PlotConstructor.ASSISTANT)+ "】") ){ if(reply.startsWith("【" + historyInfoPoint.get(PlotConstructor.ASSISTANT)+ "】") ){
return reply; return reply;
}else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1){ }else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 1){
return reply; return reply;
}else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0){ }else if (sameLocation(reply, curInfoPoint, historyInfoPoint, gameEnv, worldController, site, logWriter) == 0 && !reply.equals("")){
return appendTalker(reply,curInfoPoint, historyInfoPoint, gameEnv, worldController); return appendTalker(reply,curInfoPoint, historyInfoPoint, gameEnv, worldController);
}else if (reply.equals("") && !historyInfoPoint.get("common_answer").equals("")){
return reply;
} }
if(userInfo.varInitialMap.get("unknownTextList") != null){ if(userInfo.varInitialMap.get("unknownTextList") != null){
...@@ -57,8 +62,6 @@ public class ResponseFilter { ...@@ -57,8 +62,6 @@ public class ResponseFilter {
} }
if(unknownList.size() > 0){ if(unknownList.size() > 0){
return MyFunc.randStr(unknownList); return MyFunc.randStr(unknownList);
} }
} }
......
...@@ -92,6 +92,9 @@ public class UserInfoController { ...@@ -92,6 +92,9 @@ public class UserInfoController {
try { try {
text = line.split("---"); text = line.split("---");
if (line.startsWith("yaobuyao::match-Keyword=+海边|game_step::match-Include=9826795627_plot_1-2,1-2::number-BiggerOrEqual=1|match,updateKnowledge-<-unknownText:-<-site:武器库-<--<-game_step:9826795627_plot_1-5-<-unknownText:还没到下一个地点-<-site:武器库-<-npc_update:updateFollowerLocation:武"))
System.out.println("xxx");
line = text[0]; line = text[0];
if(text.length > 1 && text[1].startsWith("<-")) if(text.length > 1 && text[1].startsWith("<-"))
line += "---" + text[1]; line += "---" + text[1];
......
...@@ -25,8 +25,8 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -25,8 +25,8 @@ public class GetAnswerServlet extends HttpServlet {
*/ */
String NAME_MISSING_ERROR = "没找到bot名?"; String NAME_MISSING_ERROR = "没找到bot名?";
String LOG_PATH = "/var/www/html/logs/"; //String LOG_PATH = "/var/www/html/logs/";
String LOG_PATH = "/home/hxw/source/logs/";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8");
...@@ -108,7 +108,7 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -108,7 +108,7 @@ public class GetAnswerServlet extends HttpServlet {
//信息点预处理 //信息点预处理
QuestionMatcher matcher = new QuestionMatcher(curInfoPoint, historyInfoPoint, logWriter, userinfo, context, chatCache.getGameEnv()); QuestionMatcher matcher = new QuestionMatcher(curInfoPoint, historyInfoPoint, logWriter, userinfo, context, chatCache.getGameEnv(), botUtil);
/*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/ /*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/
......
package com.qiwu.chatbotbackend.servlet;
import com.qiwu.chatbotbackend.entity.ChatCache;
import com.qiwu.chatbotbackend.processing.ChatCacheController;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Iterator;
import java.util.Map;
@WebListener
public class MyListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("destory context....");
/**
* 在程序中断时先把所有存在servletContext中的对话缓存数据保存下来
*/
String chatKey;
ServletContext servletContext = sce.getServletContext();
Object obj = servletContext.getAttribute("chatCache");
ChatCache cache = null;
if(obj != null){
Map<String, ChatCache> chatCacheMap = (Map<String, ChatCache>)obj;
Iterator<String> cacheIter = chatCacheMap.keySet().iterator();
while(cacheIter.hasNext()){
chatKey = cacheIter.next();
cache = chatCacheMap.get(chatKey);
try {
System.out.println(String.format("saving %s", chatKey));
ChatCacheController.saveChatCache(chatKey, cache);
}catch (Exception e){
System.out.println(String.format("保存%s时出现异常", chatKey));
}
cacheIter.remove();
}
}
}
}
package com.qiwu.chatbotbackend.task.utils; package com.qiwu.chatbotbackend.task.utils;
import com.qiwu.chatbotbackend.GameEnvironment.GameEnv;
import com.qiwu.chatbotbackend.entity.Solution; import com.qiwu.chatbotbackend.entity.Solution;
import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.entity.UserInfo;
import com.qiwu.chatbotbackend.gameDesign.GameUtil; import com.qiwu.chatbotbackend.gameDesign.GameUtil;
...@@ -123,4 +124,32 @@ public class BasicBotUtil { ...@@ -123,4 +124,32 @@ public class BasicBotUtil {
public boolean postprocessing(String chatKey, ServletContext context, Solution bestMatch){ public boolean postprocessing(String chatKey, ServletContext context, Solution bestMatch){
return true; return true;
} }
public String getKeyword(Map<String, String> infoPoint, Map<String, String> infoPoint2, Boolean chooseLater, String actionName){
String keyword = "";
String nameSeries[] = actionName.split("_");
/**
* 如果chooseLater为真,则先提取infoPint2中的关键字,如果infoPoint2中不存在,再提取infoPoint中的关键字
*/
if(chooseLater){
keyword = infoPoint2.get(actionName);
if(keyword != null && !keyword.equals(""))
return keyword;
}
keyword = infoPoint.get(actionName);
return keyword;
};
public void putKeyword(String variableName, String updateValue){
historyInfoPoint.put(variableName, updateValue);
}
public String knowledgeProcess(String variableName, String updateValue){
return updateValue;
}
} }
...@@ -67,12 +67,93 @@ public class GameBotUtil extends BasicBotUtil{ ...@@ -67,12 +67,93 @@ public class GameBotUtil extends BasicBotUtil{
e.printStackTrace(); e.printStackTrace();
} }
} }
String plot_jump_text = historyInfoPoint.get("plot_jump_text");
if(plot_jump_text != null && !plot_jump_text.equals("")) {
bestMatch.setAnswer(bestMatch.getAnswer() + plot_jump_text + " " );
historyInfoPoint.put("plot_jump_text", "");
}
String common_answer = historyInfoPoint.get("common_answer"); String common_answer = historyInfoPoint.get("common_answer");
if(common_answer != null && !common_answer.equals("") && !common_answer.equals("no answer")){ if(common_answer != null && !common_answer.equals("") && !common_answer.equals("no answer")){
bestMatch.setAnswer(common_answer + ", " + bestMatch.getAnswer()); bestMatch.setAnswer(common_answer + " " + bestMatch.getAnswer());
historyInfoPoint.put("common_answer", ""); historyInfoPoint.put("common_answer", "");
} }
return true; return true;
} }
public String getKeyword(Map<String, String> infoPoint, Map<String, String> infoPoint2, Boolean chooseLater, String actionName){
String keyword = "";
String nameSeries[] = actionName.split("_");
//变量名以npc_开头,就从游戏环境中查询相应的值
if(actionName.startsWith("npc_")){
if(nameSeries.length == 4){
keyword = gameEnv.queryNpcAttrByNpcName(nameSeries[2], nameSeries[3]);
}
}else if (actionName.startsWith("goods_")) {
if (nameSeries.length == 4) {
keyword = gameEnv.queryGoodsAttrByGoodsName(nameSeries[2], nameSeries[3]);
System.out.println("return" + keyword);
}
}else if(actionName.startsWith("isNeighbour")){
String opPair[] = actionName.split(":");
if(!curInfoPoint.containsKey(opPair[1]) || curInfoPoint.get(opPair[1]).equals("unknown") || curInfoPoint.get(opPair[1]).equals("")){
keyword = "not_exist";
}else {
logWriter.record("isNeighbour(" + historyInfoPoint.get("site") + "," + curInfoPoint.get(opPair[1]).split(":")[1] + ")", 1);
keyword = String.valueOf(gameEnv.isNeighbour(historyInfoPoint.get("site"), curInfoPoint.get(opPair[1]).split(":")[1]));
logWriter.record("返回:" + keyword, 1);
}
}else {
/**
* 如果chooseLater为真,则先提取infoPint2中的关键字,如果infoPoint2中不存在,再提取infoPoint中的关键字
*/
if(chooseLater){
keyword = infoPoint2.get(actionName);
if(keyword != null && !keyword.equals(""))
return keyword;
}
keyword = infoPoint.get(actionName);
}
return keyword;
};
@Override
public void putKeyword(String variableName, String updateValue){
//如果变量名开头为NPC或者item,就专门交给world处理
if((variableName.startsWith("npc_") || variableName.startsWith("goods_") || variableName.startsWith("map_") || variableName.startsWith("place_"))){
try {
if(!updateValue.startsWith("query") && !updateValue.startsWith("update")){
updateValue = "update:" + updateValue;
}
String returnStr = worldController.worldOperation(gameEnv, variableName, updateValue, curInfoPoint, historyInfoPoint);
updateValue = returnStr;
}catch (Exception e){
e.printStackTrace();
updateValue = "游戏模块处理出错";
}
}
historyInfoPoint.put(variableName, updateValue);
}
public String knowledgeProcess(String variableName, String updateValue){
String values[] = updateValue.split(":",2), returnValue;
if(values.length > 1 && values[0].equals("update"))
returnValue = values[1];
else
returnValue = updateValue;
return returnValue;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment