Commit db12838b by hxw

添加更新变量等功能

parent f38204e0
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<default-config> <default-config>
<property name="initialPoolSize">10</property> <property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property> <property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property> <property name="maxPoolSize">50</property>
<property name="minPoolSize">10</property> <property name="minPoolSize">10</property>
<property name="maxStatements">200</property> <property name="maxStatements">200</property>
</default-config> </default-config>
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<property name="password">admin</property> <property name="password">admin</property>
<property name="initialPoolSize">10</property> <property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property> <property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property> <property name="maxPoolSize">50</property>
<property name="minPoolSize">10</property> <property name="minPoolSize">10</property>
<property name="maxStatements">200</property> <property name="maxStatements">200</property>
</named-config> </named-config>
......
...@@ -290,7 +290,7 @@ public class SqlHelper { ...@@ -290,7 +290,7 @@ public class SqlHelper {
public JSONObject queryGameElementsByBotId(int id) { public JSONObject queryGameElementsByBotId(int id) {
String sql = "select * from generate_json_tb where id = ?"; String sql = "select * from generate_json_tb where project_id = ?";
String userName = ""; String userName = "";
String result = "empty"; String result = "empty";
try { try {
...@@ -334,5 +334,35 @@ public class SqlHelper { ...@@ -334,5 +334,35 @@ public class SqlHelper {
} }
public boolean insertConfigByBotId(int botId, ElementConstructor constructor, JConfig jConfig, List<Integer> idList) {
String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)";
String userName = "";
String result = "empty";
ResultSet rs = null;
int id = 0;
boolean b = true;
try {
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, String.valueOf(botId));
ps.setString(2, String.valueOf(0));
ps.setString(3, jConfig.toJson(constructor.getConditionDescription()).toJSONString());
ps.setString(4, "");
ps.executeUpdate();
rs = ps.getGeneratedKeys();
if(rs.next()){
id = rs.getInt(1);
idList.add(id);
}
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
} }
package entity;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ChatCache {
private Map<String, String> infoPoint = null;
private World world = null;
public Map<String, String> getInfoPoint() {
return infoPoint;
}
public void setInfoPoint(Map<String, String> infoPoint) {
this.infoPoint = infoPoint;
}
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public ChatCache(Map<String, String[]> curInfoPoint, UserInfo userInfo) {
world = new World();
String k;
infoPoint = new HashMap<String, String>();
Iterator<String> iit = curInfoPoint.keySet().iterator();
while (iit.hasNext()) {
k = iit.next();
infoPoint.put(k, "");
}
//变量初始化
iit = userInfo.varInitialMap.keySet().iterator();
while (iit.hasNext()) {
k = iit.next();
infoPoint.put(k, userInfo.varInitialMap.get(k));
}
}
}
...@@ -30,9 +30,8 @@ public class Condition { ...@@ -30,9 +30,8 @@ public class Condition {
this.activePriority = priority; this.activePriority = priority;
} }
public Condition(Condition originCondition, int basePriority) { public Condition(Condition originCondition, int basePriority, int baseAuxiliaryPriority) {
this.priority = originCondition.priority; this.priority = originCondition.priority;
this.auxiliaryPriority = originCondition.auxiliaryPriority;
this.keywordSet = originCondition.keywordSet; this.keywordSet = originCondition.keywordSet;
this.historyPremise = originCondition.historyPremise; this.historyPremise = originCondition.historyPremise;
this.currentPremise = originCondition.currentPremise; this.currentPremise = originCondition.currentPremise;
...@@ -44,6 +43,7 @@ public class Condition { ...@@ -44,6 +43,7 @@ public class Condition {
this.id = originCondition.id; this.id = originCondition.id;
this.status = originCondition.status; this.status = originCondition.status;
this.activePriority = originCondition.activePriority + basePriority; this.activePriority = originCondition.activePriority + basePriority;
this.auxiliaryPriority = originCondition.auxiliaryPriority + baseAuxiliaryPriority;
} }
public Condition() { public Condition() {
......
package entity;
public class Goods {
}
...@@ -19,7 +19,12 @@ public class JConfig { ...@@ -19,7 +19,12 @@ public class JConfig {
private String jNextStep = ""; private String jNextStep = "";
private String jReply = ""; private String jReply = "";
private String jAuxiliaryPriority = "0"; private String jAuxiliaryPriority = "0";
private String jStatus = "0";
public void setjStatus(int jStatus) {
this.jStatus = jStatus;
}
private int jStatus = 0;
private String curPremise = ""; private String curPremise = "";
private String hisPremise = ""; private String hisPremise = "";
...@@ -29,6 +34,12 @@ public class JConfig { ...@@ -29,6 +34,12 @@ public class JConfig {
private String reply = ""; private String reply = "";
private String nextStep = ""; private String nextStep = "";
public void setConfigName(String configName) {
this.configName = configName;
}
private String configName = "";
public String getCurPremise() { public String getCurPremise() {
return curPremise; return curPremise;
} }
...@@ -49,7 +60,9 @@ public class JConfig { ...@@ -49,7 +60,9 @@ public class JConfig {
} }
}; };
public void setStatus(String status) {
this.status = status;
}
public void setAuxiliaryPriority(String auxiliaryPriority) { public void setAuxiliaryPriority(String auxiliaryPriority) {
this.auxiliaryPriority = auxiliaryPriority; this.auxiliaryPriority = auxiliaryPriority;
} }
...@@ -63,7 +76,7 @@ public class JConfig { ...@@ -63,7 +76,7 @@ public class JConfig {
this.hisPremise = hisPremise; this.hisPremise = hisPremise;
this.feature = feature; this.feature = feature;
if (!updateKnowledge.equals("")) if (!updateKnowledge.equals(""))
this.updateKnowledge += "-<-" + updateKnowledge; this.updateKnowledge += "updateKnowledge-<-" + updateKnowledge;
this.reply = reply; this.reply = reply;
this.nextStep = nextStep; this.nextStep = nextStep;
...@@ -111,7 +124,29 @@ public class JConfig { ...@@ -111,7 +124,29 @@ public class JConfig {
while (iit.hasNext()) { while (iit.hasNext()) {
varName = iit.next(); varName = iit.next();
varValue = knowledge.get(varName); varValue = knowledge.get(varName);
sb.append("-<-" + varName + ":" + varValue); if(varName.startsWith("NPC")) {
//当名字前为NPC或者items时,需要更新World的状态,所以要在更新信息前加上操作符
if(varValue.startsWith("update:")){
sb.append("-<-" + varName + ":" + "updateAttrByNPCName:" + varValue.substring(5));
} else if(varValue.startsWith("insert:")){
sb.append("-<-" + varName + ":" + "addNpc:" + varValue.substring(5));
} else if(varValue.startsWith("delete")){
sb.append("-<-" + varName + ":" + "deleteNpc:" + varValue.substring(5));
}
}else if(varName.startsWith("item_")){
sb.append("-<-" + varName + ":" + "updateAttrByGoodsName:" + varValue);
if(varValue.startsWith("update:")){
sb.append("-<-" + varName + ":" + "updateAttrByGoodsName:" + varValue.substring(5));
} else if(varValue.startsWith("insert:")){
sb.append("-<-" + varName + ":" + "addGoodsBase:" + varValue.substring(5));
} else if(varValue.startsWith("delete")){
sb.append("-<-" + varName + ":" + "deleteGoods:" + varValue.substring(5));
}
}else {
sb.append("-<-" + varName + ":" + varValue);
}
} }
if (!extraKL.equals("")) { if (!extraKL.equals("")) {
sb.append("-<-" + extraKL); sb.append("-<-" + extraKL);
...@@ -156,7 +191,7 @@ public class JConfig { ...@@ -156,7 +191,7 @@ public class JConfig {
} }
public void appendJKnowledge(JSONArray knowledgeList, String knowledge, Map<String, String> conditionDescription) { public void appendJKnowledge(JSONArray knowledgeList, String knowledge, Map<String, String> conditionDescription) {
String pair[] = knowledge.split(":"); String pair[] = knowledge.split(":", 2);
if (knowledge.equals("") || pair.length < 2) if (knowledge.equals("") || pair.length < 2)
return; return;
...@@ -213,10 +248,10 @@ public class JConfig { ...@@ -213,10 +248,10 @@ public class JConfig {
data.put("nextStepStatus", statusMap.get(nextStepStatus)); data.put("nextStepStatus", statusMap.get(nextStepStatus));
//添加configName //添加configName
data.put("configName", ""); data.put("configName", configName);
//添加config状态 //添加config状态
data.put("configStatus", 0); data.put("configStatus", jStatus);
boolean doPost = false; boolean doPost = false;
......
package entity;
public class Npc {
}
package entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class World {
public List<String> queryAroundAllBySite(String site){ return new ArrayList<>();}; // 返回所有模块下的实例名称列表
public List<String> queryAroundNpcBySite(String site){ return new ArrayList<>();}; // 返回npcname 字符串列表
public List<String> queryAroundGoodsBySite(String site){ return new ArrayList<>();}; // 返回goodsname 字符串列表
public List<String> queryCanGoBySite(String site){ return new ArrayList<>();}; //地点名称的列表
public List<String> queryPathBySite(String start, String destination){ return new ArrayList<>();}; // 返回一条,地点名称的列表
public boolean addGoodsBase(String categoryName, String instanceName, String goodsIntro){ return true;};; // 添加成功为 true,否则false
public boolean deleteGoods(String categoryName, String instanceName){ return true;};; // 删除成功为 true,否则false
public boolean updateGoodsBase(String categoryName, String instanceName, String goodsIntro){ return true;};; // 更新成功为 true,否则false
public String queryGoodsIntro(String name, String site){ return "";};; // 返回一个物品简介
public String queryAttrByGoodsName(String name, String attrName, String site){ return "";}; //
public List<String> queryAllAttrByGoodsName(String name, String site){ return new ArrayList<>();}; // 获得所有属性名称列表
public boolean updateAttrByGoodsName(String categoryName, String instanceName, String attrName, String value){ return true;}; // 更新成功为 true,否则false
public boolean addAttrByGoodsName(String categoryName, String instanceName, String attrName, String value){ return true;}; // 添加成功为 true,否则false
public boolean deleteAttrByGoodsName(String categoryName, String instanceName, String attrName){ return true;}; // 删除成功为 true,否则false
public boolean addNpc(String categoryName, String instanceName, String npcIntro){ return true;}; // 添加成功为 true,否则false
public boolean deleteNpc(String categoryName, String instanceName){ return true;}; // 删除成功为 true,否则false
public boolean updateNpc(String categoryName, String instanceName, String npcIntro){ return true;}; // 更新成功为 true,否则false
public String queryNpcIntro(String categoryName, String instanceName){ return "";}; // 返回一个npc简介
public String queryAttrByNPCName(String categoryName, String instanceName, String attrName){ return "沙滩";}; // 返回npc属性值
public boolean updateLocationByNPCName(String categoryName, String instanceName, String attrName, String value){ return true;}; // 更新成功为 true,否则false
public boolean addAttrByNPCName(String categoryName, String instanceName, String attrName,String value){ return true;}; // 添加成功为 true,否则false
public boolean deleteAttrByNPCName(String categoryName, String instanceName, String attrName){ return true;}; // 删除成功为 true,否则false
}
...@@ -3,6 +3,7 @@ package gameDesign; ...@@ -3,6 +3,7 @@ package gameDesign;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import db.SqlHelper; import db.SqlHelper;
import entity.JConfig;
import entity.UserInfo; import entity.UserInfo;
import java.util.Iterator; import java.util.Iterator;
...@@ -21,6 +22,8 @@ public class CharacterConstructor extends ElementConstructor { ...@@ -21,6 +22,8 @@ public class CharacterConstructor extends ElementConstructor {
JSONArray allNPC = jsonObject.getJSONArray("npc"); JSONArray allNPC = jsonObject.getJSONArray("npc");
Map<String, String> attribute = null; Map<String, String> attribute = null;
Iterator<String> attrIter = null; Iterator<String> attrIter = null;
String conditionName1 = "wenzainali";
String conditionValue = "哪里:人称", repValue;
for (JSONObject npc : allNPC.toJavaList(JSONObject.class)) { for (JSONObject npc : allNPC.toJavaList(JSONObject.class)) {
category = (String) npc.get("category"); category = (String) npc.get("category");
...@@ -34,6 +37,13 @@ public class CharacterConstructor extends ElementConstructor { ...@@ -34,6 +37,13 @@ public class CharacterConstructor extends ElementConstructor {
attrName = attrIter.next(); attrName = attrIter.next();
attrValue = attribute.get(attrName); attrValue = attribute.get(attrName);
varInitialMap.put(category + "_" + name + "_" + attrName, attrValue); varInitialMap.put(category + "_" + name + "_" + attrName, attrValue);
repValue = conditionValue.replace("人称", name);
jConfig = new JConfig(
conditionName1 + "::match-Keyword=" + repValue, " ",
"match,return-0", "{NPC_reply-2}", "yaobuyao", "NPC_reply:queryAttrByNPCName:" + conditionName1
);
jConfigList.add(jConfig);
} }
} }
} }
......
...@@ -70,6 +70,12 @@ public class ElementConstructor { ...@@ -70,6 +70,12 @@ public class ElementConstructor {
} }
/** /**
* 根据插入的数据做后处理
* @param idList
*/
public void postProcessing(List<Integer> idList){}
/**
* 构造参数配置文件的json版 * 构造参数配置文件的json版
*/ */
public void buildJconfig() { public void buildJconfig() {
......
...@@ -3,6 +3,7 @@ package gameDesign; ...@@ -3,6 +3,7 @@ package gameDesign;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import db.SqlHelper; import db.SqlHelper;
import entity.JConfig;
import entity.UserInfo; import entity.UserInfo;
import java.util.Iterator; import java.util.Iterator;
...@@ -23,6 +24,8 @@ public class ItemConstructor extends ElementConstructor { ...@@ -23,6 +24,8 @@ public class ItemConstructor extends ElementConstructor {
JSONArray goods = jsonObject.getJSONArray("goods"); JSONArray goods = jsonObject.getJSONArray("goods");
Map<String, String> attribute = null; Map<String, String> attribute = null;
Iterator<String> attrIter = null; Iterator<String> attrIter = null;
String conditionName1 = "nums";
String keywords1 = "";
for (JSONObject good : goods.toJavaList(JSONObject.class)) { for (JSONObject good : goods.toJavaList(JSONObject.class)) {
...@@ -37,8 +40,18 @@ public class ItemConstructor extends ElementConstructor { ...@@ -37,8 +40,18 @@ public class ItemConstructor extends ElementConstructor {
attrName = attrIter.next(); attrName = attrIter.next();
attrValue = attribute.get(attrName); attrValue = attribute.get(attrName);
varInitialMap.put(category + "_" + name + "_" + attrName, attrValue); varInitialMap.put(category + "_" + name + "_" + attrName, attrValue);
keywords1 += "+" + name + ":" + attrName ;
} }
} }
jConfig = new JConfig(
conditionName1 + "::match-Keyword=" + keywords1, " ",
"match,return-0",
"{items_reply-2}",
" ",
"items_reply:queryAttrByGoodsName:" + conditionName1
);
jConfigList.add(jConfig);
} }
} }
} }
...@@ -58,16 +58,15 @@ public class MapConstructor extends ElementConstructor { ...@@ -58,16 +58,15 @@ public class MapConstructor extends ElementConstructor {
matrix[index][i] = 1; matrix[index][i] = 1;
} }
} }
} }
public void construct(JSONObject jsonObject, UserInfo userinfo) { public void construct(JSONObject jsonObject, UserInfo userinfo) {
conditionDescription.put("site", "当前地点"); conditionDescription.put("site", "当前地点");
varInitialMap.put("site", ""); varInitialMap.put("site", "海边");
JSONObject map = jsonObject.getJSONObject("map"); JSONObject map = jsonObject.getJSONObject("map");
Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class);
JSONArray locations = map.getJSONArray("locations"); JSONArray locations = map.getJSONArray("locations");
String botName = (String)jsonObject.get("botName");
this.locations = locations; this.locations = locations;
this.question2keyword = question2keyword; this.question2keyword = question2keyword;
//通过邻接表构造邻接矩阵 //通过邻接表构造邻接矩阵
...@@ -79,7 +78,7 @@ public class MapConstructor extends ElementConstructor { ...@@ -79,7 +78,7 @@ public class MapConstructor extends ElementConstructor {
floyd.printPath(matrix, locationNum); floyd.printPath(matrix, locationNum);
generateWantToThere(floyd.pathMatirx, floyd.preTable, jsonObject); generateWantToThere(floyd.pathMatirx, floyd.preTable, jsonObject);
generateWhereCanIgo(floyd.pathMatirx, floyd.preTable, jsonObject); generateWhereCanIgo(floyd.pathMatirx, floyd.preTable, jsonObject);
generateWhereIam(jsonObject); generateWhereIam(jsonObject, botName);
generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject); generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject);
} }
...@@ -87,10 +86,12 @@ public class MapConstructor extends ElementConstructor { ...@@ -87,10 +86,12 @@ public class MapConstructor extends ElementConstructor {
/** /**
* 生成‘我在哪里’所需要的配置文件 * 生成‘我在哪里’所需要的配置文件
*/ */
public void generateWhereIam(JSONObject jsonObject) { public void generateWhereIam(JSONObject jsonObject, String botName) {
String zainaKeywords = question2keyword.get("zaina"); String conditionName = "rencheng";
String conditionKeywords = "botName:哪里+botName:哪";
String zainaliKeywords = conditionKeywords.replace("botName", botName);
jConfig = new JConfig( jConfig = new JConfig(
"zaina::match-Keyword=" + zainaKeywords, " ", conditionName + "::match-Keyword=" + zainaliKeywords, " ",
"match", "你在{site-2}", "yaobuyao", "" "match", "你在{site-2}", "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -98,13 +99,18 @@ public class MapConstructor extends ElementConstructor { ...@@ -98,13 +99,18 @@ public class MapConstructor extends ElementConstructor {
} }
/** /**
* 生成‘怎么去哪里’所需要的配置文件 * 生成‘怎么去哪里’所需要的配置文件
*/ */
public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) {
String conditionName = "want_know_zenmezuo";
String conditionKeywords = "去:地点";
String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String placeA, placeB, descriptionB, placeKeywords, text, pathNode;
String zenmequKeywords = question2keyword.get("zenmequ"); String want_know_zenmezuoKeywords = question2keyword.get("want_know_zenmezuo");
for (int i = 0; i < locationNum; i++) { for (int i = 0; i < locationNum; i++) {
...@@ -113,11 +119,11 @@ public class MapConstructor extends ElementConstructor { ...@@ -113,11 +119,11 @@ public class MapConstructor extends ElementConstructor {
placeA = (String) locations.getJSONObject(i).get("name"); placeA = (String) locations.getJSONObject(i).get("name");
placeB = (String) locations.getJSONObject(j).get("name"); placeB = (String) locations.getJSONObject(j).get("name");
placeKeywords = zenmequKeywords.replace("地点", placeB); placeKeywords = conditionKeywords.replace("地点", placeB);
if (i == j) { if (i == j) {
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "你已经在" + placeB + "了", "yaobuyao", "" "match", "你已经在" + placeB + "了", "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -126,7 +132,7 @@ public class MapConstructor extends ElementConstructor { ...@@ -126,7 +132,7 @@ public class MapConstructor extends ElementConstructor {
; ;
if (pathMatirx[i][j] == 1) { if (pathMatirx[i][j] == 1) {
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", placeB + "就在附近哦", "yaobuyao", "" "match", placeB + "就在附近哦", "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -140,7 +146,7 @@ public class MapConstructor extends ElementConstructor { ...@@ -140,7 +146,7 @@ public class MapConstructor extends ElementConstructor {
} }
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao", "" "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -154,7 +160,8 @@ public class MapConstructor extends ElementConstructor { ...@@ -154,7 +160,8 @@ public class MapConstructor extends ElementConstructor {
* 生成‘我能去哪里’所需要的配置文件 * 生成‘我能去哪里’所需要的配置文件
*/ */
public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) {
String nengqunaKeywords = question2keyword.get("nengquna"); String conditionKeywords = "能去:哪+能去:哪里+能去:哪儿";
String conditionName = "choose_gowhere";
String placeA, placeB, pathNode; String placeA, placeB, pathNode;
for (int i = 0; i < locationNum; i++) { for (int i = 0; i < locationNum; i++) {
...@@ -169,7 +176,7 @@ public class MapConstructor extends ElementConstructor { ...@@ -169,7 +176,7 @@ public class MapConstructor extends ElementConstructor {
} }
jConfig = new JConfig( jConfig = new JConfig(
"nengquna::match-Keyword=" + nengqunaKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + conditionKeywords, "site::match-Keyword=" + placeA,
"match", "你可以去" + pathNode, "yaobuyao", "" "match", "你可以去" + pathNode, "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -185,8 +192,9 @@ public class MapConstructor extends ElementConstructor { ...@@ -185,8 +192,9 @@ public class MapConstructor extends ElementConstructor {
* 生成‘我要去那里’所需要的配置文件 * 生成‘我要去那里’所需要的配置文件
*/ */
public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) {
String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String placeA, placeB, descriptionB, placeKeywords, text, pathNode, lastTaskCondition, lastTaskReply;
String qunaKeywords = question2keyword.get("quna"); String conditionName = "quna";
String conditionKeywords = "地点";
for (int i = 0; i < locationNum; i++) { for (int i = 0; i < locationNum; i++) {
...@@ -194,12 +202,14 @@ public class MapConstructor extends ElementConstructor { ...@@ -194,12 +202,14 @@ public class MapConstructor extends ElementConstructor {
placeA = (String) locations.getJSONObject(i).get("name"); placeA = (String) locations.getJSONObject(i).get("name");
placeB = (String) locations.getJSONObject(j).get("name"); placeB = (String) locations.getJSONObject(j).get("name");
descriptionB = (String) locations.getJSONObject(j).get("description"); descriptionB = (String) locations.getJSONObject(j).get("description");
placeKeywords = qunaKeywords.replace("地点", placeB); placeKeywords = conditionKeywords.replace("地点", placeB);
lastTaskCondition = "任务线" + placeB + "最后一个condition";
lastTaskReply = "任务线" + placeB + "最后的回答";
if (i == j) { if (i == j) {
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "你已经在" + placeB + "了", "yaobuyao", "" "match", "你已经在" + placeB + "了", "yaobuyao", ""
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -210,8 +220,14 @@ public class MapConstructor extends ElementConstructor { ...@@ -210,8 +220,14 @@ public class MapConstructor extends ElementConstructor {
//如果两点相邻 //如果两点相邻
if (pathMatirx[i][j] == 1) { if (pathMatirx[i][j] == 1) {
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, conditionName + "::match-Keyword=" + placeKeywords,
"match", "" + descriptionB, "yaobuyao", "site:" + placeB "site::match-Keyword=" + placeA + "," + lastTaskCondition + "::match-Keyword=null" + "," + lastTaskReply + "::match-Keyword=null",
"match, reutrn-0", "地点尚未激活", "yaobuyao", ""
);
jConfigList.add(jConfig);
jConfig = new JConfig(
conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"match", "{" + lastTaskReply + "-2}", "yaobuyao", "site:" + placeB + "-<-process-nextStep:var={" + lastTaskCondition +"-2}"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -225,7 +241,7 @@ public class MapConstructor extends ElementConstructor { ...@@ -225,7 +241,7 @@ public class MapConstructor extends ElementConstructor {
k = preTable[k][j]; k = preTable[k][j];
} }
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, conditionName + "::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA, "site::match-Keyword=" + placeA,
"match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", "" "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", ""
); );
...@@ -235,5 +251,27 @@ public class MapConstructor extends ElementConstructor { ...@@ -235,5 +251,27 @@ public class MapConstructor extends ElementConstructor {
} }
} }
public void generateSurroundingPeople(){
String conditionName = "know_exist_what";
String conditionKeywords = "周围:人";
jConfig = new JConfig(
conditionName + "::match-Keyword=" + conditionKeywords, " ",
"match,return-0", "{map_reply-2}", "yaobuyao", "map_reply:queryAroundNpcBySite:"
);
jConfigList.add(jConfig);
}
public void generateSurroundingItem(){
String conditionName = "know_exist_what";
String conditionKeywords = "周围:物品,周围:东西";
jConfig = new JConfig(
conditionName + "::match-Keyword=" + conditionKeywords, " ",
"match,return-0", "{map_reply-2}", "yaobuyao", "map_reply:queryAroundGoodsBySite:"
);
jConfigList.add(jConfig);
}
} }
package processing; package processing;
import entity.ChatCache;
import entity.UserInfo;
import entity.World;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import java.io.*; import java.io.*;
import java.util.HashMap; import java.util.HashMap;
...@@ -15,6 +19,8 @@ public class ChatCacheController { ...@@ -15,6 +19,8 @@ public class ChatCacheController {
* @return 历史信息点,对话过程中引擎提取的用户信息 * @return 历史信息点,对话过程中引擎提取的用户信息
*/ */
public static synchronized Map<String, String> getChatCache(String chatKey, ServletContext context) { public static synchronized Map<String, String> getChatCache(String chatKey, ServletContext context) {
World world = new World();
Map<String, String> historyInfoPoint = null; Map<String, String> historyInfoPoint = null;
Map<String, Map<String, String>> chatCache = null; Map<String, Map<String, String>> chatCache = null;
if (context.getAttribute("chatCache") == null) { if (context.getAttribute("chatCache") == null) {
...@@ -126,4 +132,79 @@ public class ChatCacheController { ...@@ -126,4 +132,79 @@ public class ChatCacheController {
return true; return true;
} }
public static synchronized ChatCache getChatCache2(String chatKey, ServletContext context, Map<String, String[]> curInfoPoint, UserInfo userinfo) {
Map<String, ChatCache> chatCacheMap = null;
ChatCache chatCache = null;
if (context.getAttribute("chatCache") == null) {
chatCacheMap = new HashMap<String, ChatCache>();
chatCacheMap.put(chatKey, new ChatCache(curInfoPoint, userinfo));
context.setAttribute("chatCache", chatCacheMap);
} else {
chatCacheMap = (Map<String, ChatCache>) context.getAttribute("chatCache");
}
String chatkey;
try {
chatCache = chatCacheMap.get(chatKey);
if(chatCache == null){
chatCache = new ChatCache(curInfoPoint, userinfo);
chatCacheMap.put(chatKey, chatCache);
}
//historyInfoPoint = (Map<String, String>) context.getAttribute("chat_key+"+userinfo.userName);
} catch (Exception e) {
//可整合
chatCache = new ChatCache(curInfoPoint, userinfo);
}
return chatCache;
}
/**
* chatChche中每个chatkey都有对应的最后更新时间,如果最后更新时间已经是x分钟前(超时),就清除该chatKey的数据
*/
public static synchronized void updateChatCache4Delete2(String curChatKey, ServletContext context) {
String chatKey;
ChatCache chatCache = null;
long lastChatTime, now;
Map<String, String> history = new HashMap<String, String>();
Map<String, ChatCache> chatCacheMap = (Map<String, ChatCache>) context.getAttribute("chatCache");
Iterator<String> it = chatCacheMap.keySet().iterator();
while (it.hasNext()) {
chatKey = it.next();
if(curChatKey.equals(chatKey))
continue;
history = chatCacheMap.get(chatKey).getInfoPoint();
lastChatTime = Long.valueOf(history.get("lastUpdateTime"));
now = System.currentTimeMillis();
if (now - lastChatTime > 300000) {
it.remove();
}
}
context.setAttribute("chatCache", chatCacheMap);
}
/**
* 根据chatKey更新chatCache的数据,并修改最后更新时间
*/
public static synchronized void updateChatCache4Insert2(Map<String, String[]> curInfoPoint, Map<String, String> historyInfoPoint, World world, ServletContext context) {
historyInfoPoint.put("lastUpdateTime", String.valueOf(System.currentTimeMillis()));
Map<String, ChatCache> keyCacheMap = (Map<String, ChatCache>) context.getAttribute("chatCache");
ChatCache chatCache = keyCacheMap.get(curInfoPoint.get("chat_key")[0]);
chatCache.setInfoPoint(historyInfoPoint);
chatCache.setWorld(world);
context.setAttribute("chatCache", keyCacheMap);
}
} }
...@@ -93,5 +93,9 @@ public class MyFunc { ...@@ -93,5 +93,9 @@ public class MyFunc {
} }
public static boolean isInteger(String str){
return true;
}
} }
package processing;
import entity.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class WorldController {
public static String worldOperation(World world, String varName, String varValue, Map<String, String[]> currentInfoPoint, Map<String, String>historyInfoPoint){
String moduleName = "", valuePair[] = varValue.split(":", 2), returnStr="";
String namePair[] = varName.split("_");
boolean operationFlag = true;
if (varName.startsWith("NPC_"))
moduleName = "NPC";
else if(varName.startsWith("items"))
moduleName = "items";
if(varName.startsWith("item_")) {
switch (valuePair[0]) {
case "addGoodsBase":
operationFlag = world.addGoodsBase(namePair[1], namePair[2], valuePair[1]);
break;
case "deleteGoods":
operationFlag = world.deleteGoods(namePair[1], namePair[2]);
break;
case "queryGoodsIntro":
returnStr = world.queryGoodsIntro(varName, historyInfoPoint.get("site"));
break;
case "queryAttrByGoodsName":
String subValuePair[] = currentInfoPoint.get(valuePair[1])[0].split(":", 2);
returnStr = world.queryAttrByGoodsName(subValuePair[0], subValuePair[1], historyInfoPoint.get("site"));
break;
case "queryAllAttrByGoodsName":
List<String> attrs = world.queryAllAttrByGoodsName(varName, historyInfoPoint.get("site"));
break;
case "updateAttrByGoodsName":
operationFlag = world.updateAttrByGoodsName(namePair[1], namePair[2], namePair[3], valuePair[1]);
break;
case "addAttrByGoodsName":
operationFlag = world.addAttrByGoodsName(namePair[1], namePair[2], namePair[3], valuePair[1]);
break;
case "deleteAttrByGoodsName":
operationFlag = world.deleteAttrByGoodsName(namePair[1], namePair[2], namePair[3]);
break;
default:
}
}else if(varName.startsWith("NPC")) {
switch(valuePair[0]){
case "addNpc":
operationFlag = world.addNpc(namePair[1], namePair[2], valuePair[1]);
break;
case "deleteNpc":
operationFlag = world.deleteNpc(namePair[1], namePair[2]);
break;
case "queryNpcIntro":
//operationFlag = world.queryNpcIntro(valuePair[1], namePair[2]);
break;
case "queryAttrByNPCName":
String subValuePair[] = currentInfoPoint.get(valuePair[1])[0].split(":", 2);
subValuePair[1] = subValuePair[1].replace("哪里", "location");
returnStr = world.queryAttrByNPCName(subValuePair[0], subValuePair[1], historyInfoPoint.get("site"));
break;
case "updateLocationByNPCName":
operationFlag = world.updateLocationByNPCName(namePair[1], namePair[2], namePair[3], "");
break;
case "addAttrByNPCName":
operationFlag = world.addAttrByNPCName(namePair[1], namePair[2], namePair[3], valuePair[1]);
break;
case "deleteAttrByNPCName":
operationFlag = world.deleteAttrByNPCName(namePair[1], namePair[2], namePair[3]);
break;
default:
}
}else if (varName.startsWith("map")){
List<String> sites = new ArrayList<>();
switch(valuePair[0]){
case "queryAroundAllBySite":
sites = world.queryAroundAllBySite(historyInfoPoint.get("site"));
break;
case "queryAroundNpcBySite":
sites = world.queryAroundNpcBySite(historyInfoPoint.get("site"));
break;
case "queryAroundGoodsBySite":
sites = world.queryAroundGoodsBySite(historyInfoPoint.get("site"));
break;
case "queryCanGoBySite":
sites = world.queryCanGoBySite(historyInfoPoint.get("site"));
break;
case "queryPathBySite":
sites = world.queryPathBySite(historyInfoPoint.get("site"), valuePair[1]);
break;
}
for(String site: sites){
returnStr += site;
}
}
return returnStr;
}
}
...@@ -14,10 +14,7 @@ import javax.servlet.annotation.WebServlet; ...@@ -14,10 +14,7 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
...@@ -49,7 +46,7 @@ public class GameDesignServlet extends HttpServlet { ...@@ -49,7 +46,7 @@ public class GameDesignServlet extends HttpServlet {
dataId = (Integer) jsonObject.get("id"); dataId = (Integer) jsonObject.get("id");
JSONObject gameElements = helper.queryGameElementsByBotId(dataId); JSONObject gameElements = helper.queryGameElementsByBotId(dataId);
botId = (Integer) gameElements.get("botId"); botId = Integer.parseInt((String) gameElements.get("botId"));
userName = (String) gameElements.get("userName"); userName = (String) gameElements.get("userName");
//假设重新重新建用户信息 //假设重新重新建用户信息
...@@ -101,6 +98,7 @@ public class GameDesignServlet extends HttpServlet { ...@@ -101,6 +98,7 @@ public class GameDesignServlet extends HttpServlet {
idList.clear(); idList.clear();
helper.batchInsertVariableByBotId(botId, constructor); helper.batchInsertVariableByBotId(botId, constructor);
helper.batchInsertConfigByBotId(botId, constructor, idList); helper.batchInsertConfigByBotId(botId, constructor, idList);
constructor.postProcessing(idList);
constructor.generateConfigText(idList); constructor.generateConfigText(idList);
mergedConfigText.append(constructor.getConfigText()); mergedConfigText.append(constructor.getConfigText());
...@@ -124,7 +122,9 @@ public class GameDesignServlet extends HttpServlet { ...@@ -124,7 +122,9 @@ public class GameDesignServlet extends HttpServlet {
} }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream out = response.getOutputStream();
out.write("ssss".getBytes());
out.flush();
} }
public boolean clearOldConfig(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException { public boolean clearOldConfig(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException {
......
package servlet; package servlet;
import entity.Solution; import entity.*;
import entity.Condition;
import entity.UserInfo;
import processing.ChatCacheController; import processing.ChatCacheController;
import processing.MyFunc; import processing.MyFunc;
import processing.QuestionMatcher; import processing.QuestionMatcher;
...@@ -14,8 +12,7 @@ import javax.servlet.annotation.WebServlet; ...@@ -14,8 +12,7 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.*;
import java.io.OutputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.ParseException; import java.text.ParseException;
import java.util.*; import java.util.*;
...@@ -38,12 +35,34 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -38,12 +35,34 @@ public class GetAnswerServlet extends HttpServlet {
Condition ansCondition = null; Condition ansCondition = null;
String ansJ, return_str = "", state = ""; String ansJ, return_str = "", state = "";
String chatKey = curInfoPoint.get("chat_key")[0]; String chatKey = curInfoPoint.get("chat_key")[0];
QuestionMatcher matcher = new QuestionMatcher();
OutputStream out = response.getOutputStream(); OutputStream out = response.getOutputStream();
ServletContext context = this.getServletContext(); ServletContext context = this.getServletContext();
boolean debugFlag = false; boolean debugFlag = false;
//获得用户历史信息
historyInfoPoint = ChatCacheController.getChatCache(chatKey, context);
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("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>log</title>\n" +
"</head>");
UserInfo userinfo = null; UserInfo userinfo = null;
...@@ -57,31 +76,20 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -57,31 +76,20 @@ public class GetAnswerServlet extends HttpServlet {
e.printStackTrace(); e.printStackTrace();
} }
//如果historyInfoPoint不存在,就新建一个 //获得用户历史信息
if (historyInfoPoint == null) { ChatCache chatCache = ChatCacheController.getChatCache2(chatKey, context, curInfoPoint, userinfo);
String k; historyInfoPoint = chatCache.getInfoPoint();
historyInfoPoint = new HashMap<String, String>();
Iterator<String> iit = curInfoPoint.keySet().iterator();
while (iit.hasNext()) {
k = iit.next();
historyInfoPoint.put(k, "");
}
//变量初始化
iit = userinfo.varInitialMap.keySet().iterator();
while (iit.hasNext()) {
k = iit.next();
historyInfoPoint.put(k, userinfo.varInitialMap.get(k));
}
}
//信息点预处理 //信息点预处理
debugFlag = matcher.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo); QuestionMatcher matcher = new QuestionMatcher(pw,userinfo, context, chatCache.getWorld());
debugFlag = matcher.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo, context);
/*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/ /*得到答案更新历史信息点&生成答案,,清楚超时chatkey_key,生成更新数据库,转成json,输出*/
//process.removeTimeoutChatkey(context); //process.removeTimeoutChatkey(context);
//System.out.println(userinfo.conditionTable); //测试 //System.out.println(userinfo.conditionTable); //测试
ansCondition = matcher.getBestMatch(curInfoPoint, historyInfoPoint, userinfo.conditionTable, context); ansCondition = matcher.getBestMatch(curInfoPoint, historyInfoPoint, userinfo.conditionTable, context, userinfo);
if (state.equals("") && ansCondition.priority == -1) if (state.equals("") && ansCondition.priority == -1)
state = "我不理解,"; state = "我不理解,";
if (state.equals("")) { if (state.equals("")) {
...@@ -99,7 +107,7 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -99,7 +107,7 @@ public class GetAnswerServlet extends HttpServlet {
if (context.getAttribute("cacheUpdateTime") == null) if (context.getAttribute("cacheUpdateTime") == null)
context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); context.setAttribute("cacheUpdateTime", System.currentTimeMillis());
if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > 300000) { if (System.currentTimeMillis() - (long) context.getAttribute("cacheUpdateTime") > 300000) {
ChatCacheController.updateChatCache4Delete(context); ChatCacheController.updateChatCache4Delete2(chatKey, context);
context.setAttribute("cacheUpdateTime", System.currentTimeMillis()); context.setAttribute("cacheUpdateTime", System.currentTimeMillis());
} }
//bestMatch.setAnswer(" \n" + return_str + "\n " + bestMatch.getAnswer()); //bestMatch.setAnswer(" \n" + return_str + "\n " + bestMatch.getAnswer());
...@@ -115,6 +123,10 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -115,6 +123,10 @@ public class GetAnswerServlet extends HttpServlet {
matcher.infoTransfer(curInfoPoint, historyInfoPoint, bestMatch, context, debugFlag); matcher.infoTransfer(curInfoPoint, historyInfoPoint, bestMatch, context, debugFlag);
bw.write("<br><br>" +
"-----------------------------------------对话处理结束-----------------------------------<br><br><br>");
bw.close();
fw.close();
ansJ = MyFunc.toJson(bestMatch); ansJ = MyFunc.toJson(bestMatch);
System.out.println(ansJ); System.out.println(ansJ);
out.write(ansJ.getBytes()); out.write(ansJ.getBytes());
......
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