package 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 { private int id = 0; private String jCurPremise = ""; private String jHisPremise = ""; private String jFeature = ""; private String jNextStep = ""; private String jReply = ""; private String jAuxiliaryPriority = "0"; private String jStatus = "0"; private String curPremise = ""; private String hisPremise = ""; private String feature = ""; private String reply = ""; private String nextStep = ""; public String getCurPremise() { return curPremise; } private String auxiliaryPriority = "0"; private String status = "waiting"; private String updateKnowledge = ""; private String nextStepStatus = "return"; Map statusMap = new HashMap() { { put("return", "return"); put("return-1", "back"); put("return-0", "stay"); put("activate", "activate"); } }; 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; 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(); while (iit.hasNext()) { varName = iit.next(); varValue = knowledge.get(varName); sb.append("-<-" + varName + ":" + varValue); } if (!extraKL.equals("")) { sb.append("-<-" + extraKL); } 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(":"); 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); //添加下一步状态 data.put("nextStepStatus", statusMap.get(nextStepStatus)); //添加configName data.put("configName", ""); //添加config状态 data.put("configStatus", 0); 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]; } } //添加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 + "|=>|" + reply + "|" + "nextStep=" + nextStep + "|" + "status=" + status + "|" + "configId="; return text; } }