Commit d70969e1 by hxw

routine

parent 2f713101
......@@ -59,6 +59,7 @@ public class ChatCache {
k = iit.next();
infoPoint.put(k, userInfo.varInitialMap.get(k));
}
infoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis()));
}
}
......@@ -8,7 +8,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class JConfig {
public class JConfig implements Cloneable{
private int id = 0;
private String jCurPremise = "";
......@@ -69,6 +69,98 @@ public class JConfig {
private String status = "waiting";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getjCurPremise() {
return jCurPremise;
}
public void setjCurPremise(String jCurPremise) {
this.jCurPremise = jCurPremise;
}
public String getjHisPremise() {
return jHisPremise;
}
public void setjHisPremise(String jHisPremise) {
this.jHisPremise = jHisPremise;
}
public String getjFeature() {
return jFeature;
}
public void setjFeature(String jFeature) {
this.jFeature = jFeature;
}
public String getjNextStep() {
return jNextStep;
}
public void setjNextStep(String jNextStep) {
this.jNextStep = jNextStep;
}
public String getjReply() {
return jReply;
}
public void setjReply(String jReply) {
this.jReply = jReply;
}
public String getjAuxiliaryPriority() {
return jAuxiliaryPriority;
}
public void setjAuxiliaryPriority(String jAuxiliaryPriority) {
this.jAuxiliaryPriority = jAuxiliaryPriority;
}
public int getjStatus() {
return jStatus;
}
public void setCurPremise(String curPremise) {
this.curPremise = curPremise;
}
public String getHisPremise() {
return hisPremise;
}
public void setHisPremise(String hisPremise) {
this.hisPremise = hisPremise;
}
public String getReply() {
return reply;
}
public void setReply(String reply) {
this.reply = reply;
}
public String getAuxiliaryPriority() {
return auxiliaryPriority;
}
public String getStatus() {
return status;
}
public String getNextStepStatus() {
return nextStepStatus;
}
public String getUpdateKnowledge() {
return updateKnowledge;
}
......@@ -80,14 +172,15 @@ public class JConfig {
private String updateKnowledge = "";
private String nextStepStatus = "return";
Map<String, String> statusMap = new HashMap<String, String>() {
static Map<String, String> statusMap = new HashMap<String, String>() {
{
put("return", "return");
put("return-1", "back");
put("return-0", "stay");
put("active", "activate");
put("activate", "activate");
}
};
static int querySkipCount = 10;
public void setStatus(String status) {
this.status = status;
......@@ -252,7 +345,7 @@ public class JConfig {
data.put("historyPremiseList", hisPremiseList);
//添加下一步状态
data.put("nextStepStatus", statusMap.get(nextStepStatus));
//添加configName
data.put("configName", configName);
......@@ -270,8 +363,15 @@ public class JConfig {
pair = feat.substring(0, feat.length() - 1).split("\\(", 2);
postURL = pair[1];
postType = pair[0].split("::")[1];
}else if(feat.equals("return-0")){
nextStepStatus = "return-0";
}else if(feat.equals("activate")){
nextStepStatus = "activate";
}
}
data.put("nextStepStatus", statusMap.get(nextStepStatus));
//添加post参数
data.put("doPost", doPost);
data.put("postURL", postURL);
......@@ -319,4 +419,37 @@ public class JConfig {
return text;
}
public Object clone() {
Object o=null;
try{
o= super.clone();
}catch(CloneNotSupportedException e){
System.out.println(e.toString());
}
return o;
}
public static JConfig activateCopy(JConfig jConfig){
JConfig jConfig2 = (JConfig) jConfig.clone();
jConfig2.setReply(jConfig2.getReply() + ",{临时变量2-2}");
jConfig2.setUpdateKnowledge(jConfig2.getUpdateKnowledge() + "-<-process-nextStep:var={临时变量1-2}");
jConfig2.setStatus("sleeping");
jConfig2.setConfigName("queryskip" + String.valueOf(querySkipCount));
querySkipCount++;
jConfig2.setjStatus(1);
jConfig2.setAuxiliaryPriority("2");
return jConfig2;
}
public static JConfig simpleCopy(JConfig jConfig){
JConfig jConfig2 = (JConfig) jConfig.clone();
return jConfig2;
}
}
......@@ -10,11 +10,14 @@ import java.util.*;
public class CharacterConstructor extends ElementConstructor {
public CharacterConstructor(SqlHelper helper, int botId, HashSet<String> exceptRelation) {
public CharacterConstructor(SqlHelper helper, int botId, int skipCount, String botAccount, HashSet<String> exceptRelation, StringBuffer POS) {
super();
this.helper = helper;
this.botId = botId;
this.botAccount = botAccount;
this.exceptRelation = exceptRelation;
this.skipCount = skipCount;
this.POS = POS;
}
public void construct(JSONObject gameElements, UserInfo userinfo) {
......@@ -50,7 +53,11 @@ public class CharacterConstructor extends ElementConstructor {
for (JSONObject character : allNPC.toJavaList(JSONObject.class)) {
name = (String) character.get("name");
if(!name.equals(""))
POS.append("=" + name + "|人名\r\n");
className = (String) character.get("class");
if(!className.equals(""))
POS.append("=" + className + "|人名\r\n");
if (!classSet.contains(className)) {
classSet.add(className);
......@@ -64,6 +71,8 @@ public class CharacterConstructor extends ElementConstructor {
keywords.append("+" + repValue);
while (attrIter.hasNext()) {
attrName = attrIter.next();
if(!attrName.equals(""))
POS.append("=" + attrName + "|名词\r\n");
attrValue = attribute.get(attrName);
}
......@@ -75,17 +84,25 @@ public class CharacterConstructor extends ElementConstructor {
for (String conditionName : relateConditions) {
jConfig = new JConfig(
conditionName + "::match-Include=" + keywords, " ",
"match,return-0", "{NPC_query-2}", "yaobuyao", "NPC_query:queryAttrByNPCName:" + conditionName
"match,return-0", "【{智能助手-2}】{NPC_query-2}", "yaobuyao", "NPC_query:queryAttrByNPCName:" + conditionName
);
jConfig.setAuxiliaryPriority("1");
jConfigList.add(jConfig);
}
//根据根据NPC类别和地点属性查询所有NPC实例
for (String conditionName : relateConditions) {
jConfig = new JConfig(
conditionName + "::match-Include=" + classKeywords, " ",
"match,return-0", "{NPC_query-2}", "yaobuyao", "NPC_query:queryAroundNpcByClassName:" + conditionName
"match,return-0", "【{智能助手-2}】{NPC_query-2}", "yaobuyao", "NPC_query:queryAroundNpcByClassName:" + conditionName
);
jConfig.setAuxiliaryPriority("1");
jConfigList.add(jConfig);
}
}catch (Exception e){
operationStatus = 0;
......
......@@ -16,11 +16,12 @@ public class ElementConstructor {
int operationStatus = 1;
int skipCount = 0;
SqlHelper helper = null;
HashSet<String> exceptRelation = null;
JConfig jConfig = null;
String CHITCHAT_URL = "http:192.168.1.178:1111/test";
String userName;
String botAccount;
......@@ -32,6 +33,7 @@ public class ElementConstructor {
StringBuffer configText = new StringBuffer();
Map<String, String> varInitialMap = new HashMap<String, String>();
Map<String, String> conditionDescription = new HashMap<String, String>();
StringBuffer POS = new StringBuffer();
public String getMessage() { return message; }
......@@ -57,7 +59,7 @@ public class ElementConstructor {
}
public void ElementConstructor(String userName) {
this.userName = userName;
this.botAccount = botAccount;
}
public String getConfigText() {
......
......@@ -12,11 +12,14 @@ import java.util.Map;
public class GlobalVariableConstructor extends ElementConstructor {
public GlobalVariableConstructor(SqlHelper helper, int botId, HashSet<String> exceptRelation) {
public GlobalVariableConstructor(SqlHelper helper, int botId, int skipCount, String botAccount, HashSet<String> exceptRelation, StringBuffer POS) {
super();
this.helper = helper;
this.botId = botId;
this.botAccount = botAccount;
this.exceptRelation = exceptRelation;
this.skipCount = skipCount;
this.POS = POS;
}
public void construct(JSONObject gameElements, UserInfo userinfo) {
......@@ -34,7 +37,7 @@ public class GlobalVariableConstructor extends ElementConstructor {
while (attrIter.hasNext()) {
attrName = attrIter.next();
attrValue = attribute.get(attrName);
varInitialMap.put(category + "_" + attrName, attrValue);
varInitialMap.put(attrName, attrValue);
}
}
}catch (Exception e){
......
......@@ -11,11 +11,15 @@ import java.util.*;
public class ItemConstructor extends ElementConstructor {
public ItemConstructor(SqlHelper helper, int botId, HashSet<String> exceptRelation) {
public ItemConstructor(SqlHelper helper, int botId, int skipCount, String botAccount, HashSet<String> exceptRelation, StringBuffer POS) {
super();
this.helper = helper;
this.botId = botId;
this.botAccount = botAccount;
this.exceptRelation = exceptRelation;
this.skipCount = skipCount;
this.POS = POS;
}
......@@ -46,7 +50,11 @@ public class ItemConstructor extends ElementConstructor {
for (JSONObject good : goods.toJavaList(JSONObject.class)) {
name = (String) good.get("name");
if(!name.equals(""))
POS.append("=" + name + "|名词\r\n");
className = (String) good.get("class");
if(!className.equals(""))
POS.append("=" + className + "|名词\r\n");
if (!classSet.contains(className)) {
classSet.add(className);
......@@ -58,6 +66,9 @@ public class ItemConstructor extends ElementConstructor {
attrIter = attribute.keySet().iterator();
while (attrIter.hasNext()) {
attrName = attrIter.next();
if(!attrName.equals(""))
POS.append("=" + attrName + "|名词\r\n");
attrValue = attribute.get(attrName);
keywords.append("+" + name + ":" + attrName);
......@@ -71,10 +82,11 @@ public class ItemConstructor extends ElementConstructor {
jConfig = new JConfig(
conditionName + "::match-Include=" + keywords, " ",
"match,return-0",
"{goods_query-2}",
"【{智能助手-2}】{goods_query-2}",
" ",
"goods_query:queryAttrByGoodsName:" + conditionName
);
jConfig.setAuxiliaryPriority("1");
jConfigList.add(jConfig);
}
......@@ -83,10 +95,11 @@ public class ItemConstructor extends ElementConstructor {
jConfig = new JConfig(
conditionName + "::match-Include=" + classKeywords, " ",
"match,return-0",
"{goods_query-2}",
"【{智能助手-2}】{goods_query-2}",
" ",
"goods_query:queryAroundGoodsByClassName:" + conditionName
);
jConfig.setAuxiliaryPriority("1");
jConfigList.add(jConfig);
}
} catch(Exception e){
......
......@@ -19,7 +19,7 @@ import java.util.Map;
public class ChatCacheController {
public static long TIME_OUT = 5000;
public static long TIME_OUT = 300000;
private static String CHAT_CACHE_DIR = "ChatCache";
......@@ -189,8 +189,8 @@ public class ChatCacheController {
try {
chatCache = chatCacheMap.get(chatKey);
if(chatCache == null){
if(receiveParamters.containsKey("yaobuyao") && (receiveParamters.get("yaobuyao")[0].contains("开始游戏") ||
receiveParamters.get("yaobuyao")[0].contains("继续游戏")|| receiveParamters.get("yaobuyao")[0].contains("开始"))) {
if(receiveParamters.containsKey("start_options") && (receiveParamters.get("start_options")[0].contains("开始游戏") ||
receiveParamters.get("start_options")[0].contains("继续游戏")|| receiveParamters.get("start_options")[0].contains("继续"))) {
chatCache = loadChatCache(chatKey, context, userinfo, loadCacheStatus);
}else {
chatCache = new ChatCache(userinfo);
......
......@@ -336,8 +336,15 @@ public class QuestionMatcher {
keyword = gameEnv.queryGoodsAttrByGoodsName(nameSeries[2], nameSeries[3]);
System.out.println("return" + keyword);
}
}else if(actionName.startsWith("is_neighbour")){
//keyword = gameEnv.isNeighbour(historyInfoPoint.get("site"), actionName.split(":")[1]);
}else if(actionName.startsWith("isNeighbour")){
String opPair[] = actionName.split(":");
if(!curInfoPoint.containsKey(opPair[1]) || curInfoPoint.get(opPair[1]).equals("unknown")){
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 {
keyword = infoPoint.get(actionName);
......@@ -735,7 +742,10 @@ public class QuestionMatcher {
//停留
} else if (ansCondition.featureMap.containsKey("return-0")) {
String activeList[] = historyInfoPoint.get("activeList").split("\\|");
ans.setNext("nextStep=" + activeList[activeList.length - 1]);
if(activeList.length > 0)
ans.setNext("nextStep=" + activeList[activeList.length - 1]);
else
ans.setNext("nextStep= ");
//激活
} else if (ansCondition.featureMap.containsKey("activate")) {
String activeList[] = historyInfoPoint.get("activeList").split("\\|");
......@@ -759,13 +769,19 @@ public class QuestionMatcher {
//用正则表达式将回答中的变量模板{conditionName-2}替换成后台存储的变量值
String keyword;
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).equals(""))
reply = reply.replace("{" + variable + "-1}", curInfoPoint.get(variable));
if (curInfoPoint.get(variable) != null && !curInfoPoint.get(variable).equals("")) {
keyword = curInfoPoint.get(variable);
if(keyword.contains(":"))
keyword = keyword.split(":")[1];
reply = reply.replace("{" + variable + "-1}", keyword);
}
} else if (m.group(1).endsWith("-2}")) {
if (historyInfoPoint.get(variable) != null)
if(historyInfoPoint.get(variable).equals("do_reset")){
......
......@@ -130,28 +130,69 @@ public class WorldController {
}
break;
case "queryCanGoBySite":
if(!currentInfoPoint.containsKey(valuePair[1])) {
returnStr = "不可到达";
break;
}
sites = gameEnv.queryCanGoBySite(historyInfoPoint.get("site"));
if(sites == null || sites.size() == 0)
if(sites == null) {
returnStr = "没什么地方可去的,继续玩吧";
else {
}
else if(sites.size() == 0){
returnStr = "没什么地方可去的,继续玩吧";
} else {
returnStr = "可以去";
if(sites.size() > 0) {
for (String site : sites)
returnStr += "、" + site;
System.out.println("returnStr" + returnStr);
}else{
returnStr = "没什么地方可去的,继续玩吧";
}
}
break;
case "queryPathBySite":
sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), valuePair[1]);
if(sites == null || sites.size() == 0)
case "queryHowToPathBySite":
if(!currentInfoPoint.containsKey(valuePair[1])) {
returnStr = "不可到达";
break;
}
sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), currentInfoPoint.get(valuePair[1]).split(":")[1]);
if(sites == null) {
returnStr = "不可到达";
}
else if(sites.size() == 0){
returnStr = "就在附近哦";
else {
}else {
returnStr = "通过";
if(sites.size() > 0) {
for (String site : sites)
returnStr += "、" + site;
returnStr += "可以到达";
}else{
returnStr = "就在附近哦";
}
}
break;
case "queryToDoPathBySite":
if(!currentInfoPoint.containsKey(valuePair[1])) {
returnStr = "不可到达";
break;
}
sites = gameEnv.queryPathBySite(historyInfoPoint.get("site"), currentInfoPoint.get(valuePair[1]).split(":")[1]);
if(sites == null) {
returnStr = "不可到达";
}
else if(sites.size() == 0){
returnStr = "就在附近哦";
}else {
returnStr = "通过";
if(sites.size() > 0) {
for (String site : sites)
returnStr += "、" + site;
returnStr += "可以到达";
}else{
returnStr = "就在附近哦";
}
......
......@@ -35,15 +35,23 @@ public class GameDesignServlet extends HttpServlet {
}
};
//每个condition的三元组两端需要补充的关键词,比如npc名字、npc属性等
HashMap<String, HashSet<String>> relationKeywords = new HashMap<String, HashSet<String>>();
//特定的relation的三元组不会被添加到relationKeywords
HashSet<String> exceptRelation = new HashSet<String>();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//所有需要修改的词性
StringBuffer POS = new StringBuffer();
//每个condition的三元组两端需要补充的关键词,比如npc名字、npc属性等
HashMap<String, HashSet<String>> relationKeywords = new HashMap<String, HashSet<String>>();
//特定的relation的三元组不会被添加到relationKeywords
HashSet<String> exceptRelation = new HashSet<String>();
Integer botId, projectId, dataId, returnStatus = 1;
String botAccount = null, moduleName, userName, moduleName2, returnMessage = "Success";
String botAccount = null, userName, moduleName2, returnMessage = "Success";
ServletContext context = this.getServletContext();
JSONObject json = null, root = null;
Boolean flag;
......@@ -95,37 +103,38 @@ public class GameDesignServlet extends HttpServlet {
}
Iterator<String> moduleIter = gameElements.keySet().iterator();
StringBuffer mergedConfigText = new StringBuffer();
while (moduleIter.hasNext()) {
int skipCount = 0;
String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"};
for(String moduleName: moduleList){
moduleName = moduleIter.next();
switch (moduleName) {
case "map":
moduleName2 = "地图";
constructor = new MapConstructor(helper, botId, exceptRelation);
constructor = new MapConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
case "plots":
moduleName2 = "剧情";
constructor = new PlotConstructor(helper, botId, exceptRelation);
constructor = new PlotConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
root = ((PlotConstructor) constructor).getId2j().get((String) gameElements.get("start"));
break;
case "goods":
moduleName2 = "物品";
constructor = new ItemConstructor(helper, botId, exceptRelation);
constructor = new ItemConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
case "globalVarialbe":
case "globalVariable":
moduleName2 = "全局变量";
constructor = new GlobalVariableConstructor(helper, botId, exceptRelation);
constructor = new GlobalVariableConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
case "npc":
moduleName2 = "NPC";
constructor = new CharacterConstructor(helper, botId, exceptRelation);
constructor = new CharacterConstructor(helper, botId, skipCount, botAccount, exceptRelation, POS);
constructor.construct(gameElements, userinfo);
break;
default:
......@@ -166,7 +175,7 @@ public class GameDesignServlet extends HttpServlet {
UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(botAccount, userinfo);
ConditionTableController.writeConfigText(botAccount, userinfo);
String postResult = actionRegenerate(root, gameElements, userinfo);
String postResult = actionRegenerate(root, gameElements, userinfo, relationKeywords, exceptRelation, POS);
if(!postResult.equals("success")){
returnStatus = 0;
......@@ -221,22 +230,29 @@ public class GameDesignServlet extends HttpServlet {
return deleteConfigFlag && deleteVariableFlag;
}
public String actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo){
String startCondition;
public String actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo, HashMap<String, HashSet<String>> relationKeywords, HashSet<String> exceptRelation, StringBuffer POS){
String startCondition, POS2 = "";
String botName = (String)gameElements.get("botName");
JSONObject returnMessage = null;
JSONObject actionPostData = new JSONObject();
System.out.println("root"+ root.toJSONString());
startCondition = (String)root.get("conditionName");
actionPostData.put("startCondition", startCondition);
actionPostData.put("startCondition", "start_options");
actionPostData.put("botName_yhm", (String)gameElements.get("botName"));
actionPostData.put("botName", botName);
actionPostData.put("botId", (Integer)gameElements.get("botId"));
actionPostData.put("type", "game_plot_init");
actionPostData.put("welcome", (String)gameElements.get("plotStartText"));
actionPostData.put("welcome", "欢迎来到" + (String)gameElements.get("botName") + ",请说开始游戏或者继续游戏");
actionPostData.put("relationKeywords", relationKeywords);
if (gameElements.containsKey("POS"))
POS2 = (String)gameElements.get("POS");
actionPostData.put("POS", POS.toString() + POS2);
System.out.println(actionPostData.toJSONString());
userInfo.extraInfo.put("activeAction", startCondition);
......
......@@ -35,7 +35,7 @@ public class GetAnswerServlet extends HttpServlet {
String ansJ = "", return_str = "", state = "", chatKey = "";
String botName = receiveParamters.get("bot_name")[0];
chatKey = receiveParamters.get("chat_key")[0].split("-")[0];
chatKey = receiveParamters.get("chat_key")[0].split("@")[0];
receiveParamters.get("chat_key")[0] = chatKey;
OutputStream out = response.getOutputStream();
......
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