Commit b8dd9e53 by hxw

添加变量等

parent d592cee2
package db; package db;
import com.alibaba.fastjson.JSONObject;
import entity.JConfig; import entity.JConfig;
import entity.QuestionKeyWord; import entity.QuestionKeyWord;
import processing.MyFunc;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -130,6 +132,24 @@ public class SqlHelper { ...@@ -130,6 +132,24 @@ public class SqlHelper {
return b; return b;
} }
public boolean deleteConfigByBotId(int botId){
String sql = "delete from chatbot_config where botId= ?";
boolean b;
try{
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, String.valueOf(botId));
b = ps.execute();
ps.close();
return b;
} catch (SQLException e) {
System.out.println("删除旧数据失败");
e.printStackTrace();
return false;
}
}
public boolean batchInsertConfigByBotId(int botId, List<JConfig> jConfigList, List<Integer> idList) { public boolean batchInsertConfigByBotId(int botId, List<JConfig> jConfigList, List<Integer> idList) {
int count[]; int count[];
boolean insertFlag = true; boolean insertFlag = true;
...@@ -149,11 +169,11 @@ public class SqlHelper { ...@@ -149,11 +169,11 @@ public class SqlHelper {
} }
count = ps.executeBatch(); count = ps.executeBatch();
conn.commit(); //提交事务,这个非常重要 conn.commit();
for (int i : count) { for (int i : count) {
if (i == 0) { if (i == 0) {
conn.rollback(); // 回滚,非常重要 conn.rollback();
System.out.println("======出现异常,回滚========="); System.out.println("======出现异常,回滚=========");
insertFlag = false; insertFlag = false;
} }
...@@ -168,7 +188,7 @@ public class SqlHelper { ...@@ -168,7 +188,7 @@ public class SqlHelper {
} catch (SQLException e) { } catch (SQLException e) {
try { try {
conn.rollback(); // 回滚,非常重要 conn.rollback();
} catch (SQLException e1) { } catch (SQLException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
...@@ -179,6 +199,27 @@ public class SqlHelper { ...@@ -179,6 +199,27 @@ public class SqlHelper {
return insertFlag ; return insertFlag ;
} }
public JSONObject queryGameElementsByBotId(int id){
String sql = "select * from generate_json_tb where id = ?";
String userName = "";
String result = "empty";
try{
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, String.valueOf(id));
ResultSet rs = ps.executeQuery();
if(rs.next())
result = rs.getString("json_text");
rs.close();
ps.close();
}catch(Exception e){
e.printStackTrace();
}
return JSONObject.parseObject(result);
}
......
...@@ -16,9 +16,9 @@ public class Condition { ...@@ -16,9 +16,9 @@ public class Condition {
public HashSet<String> keywordSet = new HashSet<String>(); public HashSet<String> keywordSet = new HashSet<String>();
public HashMap<String,HashSet<String>> historyPremise = new HashMap<String,HashSet<String>>(); public HashMap<String,HashSet<String>> historyPremise = new HashMap<String,HashSet<String>>();
public HashMap<String,HashSet<String>> currentPremise = new HashMap<String,HashSet<String>>(); public HashMap<String,HashSet<String>> currentPremise = new HashMap<String,HashSet<String>>();
public HashMap<String, String> updateKnowledge = new HashMap<String, String>();
public String[] answer = {"",}; public String[] answer = {"",};
public String nextStep = ""; public String nextStep = "";
public String feature = "";
public HashMap<String, String> featureMap = new HashMap<String, String>(); public HashMap<String, String> featureMap = new HashMap<String, String>();
public String indexName = ""; public String indexName = "";
public int id =0; public int id =0;
...@@ -38,8 +38,8 @@ public class Condition { ...@@ -38,8 +38,8 @@ public class Condition {
this.currentPremise = originCondition.currentPremise; this.currentPremise = originCondition.currentPremise;
this.answer = originCondition.answer; this.answer = originCondition.answer;
this.nextStep = originCondition.nextStep; this.nextStep = originCondition.nextStep;
this.feature = originCondition.feature;
this.featureMap = originCondition.featureMap; this.featureMap = originCondition.featureMap;
this.updateKnowledge = originCondition.updateKnowledge;
this.indexName = originCondition.indexName; this.indexName = originCondition.indexName;
this.id = originCondition.id; this.id = originCondition.id;
this.status = originCondition.status; this.status = originCondition.status;
......
...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray; ...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -23,17 +24,21 @@ public class JConfig { ...@@ -23,17 +24,21 @@ public class JConfig {
private String curPremise = ""; private String curPremise = "";
private String hisPremise = ""; private String hisPremise = "";
private String feature = ""; private String feature = "";
private String reply = ""; private String reply = "";
private String nextStep = ""; private String nextStep = "";
public String getCurPremise() {
return curPremise;
}
private String auxiliaryPriority = "0"; private String auxiliaryPriority = "0";
private String status = "waiting"; private String status = "waiting";
public void setAuxiliaryPriority(String auxiliaryPriority) {
this.auxiliaryPriority = auxiliaryPriority;
}
private String updateKnowledge = ""; private String updateKnowledge = "";
private String nextStepStatus = "return"; private String nextStepStatus = "return";
...@@ -46,78 +51,80 @@ public class JConfig { ...@@ -46,78 +51,80 @@ public class JConfig {
} }
}; };
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep)
public void setAuxiliaryPriority(String auxiliaryPriority) {
this.auxiliaryPriority = auxiliaryPriority;
}
public void setNextStepStatus(String nextStepStatus) {
this.nextStepStatus = nextStepStatus;
}
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge)
{ {
this.curPremise = curPremise; this.curPremise = curPremise;
this.hisPremise = hisPremise; this.hisPremise = hisPremise;
this.feature = feature; this.feature = feature;
if(!updateKnowledge.equals(""))
this.updateKnowledge += "-<-" + updateKnowledge;
this.reply = reply; this.reply = reply;
this.nextStep = nextStep; this.nextStep = nextStep;
} }
public JConfig(List<String> curPremise, List<String> hisPremise, String extraCP, String extraHP, String feature, String reply, String 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){
{ this.curPremise = appendPremiseStr(curPremise, extraCP);
String curPremiseStr = ""; this.hisPremise = appendPremiseStr(hisPremise, extraHP);
String hisPremiseStr = ""; this.updateKnowledge = appendKnowledgeStr(updateKnowledge, extraUK);
String extraCurPremise = "";
String extraHisPremise = "";
for(String premise: curPremise)
curPremiseStr += "," + premise;
for(String premise: hisPremise)
hisPremiseStr += "," + premise;
if(!extraCP.equals(""))
curPremiseStr += "," + extraCP;
if(!extraHP.equals(""))
hisPremiseStr += "," + extraHP;
this.curPremise = curPremiseStr.substring(1, curPremiseStr.length());
this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length());
this.feature = feature; this.feature = feature;
this.reply = reply; this.reply = reply;
this.nextStep = nextStep; this.nextStep = nextStep;
this.auxiliaryPriority = auxiliaryPriority;
} }
public JConfig(List<String> curPremise, List<String> hisPremise, String extraCP, String extraHP, String feature, String reply, String nextStep, List<String> updateKnowledgeList, String extraUK) public String appendPremiseStr(List<String> premises, String extraPM){
{ StringBuffer sb = new StringBuffer();
String curPremiseStr = ""; String premiseStr = "";
String hisPremiseStr = "";
String extraCurPremise = ""; for (String premise:premises)
String extraHisPremise = ""; sb.append("," + premise);
if(sb.length() > 0)
for(String premise: curPremise) sb.deleteCharAt(0); //删除第一个逗号
curPremiseStr += "," + premise;
for(String premise: hisPremise)
hisPremiseStr += "," + premise; if(!extraPM.equals("")){
if(sb.length() == 0)
if(!extraCP.equals("")) sb.append(extraPM);
curPremiseStr += "," + extraCP; else
if(!extraHP.equals("")) sb.append("," + extraPM);
hisPremiseStr += "," + extraHP; }else if(sb.length() == 0){
sb.append(" ");
for(String knowledge:updateKnowledgeList) }
updateKnowledge += "-<-" + knowledge; premiseStr = sb.toString();
updateKnowledge += "-<-" + extraUK; return premiseStr;
this.curPremise = curPremiseStr.substring(1, curPremiseStr.length());
this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length());
this.feature = feature;
this.reply = reply;
this.nextStep = nextStep;
} }
public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge) public String appendKnowledgeStr(Map<String, String> knowledge, String extraKL){
{ StringBuffer sb = new StringBuffer();
this.curPremise = curPremise; String varName = "", varValue = "", knowledgeStr;
this.hisPremise = hisPremise; Iterator<String> iit = knowledge.keySet().iterator();
this.feature = feature;
this.updateKnowledge = "-<-" + updateKnowledge;
this.reply = reply; while(iit.hasNext()){
this.nextStep = nextStep; varName = iit.next();
varValue = knowledge.get(varName);
sb.append("-<-" + varName + ":" + varValue);
}
if(!extraKL.equals("")){
sb.append("-<-" + extraKL);
}
if(sb.length() > 0)
knowledgeStr = "updateKnowledge" + sb.toString();
else
knowledgeStr = "";
return knowledgeStr;
} }
...@@ -146,10 +153,12 @@ public class JConfig { ...@@ -146,10 +153,12 @@ public class JConfig {
} }
public void appendJKnowledge(JSONArray knowledgeList, String knowledge){ public void appendJKnowledge(JSONArray knowledgeList, String knowledge){
if(knowledge.equals("")) String pair[] = knowledge.split(":");
if(knowledge.equals("") || pair.length < 2)
return; return;
String pair[] = knowledge.split(":");
JSONObject knowledgeJ = new JSONObject(); JSONObject knowledgeJ = new JSONObject();
knowledgeJ.put("variableName", pair[0]); knowledgeJ.put("variableName", pair[0]);
knowledgeJ.put("keyword", pair[1]); knowledgeJ.put("keyword", pair[1]);
...@@ -234,12 +243,9 @@ public class JConfig { ...@@ -234,12 +243,9 @@ public class JConfig {
public String toString(){ public String toString(){
if(!updateKnowledge.equals(""))
updateKnowledge = ",updateKnowledge" + updateKnowledge;
String text = curPremise + "|" + String text = curPremise + "|" +
hisPremise + "|" + hisPremise + "|" +
feature + updateKnowledge + "|" + feature + "," + updateKnowledge + "," + nextStepStatus + "|" +
"auxiliaryPriority=" + auxiliaryPriority + "|=>|" + "auxiliaryPriority=" + auxiliaryPriority + "|=>|" +
reply + "|" + reply + "|" +
"nextStep=" + nextStep + "|" + "nextStep=" + nextStep + "|" +
......
...@@ -6,9 +6,9 @@ package entity; ...@@ -6,9 +6,9 @@ package entity;
* 暂时没用 * 暂时没用
*/ */
public class QuestionKeyWord { public class QuestionKeyWord {
String actionName; private String actionName;
String chatKey; private String chatKey;
String keyWord; private String keyWord;
public String getActionName() { public String getActionName() {
return actionName; return actionName;
......
...@@ -5,9 +5,9 @@ package entity; ...@@ -5,9 +5,9 @@ package entity;
* 包含后台返回数据的实体类。 * 包含后台返回数据的实体类。
*/ */
public class Solution { public class Solution {
int status = 0; private int status = 0;
String answer; private String answer;
String next; private String next;
public String getAnswer() { public String getAnswer() {
return answer; return answer;
......
...@@ -13,11 +13,12 @@ public class UserInfo { ...@@ -13,11 +13,12 @@ public class UserInfo {
this.userName = userName; this.userName = userName;
this.extraInfo = new HashMap<String,String>(){ this.extraInfo = new HashMap<String,String>(){
{ {
put("activeAction",""); put("activeAction"," ");
} }
}; };
this.id2config = new HashMap<Integer, String>(); this.id2config = new HashMap<Integer, String>();
this.id2condition = new HashMap<Integer, Condition>(); this.id2condition = new HashMap<Integer, Condition>();
this.varInitialMap = new HashMap<String, String>();
} }
public HashMap<String,List<Condition>> conditionTable; public HashMap<String,List<Condition>> conditionTable;
...@@ -26,6 +27,8 @@ public class UserInfo { ...@@ -26,6 +27,8 @@ public class UserInfo {
public HashMap<String,String> extraInfo; public HashMap<String,String> extraInfo;
public HashMap<Integer, String> id2config; public HashMap<Integer, String> id2config;
public HashMap<Integer, Condition> id2condition; public HashMap<Integer, Condition> id2condition;
//存储需要初始化的变量
public HashMap<String, String> varInitialMap;
......
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class CharacterConstructor extends ElementConstructor{
public CharacterConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray allNPC = jsonObject.getJSONArray("npc");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject npc:allNPC.toJavaList(JSONObject.class)) {
category = (String) npc.get("category");
JSONArray characters = npc.getJSONArray("characters");
for (JSONObject character : characters.toJavaList(JSONObject.class)) {
name = (String) character.get("name");
description = (String) character.get("intro");
attribute = JSONObject.toJavaObject(character.getJSONObject("attr"), Map.class);
attrIter = attribute.keySet().iterator();
while (attrIter.hasNext()) {
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + name + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
}
...@@ -4,10 +4,12 @@ import com.alibaba.fastjson.JSONArray; ...@@ -4,10 +4,12 @@ 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.JConfig;
import entity.UserInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class ElementConstructor { public class ElementConstructor {
...@@ -16,18 +18,17 @@ public class ElementConstructor { ...@@ -16,18 +18,17 @@ public class ElementConstructor {
List<JSONObject> configDataList = new ArrayList<JSONObject>(); List<JSONObject> configDataList = new ArrayList<JSONObject>();
List<JConfig> jConfigList = new ArrayList<JConfig>(); List<JConfig> jConfigList = new ArrayList<JConfig>();
String userName; String userName;
String configText = ""; StringBuffer configText = new StringBuffer();
JConfig jConfig = null; JConfig jConfig = null;
Map<String, String> conditionDescription = new HashMap<String, String>();
public void construct(JSONObject jsonObject, UserInfo userinfo){}
public List<JConfig> getjConfigList() { return jConfigList; } public List<JConfig> getjConfigList() { return jConfigList; }
public void construct(JSONObject jsonObject){}
public List<JSONObject> getConfigDataList() { return configDataList; } public List<JSONObject> getConfigDataList() { return configDataList; }
public void ElementConstructor(String userName){ this.userName = userName; } public void ElementConstructor(String userName){ this.userName = userName; }
public String getConfigText() { return configText.toString(); }
public String getConfigText() { return configText; }
public void generateConfigText(List<Integer> idList){ public void generateConfigText(List<Integer> idList){
int id, length = idList.size(); int id, length = idList.size();
JConfig jConfig = null; JConfig jConfig = null;
...@@ -35,7 +36,7 @@ public class ElementConstructor { ...@@ -35,7 +36,7 @@ public class ElementConstructor {
jConfig = jConfigList.get(i); jConfig = jConfigList.get(i);
id = idList.get(i); id = idList.get(i);
configText += jConfig.toString() + id + "\n"; configText.append(jConfig.toString() + id + "\n");
} }
} }
......
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class GlobalVariableConstructor extends ElementConstructor{
public GlobalVariableConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray globalVariable = jsonObject.getJSONArray("globalVariable");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject variableList:globalVariable.toJavaList(JSONObject.class)) {
category = (String) variableList.get("category");
attribute = JSONObject.toJavaObject(variableList.getJSONObject("variables"), Map.class);
attrIter = attribute.keySet().iterator();
while(attrIter.hasNext()){
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
package gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import db.SqlHelper;
import entity.UserInfo;
import java.util.Iterator;
import java.util.Map;
public class ItemConstructor extends ElementConstructor{
public ItemConstructor(SqlHelper helper, int botId) {
super();
this.helper = helper;
this.botId = botId;
}
public void construct(JSONObject jsonObject, UserInfo userinfo){
String category, name, description, attrName, attrValue;
JSONArray goods = jsonObject.getJSONArray("goods");
Map<String, String> attribute = null;
Iterator<String> attrIter = null;
for(JSONObject good:goods.toJavaList(JSONObject.class)) {
category = (String) good.get("category");
JSONArray items = good.getJSONArray("items");
for (JSONObject item : items.toJavaList(JSONObject.class)) {
name = (String) item.get("name");
description = (String) item.get("intro");
attribute = JSONObject.toJavaObject(item.getJSONObject("attr"), Map.class);
attrIter = attribute.keySet().iterator();
while (attrIter.hasNext()) {
attrName = attrIter.next();
attrValue = attribute.get(attrName);
configText.append("-varInitial-|" + category + "_" + name + "_" + attrName + "|" + attrValue + "\n");
}
}
}
}
}
...@@ -8,27 +8,16 @@ import entity.UserInfo; ...@@ -8,27 +8,16 @@ import entity.UserInfo;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
public class MapConstructor extends ElementConstructor{ public class MapConstructor extends ElementConstructor{
int[][] matrix; int[][] matrix;
int locationNum; int locationNum;
HashMap<Integer, JSONObject> id2j = new HashMap<Integer, JSONObject>(); HashMap<Integer, JSONObject> id2j = new HashMap<Integer, JSONObject>();
Map<String, String> question2keyword = new HashMap<String, String>();
JSONArray locations = null; JSONArray locations = null;
public MapConstructor(SqlHelper helper, int botId) { public MapConstructor(SqlHelper helper, int botId) {
super(); super();
this.helper = helper; this.helper = helper;
...@@ -74,9 +63,13 @@ public class MapConstructor extends ElementConstructor{ ...@@ -74,9 +63,13 @@ public class MapConstructor extends ElementConstructor{
} }
public void construct(JSONObject jsonObject){ public void construct(JSONObject jsonObject, UserInfo userinfo){
JSONArray locations = jsonObject.getJSONArray("locations"); conditionDescription.put("site","当前地点");
JSONObject map = jsonObject.getJSONObject("map");
Map<String, String> question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class);
JSONArray locations = map.getJSONArray("locations");
this.locations = locations; this.locations = locations;
this.question2keyword = question2keyword;
//通过邻接表构造邻接矩阵 //通过邻接表构造邻接矩阵
constructGreph(locations); constructGreph(locations);
...@@ -89,21 +82,16 @@ public class MapConstructor extends ElementConstructor{ ...@@ -89,21 +82,16 @@ public class MapConstructor extends ElementConstructor{
generateWhereIam(jsonObject); generateWhereIam(jsonObject);
generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject); generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject);
} }
/** /**
* 生成‘我在哪里’所需要的配置文件 * 生成‘我在哪里’所需要的配置文件
*/ */
public void generateWhereIam(JSONObject jsonObject){ public void generateWhereIam(JSONObject jsonObject){
String zainaKeywords = (String)jsonObject.get("zaina"); String zainaKeywords = question2keyword.get("zaina");
jConfig = new JConfig( jConfig = new JConfig(
"zaina::match-Keyword=" + zainaKeywords, "zaina::match-Keyword=" + zainaKeywords, " ",
" ", "match", "你在{site-2}", "yaobuyao",""
"match",
"你在{site-2}",
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -118,7 +106,7 @@ public class MapConstructor extends ElementConstructor{ ...@@ -118,7 +106,7 @@ public class MapConstructor extends ElementConstructor{
public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){
String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String placeA, placeB, descriptionB, placeKeywords, text, pathNode;
String zenmequKeywords = (String)jsonObject.get("zenmequ"); String zenmequKeywords = question2keyword.get("zenmequ");
for(int i = 0;i < locationNum;i++){ for(int i = 0;i < locationNum;i++){
...@@ -131,22 +119,16 @@ public class MapConstructor extends ElementConstructor{ ...@@ -131,22 +119,16 @@ public class MapConstructor extends ElementConstructor{
if(i == j){ if(i == j){
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao",""
"match",
"你已经在" + placeB + "了",
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
continue; continue;
}; };
if(pathMatirx[i][j] == 1){ if(pathMatirx[i][j] == 1){
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", placeB + "就在附近哦", "yaobuyao", ""
"match",
placeB + "就在附近哦",
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -159,11 +141,8 @@ public class MapConstructor extends ElementConstructor{ ...@@ -159,11 +141,8 @@ public class MapConstructor extends ElementConstructor{
} }
jConfig = new JConfig( jConfig = new JConfig(
"zenmequ::match-Keyword=" + placeKeywords, "zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao",""
"match",
"经过" + pathNode + "就可以到达" + placeB,
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
} }
...@@ -176,7 +155,7 @@ public class MapConstructor extends ElementConstructor{ ...@@ -176,7 +155,7 @@ 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 = (String)jsonObject.get("nengquna"); String nengqunaKeywords = question2keyword.get("nengquna");
String placeA, placeB, pathNode; String placeA, placeB, pathNode;
for(int i = 0;i < locationNum;i++){ for(int i = 0;i < locationNum;i++){
...@@ -191,11 +170,8 @@ public class MapConstructor extends ElementConstructor{ ...@@ -191,11 +170,8 @@ public class MapConstructor extends ElementConstructor{
} }
jConfig = new JConfig( jConfig = new JConfig(
"nengquna::match-Keyword=" + nengqunaKeywords, "nengquna::match-Keyword=" + nengqunaKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", "你可以去" + pathNode, "yaobuyao", ""
"match",
"你可以去" + pathNode,
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
...@@ -212,7 +188,7 @@ public class MapConstructor extends ElementConstructor{ ...@@ -212,7 +188,7 @@ 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;
String qunaKeywords = (String)jsonObject.get("quna"); String qunaKeywords = question2keyword.get("quna");
...@@ -227,26 +203,23 @@ public class MapConstructor extends ElementConstructor{ ...@@ -227,26 +203,23 @@ public class MapConstructor extends ElementConstructor{
if(i == j){ if(i == j){
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, "quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao", ""
"match",
"你已经在" + placeB + "了",
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
continue; continue;
}; };
//如果两点相邻
if(pathMatirx[i][j] == 1){ if(pathMatirx[i][j] == 1){
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, "quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA,
"site::match-Keyword=" + placeA, "match", "" + descriptionB, "yaobuyao", "site:" + placeB
"match,updateKnowledge-<-site:" + placeB,
"" + descriptionB,
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
//如果两点不是相邻点
}else if(pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE){ }else if(pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE){
int k = preTable[i][j]; int k = preTable[i][j];
pathNode = ""; pathNode = "";
...@@ -257,9 +230,7 @@ public class MapConstructor extends ElementConstructor{ ...@@ -257,9 +230,7 @@ public class MapConstructor extends ElementConstructor{
jConfig = new JConfig( jConfig = new JConfig(
"quna::match-Keyword=" + placeKeywords, "quna::match-Keyword=" + placeKeywords,
"site::match-Keyword=" + placeA, "site::match-Keyword=" + placeA,
"match", "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", ""
"从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB,
"yaobuyao"
); );
jConfigList.add(jConfig); jConfigList.add(jConfig);
} }
......
...@@ -18,12 +18,13 @@ public class ConditionTableController { ...@@ -18,12 +18,13 @@ public class ConditionTableController {
int id = Integer.parseInt(configId); int id = Integer.parseInt(configId);
Condition condition = new Condition(); Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion(); ProcessQuestion process = new ProcessQuestion();
UserInfoController userInfoController = new UserInfoController();
//data=configLine+"---"+conditionNameList //data=configLine+"---"+conditionNameList
String conditionNameList[] = data.split(","); String conditionNameList[] = data.split(",");
String conditionName = ""; String conditionName = "";
if(context.getAttribute(userName) == null){ if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context); userInfoController.userConfigInit(userName, context);
} }
UserInfo userInfo = (UserInfo) context.getAttribute(userName); UserInfo userInfo = (UserInfo) context.getAttribute(userName);
...@@ -114,12 +115,13 @@ public class ConditionTableController { ...@@ -114,12 +115,13 @@ public class ConditionTableController {
String dataPair[] = data.split("---"); String dataPair[] = data.split("---");
String configLine = dataPair[0]; String configLine = dataPair[0];
String conditionNameList[]; String conditionNameList[];
UserInfoController userInfoController = new UserInfoController();
int id = Integer.parseInt(configId); int id = Integer.parseInt(configId);
condition = process.buildCondition(condition, configLine); condition = userInfoController.buildCondition(condition, configLine);
if(context.getAttribute(userName) == null){ if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context); userInfoController.userConfigInit(userName, context);
} }
UserInfo userinfo = (UserInfo) context.getAttribute(userName); UserInfo userinfo = (UserInfo) context.getAttribute(userName);
...@@ -135,13 +137,54 @@ public class ConditionTableController { ...@@ -135,13 +137,54 @@ public class ConditionTableController {
} }
public static boolean updateVariable(String userName, String configId, String data, ServletContext context) throws IOException {
Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion();
//data=configLine+"---"+conditionNameList
String dataPair[] = data.split("---", -1);
String name = dataPair[0],value = dataPair[1];
public static boolean updateExtraInfo(String userName, String data, ServletContext context) throws IOException { UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
userinfo.varInitialMap.put(name, value);
return true;
}
public static boolean deleteVariable(String userName, String configId, String data, ServletContext context) throws IOException {
Condition condition = new Condition();
ProcessQuestion process = new ProcessQuestion(); ProcessQuestion process = new ProcessQuestion();
//data=configLine+"---"+conditionNameList
String name = data;
UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){
userInfoController.userConfigInit(userName, context);
}
UserInfo userinfo = (UserInfo) context.getAttribute(userName);
userinfo.varInitialMap.remove(name);
return true;
}
public static boolean updateExtraInfo(String userName, String data, ServletContext context) throws IOException {
UserInfoController userInfoController = new UserInfoController();
if(context.getAttribute(userName) == null){ if(context.getAttribute(userName) == null){
process.userConfigInit(userName, context); userInfoController.userConfigInit(userName, context);
} }
UserInfo userinfo = (UserInfo) context.getAttribute(userName); UserInfo userinfo = (UserInfo) context.getAttribute(userName);
...@@ -177,6 +220,13 @@ public class ConditionTableController { ...@@ -177,6 +220,13 @@ public class ConditionTableController {
if(!userinfo.extraInfo.get(info).equals("")) if(!userinfo.extraInfo.get(info).equals(""))
bw.write("-extraInfo-|" + info + "|" + userinfo.extraInfo.get(info) + "\n"); bw.write("-extraInfo-|" + info + "|" + userinfo.extraInfo.get(info) + "\n");
} }
keyIter = userinfo.varInitialMap.keySet().iterator();
while (keyIter.hasNext()) {
String info = keyIter.next();
bw.write("-varInitial-|" + info + "|" + userinfo.varInitialMap.get(info) + "\n");
}
Iterator<Integer> idIter = userinfo.id2config.keySet().iterator(); Iterator<Integer> idIter = userinfo.id2config.keySet().iterator();
while (idIter.hasNext()) { while (idIter.hasNext()) {
int id = idIter.next(); int id = idIter.next();
......
...@@ -5,12 +5,11 @@ import com.alibaba.fastjson.JSONObject; ...@@ -5,12 +5,11 @@ import com.alibaba.fastjson.JSONObject;
import db.SqlHelper; import db.SqlHelper;
import entity.Condition; import entity.Condition;
import entity.UserInfo; import entity.UserInfo;
import gameDesign.ElementConstructor; import gameDesign.*;
import gameDesign.MapConstructor;
import gameDesign.PlotConstructor;
import processing.MyFunc; import processing.MyFunc;
import processing.ProcessQuestion; import processing.ProcessQuestion;
import processing.ConditionTableController; import processing.ConditionTableController;
import processing.UserInfoController;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -24,13 +23,14 @@ import java.io.PrintWriter; ...@@ -24,13 +23,14 @@ import java.io.PrintWriter;
import java.io.StringReader; 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.List; import java.util.List;
@WebServlet(name = "GameDesignServlet",urlPatterns = {"/gameDesign"}) @WebServlet(name = "GameDesignServlet",urlPatterns = {"/gameDesign"})
public class GameDesignServlet extends HttpServlet { public class GameDesignServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer botId; Integer botId, projectId, dataId;
String userName = null, mergedConfigText = ""; String userName = null, moduleName;
ServletContext context = this.getServletContext(); ServletContext context = this.getServletContext();
boolean flag; boolean flag;
SqlHelper helper = null; SqlHelper helper = null;
...@@ -47,56 +47,73 @@ public class GameDesignServlet extends HttpServlet { ...@@ -47,56 +47,73 @@ public class GameDesignServlet extends HttpServlet {
String jsonStr = MyFunc.readJSONString(request); String jsonStr = MyFunc.readJSONString(request);
if(jsonStr != null && !jsonStr.equals("")){ if(jsonStr != null && !jsonStr.equals("")){
JSONObject jsonObject = JSONObject.parseObject(jsonStr); JSONObject jsonObject = JSONObject.parseObject(jsonStr);
botId = (Integer)jsonObject.get("botId"); dataId = (Integer)jsonObject.get("id");
userName = (String)jsonObject.get("userName");
//moduleType = (String)jsonObject.get("moduleType");
List<Integer> idGroup = JSONArray.parseArray(jsonObject.getJSONArray("configIdGroup").toJSONString(), Integer.class);
JSONObject gameElements = helper.queryGameElementsByBotId(dataId);
botId = (Integer)gameElements.get("botId");
userName = (String)gameElements.get("userName");
/* //假设重新重新建用户信息
假设重新重新建用户信息
if(context.getAttribute(userName) == null){ if(context.getAttribute(userName) == null){
ProcessQuestion.userConfigInit(userName, context); UserInfoController.userConfigInit(userName, context);
} }
UserInfo userinfo = (UserInfo) context.getAttribute(userName); UserInfo userinfo = (UserInfo) context.getAttribute(userName);
*/
UserInfo userinfo = new UserInfo(userName);
//UserInfo userinfo = new UserInfo(userName);
//清空之前生成的配置条目 //清空之前生成的配置条目
if(idGroup.size() > 0) { try {
try { flag = clearOldConfig(botId, userinfo, helper);
flag = clearOldConfig(botId, idGroup, userinfo, helper); } catch (SQLException e) {
} catch (SQLException e) { System.out.println("清空旧数据失败");
e.printStackTrace(); e.printStackTrace();
}
} }
for(String moduleType:jsonObject.parseArray(jsonObject.getJSONArray("moduleType").toJSONString(), String.class)) {
switch (moduleType) { Iterator<String> moduleIter = gameElements.keySet().iterator();
StringBuffer mergedConfigText = new StringBuffer();
while(moduleIter.hasNext()) {
moduleName = moduleIter.next();
switch (moduleName) {
case "map": case "map":
constructor = new MapConstructor(helper, botId); constructor = new MapConstructor(helper, botId);
break; break;
case "plot": case "plots":
constructor = new PlotConstructor(helper, botId); constructor = new PlotConstructor(helper, botId);
break; break;
case "goods":
constructor = new ItemConstructor(helper, botId);
break;
case "globalVarialbe":
constructor = new GlobalVariableConstructor(helper, botId);
break;
case "npc":
constructor = new CharacterConstructor(helper, botId);
break;
default:
continue;
} }
constructor.construct(jsonObject); System.out.println(moduleName);
constructor.construct(gameElements, userinfo);
//先清空再插入 //先清空再插入
idList.clear(); idList.clear();
helper.batchInsertConfigByBotId(botId, constructor.getjConfigList(), idList); if(moduleName.equals("map") || moduleName.equals("plots")) {
constructor.generateConfigText(idList); helper.batchInsertConfigByBotId(botId, constructor.getjConfigList(), idList);
mergedConfigText += constructor.getConfigText(); constructor.generateConfigText(idList);
}
mergedConfigText.append(constructor.getConfigText());
} }
StringReader sr = new StringReader(mergedConfigText); StringReader sr = new StringReader(mergedConfigText.toString());
BufferedReader br = new BufferedReader(sr); BufferedReader br = new BufferedReader(sr);
ProcessQuestion.buildUserInfo(br, userinfo, true); UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userName, userinfo); context.setAttribute(userName, userinfo);
ConditionTableController.writeConfigText(userName, userinfo); ConditionTableController.writeConfigText(userName, userinfo);
helper.destroy(); helper.destroy();
...@@ -114,10 +131,9 @@ public class GameDesignServlet extends HttpServlet { ...@@ -114,10 +131,9 @@ public class GameDesignServlet extends HttpServlet {
} }
public boolean clearOldConfig(int botId, List<Integer> idGroup, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException { public boolean clearOldConfig(int botId, UserInfo userInfo, SqlHelper helper) throws SQLException, IOException {
boolean deleteFlag = helper.deleteConfigByIdGroup(botId, idGroup); boolean deleteFlag = helper.deleteConfigByBotId(botId);
boolean deleteFlag2 = ConditionTableController.batchDeleteConfig(userInfo.userName, idGroup, userInfo);
return deleteFlag && deleteFlag2; return deleteFlag;
} }
} }
...@@ -6,6 +6,7 @@ import entity.UserInfo; ...@@ -6,6 +6,7 @@ import entity.UserInfo;
import processing.ChatCacheController; import processing.ChatCacheController;
import processing.MyFunc; import processing.MyFunc;
import processing.ProcessQuestion; import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -25,6 +26,8 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -25,6 +26,8 @@ public class GetAnswerServlet extends HttpServlet {
/** /**
* GetAnswerServlet就是处理post请求并返回答案的入口 * GetAnswerServlet就是处理post请求并返回答案的入口
*/ */
String NAME_MISSING_ERROR = "没找到bot名?";
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");
Map<String,String[]> curInfoPoint = new HashMap<>(request.getParameterMap()); Map<String,String[]> curInfoPoint = new HashMap<>(request.getParameterMap());
...@@ -45,16 +48,16 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -45,16 +48,16 @@ public class GetAnswerServlet extends HttpServlet {
UserInfo userinfo = null; UserInfo userinfo = null;
UserInfoController uiController = new UserInfoController();
try { try {
//userinfo = process.userInfoQuery(chatKey,context); //userinfo = process.userInfoQuery(chatKey,context);
//从servlet根据用户名查询用户信息 //从servlet根据用户名查询用户信息
userinfo = process.userInfoQuery(request.getParameter("bot_name"),context); userinfo = uiController.userInfoQuery(request.getParameter("bot_name"),context);
} catch (SQLException e) { } catch (SQLException e) {
state = "没找到bot名?"; state = NAME_MISSING_ERROR;
e.printStackTrace(); e.printStackTrace();
} }
//如果historyInfoPoint不存在,就新建一个 //如果historyInfoPoint不存在,就新建一个
if(historyInfoPoint == null) { if(historyInfoPoint == null) {
String k; String k;
...@@ -64,7 +67,14 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -64,7 +67,14 @@ public class GetAnswerServlet extends HttpServlet {
k = iit.next(); k = iit.next();
historyInfoPoint.put(k,""); historyInfoPoint.put(k,"");
} }
//变量初始化
iit = userinfo.varInitialMap.keySet().iterator();
while(iit.hasNext()){
k = iit.next();
historyInfoPoint.put(k, userinfo.varInitialMap.get(k));
}
} }
//信息点预处理 //信息点预处理
debugFlag = process.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo, curInfoRecord); debugFlag = process.infoPointPreprocessing(curInfoPoint, historyInfoPoint, userinfo, curInfoRecord);
...@@ -115,6 +125,6 @@ public class GetAnswerServlet extends HttpServlet { ...@@ -115,6 +125,6 @@ public class GetAnswerServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("do get m ethod"); System.out.println("do get method");
} }
} }
...@@ -3,6 +3,7 @@ package servlet; ...@@ -3,6 +3,7 @@ package servlet;
import entity.UserInfo; import entity.UserInfo;
import processing.MyFunc; import processing.MyFunc;
import processing.ProcessQuestion; import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -38,7 +39,7 @@ public class ReloadConfigServlet extends HttpServlet { ...@@ -38,7 +39,7 @@ public class ReloadConfigServlet extends HttpServlet {
BufferedReader br = new BufferedReader(reader); BufferedReader br = new BufferedReader(reader);
try { try {
userinfo = ProcessQuestion.buildUserInfo(br, userinfo, true); userinfo = UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userinfo.userName,userinfo); context.setAttribute(userinfo.userName,userinfo);
}catch(Exception e){ }catch(Exception e){
flag = false; flag = false;
......
...@@ -4,6 +4,7 @@ import entity.Condition; ...@@ -4,6 +4,7 @@ import entity.Condition;
import entity.UserInfo; import entity.UserInfo;
import processing.MyFunc; import processing.MyFunc;
import processing.ProcessQuestion; import processing.ProcessQuestion;
import processing.UserInfoController;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -58,7 +59,7 @@ public class UpdateServlet extends HttpServlet { ...@@ -58,7 +59,7 @@ public class UpdateServlet extends HttpServlet {
userinfo.configStr += line+"\n"; userinfo.configStr += line+"\n";
Condition condition = new Condition(); Condition condition = new Condition();
try { try {
condition = ProcessQuestion.buildCondition(condition,line); condition = UserInfoController.buildCondition(condition,line);
conditionList = userinfo.conditionTable.get(condition.indexName); conditionList = userinfo.conditionTable.get(condition.indexName);
if(conditionList == null) if(conditionList == null)
conditionList = new ArrayList<Condition>(); conditionList = new ArrayList<Condition>();
......
...@@ -3,6 +3,7 @@ package servlet; ...@@ -3,6 +3,7 @@ package servlet;
import entity.UserInfo; import entity.UserInfo;
import processing.ProcessQuestion; import processing.ProcessQuestion;
import processing.ConditionTableController; import processing.ConditionTableController;
import processing.UserInfoController;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -35,9 +36,19 @@ public class UpdateSingleConfigServlet extends HttpServlet { ...@@ -35,9 +36,19 @@ public class UpdateSingleConfigServlet extends HttpServlet {
case "delete_config": case "delete_config":
ConditionTableController.deleteConfig(userName, configId, data, context); ConditionTableController.deleteConfig(userName, configId, data, context);
break; break;
case "insert_variable":
ConditionTableController.updateVariable(userName, configId, data, context);
break;
case "delete_variable":
ConditionTableController.deleteVariable(userName, configId, data, context);
break;
case "update_variable":
ConditionTableController.updateVariable(userName, configId, data, context);
break;
case "extra_info": case "extra_info":
ConditionTableController.updateExtraInfo(userName, data, context); ConditionTableController.updateExtraInfo(userName, data, context);
break; break;
case "batch_update": case "batch_update":
batchUpdate(userName, data, context); batchUpdate(userName, data, context);
...@@ -81,7 +92,7 @@ public class UpdateSingleConfigServlet extends HttpServlet { ...@@ -81,7 +92,7 @@ public class UpdateSingleConfigServlet extends HttpServlet {
public void batchUpdate(String userName, String data, ServletContext context) throws IOException { public void batchUpdate(String userName, String data, ServletContext context) throws IOException {
BufferedReader br = new BufferedReader(new StringReader(data)); BufferedReader br = new BufferedReader(new StringReader(data));
UserInfo userinfo = new UserInfo(userName); UserInfo userinfo = new UserInfo(userName);
userinfo = ProcessQuestion.buildUserInfo(br, userinfo, true); userinfo = UserInfoController.buildUserInfo(br, userinfo, true);
context.setAttribute(userinfo.userName,userinfo); context.setAttribute(userinfo.userName,userinfo);
System.out.println("batchupdate"); System.out.println("batchupdate");
} }
......
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