diff --git a/src/main/java/com/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java b/src/main/java/com/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java index 8686840..602d9af 100644 --- a/src/main/java/com/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java +++ b/src/main/java/com/qiwu/chatbotbackend/actiongenerate/DB/EngineUtils.java @@ -1,185 +1,204 @@ -package com.qiwu.chatbotbackend.actiongenerate.DB; - -import com.google.common.base.Joiner; -import com.qiwu.chatbotbackend.actiongenerate.entity.EngineUnit; -import com.qiwu.chatbotbackend.db.C3p0Utils; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class EngineUtils { - - private Connection conn = null; - - - public EngineUtils() throws SQLException { - conn = C3p0Utils.getEngineConnection(); - } - - - public void destroy() { - if (conn != null) { - try { - conn.close(); - } catch (Exception e) { - e.printStackTrace(); - } - - } - } - - - - public String queryKeywordUnitText(int botId){ - String sql = "select text from units where user_id = ? and name = 'keywords'"; - String keywordText = ""; - try{ - PreparedStatement ps = conn.prepareStatement(sql); - ps.setInt(1,botId); - ResultSet rs = ps.executeQuery(); - if(rs.next()){ - keywordText = (rs.getString("text")); - } - - ps.close(); - - }catch(Exception e){ - e.printStackTrace(); - } - return keywordText; - } - - public Boolean updateKeywordUnitText(int botId, String keywordText){ - Boolean b = true; - String sql = "update units set text = ? where user_id = ? and name = 'keywords'"; - try{ - PreparedStatement ps = conn.prepareStatement(sql); - ps.setString(1, keywordText); - ps.setInt(2,botId); - - b = ps.executeUpdate() == 1; - ps.close(); - - - - }catch(Exception e){ - e.printStackTrace(); - } - return b; - } - - - - public Boolean updateWelcomeText(int botId, String welcome){ - boolean b; - - try { - String sql = "update users set welcome = ? where id=?"; - PreparedStatement ps1 = conn.prepareStatement(sql); - ps1.setString(1, welcome + "#welcome"); - ps1.setInt(2, botId); - - - b = ps1.executeUpdate() == 1; - ps1.close(); - - return b; - - } catch (SQLException e) { - e.printStackTrace(); - return false; - } - } - - - - public Map queryConditionPatternText(int botId, List patternRelationList){ - String sql = "select user_id, text from `users` join `units` on users.id = units.user_id where users.username in ('" + Joiner.on("','").join(patternRelationList)+ "') and units.name = 'actions'"; - String keywordText = ""; - Map patternTextMap = new HashMap(); - try{ - PreparedStatement ps = conn.prepareStatement(sql); - //ps.setString(1, Joiner.on(",").join(patternRelationList)); - ResultSet rs = ps.executeQuery(); - - - - while(rs.next()){ - patternTextMap.put(rs.getInt("user_id"), rs.getString("text")); - } - - ps.close(); - rs.close(); - - }catch(Exception e){ - e.printStackTrace(); - } - return patternTextMap; - } - - public Map queryUnitsByIdList(List idList){ - String sql = "select user_id, name, text from units where user_id in (" + Joiner.on(",").join(idList) + ") and name in ('actions','keywords')"; - - Integer id; - Map id2unit= new HashMap(); - try{ - PreparedStatement ps = conn.prepareStatement(sql); - //ps.setArray(1, idList.toArray()); - ResultSet rs = ps.executeQuery(); - - while(rs.next()){ - id = rs.getInt("user_id"); - if(id2unit.get(id) == null){ - id2unit.put(id, new EngineUnit()); - } - switch(rs.getString("name")){ - case "actions": - id2unit.get(id).setActions(rs.getString("text")); - break; - case "keywords": - id2unit.get(id).setKeywords(rs.getString("text")); - break; - } - } - - ps.close(); - rs.close(); - - }catch(Exception e){ - e.printStackTrace(); - } - return id2unit; - } - - - public String queryBotAccountByBotId(int botId){ - String sql = "select username from users where id= ?"; - String botAccount = ""; - try { - PreparedStatement ps = conn.prepareStatement(sql); - ps.setInt(1, botId); - ResultSet rs = ps.executeQuery(); - if (rs.next()) - botAccount = rs.getString("username"); - - ps.close(); - rs.close(); - conn.close(); - - } catch (Exception e) { - e.printStackTrace(); - } - - return botAccount; - } - - - - - -} +package com.qiwu.chatbotbackend.actiongenerate.DB; + +import com.google.common.base.Joiner; +import com.qiwu.chatbotbackend.actiongenerate.entity.EngineUnit; +import com.qiwu.chatbotbackend.db.C3p0Utils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class EngineUtils { + + private Connection conn = null; + + + public EngineUtils() throws SQLException { + conn = C3p0Utils.getEngineConnection(); + } + + + public void destroy() { + if (conn != null) { + try { + conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + + + public String queryKeywordUnitText(int botId){ + String sql = "select text from units where user_id = ? and name = 'keywords'"; + String keywordText = ""; + try{ + PreparedStatement ps = conn.prepareStatement(sql); + ps.setInt(1,botId); + ResultSet rs = ps.executeQuery(); + if(rs.next()){ + keywordText = (rs.getString("text")); + } + + ps.close(); + + }catch(Exception e){ + e.printStackTrace(); + } + return keywordText; + } + + public Boolean updateKeywordUnitText(int botId, String keywordText){ + Boolean b = true; + String sql = "update units set text = ? where user_id = ? and name = 'keywords'"; + try{ + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, keywordText); + ps.setInt(2,botId); + + b = ps.executeUpdate() == 1; + ps.close(); + + + + }catch(Exception e){ + e.printStackTrace(); + } + return b; + } + + + + public Boolean updateWelcomeText(int botId, String welcome){ + boolean b; + + try { + String sql = "update users set welcome = ? where id=?"; + PreparedStatement ps1 = conn.prepareStatement(sql); + ps1.setString(1, welcome + "#welcome"); + ps1.setInt(2, botId); + + + b = ps1.executeUpdate() == 1; + ps1.close(); + + return b; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + + + public Map queryConditionPatternText(int botId, List patternRelationList){ + String sql = "select user_id, text from `users` join `units` on users.id = units.user_id where users.username in ('" + Joiner.on("','").join(patternRelationList)+ "') and units.name = 'actions'"; + String keywordText = ""; + Map patternTextMap = new HashMap(); + try{ + PreparedStatement ps = conn.prepareStatement(sql); + //ps.setString(1, Joiner.on(",").join(patternRelationList)); + ResultSet rs = ps.executeQuery(); + + + + while(rs.next()){ + patternTextMap.put(rs.getInt("user_id"), rs.getString("text")); + } + + ps.close(); + rs.close(); + + }catch(Exception e){ + e.printStackTrace(); + } + return patternTextMap; + } + + public Map queryUnitsByIdList(List idList){ + String sql = "select user_id, name, text from units where user_id in (" + Joiner.on(",").join(idList) + ") and name in ('actions','keywords')"; + + Integer id; + Map id2unit= new HashMap(); + try{ + PreparedStatement ps = conn.prepareStatement(sql); + //ps.setArray(1, idList.toArray()); + ResultSet rs = ps.executeQuery(); + + while(rs.next()){ + id = rs.getInt("user_id"); + if(id2unit.get(id) == null){ + id2unit.put(id, new EngineUnit()); + } + switch(rs.getString("name")){ + case "actions": + id2unit.get(id).setActions(rs.getString("text")); + break; + case "keywords": + id2unit.get(id).setKeywords(rs.getString("text")); + break; + } + } + + ps.close(); + rs.close(); + + }catch(Exception e){ + e.printStackTrace(); + } + return id2unit; + } + + + public String queryBotAccountByBotId(int botId){ + String sql = "select username from users where id= ?"; + String botAccount = ""; + try { + PreparedStatement ps = conn.prepareStatement(sql); + ps.setInt(1, botId); + ResultSet rs = ps.executeQuery(); + if (rs.next()) + botAccount = rs.getString("username"); + + ps.close(); + rs.close(); + conn.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + + return botAccount; + } + + public String queryBotNickNameByName(String userName){ + String sql = "select nickname from users where username= ?"; + String nickname = ""; + try { + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, userName); + ResultSet rs = ps.executeQuery(); + if (rs.next()) + nickname = rs.getString("nickname"); + + ps.close(); + rs.close(); + conn.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + + return nickname; + } + + + +} diff --git a/src/main/java/com/qiwu/chatbotbackend/actiongenerate/utils/ActionUtils.java b/src/main/java/com/qiwu/chatbotbackend/actiongenerate/utils/ActionUtils.java index 6a737f7..8d37056 100644 --- a/src/main/java/com/qiwu/chatbotbackend/actiongenerate/utils/ActionUtils.java +++ b/src/main/java/com/qiwu/chatbotbackend/actiongenerate/utils/ActionUtils.java @@ -846,19 +846,19 @@ public class ActionUtils { conditionExtraKeywords.putIfAbsent(DEFAULT_CONDITION + "_head", new HashSet()); conditionExtraKeywords.putIfAbsent(DEFAULT_CONDITION + "_tail", new HashSet()); String condTemple = patternConditionJson.toJSONString(); - - for(int i = 0;i < 100;i++){ + + int i = 0; + boolean beEnd = false; + while(true) { //给condition的名字添加后缀,因为游戏中可能用到condition的复制,但值域和名字不一样 if(i == 0) postfix = ""; else postfix = "_" + String.valueOf(i); - - activeFlag = "0"; conditionJson = (JSONObject) JSON.parse(condTemple); -// conditionJson = (JSONObject) patternConditionJson.clone(); - conditionName = conditionJson.getString("conditionName") + postfix; + String originName = conditionJson.getString("conditionName"); + conditionName = originName + postfix; conditionName2 = conditionName + "_indep"; if(conditionExtraKeywords.containsKey(conditionName + "_tail")) { @@ -868,13 +868,20 @@ public class ActionUtils { conditionJson.put("conditionName", conditionName2); conditionAppendKeyword2(conditionName2, conditionJson, conditionExtraKeywords, macroAppendMap); conditionName = conditionName2; - } else { + } else if(i>1 && !conditionExtraKeywords.containsKey(originName + "_tail")) { + conditionName = originName; + beEnd = true; + } else if(i == 0) { + ++i; continue; + } else { + break; } if(conditionJson.getBoolean("active") && !conditionName.equals(startCondition)) conditionJson.put("active", false); + activeFlag = "0"; actionText = generateSingleActionText(conditionName, conditionJson); if(startCondition.equals(conditionName)) { conditionJson.put("active", true); @@ -894,6 +901,9 @@ public class ActionUtils { add(finalRelationName); } }); + ++i; + if(beEnd == true) + break; } } parameters.put("conditionData", conditionData); diff --git a/src/main/java/com/qiwu/chatbotbackend/processing/SimpleUtil.java b/src/main/java/com/qiwu/chatbotbackend/processing/SimpleUtil.java index d1cfc67..06888d6 100644 --- a/src/main/java/com/qiwu/chatbotbackend/processing/SimpleUtil.java +++ b/src/main/java/com/qiwu/chatbotbackend/processing/SimpleUtil.java @@ -1,270 +1,272 @@ -package com.qiwu.chatbotbackend.processing; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.qiwu.chatbotbackend.entity.Solution; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; - -import javax.servlet.http.HttpServletRequest; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; - - -public class SimpleUtil { - - /** - * 将后台返回的数据转成json格式 - */ - public static String toJson(Solution ans) { - JSONObject jobj = new JSONObject(); - jobj.put("solution", ans.getAnswer()); - jobj.put("status", ans.getStatus()); - jobj.put("process", ans.getNext()); - return jobj.toString(); - } - - public static String toJson(String returnStr) { - JSONObject jobj = new JSONObject(); - jobj.put("returnString", returnStr); - return jobj.toString(); - } - - /** - * 当请求中的数据是json格式时,该函数用于提取request中的json数据 - * - * @param request - * @return json字符串 - */ - public static String readJSONString(HttpServletRequest request) { - StringBuffer json = new StringBuffer(); - String line = null; - - try { - BufferedReader reader = request.getReader(); - while ((line = reader.readLine()) != null) { - json.append(line); - } - } catch (IOException e) { - e.printStackTrace(); - } - - return json.toString(); - } - - - public static JSONObject getMethod(String url, String auth){ - CloseableHttpClient client = HttpClients.createDefault(); - CloseableHttpResponse resp = null; - String respContent = null; - String keyName = null; - JSONObject jobj = new JSONObject(); - - - try { - HttpGet httpGet = new HttpGet(url); - - if(auth != null){ - httpGet.setHeader("Authorization", auth); - } - resp = client.execute(httpGet); - if (resp.getStatusLine().getStatusCode() == 200) { - HttpEntity he = resp.getEntity(); - respContent = EntityUtils.toString(he, "UTF-8"); - jobj = JSONObject.parseObject(respContent); - jobj.put("get_status", 1); - jobj.put("status_code", 200); - - } else { - jobj.put("get_status", 0); - jobj.put("status_code", resp.getStatusLine().getStatusCode()); - jobj.put("parameter", "接收请求的程序出现异常"); - } - } catch (Exception e) { - e.printStackTrace(); - jobj.put("status_code", 0); - jobj.put("get_status", 0); - - }finally { - try { - if (resp != null) { - resp.close(); - } - if (client != null) { - client.close(); - } - }catch(IOException e){ - e.printStackTrace(); - } - } - - return jobj; - } - - - - /** - * 通过get方法传递参数时需要对参数的编码进行转换 - */ - public static String convert2Utf8(String s) { - if (s == null) return null; - try { - s = new String(s.getBytes("iso-8859-1"), "utf-8"); - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return s; - } - - public static long transferToMillisecond(String keyword) throws ParseException { - long time; - - System.out.println(keyword); - - String timeSeries[] = {"时", "分", "秒"}; - for (String f : timeSeries) { - if (!keyword.contains(f)) - keyword += "0" + f; - } - - SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); - time = format.parse(keyword).getTime(); - - return time; - } - - public static String transferToStandardTime(long time) throws ParseException { - - SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); - - Date date = new Date(time); - return format.format(date); - - } - - public static boolean isInteger(String str){ - return true; - } - - - - public static JSONObject postJSON(String url, JSON data, String auth) throws UnsupportedEncodingException { - - JSONObject jobj = new JSONObject(); - - - - - HttpPost httpPost = new HttpPost(url); - CloseableHttpClient client = HttpClients.createDefault(); - String respContent = null; - String keyName = null; - - - StringEntity entity = new StringEntity(data.toJSONString(), "utf-8"); - - entity.setContentType("application/json; charset=utf-8"); - httpPost.setEntity(entity); - - if(auth != null){ - httpPost.setHeader("Authorization", auth); - } - - try { - HttpResponse resp = client.execute(httpPost); - jobj.put("status_code", resp.getStatusLine().getStatusCode()); - if (resp.getStatusLine().getStatusCode() == 200) { - HttpEntity he = resp.getEntity(); - respContent = EntityUtils.toString(he, "UTF-8"); - jobj = JSONObject.parseObject(respContent); - jobj.put("post_status", 1); - jobj.put("status_code", 200); - } else { - jobj.put("post_status", 0); - jobj.put("status_code", resp.getStatusLine().getStatusCode()); - jobj.put("parameter", "接收请求的程序出现异常:" + resp.getStatusLine().getReasonPhrase()); - } - - } catch (Exception e) { - jobj.put("status_code", 0); - jobj.put("post_status", 0); - e.printStackTrace(); - } - - return jobj; - - } - - - - - - public static JSONObject postFORM(String url, List pairList) throws UnsupportedEncodingException { - - - HttpPost httpPost = new HttpPost(url); - CloseableHttpClient client = HttpClients.createDefault(); - String respContent = null; - String keyName = null; - - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList, "utf-8"); - entity.setContentType("application/x-www-form-urlencoded"); - httpPost.setEntity(entity); - JSONObject jobj = new JSONObject(); - - try { - System.out.println("url为:" + url); - HttpResponse resp = client.execute(httpPost); - if (resp.getStatusLine().getStatusCode() == 200) { - HttpEntity he = resp.getEntity(); - respContent = EntityUtils.toString(he, "UTF-8"); - jobj = JSONObject.parseObject(respContent); - jobj.put("post_status", 1); - - } else { - jobj.put("post_status", 0); - jobj.put("error_message", "目标程序处理失败:" + url + ", 错误码:" + resp.getStatusLine().getStatusCode()); - } - } catch (Exception e) { - jobj.put("error_message", "提交表单时出错:" + e.getMessage()); - jobj.put("post_status", 0); - } - - return jobj; - - } - - public static String randStr(String strList[]){ - String reply; - int randNum = (int) (Math.random() * strList.length); - reply = strList[randNum]; - - return reply; - } - - public static String randStr(List strList){ - String reply; - - int randNum = (int) (Math.random() * strList.size()); - reply = strList.get(randNum); - - return reply; - } - - +package com.qiwu.chatbotbackend.processing; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.qiwu.chatbotbackend.entity.Solution; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; + +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + + +public class SimpleUtil { + + /** + * 将后台返回的数据转成json格式 + */ + public static String toJson(Solution ans) { + JSONObject jobj = new JSONObject(); + jobj.put("solution", ans.getAnswer()); + jobj.put("status", ans.getStatus()); + jobj.put("process", ans.getNext()); + return jobj.toString(); + } + + public static String toJson(String returnStr) { + JSONObject jobj = new JSONObject(); + jobj.put("returnString", returnStr); + return jobj.toString(); + } + + /** + * 当请求中的数据是json格式时,该函数用于提取request中的json数据 + * + * @param request + * @return json字符串 + */ + public static String readJSONString(HttpServletRequest request) { + StringBuffer json = new StringBuffer(); + String line = null; + + try { + BufferedReader reader = request.getReader(); + while ((line = reader.readLine()) != null) { + json.append(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + + return json.toString(); + } + + + public static JSONObject getMethod(String url, String auth){ + CloseableHttpClient client = HttpClients.createDefault(); + CloseableHttpResponse resp = null; + String respContent = null; + String keyName = null; + JSONObject jobj = new JSONObject(); + + + try { + HttpGet httpGet = new HttpGet(url); + + if(auth != null){ + httpGet.setHeader("Authorization", auth); + } + resp = client.execute(httpGet); + if (resp.getStatusLine().getStatusCode() == 200) { + HttpEntity he = resp.getEntity(); + respContent = EntityUtils.toString(he, "UTF-8"); + jobj = JSONObject.parseObject(respContent); + jobj.put("get_status", 1); + jobj.put("status_code", 200); + + } else { + jobj.put("get_status", 0); + jobj.put("status_code", resp.getStatusLine().getStatusCode()); + jobj.put("parameter", "接收请求的程序出现异常"); + } + } catch (Exception e) { + e.printStackTrace(); + jobj.put("status_code", 0); + jobj.put("get_status", 0); + + }finally { + try { + if (resp != null) { + resp.close(); + } + if (client != null) { + client.close(); + } + }catch(IOException e){ + e.printStackTrace(); + } + } + + return jobj; + } + + + + /** + * 通过get方法传递参数时需要对参数的编码进行转换 + */ + public static String convert2Utf8(String s) { + if (s == null) return null; + try { + s = new String(s.getBytes("iso-8859-1"), "utf-8"); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return s; + } + + public static long transferToMillisecond(String keyword) throws ParseException { + long time; + + System.out.println(keyword); + + String timeSeries[] = {"时", "分", "秒"}; + for (String f : timeSeries) { + if (!keyword.contains(f)) + keyword += "0" + f; + } + + SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); + time = format.parse(keyword).getTime(); + + return time; + } + + public static String transferToStandardTime(long time) throws ParseException { + + SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); + + Date date = new Date(time); + return format.format(date); + + } + + public static boolean isInteger(String str){ + return true; + } + + + + public static JSONObject postJSON(String url, JSON data, String auth) throws UnsupportedEncodingException { + + JSONObject jobj = new JSONObject(); + + + + + HttpPost httpPost = new HttpPost(url); + CloseableHttpClient client = HttpClients.createDefault(); + String respContent = null; + String keyName = null; + + + StringEntity entity = new StringEntity(data.toJSONString(), "utf-8"); + + entity.setContentType("application/json; charset=utf-8"); + httpPost.setEntity(entity); + + if(auth != null){ + httpPost.setHeader("Authorization", auth); + } + + try { + HttpResponse resp = client.execute(httpPost); + jobj.put("status_code", resp.getStatusLine().getStatusCode()); + if (resp.getStatusLine().getStatusCode() == 200) { + HttpEntity he = resp.getEntity(); + respContent = EntityUtils.toString(he, "UTF-8"); + jobj = JSONObject.parseObject(respContent); + jobj.put("post_status", 1); + jobj.put("status_code", 200); + } else { + jobj.put("post_status", 0); + jobj.put("status_code", resp.getStatusLine().getStatusCode()); + jobj.put("parameter", "接收请求的程序出现异常:" + resp.getStatusLine().getReasonPhrase()); + } + + } catch (Exception e) { + jobj.put("status_code", 0); + jobj.put("post_status", 0); + System.out.println("respContent:"); + System.out.println(respContent); + e.printStackTrace(); + } + + return jobj; + + } + + + + + + public static JSONObject postFORM(String url, List pairList) throws UnsupportedEncodingException { + + + HttpPost httpPost = new HttpPost(url); + CloseableHttpClient client = HttpClients.createDefault(); + String respContent = null; + String keyName = null; + + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList, "utf-8"); + entity.setContentType("application/x-www-form-urlencoded"); + httpPost.setEntity(entity); + JSONObject jobj = new JSONObject(); + + try { + System.out.println("url为:" + url); + HttpResponse resp = client.execute(httpPost); + if (resp.getStatusLine().getStatusCode() == 200) { + HttpEntity he = resp.getEntity(); + respContent = EntityUtils.toString(he, "UTF-8"); + jobj = JSONObject.parseObject(respContent); + jobj.put("post_status", 1); + + } else { + jobj.put("post_status", 0); + jobj.put("error_message", "目标程序处理失败:" + url + ", 错误码:" + resp.getStatusLine().getStatusCode()); + } + } catch (Exception e) { + jobj.put("error_message", "提交表单时出错:" + e.getMessage()); + jobj.put("post_status", 0); + } + + return jobj; + + } + + public static String randStr(String strList[]){ + String reply; + int randNum = (int) (Math.random() * strList.length); + reply = strList[randNum]; + + return reply; + } + + public static String randStr(List strList){ + String reply; + + int randNum = (int) (Math.random() * strList.size()); + reply = strList.get(randNum); + + return reply; + } + + } \ No newline at end of file diff --git a/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java b/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java index 5d60f05..f04a0a2 100644 --- a/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java +++ b/src/main/java/com/qiwu/chatbotbackend/servlet/GetAnswerServlet.java @@ -20,6 +20,8 @@ import java.io.OutputStream; import java.text.ParseException; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import static com.qiwu.chatbotbackend.Config.Config.*; import static com.qiwu.chatbotbackend.processing.ChatCacheController.COMBINE_PUNC2; @@ -30,28 +32,37 @@ public class GetAnswerServlet extends HttpServlet { /** * GetAnswerServlet就是处理post请求并返回答案的入口 */ - - - //String LOG_PATH = "/home/hxw/source/logs/"; - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); - Map receiveParamters = new HashMap<>(request.getParameterMap()); + Map requestParamters = new HashMap<>(request.getParameterMap()); + Map receiveParamters = new HashMap<>(); + Set> entrySet = requestParamters.entrySet(); + String tmpStr; + for(Map.Entry entry : entrySet) { + if(entry.getValue().length > 0) { + tmpStr = entry.getValue()[0]; + if(tmpStr == null || tmpStr.equals("")) + continue; + tmpStr = tmpStr.replace("qiwu", "我"); + //tmpStr = tmpStr.replace("qiwu", "你"); + receiveParamters.put(entry.getKey(), tmpStr); + } + } + String botName = receiveParamters.get("bot_name"); + Map curInfoPoint = new HashMap(); - Solution bestMatch = null; BasicBotUtil botUtil = null; Action ansAction = null; boolean debugFlag = false; String ansJ = "", return_str = "", state = "", chatKey = "", botType = "", userKey = "", randKey = ""; - String botName = receiveParamters.get("bot_name")[0]; OutputStream out = response.getOutputStream(); ServletContext context = this.getServletContext(); //如果有client_id,会用@分隔 - chatKey = receiveParamters.get("chat_key")[0]; - String chatKeys[] = receiveParamters.get("chat_key")[0].split(COMBINE_PUNC2); + chatKey = receiveParamters.get("chat_key"); + String chatKeys[] = receiveParamters.get("chat_key").split(COMBINE_PUNC2); if(chatKeys.length == 1){ userKey = chatKeys[0]; randKey = chatKeys[0]; @@ -60,23 +71,19 @@ public class GetAnswerServlet extends HttpServlet { randKey = chatKeys[1]; } - receiveParamters.put("userKey", new String[]{userKey}); - receiveParamters.put("randKey", new String[]{randKey}); - + receiveParamters.put("userKey", userKey); + receiveParamters.put("randKey", randKey); try { LogWriter logWriter = new LogWriter(LOG_PATH, chatKey); - UserInfo userinfo = null; UserInfoController uiController = new UserInfoController(); logWriter.record("query bot config", 0); userinfo = uiController.userInfoQuery(request.getParameter("bot_name"), context); - - if (userinfo != null) { //获得用户历史信息 ChatCache chatCache = null; @@ -96,16 +103,12 @@ public class GetAnswerServlet extends HttpServlet { if (chatCache == null) { throw (new Exception("chatCache初始化失败")); } - - + /** * 如果读档成功,则返回存档前的状态(包括最后一个condition和最后的回答) * 如果读档失败,则开始新的游戏,读档失败的判断标准是除了初始化数据外没有其他任何数据 */ - //historyInfoPoint = chatCache.getInfoPoint(); - - switch (botType) { case "game": WorldController worldController = new WorldController(logWriter, userinfo); diff --git a/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java b/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java index d0e097a..0ce6fdb 100644 --- a/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java +++ b/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java @@ -35,7 +35,7 @@ public class BasicBotUtil { this.chatCache = chatCache; } - public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { + public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { return infoPointPreprocessing(receiveParameters, context, true); @@ -51,7 +51,7 @@ public class BasicBotUtil { * @return * @throws IOException */ - public boolean infoPointPreprocessing(Map receiveParameters, ServletContext context, Boolean setUnknown) throws IOException { + public boolean infoPointPreprocessing(Map receiveParameters, ServletContext context, Boolean setUnknown) throws IOException { String return_str = "", k, botType = ""; String receiveParameterStr = ""; @@ -75,18 +75,15 @@ public class BasicBotUtil { receiveParameterStr+= "-----------------------------------------识别数据列表-------------------------------------------
"; while (iit.hasNext()) { k = iit.next(); - // receiveParameterStr += k + ": "; - curInfoPoint.put(k, receiveParameters.get(k)[0]); + curInfoPoint.put(k, receiveParameters.get(k)); if (k.equals("debug_mode") || k.equals("debug_rule")) debugFlag = true; - String values[] = receiveParameters.get(k); - - receiveParameterStr += values[0] + " " + "
"; - - if (values[0].equals(":")) { + String values = receiveParameters.get(k); + receiveParameterStr += values + " " + "
"; + if (values.equals(":")) { curInfoPoint.put(k, ""); } } diff --git a/src/main/java/com/qiwu/chatbotbackend/task/utils/GameBotUtil.java b/src/main/java/com/qiwu/chatbotbackend/task/utils/GameBotUtil.java index 19fdaee..534d3cc 100644 --- a/src/main/java/com/qiwu/chatbotbackend/task/utils/GameBotUtil.java +++ b/src/main/java/com/qiwu/chatbotbackend/task/utils/GameBotUtil.java @@ -44,7 +44,7 @@ public class GameBotUtil extends BasicBotUtil{ * @throws IOException */ @Override - public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { + public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { Boolean debugFlag = super.infoPointPreprocessing(receiveParameters, context, false); //将要去的地点的别名替换为真名