package entity; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.HashMap; 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 = ""; private String auxiliaryPriority = "0"; private String status = "waiting"; public void setAuxiliaryPriority(String auxiliaryPriority) { this.auxiliaryPriority = auxiliaryPriority; } 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 JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep) { this.curPremise = curPremise; this.hisPremise = hisPremise; this.feature = feature; this.reply = reply; this.nextStep = nextStep; } public JConfig(List curPremise, List hisPremise, String extraCP, String extraHP, String feature, String reply, String nextStep) { String curPremiseStr = ""; String hisPremiseStr = ""; String extraCurPremise = ""; String extraHisPremise = ""; for(String premise: curPremise) curPremiseStr += "," + premise; for(String premise: hisPremise) hisPremiseStr += "," + premise; if(!extraCP.equals("")) curPremiseStr += "," + extraCP; if(!extraHP.equals("")) hisPremiseStr += "," + extraHP; this.curPremise = curPremiseStr.substring(1, curPremiseStr.length()); this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length()); this.feature = feature; this.reply = reply; this.nextStep = nextStep; } public JConfig(List curPremise, List hisPremise, String extraCP, String extraHP, String feature, String reply, String nextStep, List updateKnowledgeList, String extraUK) { String curPremiseStr = ""; String hisPremiseStr = ""; String extraCurPremise = ""; String extraHisPremise = ""; for(String premise: curPremise) curPremiseStr += "," + premise; for(String premise: hisPremise) hisPremiseStr += "," + premise; if(!extraCP.equals("")) curPremiseStr += "," + extraCP; if(!extraHP.equals("")) hisPremiseStr += "," + extraHP; for(String knowledge:updateKnowledgeList) updateKnowledge += "-<-" + knowledge; updateKnowledge += "-<-" + extraUK; this.curPremise = curPremiseStr.substring(1, curPremiseStr.length()); this.hisPremise = hisPremiseStr.substring(1, hisPremiseStr.length()); this.feature = feature; this.reply = reply; this.nextStep = nextStep; } public JConfig(String curPremise, String hisPremise, String feature, String reply, String nextStep, String updateKnowledge) { this.curPremise = curPremise; this.hisPremise = hisPremise; this.feature = feature; this.updateKnowledge = "-<-" + updateKnowledge; this.reply = reply; this.nextStep = nextStep; } 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){ if(knowledge.equals("")) return; String pair[] = knowledge.split(":"); 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(){ if(!updateKnowledge.equals("")) updateKnowledge = ",updateKnowledge" + updateKnowledge; String text = curPremise + "|" + hisPremise + "|" + feature + updateKnowledge + "|" + "auxiliaryPriority=" + auxiliaryPriority + "|=>|" + reply + "|" + "nextStep=" + nextStep + "|" + "status=" + status + "|" + "configId="; return text; } }