package com.qiwu.chatbotbackend.actiongenerate.utils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.base.Joiner; import com.qiwu.chatbotbackend.actiongenerate.entity.ConfigTerm; import com.qiwu.chatbotbackend.actiongenerate.entity.IntervalStatus; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.ConfigTermText; import com.qiwu.chatbotbackend.entity.Premise; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.processing.UserInfoController; import javax.servlet.ServletContext; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static com.qiwu.chatbotbackend.Config.Config.CONFIG_INSERT_ERROR; public class ConfigTermUtils { SqlHelper helper = null; String botAccount = ""; ServletContext context = null; UserInfo userInfo = null; public IntervalStatus intervalStatus = new IntervalStatus(1, ""); public ConfigTermUtils(String botAccount, SqlHelper helper, ServletContext context) throws IOException, SQLException { this.helper = helper; this.context = context; this.botAccount = botAccount; UserInfoController uiController = new UserInfoController(); this.userInfo = uiController.userInfoQuery(botAccount, context); } public ConfigTermUtils(SqlHelper helper){ this.helper = helper; } public void insertConfigTerm(JSONObject parameters) throws IOException { Boolean insertFlag = true; List configList = new ArrayList(); List idList = new ArrayList(); int botId = parameters.getInteger("botId"); int categoryId = parameters.getInteger("categoryId"); //处理特殊字符 ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData")); configList.add(parameters.getJSONObject("configData")); insertFlag = helper.batchInsertConfigByBotId(botId, configList, idList, categoryId); //generateSingleConfigText(); if(insertFlag){ UserInfoController.insertConfig(botAccount, String.valueOf(idList.get(0)), configText.toString() + idList.get(0), context); UserInfoController.writeConfigText(botAccount, userInfo); intervalStatus = new IntervalStatus(1,""); }else{ intervalStatus = new IntervalStatus(0, CONFIG_INSERT_ERROR); } } /** * 数据预处理 * @param configJson */ public ConfigTermText configPreprocessing(JSONObject configJson){ int premiseNum = 0, knowledgeNum = 0, nextStepNum; Premise premise = null; String reply, auxiliaryPriority = "0", nextStepStatus, configName; StringBuffer nextStepText = new StringBuffer(), featureText = new StringBuffer(); JSONObject premiseJson = null, knowledgeJson = null, nextStep; ConfigTermText configTermText = null; List curPremiseList = new ArrayList(); List hisPremiseList = new ArrayList(); Map updateKnowledge = new HashMap(); Map punctReplaceMap = new HashMap(){ { put(",", ","); put("|", ""); put("\r\n", ""); put("\n", ""); put("\t", ""); } }; //解析当前信息的前置条件 premiseNum = configJson.getJSONArray("currentPremiseList").size(); for(int i = 0;i < premiseNum;i++){ premiseJson = configJson.getJSONArray("currentPremiseList").getJSONObject(i); premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"), premiseJson.getString("operator"), premiseJson.getString("keywords")); if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){ premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap)); curPremiseList.add(premise); } premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap)); } //解析已知信息的前置条件 premiseNum = configJson.getJSONArray("historyPremiseList").size(); for(int i = 0;i < premiseNum;i++){ premiseJson = configJson.getJSONArray("historyPremiseList").getJSONObject(i); premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"), premiseJson.getString("operator"), premiseJson.getString("keywords")); if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){ premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap)); hisPremiseList.add(premise); } premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap)); } //获得辅助优先级 if(configJson.get("auxiliaryPriority") != null){ auxiliaryPriority = String.valueOf(configJson.getInteger("auxiliaryPriority")); } //获得回答 reply = textReplace(configJson.getString("reply"), punctReplaceMap); configJson.put("reply", textReplace(configJson.getString("reply"), punctReplaceMap)); //获得更新知识单元 if(configJson.getJSONArray("updateKnowledge") != null) { knowledgeNum = configJson.getJSONArray("updateKnowledge").size(); for (int i = 0; i < knowledgeNum; i++) { knowledgeJson = configJson.getJSONArray("updateKnowledge").getJSONObject(i); updateKnowledge.put(knowledgeJson.getString("variableName"), textReplace(knowledgeJson.getString("keyword"), punctReplaceMap)); knowledgeJson.put("keyword", textReplace(knowledgeJson.getString("keyword"), punctReplaceMap)); } } //多个下一步激活问题的处理 if(configJson.get("nextStepList") != null){ nextStepNum = configJson.getJSONArray("nextStepList").size(); for(int i = 0;i < nextStepNum;i++){ if(i != 0) nextStepText.append(","); nextStep = configJson.getJSONArray("nextStepList").getJSONObject(i); nextStepText.append(nextStep.getString("nextStep")); if(nextStep.get("except") != null && nextStep.getJSONArray("except").size() != 0){ nextStepText.append(":").append(Joiner.on("-").join(nextStep.getJSONArray("except"))); } } } if(curPremiseList.size() != 0 || hisPremiseList.size() != 0) //match表示需要条件匹配 featureText.append("match"); //是否提交表单 if(configJson.getBoolean("doPost")){ featureText.append("post::").append(configJson.getString("postType")); featureText.append("(").append(configJson.getString("postURL")).append(")"); } //判断下一步是选择新的问题,还是留在原来的问题,或是返回上一个问题 if(!configJson.containsKey("nextStepStatus")) featureText.append(",return-0"); else{ nextStepStatus = configJson.getString("nextStepStatus"); switch(nextStepStatus){ case "stay": featureText.append(",return-0"); break; case "back": featureText.append(",return-1"); break; case "return": case "jump": //存在多个下一步 if(configJson.get("nextStepList") != null && configJson.getJSONArray("nextStepList").size() > 1){ featureText.append(",newNext"); } break; case "activate": //下一步激活,且激活不为空 if(configJson.get("nextStepList") != null && !configJson.getJSONArray("nextStepList").getJSONObject(0).getString("nextStep").equals("")){ featureText.append(",activate"); } break; } } configTermText = new ConfigTermText(curPremiseList, hisPremiseList, updateKnowledge, "", "", "", featureText.toString(), reply, nextStepText.toString(), auxiliaryPriority); configName = configJson.getString("configName"); if(configName != null && !configName.equals("")) { configTermText.setConfigName(configName, false); } return configTermText; } public void deleteConfig(JSONObject parameters) throws IOException { Boolean deleteFlag = true; int botId = parameters.getInteger("botId"); int configId = parameters.getInteger("configId"); deleteFlag = helper.deleteConfigByConfigId(botId, configId); if(deleteFlag){ UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context); UserInfoController.writeConfigText(botAccount, userInfo); } } public void updateConfig(JSONObject parameters) throws IOException { Boolean updateFlag = true; int botId = parameters.getInteger("botId"); int categoryId = parameters.getInteger("categoryId"); int configId = parameters.getInteger("configId"); ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData")); ConfigTerm configTerm = helper.queryConfigByConfigId(botId, configId); updateFlag = helper.updateConfig(new ConfigTerm(botId, configId, categoryId, parameters.getJSONObject("configData"))); if(!updateFlag){ if(parameters.getJSONObject("configData").toJSONString().equals(configTerm.getConfigJsonText())){ updateFlag = true; } } if(updateFlag){ //更新配置条目 UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context); UserInfoController.insertConfig(botAccount, String.valueOf(configId), configText.toString() + configId, context); UserInfoController.writeConfigText(botAccount, userInfo); } } public void queryConfig(JSONObject parameters){ int botId = parameters.getInteger("botId"); int categoryId = parameters.getInteger("categoryId"); JSONArray configDataList = new JSONArray(); JSONObject configJson = null; List configTermList = helper.queryConfigByCategoryId(botId, categoryId); for(ConfigTerm configTerm:configTermList){ configJson = JSONObject.parseObject(configTerm.getConfigJsonText()); configJson.put("configId", configTerm.getConfigId()); configJson.put("categoryId", configTerm.getCategoryId()); configDataList.add(configJson); } intervalStatus = new IntervalStatus(1, "", configDataList); } public void querySleepingConfig(JSONObject parameters){ int botId = parameters.getInteger("botId"); JSONArray sleepingConfigs = new JSONArray(); JSONObject configJson = null; List configTermList = helper.querySleepingConfig(botId); for(ConfigTerm configTerm:configTermList){ sleepingConfigs.add(new JSONArray(){ { add(configTerm.getConfigId()); add(configTerm.getConfigName()); } }); } intervalStatus = new IntervalStatus(1, "", sleepingConfigs); } /** * 将文本中的一些特殊字符过滤掉 * @param text * @param replaceMap * @return */ public String textReplace(String text, Map replaceMap){ for(String key:replaceMap.keySet()){ text = text.replace(key, replaceMap.get(key)); } return text; } public void generateSingleConfigText(JSONObject configJson){ } }