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.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; public class MapConstructor extends ElementConstructor{ int[][] matrix; int locationNum; HashMap id2j = 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){ JSONArray locations = jsonObject.getJSONArray("locations"); this.locations = locations; //通过邻接表构造邻接矩阵 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); generateHowToThere(floyd.pathMatirx, floyd.preTable, jsonObject); } /** * 生成‘我在哪里’所需要的配置文件 */ public void generateWhereIam(JSONObject jsonObject){ String zainaKeywords = (String)jsonObject.get("zaina"); jConfig = new JConfig( "zaina::match-Keyword=" + zainaKeywords, " ", "match", "你在{site-2}", "yaobuyao" ); jConfigList.add(jConfig); } /** * 生成‘怎么去哪里’所需要的配置文件 */ public void generateHowToThere(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ String placeA, placeB, descriptionB, placeKeywords, text, pathNode; String zenmequKeywords = (String)jsonObject.get("zenmequ"); 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 = zenmequKeywords.replace("地点", placeB); if(i == j){ jConfig = new JConfig( "zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao" ); jConfigList.add(jConfig); continue; }; if(pathMatirx[i][j] == 1){ jConfig = new JConfig( "zenmequ::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( "zenmequ::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "经过" + pathNode + "就可以到达" + placeB, "yaobuyao" ); jConfigList.add(jConfig); } } } } /** * 生成‘我能去哪里’所需要的配置文件 */ public void generateWhereCanIgo(int[][] pathMatirx, int[][] preTable, JSONObject jsonObject){ String nengqunaKeywords = (String)jsonObject.get("nengquna"); 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( "nengquna::match-Keyword=" + nengqunaKeywords, "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; String qunaKeywords = (String)jsonObject.get("quna"); 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 = qunaKeywords.replace("地点", placeB); if(i == j){ jConfig = new JConfig( "quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "你已经在" + placeB + "了", "yaobuyao" ); jConfigList.add(jConfig); continue; }; if(pathMatirx[i][j] == 1){ jConfig = new JConfig( "quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match,updateKnowledge-<-site:" + placeB, "" + descriptionB, "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( "quna::match-Keyword=" + placeKeywords, "site::match-Keyword=" + placeA, "match", "从" + placeA + "不能直接到达" + placeB + ", 需要经过" + pathNode + "才能到达" + placeB, "yaobuyao" ); jConfigList.add(jConfig); } } } } }