package gameDesign; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import db.SqlHelper; import entity.JConfig; import entity.UserInfo; 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, UserInfo userinfo) { JSONArray plots = jsonObject.getJSONArray("plots"); String start = (String) jsonObject.get("start"); this.plots = plots; constructAdjList(plots); String taskLineName = "任务线_" + id2j.get(start).get("place") + "进度"; conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, start); varInitialMap.put("unknownText", "你说啥?"); conditionDescription.put("unknownText", "对于无法识别的问题做出的回答"); BFSGenerate3(id2j.get(start), userinfo); varInitialMap.put(start, "0"); } //根据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, UserInfo userinfo) { String id = (String) root.get("id"), adjId, nextId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, nextUnknownText, curUnknownText, nextCondition; 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); root.put("nextUnknownText", varInitialMap.get("unknownText")); ; Queue queue = new LinkedList(); queue.add(root); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); conditionDescription.put(id, "剧情" + id + "进度"); varInitialMap.put(id, ""); taskName = (String) node.get("taskName"); priority = (Integer) node.get("priority"); taskStep = (Integer) node.get("taskStep"); conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); //初始化 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; } else { if (manyParent) { nextTaskName = id; nextTaskStep = 1; priority++; } else { nextTaskName = taskName; nextTaskStep = taskStep + 1; } } branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextUnknownText = updateKnowledge.get("unknownText"); if (id2j.get(nextId) != null) nextCondition = (String) id2j.get(nextId).get("conditionName"); else nextCondition = " "; //unknown计数器更新为0, 当前任务状态+1 if (newBranch) extraUK = taskName + ":" + taskStep; //多余步骤,以后要处理掉 else extraUK = taskName + ":" + (taskStep + 1); extraHP = taskName + "::match-Keyword=" + taskStep; if (newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) extraHP += "," + nextId + "::match-Keyword=null"; extraUK += "-<-" + nextId + ":" + 1; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, String.valueOf(priority) ); jConfigList.add(jConfig); try { keywordOptions += jConfig.getCurPremise().split(",")[0].split("=")[1].split("\\+")[0] + " 或者"; } catch (Exception e) { e.printStackTrace(); } if (branchNode.get("nextPlotId") != null) { adjId = (String) branchNode.get("nextPlotId"); if (visited.get(adjId) == null) { if (id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("nextUnknownText", nextUnknownText); adjNode.put("taskStep", nextTaskStep); if (newBranch) { adjNode.put("priority", priority + 1); adjNode.put("taskName", adjId); } else { adjNode.put("priority", priority); adjNode.put("taskName", taskName); } queue.add(adjNode); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } } } //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP = taskName + "::match-Keyword=" + taskStep, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } /* jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-SmallOrEqual=0", "match", "{unknownReply-2}", " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-BiggerOrEqual=1", "match", "你可以说" + keywordOptions, " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); */ } //unknown时的处理 jConfig = new JConfig(" ", " ", "", "{unknownText-2}", " ", ""); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } //每个结点分为0、1、2三种状态 public void BFSGenerate2(JSONObject root) { String id = (String) root.get("id"), adjId, nextId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, unknownReply1, unknownReply2, nextCondition; 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"); //初始化 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; } else{ if(manyParent){ nextTaskName = id; nextTaskStep = 1; priority++; } else{ nextTaskName = taskName; nextTaskStep = taskStep + 1; } } */ branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; extraHP = id + "::match-Keyword=" + 1; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); if (id2j.get(nextId) != null) nextCondition = (String) id2j.get(nextId).get("conditionName"); else nextCondition = " "; extraUK = id + ":" + "+1-<-" + nextId + ":" + "+1"; //if(newBranch || (revAdjList.get(nextId) != null && revAdjList.get(nextId).size() > 1)) // extraUK += "-<-" + nextId + ":" + 1; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class), "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, "0" ); jConfigList.add(jConfig); try { keywordOptions += jConfig.getCurPremise().split(",")[0].split("=")[1].split("\\+")[0] + " 或者"; } catch (Exception e) { e.printStackTrace(); } if (branchNode.get("nextPlotId") != null) { adjId = (String) branchNode.get("nextPlotId"); if (visited.get(adjId) == null && id2j.get(adjId) != null) { adjNode = id2j.get(adjId); queue.add(adjNode); } } } } //unknown时的处理 jConfig = new JConfig(" ", " ", "", "{unknownText-2}", " ", ""); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } public void BFSGenerate3(JSONObject root, UserInfo userinfo) { String id = (String) root.get("id"), adjId, nextId, parentId, taskName = "", value = "", extraHP = "", extraUK = "", nextTaskName, keywordOptions, conditionName, nextUnknownText, curUnknownText, nextCondition, location, nextLocation, taskLineName, nTaskLineName; boolean newBranch, manyParent; JSONObject node = null, branchNode = null, adjNode = null; JSONArray branch = null; List adjNodeList = null; int priority, taskStep, nextPriority, nextTaskStep, branchSize; root.put("nextUnknownText", varInitialMap.get("unknownText")); root.put("parentId", "null"); Queue queue = new LinkedList(); queue.add(root); while (!queue.isEmpty()) { node = queue.remove(); id = (String) node.get("id"); parentId = (String) node.get("parentId"); location = (String) node.get("place"); conditionName = (String) node.get("conditionName"); curUnknownText = (String) node.get("nextUnknownText"); taskLineName = "任务线_" + location + "进度"; if (!varInitialMap.containsKey(taskLineName)) { conditionDescription.put(taskLineName, taskLineName); varInitialMap.put(taskLineName, ""); } //初始化 newBranch = false; manyParent = false; adjNodeList = adjList.get(id); visited.put(id, true); branch = node.getJSONArray("branch"); branchSize = branch.size(); //当两次出现unknown时给用户一下候选项 keywordOptions = ""; for (int i = 0; i < branchSize; i++) { branchNode = branch.getJSONObject(i); nextId = (String) branchNode.get("nextPlotId"); Map updateKnowledge = JSONObject.toJavaObject(branchNode.getJSONObject("updateKnowledge"), Map.class); nextUnknownText = updateKnowledge.get("unknownText"); if (id2j.get(nextId) != null) { nextCondition = (String) id2j.get(nextId).get("conditionName"); nextLocation = (String) id2j.get(nextId).get("place"); nTaskLineName = "任务线_" + nextLocation + "进度"; } else { nextCondition = " "; nTaskLineName = taskLineName; } extraHP = taskLineName + "::match-Keyword=" + id; extraUK = nTaskLineName + ":" + nextId; jConfig = new JConfig( JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), String.class), JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), String.class), updateKnowledge, "", extraHP, extraUK, "match", (String) branchNode.get("reply"), (String) nextCondition, "0" ); jConfigList.add(jConfig); jConfig = new JConfig( "yaobuyao::match-Keyword=" + location, extraHP, "match", (String) branchNode.get("reply"), (String) nextCondition, "0" ); jConfigList.add(jConfig); try { keywordOptions += jConfig.getCurPremise().split(",")[0].split("=")[1].split("\\+")[0] + " 或者"; } catch (Exception e) { e.printStackTrace(); } if (branchNode.get("nextPlotId") != null) { adjId = (String) branchNode.get("nextPlotId"); if (visited.get(adjId) == null) { if (id2j.get(adjId) != null) { adjNode = id2j.get(adjId); adjNode.put("parentId", id); adjNode.put("nextUnknownText", nextUnknownText); queue.add(adjNode); } else { conditionDescription.put(adjId, "剧情" + adjId + "进度"); varInitialMap.put(adjId, ""); } } } } //对于unknown的特殊处理 if (curUnknownText.equals("")) { jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP = "任务线_" + location + "进度::match-Keyword=" + parentId, "match,return-0,post::all(" + CHITCHAT_URL + ")", " ", " ", "" ); jConfigList.add(jConfig); } /* jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-SmallOrEqual=0", "match", "{unknownReply-2}", " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); jConfig = new JConfig( conditionName + "::match-Keyword=unknown", extraHP + ",unknown_counter::number-BiggerOrEqual=1", "match", "你可以说" + keywordOptions, " ", "unknown_counter:+1" ); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); */ } //unknown时的处理 jConfig = new JConfig(" ", " ", "", "{unknownText-2}", " ", ""); jConfig.setNextStepStatus("return-0"); jConfigList.add(jConfig); } }