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){ String pair1[] = premise.split("="); 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]; JSONObject premiseJ = new JSONObject(); premiseJ.put("keywords", keywords); premiseJ.put("actionName", conditionName); premiseJ.put("cmpType", cmpType); premiseJ.put("question", "不知"); premiseJ.put("operator", operator); premiseList.add(premiseJ); } public void appendJKnowledge(JSONArray knowledgeList, String knowledge){ String pair[] = knowledge.split(":"); if(knowledge.equals("") || pair.length < 2) return; JSONObject knowledgeJ = new JSONObject(); knowledgeJ.put("variableName", pair[0]); knowledgeJ.put("keyword", pair[1]); knowledgeList.add(knowledgeJ); } public void appendJNextStep(JSONArray nextStepList, String next){ String pair[] = next.split("-"); JSONObject nextStepJ = new JSONObject(); JSONArray exceptJ = new JSONArray(); for(int i = 1;i < pair.length;i++) exceptJ.add(pair[i]); nextStepJ.put("nextStep", pair[0]); nextStepJ.put("except", exceptJ); nextStepList.add(nextStepJ); } public JSONObject toJson(){ 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); } data.put("currentPremiseList", curPremiseList); //添加已知信息的内容 for(String premise:hisPremise.split(",")){ if(!premise.equals("") && !premise.equals(" ")) appendJPremise(hisPremiseList, premise); } data.put("historyPremiseList", hisPremiseList); //添加下一步状态 data.put("nextStepStatus", statusMap.get(nextStepStatus)); //添加configName data.put("configName", ""); //添加config状态 data.put("configStatus", 0); //添加post参数 data.put("doPost", false); data.put("postURL", ""); data.put("postType", "all"); //添加回答 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); } data.put("updateKnowledge", knowledgeList); } //添加下一步 JSONArray nextStepList = new JSONArray(); for(String next: nextStep.split(",")){ appendJNextStep(nextStepList, next); } 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; } }