package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.JConfig; import entity.UserInfo; import java.io.BufferedReader; import java.io.StringReader; import java.util.*; public class MapConstructor extends ElementConstructor { int[][] matrix; int locationNum; HashMap id2j = new HashMap(); Map question2keyword = new HashMap(); JSONArray locations = null; public MapConstructor(SqlHelper helper, int botId) { super(); this.helper = helper; this.botId = botId; } public void constructGreph(JSONArray locations) { JSONObject jobj = null; int locationId, index; //ArrayList> matrix= new ArrayList>(); //ArrayList vec1d = new ArrayList(); locationNum = locations.size(); for (int i = 0; i < locationNum; i++) { jobj = locations.getJSONObject(i); jobj.put("index", i); locationId = (Integer) jobj.get("id"); id2j.put(locationId, jobj); } //邻接矩阵初始化 matrix = new int[locationNum][locationNum]; for (int i = 0; i < locationNum; i++) { for (int j = 0; j < locationNum; j++) { matrix[i][j] = Integer.MAX_VALUE; } } for (int i = 0; i < locationNum; i++) { List neighbours = JSONArray.parseArray(locations.getJSONObject(i).getJSONArray("neighbours").toJSONString(), Integer.class); for (int neighbourId : neighbours) { JSONObject neighbourObject = id2j.get(neighbourId); index = (Integer) neighbourObject.get("index"); matrix[i][index] = 1; matrix[index][i] = 1; } } } public void construct(JSONObject jsonObject, UserInfo userinfo) { conditionDescription.put("site", "当前地点"); varInitialMap.put("site", "海边"); JSONObject map = jsonObject.getJSONObject("map"); Map question2keyword = JSONObject.toJavaObject(map.getJSONObject("question"), Map.class); JSONArray locations = map.getJSONArray("locations"); String botName = (String)jsonObject.get("botName"); this.locations = locations; this.question2keyword = question2keyword; //通过邻接表构造邻接矩阵 constructGreph(locations); //用弗洛伊德算法求最短路径 Floyd floyd = new Floyd(); floyd.findShortestPath(matrix, locationNum); floyd.printPath(matrix, locationNum); generateWantToThere(floyd.pathMatirx, floyd.preTable, jsonObject); generateWhereCanIgo(floyd.pathMatirx, floyd.preTable, jsonObject); generateWhereIam(jsonObject, botName); generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject); } /** * 生成‘我在哪里’所需要的配置文件 */ public void generateWhereIam(JSONObject jsonObject, String botName) { String conditionName = "rencheng"; String conditionKeywords = "botName:哪里+botName:哪"; String zainaliKeywords = conditionKeywords.replace("botName", botName); jConfig = new JConfig( conditionName + "::match-Keyword=" + zainaliKeywords, " ", "match", "你在{site-2}", "yaobuyao", "" ); jConfigList.add(jConfig); } /** * 生成‘怎么去哪里’所需要的配置文件 */ public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { String conditionName = "want_know_zenmezuo"; String conditionKeywords = "去:地点"; String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String want_know_zenmezuoKeywords = question2keyword.get("want_know_zenmezuo"); for (int i = 0; i < locationNum; i++) { for (int j = 0; j < locationNum; j++) { placeA = (String) locations.getJSONObject(i).get("name"); placeB = (String) locations.getJSONObject(j).get("name"); placeKeywords = conditionKeywords.replace("地点", placeB); if (i == j) { jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao", "" ); jConfigList.add(jConfig); continue; } ; if (pathMatirx[i][j] == 1) { jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", placeB + "就在附近哦", "yaobuyao", "" ); jConfigList.add(jConfig); } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { int k = preTable[i][j]; pathNode = ""; while (k != j) { pathNode += (String) locations.getJSONObject(k).get("name"); k = preTable[k][j]; } jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao", "" ); jConfigList.add(jConfig); } } } } /** * 生成‘我能去哪里’所需要的配置文件 */ public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { String conditionKeywords = "能去:哪+能去:哪里+能去:哪儿"; String conditionName = "choose_gowhere"; String placeA, placeB, pathNode; for (int i = 0; i < locationNum; i++) { placeA = (String) locations.getJSONObject(i).get("name"); pathNode = ""; for (int j = 0; j < locationNum; j++) { if (pathMatirx[i][j] == 1) { placeB = (String) locations.getJSONObject(j).get("name"); pathNode += "、" + placeB; } } jConfig = new JConfig( conditionName + "::match-Keyword=" + conditionKeywords, "site::match-Keyword=" + placeA, "match", "你可以去" + pathNode, "yaobuyao", "" ); jConfigList.add(jConfig); } } /** * 生成‘我要去那里’所需要的配置文件 */ public void generateWantToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject) { String placeA, placeB, descriptionB, placeKeywords, text, pathNode, lastTaskCondition, lastTaskReply; String conditionName = "quna"; String conditionKeywords = "地点"; for (int i = 0; i < locationNum; i++) { for (int j = 0; j < locationNum; j++) { placeA = (String) locations.getJSONObject(i).get("name"); placeB = (String) locations.getJSONObject(j).get("name"); descriptionB = (String) locations.getJSONObject(j).get("description"); placeKeywords = conditionKeywords.replace("地点", placeB); lastTaskCondition = "任务线" + placeB + "最后一个condition"; lastTaskReply = "任务线" + placeB + "最后的回答"; if (i == j) { jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao", "" ); jConfigList.add(jConfig); continue; } ; //如果两点相邻 if (pathMatirx[i][j] == 1) { jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "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); //如果两点不是相邻点 } else if (pathMatirx[i][j] > 1 && pathMatirx[i][j] < Integer.MAX_VALUE) { int k = preTable[i][j]; pathNode = ""; while (k != j) { pathNode += (String) locations.getJSONObject(k).get("name"); k = preTable[k][j]; } jConfig = new JConfig( conditionName + "::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao", "" ); jConfigList.add(jConfig); } } } } 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); } }