package com.qiwu.chatbotbackend.gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.ConfigTermText; import com.qiwu.chatbotbackend.entity.UserInfo; import java.util.*; import static com.qiwu.chatbotbackend.Config.Config.TASK_CONDITION; import static com.qiwu.chatbotbackend.Config.Config.TASK_LINE; import static com.qiwu.chatbotbackend.Config.Config.TASK_REPLY; public class MapConstructor extends ElementConstructor { int locationNum; HashMap id2j = new HashMap(); Map question2keyword = new HashMap(); Map> name2nickname = new HashMap>(); JSONArray locations = null; Map script = null; //话术模板 public MapConstructor(SqlHelper helper, int botId, String botAccount, Map keywordMap, HashSet exceptRelation, StringBuffer POS) { super(); this.helper = helper; this.botId = botId; this.botAccount = botAccount; this.exceptRelation = exceptRelation; this.keywordMap = keywordMap; this.POS = POS; } public void constructGreph(JSONArray locations) { JSONObject jobj = null; int locationId, index; String locationName, taskLineName = ""; locationNum = locations.size(); for (int i = 0; i < locationNum; i++) { jobj = locations.getJSONObject(i); jobj.put("index", i); locationName = (String) jobj.get("name"); taskLineName = String.format(TASK_LINE, locationName); if (!varInitialMap.containsKey(taskLineName)) { conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, ""); } name2nickname.putIfAbsent(locationName, new ArrayList<>()); POS.append("=" + locationName + "|地名\r\n"); if(jobj.containsKey("nickname")){ for(String nickname:JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("nickname").toJSONString(), String.class)){ if(!nickname.equals("")) { POS.append("=").append(nickname).append("|地名\r\n"); name2nickname.get(locationName).add(nickname); } } } locationId = (Integer) jobj.get("id"); id2j.put(locationId, jobj); } } public void construct(JSONObject gameElements, UserInfo userinfo) { try { conditionDescription.put("site", "当前地点"); JSONObject map = gameElements.getJSONObject("map"); Map question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); script = JSONObject.toJavaObject(gameElements.getJSONObject("speechcraft"), Map.class); if(script == null){ throw new Exception("话术不存在"); } JSONArray locations = map.getJSONArray("locations"); String locationName; this.locations = locations; this.question2keyword = question2keyword; //通过邻接表构造邻接矩阵 constructGreph(locations); generateWantToThere(gameElements, keywordMap); generateHowToThere(gameElements, keywordMap); //查询类型配置条目的生成 queryTypeConfigTermGenerate(keywordMap, "WHERE_I_AM", "map", "queryWhereIam"); queryTypeConfigTermGenerate(keywordMap, "WHERE_CAN_I_GO", "map", "queryCanGoBySite"); queryTypeConfigTermGenerate(keywordMap, "AROUND_NPC", "map", "queryAroundNpcBySite"); queryTypeConfigTermGenerate(keywordMap, "AROUND_ITEM", "map", "queryAroundGoodsBySite"); queryTypeConfigTermGenerate(keywordMap, "AROUND_THINGS", "map", "queryAroundAllBySite"); queryTypeConfigTermGenerate(keywordMap, "AROUND_PLACE", "map", "queryAroundPlace"); // queryTypeConfigTermGenerate(keywordMap, "WHO_IN_THERE", "map", "queryInnerNpcBySite"); queryTypeConfigTermGenerate(keywordMap, "TASK_IS_WHAT", "map", "queryTaskDescription"); //如果预先要添加的condition关键词是带有变量的(比如name),那么说明这是一个关键词模板,需要在生成的时候把这些变量替换成具体的值,所以需要想拷贝模板 List replaceList = new ArrayList(){ { add(new String[]{"name",""}); } }; copyPattern("WAHT_ITEM_IN_LOCATION"); copyPattern("WHAT_NPC_IN_LOCATION"); copyPattern("WHAT_THINGS_IN_LOCATION"); for (int i = 0; i < locationNum; i++) { locationName = locations.getJSONObject(i).getString("name"); replaceList.get(0)[1] = locationName; //根据模板添加关键词 appendReplacedKeyword("WAHT_ITEM_IN_LOCATION", replaceList); appendReplacedKeyword("WHAT_NPC_IN_LOCATION", replaceList); appendReplacedKeyword("WHAT_THINGS_IN_LOCATION", replaceList); } queryTypeConfigTermGenerate(keywordMap, "WAHT_ITEM_IN_LOCATION", "map", "queryItemsInPlace"); queryTypeConfigTermGenerate(keywordMap, "WHAT_NPC_IN_LOCATION", "map", "queryNpcInPlace"); queryTypeConfigTermGenerate(keywordMap, "WHAT_THINGS_IN_LOCATION", "map", "queryAllInPlace"); } catch(Exception e){ operationStatus = 0; e.printStackTrace(); message = "地图处理出现异常:"; return; } } /** * 生成‘怎么去哪里’所需要的配置文件 */ public void generateHowToThere(JSONObject gameElements, Map keywordMap) { //String conditionName = "want_know_zenmezuo"; String intent = "HOW_TO_THERE", placeA, keywordPattern, gotoSite; String conditionNameList[] = keywordMap.get(intent).toString().split(","); StringBuffer placeKeywords = new StringBuffer(); for(String conditionName:conditionNameList) { placeKeywords.setLength(0); keywordPattern = keywordMap.get(intent + "_" + conditionName).toString(); //提取关键词 //conditionKeywords = keywordMap.get(intent + "_" + conditionName).toString(); for (int i = 0; i < locationNum; i++) { placeA = (String) locations.getJSONObject(i).get("name"); gotoSite = keywordPattern.replace("name", placeA); //toAllSiteskeywords.append(gotoSite); //toSitekeywords.append(gotoSite); placeKeywords.append(gotoSite); for (String nickname : name2nickname.get(placeA)) { gotoSite = keywordPattern.replace("name", nickname); placeKeywords.append(gotoSite); //toSitekeywords.append(gotoSite); } } configTermText = new ConfigTermText( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:" + conditionName + "::match-Include=0", " ", "match,return-0", "", " ", "map_query:queryPathAt:" ); configTermText.setNextStepStatus("return-0"); configTermText.setAuxiliaryPriority("1"); ConfigTermText.activateCopy(configTermText, configTermTextList, "map"); configTermText = new ConfigTermText( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:" + conditionName + "::match-Include=1+3+4", " ", "match,return-0", "", " ", "map_query:queryPathAdjoin:" ); configTermText.setNextStepStatus("return-0"); configTermText.setAuxiliaryPriority("1"); ConfigTermText.activateCopy(configTermText, configTermTextList, "map"); configTermText = new ConfigTermText( conditionName + "::match-Include=" + placeKeywords + ",isNeighbour:" + conditionName + "::match-Include=2", " ", "match,return-0", "", " ", "map_query:queryHowToPathBySite:" + conditionName ); configTermText.setNextStepStatus("return-0"); configTermText.setAuxiliaryPriority("1"); ConfigTermText.activateCopy(configTermText, configTermTextList, "map"); } } /** * 生成‘我要去那里’所需要的配置文件 */ public void generateWantToThere(JSONObject gameElements, Map keywordMap) { String placeA, lastTaskCondition, lastTaskReply, taskLine; String intent = "GO_TO", keywordPattern; String conditionName = keywordMap.get(intent).toString().split(",")[0]; keywordPattern = keywordMap.get(intent + "_" + conditionName).toString(); //包含去xx的所有地点的关键词集合 StringBuffer toAllSiteskeywords = new StringBuffer(); //包含去xx这个特定地点的关键词的集合(包含别名) StringBuffer toSitekeywords = new StringBuffer(); String gotoSite = ""; for (int i = 0; i < locationNum; i++) { toSitekeywords.setLength(0); placeA = (String) locations.getJSONObject(i).get("name"); gotoSite = keywordPattern.replace("name", placeA); toAllSiteskeywords.append(gotoSite); toSitekeywords.append(gotoSite); for (String nickname : name2nickname.get(placeA)) { gotoSite = keywordPattern.replace("name", nickname); toAllSiteskeywords.append(gotoSite); toSitekeywords.append(gotoSite); } lastTaskCondition = String.format(TASK_CONDITION, placeA); lastTaskReply = String.format(TASK_REPLY, placeA); taskLine = String.format(TASK_LINE, placeA); //地点任务和地点回答的初始化 if (!varInitialMap.containsKey(lastTaskCondition)) { varInitialMap.put(lastTaskCondition, conditionName); varInitialMap.put(lastTaskReply, script.get("SITE_NOT_ACTIVE").replace("[地点名]", placeA)); conditionDescription.put(lastTaskCondition, lastTaskCondition); conditionDescription.put(lastTaskReply, lastTaskReply); } /** * 要去某个地方,并且该地点相邻或者可以直接到达 */ configTermText = new ConfigTermText( conditionName + "::match-Include=" + toSitekeywords + "," + ",isNeighbour:" + conditionName + "::match-Include=1+3+4", " ", "match, reutrn-0,activate", "{" + lastTaskReply + "-2}", "plot_" + placeA + "_*", "" ); configTermText.setConfigName("map", true); configTermTextList.add(configTermText); //上一个配置文件激活时的默认选项(兜底),上一条一定会激活下一条,下一条是没有条件的,如果没有找到其他可以激活的配置条目,则激活下一条 configTermText = new ConfigTermText( " ", " ", "", "{" + lastTaskReply + "-2}", " ", "process-nextStep:var={" + lastTaskCondition + "-2}-<-site:" + placeA + "-<-npc_update:updateFollowerLocation:" + placeA + "-<-game_step:var={" + taskLine + "-2}" ); configTermText.setStatus("sleeping"); configTermText.setConfigName("plot_" + placeA + "_doudi", false); configTermText.setAuxiliaryPriority("0"); configTermTextList.add(configTermText); //上一个配置文件激活时的默认选项, 地点任务线未开启,则不更新game_step configTermText = new ConfigTermText( lastTaskCondition + "::match-Include=null", " ", "", "{" + lastTaskReply + "-2}", " ", "process-nextStep:var={" + lastTaskCondition + "-2}-<-site:" + placeA + "-<-npc_update:updateFollowerLocation:" + placeA ); configTermText.setStatus("sleeping"); configTermText.setConfigName("plot_" + placeA + "_doudi2", false); configTermText.setAuxiliaryPriority("0"); configTermTextList.add(configTermText); } /** * 要去的地方就在当前地点 */ configTermText = new ConfigTermText( conditionName + "::match-Include=" + toAllSiteskeywords + ",isNeighbour:" + conditionName + "::match-Include=0", " ", "match,return-0", "", " ", "map_query:queryPathAt:" ); configTermText.setConfigName("map", true); configTermTextList.add(configTermText); /** * 要去的地方要经过几个地点才能到达 */ configTermText = new ConfigTermText( conditionName + "::match-Include=" + toAllSiteskeywords + ",isNeighbour:" + conditionName + "::match-Include=2", " ", "match,return-0", "", " ", "map_query:queryToDoPathBySite:" + conditionName ); configTermText.setConfigName("map", true); configTermTextList.add(configTermText); /** * 要去的地方不可到达 */ configTermText = new ConfigTermText( conditionName + "::match-Include=" + toAllSiteskeywords + ",isNeighbour:" + conditionName + "::match-Include=5", " ", "match,return-0", "", " ", "map_query:queryPathNotExist:" ); configTermText.setConfigName("map", true); configTermTextList.add(configTermText); } }