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 JConfig implements Cloneable{ static int iterNum = 0; private int id = 0; private String jCurPremise = ""; private String jHisPremise = ""; private String jFeature = ""; private String jNextStep = ""; private String jReply = ""; private String jAuxiliaryPriority = "0"; public void setjStatus(int jStatus) { this.jStatus = jStatus; } private int jStatus = 0; private String curPremise = ""; private String hisPremise = ""; public String getFeature() { return feature; } public void setFeature(String feature) { this.feature = feature; } private String feature = ""; private String reply = ""; public String getNextStep() { return nextStep; } public void setNextStep(String nextStep) { this.nextStep = nextStep; } private String nextStep = ""; public void setConfigName(String configName, Boolean append) { if(append) { iterNum++; this.configName = configName + iterNum; }else{ this.configName = configName; } } public String getConfigName() { return configName; } private String configName = ""; public String getCurPremise() { return curPremise; } private String auxiliaryPriority = "1"; private String status = "waiting"; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getjCurPremise() { return jCurPremise; } public void setjCurPremise(String jCurPremise) { this.jCurPremise = jCurPremise; } public String getjHisPremise() { return jHisPremise; } public void setjHisPremise(String jHisPremise) { this.jHisPremise = jHisPremise; } public String getjFeature() { return jFeature; } public void setjFeature(String jFeature) { this.jFeature = jFeature; } public String getjNextStep() { return jNextStep; } public void setjNextStep(String jNextStep) { this.jNextStep = jNextStep; } public String getjReply() { return jReply; } public void setjReply(String jReply) { this.jReply = jReply; } public String getjAuxiliaryPriority() { return jAuxiliaryPriority; } public void setjAuxiliaryPriority(String jAuxiliaryPriority) { this.jAuxiliaryPriority = jAuxiliaryPriority; } public int getjStatus() { return jStatus; } 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; } private String updateKnowledge = ""; private String nextStepStatus = "return"; static Map statusMap = new HashMap() { { put("return", "return"); put("return-1", "back"); put("return-0", "stay"); put("activate", "activate"); put("activate:after", "activate:after"); } }; static int querySkipCount = 10; 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 JConfig(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 JConfig(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 extraPM) { StringBuffer sb = new StringBuffer(); String premiseStr = ""; for (String premise : premises) sb.append("," + premise); if (sb.length() > 0) sb.deleteCharAt(0); //删除第一个逗号 if (!extraPM.equals("")) { if (sb.length() == 0) sb.append(extraPM); else sb.append("," + extraPM); } 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; 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", jStatus); 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() { String text = curPremise + "|" + hisPremise + "|" + feature + "," + updateKnowledge + "," + nextStepStatus + "|" + "auxiliaryPriority=" + auxiliaryPriority + "|" + "configName=" + configName + "|=>|" + reply + "|" + "nextStep=" + nextStep + "|" + "status=" + status + "|" + "configId="; return text; } public Object clone() { Object o=null; try{ o= super.clone(); }catch(CloneNotSupportedException e){ System.out.println(e.toString()); } return o; } public static void activateCopy(JConfig jConfig, List jConfigList, String name){ jConfig.setReply(jConfig.getReply() + "{临时变量reply-2}"); JConfig jConfig2 = (JConfig) jConfig.clone(); // if(jConfig.getHisPremise().equals(" ")) // jConfig.setHisPremise("临时变量condition::match-Include=+null"); // else // jConfig.setHisPremise(jConfig.getjHisPremise() + ",临时变量condition::match-Include=+null"); // jConfig.setConfigName(name, true); if(jConfig2.getUpdateKnowledge().equals("")) jConfig2.setUpdateKnowledge("process-nextStep:var={临时变量condition-2}"); else jConfig2.setUpdateKnowledge(jConfig2.getUpdateKnowledge() + "-<-process-nextStep:var={临时变量condition-2}"); jConfig2.setConfigName(name, true); //jConfigList.add(jConfig); jConfigList.add(jConfig2); } public static JConfig simpleCopy(JConfig jConfig){ JConfig jConfig2 = (JConfig) jConfig.clone(); return jConfig2; } }