diff --git a/src/main/java/com/qiwu/chatbotbackend/GameEnvironment/Test.java.bak b/src/main/java/com/qiwu/chatbotbackend/GameEnvironment/Test.java.bak deleted file mode 100644 index 00f89ee..0000000 --- a/src/main/java/com/qiwu/chatbotbackend/GameEnvironment/Test.java.bak +++ /dev/null @@ -1,9 +0,0 @@ -package com.qiwu.chatbotbackend.GameEnvironment; - -public class Test { - public static void main(String[] args) throws Exception { - GameEnv gameEnv = new GameEnv("7011489633"); -// System.out.println(gameEnv.queryNpcAttrByNpcName("武士", "热度")); - System.out.println(gameEnv.queryGlobalAttr("啦啦")); - } -} diff --git a/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java b/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java index 3a38cb4..9221108 100644 --- a/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java +++ b/src/main/java/com/qiwu/chatbotbackend/gameDesign/PlotConstructor2.java @@ -52,16 +52,17 @@ public class PlotConstructor2 extends ElementConstructor { } public void construct(JSONObject jsonObject, UserInfo userinfo) { - + Integer startOption = jsonObject.getInteger("startOption"); try { - addGameStartPlot(jsonObject, keywordMap); + if(startOption != null && startOption.equals(2)) { + } else { + addGameStartPlot(jsonObject, keywordMap); + } JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String) jsonObject.get("start"), startCondition; JSONObject returnMessage = null; - - this.plots = plots; constructAdjList(plots); @@ -737,10 +738,7 @@ public class PlotConstructor2 extends ElementConstructor { jsonStr = jsonStr.replace("startId", gameElements.getString("start")); jsonStr = jsonStr.replace("site_list", keywordMap.get("site_list").toString()); - //System.out.println("JSONSTR ------------------------------\n" + jsonStr); JSONObject config = JSONObject.parseObject(jsonStr); - - gameElements.getJSONArray("plots").addAll(config.getJSONArray("gameStart")); gameElements.put("start", "start1"); gameElements.put("firstUnknownText", GAME_START_HINT); diff --git a/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java b/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java index f158d06..33068b4 100644 --- a/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java +++ b/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java @@ -829,7 +829,7 @@ public class ActionMatcher { poster.postTerm(curInfoPoint, historyInfoPoint, ans, ansAction, url); }else if(postType.equals("gameQA")){ System.out.println("提交表单" + gameEnv); - poster.postGameQA(curInfoPoint, historyInfoPoint, ans, gameEnv, worldController, url, userinfo); + poster.postGameQA(curInfoPoint, historyInfoPoint, ans, gameEnv, worldController, url, userinfo); } return ans; diff --git a/src/main/java/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java b/src/main/java/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java index 6eaa161..fcf8120 100644 --- a/src/main/java/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java +++ b/src/main/java/com/qiwu/chatbotbackend/servlet/GameDesignServlet.java @@ -1,376 +1,367 @@ -package com.qiwu.chatbotbackend.servlet; - -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.qiwu.chatbotbackend.actiongenerate.DB.EngineUtils; -import com.qiwu.chatbotbackend.actiongenerate.entity.IntervalStatus; -import com.qiwu.chatbotbackend.actiongenerate.utils.ActionUtils; -import com.qiwu.chatbotbackend.db.SqlHelper; -import com.qiwu.chatbotbackend.entity.ConfigTermText; -import com.qiwu.chatbotbackend.entity.UserInfo; -import com.qiwu.chatbotbackend.gameDesign.*; -import com.qiwu.chatbotbackend.processing.ChatCacheController; -import com.qiwu.chatbotbackend.processing.SimpleUtil; -import com.qiwu.chatbotbackend.processing.UserInfoController; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; -import java.sql.SQLException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.*; - -import static com.qiwu.chatbotbackend.Config.Config.*; - - -@WebServlet(name = "GameDesignServlet", urlPatterns = {"/gameDesign"}) -public class GameDesignServlet extends HttpServlet { - - //分组及其相应的id,id初始化为0, 新建分组成功后id会更新。 - HashMap categoryMap = new HashMap(){ - { - put("地图", 0); - put("剧情", 0); - put("物品", 0); - put("NPC", 0); - put("全局变量", 0); - } - }; - - - - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - DateTimeFormatter dtf=DateTimeFormatter.ofPattern("MM-dd HH:mm:ss.SSS"); - StringBuffer timeUsedDetect = new StringBuffer(); - LocalDateTime ldt = LocalDateTime.now(); - String now = ldt.format(dtf); - timeUsedDetect.append("接收请求的时间:").append(now); - - //所有需要修改的词性 - StringBuffer POS = new StringBuffer(); - - //每个condition的三元组两端需要补充的关键词,比如npc名字、npc属性等,格式为: "conditionName_tail/head":"keywords" - HashMap> additionalKeywords = new HashMap>(); - - //预先定义好的所有condition相关的关键词,格式为 "conditionName_标签":"keywords" - Map keywordMap = new HashMap(); - - //特定的relation的三元组不会被添加到relationKeywords - HashSet exceptRelation = new HashSet(); - - - Integer botId, projectId, dataId, returnStatus = 1; - String botAccount = null, userName, botName, moduleName2, returnMessage = "Success"; - ServletContext context = this.getServletContext(); - JSONObject json = null, root = null; - Boolean flag; - SqlHelper helper = null; - List idList = new ArrayList<>(); - ElementConstructor constructor = new ElementConstructor(); - - try { - helper = new SqlHelper(); - - - //读取并解析json数据 - String jsonStr = SimpleUtil.readJSONString(request); - - - if (jsonStr != null && !jsonStr.equals("")) { - JSONObject receiveParaneters = JSONObject.parseObject(jsonStr); - dataId = (Integer) receiveParaneters.get("id"); - - JSONObject gameElements = helper.queryGameElementsByProjectId(dataId); - - gameElements.put("auth", request.getHeader("authorization")); - //System.out.println(gameElements.toJSONString()); - botId = (Integer)gameElements.get("botId"); - botAccount = (String) gameElements.get("botAccount"); - userName = (String)gameElements.get("userName"); - botName = (String)gameElements.get("botName"); - - - //假设重建用户信息 - if (context.getAttribute(botAccount) == null) { - UserInfoController.userConfigInit(botAccount, context); - } - - UserInfo userinfo = (UserInfo) context.getAttribute(botAccount); - - - //清空之前生成的配置条目 - flag = botInit(botId, botAccount, userinfo, helper, context); - - - StringBuffer mergedConfigText = new StringBuffer(); - int skipCount = 0; - - - //从文件中读取condition相关的关键词 - readKeywords(keywordMap, KEYWORD_DIR); - - - // ldt = LocalDateTime.now(); - // now = ldt.format(dtf); - // timeUsedDetect.append("\n,预处理之后:").append(now); - String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"}; - for(String moduleName: moduleList){ - - switch (moduleName) { - case "map": - moduleName2 = "地图"; - constructor = new MapConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); - constructor.construct(gameElements, userinfo); - break; - case "plots": - moduleName2 = "剧情"; - constructor = new PlotConstructor2(helper, botId, botAccount, keywordMap, exceptRelation, POS); - constructor.construct(gameElements, userinfo); - - root = ((PlotConstructor2) constructor).getPlotIndex().get((String) gameElements.get("start")); - break; - case "goods": - moduleName2 = "物品"; - constructor = new ItemConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); - constructor.construct(gameElements, userinfo); - break; - case "globalVariable": - moduleName2 = "全局变量"; - constructor = new GlobalVariableConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); - constructor.construct(gameElements, userinfo); - break; - case "npc": - moduleName2 = "NPC"; - constructor = new CharacterConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); - constructor.construct(gameElements, userinfo); - break; - default: - continue; - - } - - if (constructor.getOperationStatus() == 1) { - - //先清空再插入 - idList.clear(); - helper.batchInsertVariableByBotId(botId, constructor); - - List jsonList = new ArrayList(); - for(ConfigTermText configTermText : constructor.getConfigTermTextList()){ - json = configTermText.toJson(constructor.getConditionDescription()); - constructor.parseKeyword(json, additionalKeywords, exceptRelation, botName); - jsonList.add(json); - } - - - - - helper.batchInsertConfigByBotId(botId, jsonList, idList, categoryMap.get(moduleName2)); - constructor.postProcessing(idList, categoryMap.get(moduleName2)); - constructor.generateConfigText(idList); - - mergedConfigText.append(constructor.getConfigText()); - }else { - returnStatus = 0; - returnMessage = constructor.getMessage(); - break; - } - - ldt = LocalDateTime.now(); - now = ldt.format(dtf); - timeUsedDetect.append("\n,").append(moduleName2).append("模块:").append(now); - - } - - if(returnStatus == 1) { - // ldt = LocalDateTime.now(); - // now = ldt.format(dtf); - // timeUsedDetect.append("\n发送post请求预处理:").append(now); - StringReader sr = new StringReader(mergedConfigText.toString()); - BufferedReader br = new BufferedReader(sr); - //System.out.println(mergedConfigText.toString()); - UserInfoController.buildUserInfo(br, userinfo, true); - context.setAttribute(botAccount, userinfo); - UserInfoController.writeConfigText(botAccount, userinfo); - // ldt = LocalDateTime.now(); - // now = ldt.format(dtf); - // timeUsedDetect.append("\n发送post请求前:").append(now); - IntervalStatus generateResult = actionRegenerate(root, gameElements, userinfo, additionalKeywords, exceptRelation, POS, helper); - - // ldt = LocalDateTime.now(); - // now = ldt.format(dtf); - // timeUsedDetect.append("\n发送post请求后:").append(now); - - if(generateResult.getStatus() != 1){ - returnStatus = 0; - returnMessage = generateResult.getMessage(); - } - } - - - }else { - returnStatus = 0; - returnMessage = "请求解析失败"; - } - } catch (SQLException e) { - e.printStackTrace(); - } finally{ - helper.destroy(); - } - - - - System.out.println(timeUsedDetect.toString()); - - - JSONObject responseJSONObject = new JSONObject(); - responseJSONObject.put("status", returnStatus); - responseJSONObject.put("message", returnMessage); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application/json; charset=utf-8"); - - - - System.out.println("xx:" + responseJSONObject.toJSONString()); - - - - PrintWriter out = response.getWriter(); - out.append(responseJSONObject.toString()); - out.flush(); - - } - - - - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - OutputStream out = response.getOutputStream(); - out.write("ssss".getBytes()); - out.flush(); - } - - public boolean botInit(int botId, String botAccount, UserInfo userInfo, SqlHelper helper, ServletContext context) throws SQLException, IOException { - boolean deleteConfigFlag = helper.deleteConfigByBotId(botId); - boolean deleteVariableFlag = helper.deleteVariableByBotId(botId); - - boolean deleteCategoryFlag = helper.deleteCategoryByBotId(botId); - boolean insertCategoryFlag = helper.batchInsertCategoryByBotId(botId, categoryMap); - boolean deteteConditionFlag = helper.deleteConditionByBotId(botId); - boolean deleteArchiveFlag = ChatCacheController.deleteChatCache(helper, botAccount, context); - - - return deleteConfigFlag && deleteVariableFlag; - } - - - /** - * 重新生成action文本 - * @param root - * @param gameElements - * @param userInfo - * @param additionalKeywords - * @param exceptRelation - * @param POS - * @return - */ - public IntervalStatus actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo, HashMap> additionalKeywords, HashSet exceptRelation, StringBuffer POS, SqlHelper helper){ - String startCondition, POS2 = "", botName = gameElements.getString("botName"); - System.out.println(botName); - JSONObject returnMessage = null; - EngineUtils engine = null; - IntervalStatus intervalStatus = new IntervalStatus(1, ""); - - JSONObject actionPostData = new JSONObject(); - - startCondition = (String)root.get("conditionName"); - actionPostData.put("startCondition", "start_options"); - actionPostData.put("welcome", String.format(GAME_WELCOME_TEXT, gameElements.getString("botName"))); - actionPostData.put("relationKeywords", additionalKeywords); - actionPostData.put("botId", gameElements.getInteger("botId")); - - if(gameElements.getJSONArray("conditionCategories") != null){ - actionPostData.put("conditionCategories", gameElements.getJSONArray("conditionCategories")); - }else{ - actionPostData.put("conditionCategories", new JSONArray(){ - { - add(GAME_CONDITION_CATEGORY); - } - }); - } - - - - if (gameElements.containsKey("POS")) - POS2 = (String)gameElements.get("POS"); - actionPostData.put("POS", POS.toString() + POS2); - - System.out.println(actionPostData.toJSONString()); - - try { - engine = new EngineUtils(); - ActionUtils actionUtils = new ActionUtils(engine, helper); - actionUtils.gamePlotInit(botName, gameElements.getInteger("botId"), gameElements.getString("auth"), actionPostData, startCondition, additionalKeywords); - intervalStatus = actionUtils.intervalStatus; - }catch(Exception e){ - e.printStackTrace(); - intervalStatus = new IntervalStatus(0, "action生成失败"); - }finally { - engine.destroy(); - } - - userInfo.extraInfo.put("activeAction", startCondition); - - return intervalStatus; - - } - - - //从配置文件中读取生成状态表所需要的关键词 - public void readKeywords(Map keywordsMap, String path){ - - File file = new File(path); - String line, elem[]; - - if(file.exists()){ - FileReader reader = null; - try { - reader = new FileReader(file); - BufferedReader br = new BufferedReader(reader); - - - while ((line = br.readLine()) != null) { - if(!line.equals("") && !line.startsWith("#")) { - elem = line.split("\\|", 3); - - //查看意图是否已经存在相关的condition,如果已经存在就append,如果不存在就初始化 - if (!elem[1].equals("pattern")) { - if (keywordsMap.get(elem[0]) == null) { - keywordsMap.put(elem[0], new StringBuffer(elem[1])); - } else { - keywordsMap.get(elem[0]).append(",").append(elem[1]); - } - } - - //根据意图和condition名字添加关键字 - keywordsMap.put(elem[0] + "_" + elem[1], new StringBuffer(elem[2])); - - } - - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - } - - -} +package com.qiwu.chatbotbackend.servlet; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.qiwu.chatbotbackend.actiongenerate.DB.EngineUtils; +import com.qiwu.chatbotbackend.actiongenerate.entity.IntervalStatus; +import com.qiwu.chatbotbackend.actiongenerate.utils.ActionUtils; +import com.qiwu.chatbotbackend.db.SqlHelper; +import com.qiwu.chatbotbackend.entity.ConfigTermText; +import com.qiwu.chatbotbackend.entity.UserInfo; +import com.qiwu.chatbotbackend.gameDesign.*; +import com.qiwu.chatbotbackend.processing.ChatCacheController; +import com.qiwu.chatbotbackend.processing.SimpleUtil; +import com.qiwu.chatbotbackend.processing.UserInfoController; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +import static com.qiwu.chatbotbackend.Config.Config.*; + + +@WebServlet(name = "GameDesignServlet", urlPatterns = {"/gameDesign"}) +public class GameDesignServlet extends HttpServlet { + + //分组及其相应的id,id初始化为0, 新建分组成功后id会更新。 + HashMap categoryMap = new HashMap(){ + { + put("地图", 0); + put("剧情", 0); + put("物品", 0); + put("NPC", 0); + put("全局变量", 0); + } + }; + + + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + DateTimeFormatter dtf=DateTimeFormatter.ofPattern("MM-dd HH:mm:ss.SSS"); + StringBuffer timeUsedDetect = new StringBuffer(); + LocalDateTime ldt = LocalDateTime.now(); + String now = ldt.format(dtf); + timeUsedDetect.append("接收请求的时间:").append(now); + + //所有需要修改的词性 + StringBuffer POS = new StringBuffer(); + + //每个condition的三元组两端需要补充的关键词,比如npc名字、npc属性等,格式为: "conditionName_tail/head":"keywords" + HashMap> additionalKeywords = new HashMap>(); + + //预先定义好的所有condition相关的关键词,格式为 "conditionName_标签":"keywords" + Map keywordMap = new HashMap(); + + //特定的relation的三元组不会被添加到relationKeywords + HashSet exceptRelation = new HashSet(); + + + Integer botId, projectId, dataId, returnStatus = 1; + String botAccount = null, userName, botName, moduleName2, returnMessage = "Success"; + ServletContext context = this.getServletContext(); + JSONObject json = null, root = null; + Boolean flag; + SqlHelper helper = null; + List idList = new ArrayList<>(); + ElementConstructor constructor = new ElementConstructor(); + + try { + helper = new SqlHelper(); + + + //读取并解析json数据 + String jsonStr = SimpleUtil.readJSONString(request); + + + if (jsonStr != null && !jsonStr.equals("")) { + JSONObject receiveParaneters = JSONObject.parseObject(jsonStr); + dataId = (Integer) receiveParaneters.get("id"); + + JSONObject gameElements = helper.queryGameElementsByProjectId(dataId); + + gameElements.put("auth", request.getHeader("authorization")); + //System.out.println(gameElements.toJSONString()); + botId = (Integer)gameElements.get("botId"); + botAccount = (String) gameElements.get("botAccount"); + userName = (String)gameElements.get("userName"); + botName = (String)gameElements.get("botName"); + + + //假设重建用户信息 + if (context.getAttribute(botAccount) == null) { + UserInfoController.userConfigInit(botAccount, context); + } + + UserInfo userinfo = (UserInfo) context.getAttribute(botAccount); + + + //清空之前生成的配置条目 + flag = botInit(botId, botAccount, userinfo, helper, context); + + + StringBuffer mergedConfigText = new StringBuffer(); + int skipCount = 0; + + + //从文件中读取condition相关的关键词 + readKeywords(keywordMap, KEYWORD_DIR); + + + // ldt = LocalDateTime.now(); + // now = ldt.format(dtf); + // timeUsedDetect.append("\n,预处理之后:").append(now); + String moduleList[] = {"map", "goods", "globalVariable", "npc", "plots"}; + for(String moduleName: moduleList){ + + switch (moduleName) { + case "map": + moduleName2 = "地图"; + constructor = new MapConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); + constructor.construct(gameElements, userinfo); + break; + case "plots": + moduleName2 = "剧情"; + constructor = new PlotConstructor2(helper, botId, botAccount, keywordMap, exceptRelation, POS); + constructor.construct(gameElements, userinfo); + + root = ((PlotConstructor2) constructor).getPlotIndex().get((String) gameElements.get("start")); + break; + case "goods": + moduleName2 = "物品"; + constructor = new ItemConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); + constructor.construct(gameElements, userinfo); + break; + case "globalVariable": + moduleName2 = "全局变量"; + constructor = new GlobalVariableConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); + constructor.construct(gameElements, userinfo); + break; + case "npc": + moduleName2 = "NPC"; + constructor = new CharacterConstructor(helper, botId, botAccount, keywordMap, exceptRelation, POS); + constructor.construct(gameElements, userinfo); + break; + default: + continue; + + } + + if (constructor.getOperationStatus() == 1) { + + //先清空再插入 + idList.clear(); + helper.batchInsertVariableByBotId(botId, constructor); + + List jsonList = new ArrayList(); + for(ConfigTermText configTermText : constructor.getConfigTermTextList()){ + json = configTermText.toJson(constructor.getConditionDescription()); + constructor.parseKeyword(json, additionalKeywords, exceptRelation, botName); + jsonList.add(json); + } + + helper.batchInsertConfigByBotId(botId, jsonList, idList, categoryMap.get(moduleName2)); + constructor.postProcessing(idList, categoryMap.get(moduleName2)); + constructor.generateConfigText(idList); + + mergedConfigText.append(constructor.getConfigText()); + }else { + returnStatus = 0; + returnMessage = constructor.getMessage(); + break; + } + + ldt = LocalDateTime.now(); + now = ldt.format(dtf); + timeUsedDetect.append("\n,").append(moduleName2).append("模块:").append(now); + + } + + if(returnStatus == 1) { + StringReader sr = new StringReader(mergedConfigText.toString()); + BufferedReader br = new BufferedReader(sr); + UserInfoController.buildUserInfo(br, userinfo, true); + context.setAttribute(botAccount, userinfo); + IntervalStatus generateResult = actionRegenerate(root, gameElements, userinfo, additionalKeywords, exceptRelation, POS, helper); + UserInfoController.writeConfigText(botAccount, userinfo); + + if(generateResult.getStatus() != 1){ + returnStatus = 0; + returnMessage = generateResult.getMessage(); + } + } + + + }else { + returnStatus = 0; + returnMessage = "请求解析失败"; + } + } catch (SQLException e) { + e.printStackTrace(); + } finally{ + helper.destroy(); + } + + + + System.out.println(timeUsedDetect.toString()); + + + JSONObject responseJSONObject = new JSONObject(); + responseJSONObject.put("status", returnStatus); + responseJSONObject.put("message", returnMessage); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json; charset=utf-8"); + + + + System.out.println("xx:" + responseJSONObject.toJSONString()); + + + + PrintWriter out = response.getWriter(); + out.append(responseJSONObject.toString()); + out.flush(); + + } + + + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + OutputStream out = response.getOutputStream(); + out.write("ssss".getBytes()); + out.flush(); + } + + public boolean botInit(int botId, String botAccount, UserInfo userInfo, SqlHelper helper, ServletContext context) throws SQLException, IOException { + boolean deleteConfigFlag = helper.deleteConfigByBotId(botId); + boolean deleteVariableFlag = helper.deleteVariableByBotId(botId); + + boolean deleteCategoryFlag = helper.deleteCategoryByBotId(botId); + boolean insertCategoryFlag = helper.batchInsertCategoryByBotId(botId, categoryMap); + boolean deteteConditionFlag = helper.deleteConditionByBotId(botId); + boolean deleteArchiveFlag = ChatCacheController.deleteChatCache(helper, botAccount, context); + + + return deleteConfigFlag && deleteVariableFlag; + } + + + /** + * 重新生成action文本 + * @param root + * @param gameElements + * @param userInfo + * @param additionalKeywords + * @param exceptRelation + * @param POS + * @return + */ + public IntervalStatus actionRegenerate(JSONObject root, JSONObject gameElements, UserInfo userInfo, HashMap> additionalKeywords, HashSet exceptRelation, StringBuffer POS, SqlHelper helper){ + String startCondition, POS2 = "", botName = gameElements.getString("botName"); + System.out.println(botName); + Integer startOption = gameElements.getInteger("startOption"); + JSONObject returnMessage = null; + EngineUtils engine = null; + IntervalStatus intervalStatus = new IntervalStatus(1, ""); + + JSONObject actionPostData = new JSONObject(); + + startCondition = (String)root.get("conditionName"); + actionPostData.put("startCondition", "start_options"); + if(startOption != null && startOption.equals(2)) { + actionPostData.put("welcome", gameElements.getString("plotStartText")); + } else { + actionPostData.put("welcome", String.format(GAME_WELCOME_TEXT, botName)); + } + actionPostData.put("relationKeywords", additionalKeywords); + actionPostData.put("botId", gameElements.getInteger("botId")); + + if(gameElements.getJSONArray("conditionCategories") != null){ + actionPostData.put("conditionCategories", gameElements.getJSONArray("conditionCategories")); + }else{ + actionPostData.put("conditionCategories", new JSONArray(){ + { + add(GAME_CONDITION_CATEGORY); + } + }); + } + + + + if (gameElements.containsKey("POS")) + POS2 = (String)gameElements.get("POS"); + actionPostData.put("POS", POS.toString() + POS2); + + System.out.println(actionPostData.toJSONString()); + + try { + engine = new EngineUtils(); + ActionUtils actionUtils = new ActionUtils(engine, helper); + actionUtils.gamePlotInit(botName, gameElements.getInteger("botId"), gameElements.getString("auth"), actionPostData, startCondition, additionalKeywords); + intervalStatus = actionUtils.intervalStatus; + }catch(Exception e){ + e.printStackTrace(); + intervalStatus = new IntervalStatus(0, "action生成失败"); + }finally { + engine.destroy(); + } + + userInfo.extraInfo.put("activeAction", startCondition); + + return intervalStatus; + + } + + + //从配置文件中读取生成状态表所需要的关键词 + public void readKeywords(Map keywordsMap, String path){ + + File file = new File(path); + String line, elem[]; + + if(file.exists()){ + FileReader reader = null; + try { + reader = new FileReader(file); + BufferedReader br = new BufferedReader(reader); + + + while ((line = br.readLine()) != null) { + if(!line.equals("") && !line.startsWith("#")) { + elem = line.split("\\|", 3); + + //查看意图是否已经存在相关的condition,如果已经存在就append,如果不存在就初始化 + if (!elem[1].equals("pattern")) { + if (keywordsMap.get(elem[0]) == null) { + keywordsMap.put(elem[0], new StringBuffer(elem[1])); + } else { + keywordsMap.get(elem[0]).append(",").append(elem[1]); + } + } + + //根据意图和condition名字添加关键字 + keywordsMap.put(elem[0] + "_" + elem[1], new StringBuffer(elem[2])); + + } + + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + +} diff --git a/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java b/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java index f04a0a2..88df3d4 100644 --- a/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java +++ b/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java @@ -1,6 +1,7 @@ package com.qiwu.chatbotbackend.servlet; import com.alibaba.fastjson.JSONObject; +import com.qiwu.chatbotbackend.actiongenerate.DB.EngineUtils; import com.qiwu.chatbotbackend.entity.Action; import com.qiwu.chatbotbackend.entity.ChatCache; import com.qiwu.chatbotbackend.entity.Solution; @@ -17,6 +18,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; +import java.sql.SQLException; import java.text.ParseException; import java.util.HashMap; import java.util.Map; @@ -44,11 +46,29 @@ public class GetAnswerServlet extends HttpServlet { if(tmpStr == null || tmpStr.equals("")) continue; tmpStr = tmpStr.replace("qiwu", "我"); - //tmpStr = tmpStr.replace("qiwu", "你"); receiveParamters.put(entry.getKey(), tmpStr); } } String botName = receiveParamters.get("bot_name"); + /* + EngineUtils engineDBConn = null; + try { + engineDBConn = new EngineUtils(); + String nickname = engineDBConn.queryBotNickNameByName(botName); + } catch (SQLException e1) { + e1.printStackTrace(); + } finally { + engineDBConn.destroy(); + } + Set> entrySet2 = receiveParamters.entrySet(); + for(Entry entry : entrySet2) { + String value = entry.getValue(); + if(value.indexOf("")) { + + } + } + */ + Map curInfoPoint = new HashMap(); Solution bestMatch = null;