package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.JConfig; import java.util.*; public class PlotConstructor extends ElementConstructor{ JSONArray plots = null; HashMap> adjList = new HashMap>(); HashMap> revAdjList = new HashMap>(); HashMap id2j = new HashMap(); HashMap visited = new HashMap(); public PlotConstructor(SqlHelper helper, int botId){ super(); this.helper = helper; this.botId = botId; } public void construct(JSONObject jsonObject){ JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String)jsonObject.get("start"); this.plots = plots; constructAdjList(plots); BFSGenerate(id2j.get(start)); } //根据id构造邻接表 public void constructAdjList(JSONArray plots){ int length = plots.size(), branchSize; String id = "", nextPlotId = ""; JSONObject plot = null; JSONArray branch = null; for(int i = 0;i < length;i++){ plot = plots.getJSONObject(i); id = (String)plot.get("id"); plot.put("index", i); if(adjList.get(id) == null){ adjList.put(id, new ArrayList<>()); } //添加branch中的下一个plot branch = plot.getJSONArray("branch"); branchSize = branch.size(); for(int j =0;j < branchSize;j++) { nextPlotId = (String)branch.getJSONObject(j).get("nextPlotId"); if(revAdjList.get(nextPlotId) == null){ revAdjList.put(nextPlotId, new ArrayList<>()); } adjList.get(id).add(nextPlotId); revAdjList.get(nextPlotId).add(id); } id2j.put(id, plot); } } public void BFSGenerate(JSONObject root){ String id = (String)root.get("id"), adjId, nextId, taskName = "", value = "" ,extraHP = "", extraUK="", nextTaskName; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("priority", 0); root.put("taskName", id); root.put("taskStep", 0); Queue queue = new LinkedList(); queue.add(root); while(!queue.isEmpty()){ node = queue.remove(); id = (String)node.get("id"); taskName = (String)node.get("taskName"); priority = (Integer)node.get("priority"); taskStep = (Integer)node.get("taskStep"); //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id ,true); //查看是否存在多个子结点,如果有就是一个分支 if(adjNodeList.size() > 1) newBranch = true; //查看是否存在多个父节点 if(revAdjList.get(id) != null) manyParent = revAdjList.get(id).size() > 1; if(newBranch){ //多个分支时nextTaskName为子结点的id名,不在这里设 nextTaskStep = 1; priority++; } else{ if(manyParent){ nextTaskName = id; nextTaskStep = 1; priority++; } else{ nextTaskName = taskName; nextTaskStep = taskStep + 1; } } branch = node.getJSONArray("branch"); branchSize = branch.size(); for(int i =0;i < branchSize;i++) { branchNode = branch.getJSONObject(i); nextId = (String)branchNode.get("nextPlotId"); if(node == root) extraHP = " "; else extraHP = taskName + "::match-Keyword=" + taskStep; if(newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) extraUK = nextId + ":" + 1; else extraUK = taskName + ":" + (taskStep + 1); jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), "", extraHP, "match", (String) branchNode.get("reply"), (String) branchNode.get("nextStep"), JSONArray.parseArray(branchNode.getJSONArray("updateKnowledge").toJSONString(), String.class), extraUK ); jConfig.setAuxiliaryPriority("" + priority); jConfigList.add(jConfig); if(branchNode.get("nextPlotId") != null){ adjId = (String)branchNode.get("nextPlotId"); if(visited.get(adjId) == null && id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("priority", priority); adjNode.put("taskStep", nextTaskStep); if(newBranch){ adjNode.put("taskName", adjId); }else{ adjNode.put("taskName", taskName); } queue.add(adjNode); } } } } } }