package com.qiwu.chatbotbackend.entity; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class ConfigTermText implements Cloneable{ public static ThreadLocal iterNum=new ThreadLocal() { protected Integer initialValue() { return 0; } }; private int id = 0; private String curPremise = ""; private String hisPremise = ""; private String feature = ""; private String reply = ""; private String nextStep = ""; private String updateKnowledge = ""; private String nextStepStatus = "return"; private String configName = ""; private String auxiliaryPriority = "1"; private String status = "waiting"; static HashMap StatusMap = new HashMap(){ { put("waiting", 0); put("sleeping", 1); } }; public String getFeature() { return feature; } public void setFeature(String feature) { this.feature = feature; } public String getCurPremise() { return curPremise; } public String getNextStep() { return nextStep; } public void setNextStep(String nextStep) { this.nextStep = nextStep; } public String getConfigName() { return configName; } public void setConfigName(String configName, Boolean append) { if(append) { iterNum.set(iterNum.get() + 1); this.configName = configName + "_" + iterNum.get(); }else{ this.configName = configName; } } public int getId() { return id; } public void setId(int id) { this.id = id; } public void setCurPremise(String curPremise) { this.curPremise = curPremise; } public String getHisPremise() { return hisPremise; } public void setHisPremise(String hisPremise) { this.hisPremise = hisPremise; } public String getReply() { return reply; } public void setReply(String reply) { this.reply = reply; } public String getAuxiliaryPriority() { return auxiliaryPriority; } public String getStatus() { return status; } public String getNextStepStatus() { return nextStepStatus; } public String getUpdateKnowledge() { return updateKnowledge; } public void setUpdateKnowledge(String updateKnowledge) { this.updateKnowledge = updateKnowledge; } static Map statusMap = new HashMap() { { put("return", "return"); put("return-1", "back"); put("return-0", "stay"); put("activate", "activate"); put("activate:after", "activate:after"); } }; public void setStatus(String status) { this.status = status; } public void setAuxiliaryPriority(String auxiliaryPriority) { this.auxiliaryPriority = auxiliaryPriority; } public void setNextStepStatus(String nextStepStatus) { this.nextStepStatus = nextStepStatus; } public ConfigTermText(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge) { this.curPremise = curPremise; this.hisPremise = hisPremise; this.feature = feature; if (!updateKnowledge.equals("")) this.updateKnowledge += "updateKnowledge-<-" + updateKnowledge; this.reply = reply; this.nextStep = nextStep; } public ConfigTermText(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge, Map autoJumpUpdateKnowledge) { this.curPremise = curPremise; this.hisPremise = hisPremise; this.feature = feature; this.updateKnowledge = appendKnowledgeStr(autoJumpUpdateKnowledge, updateKnowledge); //if (!updateKnowledge.equals("")) // this.updateKnowledge += "updateKnowledge-<-" + updateKnowledge; this.reply = reply; this.nextStep = nextStep; } public ConfigTermText(List curPremise, List hisPremise, Map updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority) { this.curPremise = appendPremiseStr(curPremise, extraCP); this.hisPremise = appendPremiseStr(hisPremise, extraHP); this.updateKnowledge = appendKnowledgeStr(updateKnowledge, extraUK); this.feature = feature; this.reply = reply; this.nextStep = nextStep; this.auxiliaryPriority = auxiliaryPriority; } public String appendPremiseStr(List premises, String extraPremise) { StringBuffer sb = new StringBuffer(); String premiseStr = "", conditionName; Map nameCount = new HashMap(); for (Premise premise : premises){ conditionName = premise.getConditionName(); if(conditionName.startsWith("npc_") || conditionName.startsWith("goods_") || conditionName.startsWith("globalVariable_") || conditionName.startsWith("map_")) {} else { nameCount.putIfAbsent(conditionName, 0); //nameCount加一 nameCount.put(conditionName, nameCount.get(conditionName) + 1); //在conditionName后添加count if(nameCount.get(conditionName) > 1) { premise.setConditionName(conditionName + nameCount.get(conditionName)); } } sb.append(","); sb.append(premise.getConditionName()); sb.append("::"); sb.append(premise.getCmpType()); sb.append("-"); sb.append(premise.getOperator()); sb.append("="); sb.append(premise.getKeyword()); } if (sb.length() > 0) sb.deleteCharAt(0); //删除第一个逗号 if (!extraPremise.equals("")) { if (sb.length() == 0) sb.append(extraPremise); else sb.append("," + extraPremise); } else if (sb.length() == 0) { sb.append(" "); } premiseStr = sb.toString(); return premiseStr; } public String appendKnowledgeStr(Map knowledge, String extraKL) { StringBuffer sb = new StringBuffer(); String varName = "", varValue = "", knowledgeStr; Iterator iit = knowledge.keySet().iterator(); Boolean delayUpdateFollowerLocation = false; //updateFollowerLocation:更新跟随玩家的NPC的位置,delayUpdateFollowerLocation将更新跟随玩家的角色放在更新答案的最后 String delayUpdateFollowerLocationValue = "", delayUpdateFollowerLocationKey = ""; while (iit.hasNext()) { varName = iit.next(); varValue = knowledge.get(varName); if(varValue.startsWith("updateFollowerLocation")){ delayUpdateFollowerLocation = true; delayUpdateFollowerLocationKey = varName; delayUpdateFollowerLocationValue = varValue; continue; } sb.append("-<-").append(varName).append(":").append(varValue); } if (!extraKL.equals("")) { sb.append("-<-" + extraKL); } if(delayUpdateFollowerLocation){ sb.append("-<-").append(delayUpdateFollowerLocationKey).append(":").append(delayUpdateFollowerLocationValue); } if (sb.length() > 0) knowledgeStr = "updateKnowledge" + sb.toString(); else knowledgeStr = ""; return knowledgeStr; } public void appendJPremise(JSONArray premiseList, String premise, Map conditionDescription) { //System.out.println(premise); String pair1[] = premise.split("=", 2); String key = pair1[0]; String keywords = pair1[1]; String pair2[] = key.split("::"); String conditionName = pair2[0]; String pair3[] = pair2[1].split("-"); String cmpType = pair3[0]; String operator = ""; if (pair3.length > 1) operator = pair3[1]; String question = ""; if (conditionDescription.get(conditionName) != null) question = conditionDescription.get(conditionName); JSONObject premiseJ = new JSONObject(); premiseJ.put("keywords", keywords); premiseJ.put("actionName", conditionName); premiseJ.put("cmpType", cmpType); premiseJ.put("question", question); premiseJ.put("operator", operator); premiseList.add(premiseJ); } public void appendJKnowledge(JSONArray knowledgeList, String knowledge, Map conditionDescription) { String pair[] = knowledge.split(":", 2); if (knowledge.equals("") || pair.length < 2) return; String question = ""; if (conditionDescription.get(pair[0]) != null) question = conditionDescription.get(pair[0]); JSONObject knowledgeJ = new JSONObject(); knowledgeJ.put("variableName", pair[0]); knowledgeJ.put("keyword", pair[1]); knowledgeList.add(knowledgeJ); } public void appendJNextStep(JSONArray nextStepList, String next, Map conditionDescription) { String pair[] = next.split("-"); JSONObject nextStepJ = new JSONObject(); JSONArray exceptJ = new JSONArray(); for (int i = 1; i < pair.length; i++) exceptJ.add(pair[i]); String question = ""; if (conditionDescription.get(pair[0]) != null) question = conditionDescription.get(pair[0]); nextStepJ.put("nextStep", pair[0]); nextStepJ.put("except", exceptJ); nextStepJ.put("nextStepText", question); nextStepList.add(nextStepJ); } public JSONObject toJson(Map conditionDescription) { JSONObject data = new JSONObject(); JSONArray curPremiseList = new JSONArray(); JSONArray hisPremiseList = new JSONArray(); //添加当前对话内容 for (String premise : curPremise.split(",")) { if (!premise.equals("") && !premise.equals(" ")) appendJPremise(curPremiseList, premise, conditionDescription); } data.put("currentPremiseList", curPremiseList); //添加已知信息的内容 for (String premise : hisPremise.split(",")) { if (!premise.equals("") && !premise.equals(" ")) appendJPremise(hisPremiseList, premise, conditionDescription); } data.put("historyPremiseList", hisPremiseList); //添加下一步状态 //添加configName data.put("configName", configName); //添加config状态 data.put("configStatus", statusMap.get(status)); boolean doPost = false; String postURL = "", postType = "all", pair[]; for (String feat : feature.split(",")) { if (feat.startsWith("post")) { doPost = true; pair = feat.substring(0, feat.length() - 1).split("\\(", 2); postURL = pair[1]; postType = pair[0].split("::")[1]; }else if(feat.equals("return-0")){ nextStepStatus = "return-0"; }else if(feat.equals("activate")){ nextStepStatus = "activate"; }else if(feat.equals("activate:after")){ nextStepStatus = "activate:after"; } } data.put("nextStepStatus", statusMap.get(nextStepStatus)); //添加post参数 data.put("doPost", doPost); data.put("postURL", postURL); data.put("postType", postType); //添加回答 data.put("reply", reply); //添加config类别id data.put("categoryId", 0); //添加辅助优先级 data.put("auxiliaryPriority", auxiliaryPriority); //添加更新答案处的信息 JSONArray knowledgeList = new JSONArray(); if (!updateKnowledge.equals("")) { for (String knowledge : updateKnowledge.split("-<-")) { appendJKnowledge(knowledgeList, knowledge, conditionDescription); } } data.put("updateKnowledge", knowledgeList); //添加下一步 JSONArray nextStepList = new JSONArray(); for (String next : nextStep.split(",")) { appendJNextStep(nextStepList, next, conditionDescription); } data.put("nextStepList", nextStepList); return data; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append(curPremise).append("|"); sb.append(hisPremise); sb.append("|").append(feature).append(","); sb.append(updateKnowledge).append(","); //sb.append(nextStepStatus).append( "|"); sb.append( "|"); sb.append("auxiliaryPriority="); sb.append(auxiliaryPriority).append("|").append("configName="); sb.append(configName); sb.append("|=>|").append(reply); sb.append("|").append("nextStep="); sb.append(nextStep).append("|"); sb.append("status=").append(status); sb.append("|").append("configId="); return sb.toString(); } public Object clone() { Object o=null; try{ o= super.clone(); }catch(CloneNotSupportedException e){ System.out.println(e.toString()); } return o; } public static void activateCopy(ConfigTermText configTermText, List configTermTextList, String name){ configTermText.setReply(configTermText.getReply() + "{临时变量reply-2}"); ConfigTermText configTermText2 = (ConfigTermText) configTermText.clone(); if(configTermText2.getUpdateKnowledge().equals("")) configTermText2.setUpdateKnowledge("process-nextStep:var={临时变量condition-2}"); else configTermText2.setUpdateKnowledge(configTermText2.getUpdateKnowledge() + "-<-process-nextStep:var={临时变量condition-2}"); configTermText2.setConfigName(name, true); configTermTextList.add(configTermText2); } public ConfigTermText simpleCopy(ConfigTermText configTermText){ ConfigTermText configTermText2 = (ConfigTermText) configTermText.clone(); return configTermText2; } }