package com.smtaiserver.smtaiserver.javaai.jsonflow.node; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.ibatis.ognl.Ognl; import com.smtaiserver.smtaiserver.javaai.SMTJavaAIError; import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowExecArg; import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowManager; import com.smtaiserver.smtaiserver.javaai.jsonflow.core.SMTJsonFlowNode; import com.smtservlet.util.Json; import com.smtservlet.util.SMTStatic; public class SMTJsonFlowNodeCondJson extends SMTJsonFlowNode { protected enum SMTJsonFlowNodeCondType { AND, OR } protected enum SMTJsonFlowNodeCondOP { include, notInclude, empty, notEmpty, startWith, notStartWith, endWith, notEndWith, eq, neq, gt, lt, gte, lte } protected static class SMTJsonFlowNodeCondExpr { public SMTJsonFlowNodeCondOP _op; public Object _leftExpr; public Object _righExpr; public SMTJsonFlowNodeCondExpr(Json jsonCondExpr) throws Exception { _op = SMTJsonFlowNodeCondOP.valueOf(jsonCondExpr.getJson("comparison_operation").asString()); _leftExpr = Ognl.parseExpression(jsonCondExpr.safeGetStr("left_value", "null")); _righExpr = Ognl.parseExpression(jsonCondExpr.safeGetStr("right_value", "null")); } public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg, boolean[] r_eq) throws Exception { OgnlRecMap ognlEnv = new OgnlRecMap(execArg); Object leftValue = Ognl.getValue(_leftExpr, ognlEnv); Object rightValue = Ognl.getValue(_righExpr, ognlEnv); // 检查非大小判断 switch(_op) { case include: return checkInclude(leftValue, rightValue, true, r_eq); case notInclude: return checkInclude(leftValue, rightValue, false, r_eq); case empty: return checkEmpty(leftValue, true, r_eq); case notEmpty: return checkEmpty(leftValue, false, r_eq); case startWith: return checkStartWith(leftValue, rightValue, true, r_eq); case notStartWith: return checkStartWith(leftValue, rightValue, false, r_eq); case endWith: return checkEndWith(leftValue, rightValue, true, r_eq); case notEndWith: return checkEndWith(leftValue, rightValue, false, r_eq); default: break; } // 检查大小判断 int[] result = new int[1]; SMTJavaAIError error = compareValue(leftValue, rightValue, result); if(error != null) return error; if(result[0] == -2) { r_eq[0] = false; return null; } switch(_op) { case eq: r_eq[0] = result[0] == 0; return null; case neq: r_eq[0] = result[0] != 0; return null; case gt: r_eq[0] = result[0] > 0; return null; case lt: r_eq[0] = result[0] < 0; return null; case gte: r_eq[0] = result[0] >= 0; return null; case lte: r_eq[0] = result[0] <= 0; return null; default: break; } return null; } private SMTJavaAIError compareValue(Object leftValue, Object rightValue, int[] r_result) { if(leftValue == null || rightValue == null) { r_result[0] = (leftValue == null && rightValue == null) ? 0 : -2; ; return null; } if(leftValue instanceof Integer || leftValue instanceof Double || leftValue instanceof Float) { try { double fLeft = SMTStatic.toDouble(leftValue); double fRight = SMTStatic.toDouble(rightValue); int result = 0; if(fLeft < fRight) result = -1; else if(fLeft > fRight) result = 1; r_result[0] = result; return null; } catch(Exception ex) { r_result[0] = -2; return null; } } String sLeft = SMTStatic.toString(leftValue); String sRight = SMTStatic.toString(rightValue); r_result[0] = sLeft.compareTo(sRight); return null; } private SMTJavaAIError checkEmpty(Object leftValue, boolean isEmpty, boolean[] r_eq) { boolean result = false; if(leftValue == null) result = true; else if(SMTStatic.isNullOrEmpty(SMTStatic.toString(leftValue))) result = true; if(!isEmpty) result = !result; r_eq[0] = result; return null; } private SMTJavaAIError checkInclude(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq) { if(leftValue == null || rightValue == null) { r_eq[0] = false; return null; } String sLeft = SMTStatic.toString(leftValue); String sRight = SMTStatic.toString(rightValue); boolean result = sLeft.indexOf(sRight) >= 0; if(!isInclude) result = !result; r_eq[0] = result; return null; } private SMTJavaAIError checkStartWith(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq) { if(leftValue == null || rightValue == null) { r_eq[0] = false; return null; } String sLeft = SMTStatic.toString(leftValue); String sRight = SMTStatic.toString(rightValue); boolean result = sLeft.startsWith(sRight); if(!isInclude) result = !result; r_eq[0] = result; return null; } private SMTJavaAIError checkEndWith(Object leftValue, Object rightValue, boolean isInclude, boolean[] r_eq) { if(leftValue == null || rightValue == null) { r_eq[0] = false; return null; } String sLeft = SMTStatic.toString(leftValue); String sRight = SMTStatic.toString(rightValue); boolean result = sLeft.startsWith(sRight); if(!isInclude) result = !result; r_eq[0] = result; return null; } } protected static class SMTJsonFlowNodeCondItem { public SMTJsonFlowNodeCondType _condJoin; public String _sourceHandle; public List _listDownNodes = new ArrayList<>(); public List _listCondExpr = new ArrayList<>(); public SMTJsonFlowNodeCondItem(Json jsonCondItem) throws Exception { _sourceHandle = jsonCondItem.getJson("id").asString(); _condJoin = "and".equalsIgnoreCase(jsonCondItem.getJson("operator").asString()) ? SMTJsonFlowNodeCondType.AND : SMTJsonFlowNodeCondType.OR; for(Json jsonCondExpr : jsonCondItem.getJson("conditions").asJsonList()) { SMTJsonFlowNodeCondExpr condExpr = new SMTJsonFlowNodeCondExpr(jsonCondExpr); _listCondExpr.add(condExpr); } } public void initEdge(SMTJsonFlowNode tagNode, String sourceHandle) { if(!_sourceHandle.equals(sourceHandle)) return; _listDownNodes.add(tagNode); } public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg, boolean[] r_eq) throws Exception { r_eq[0] = false; if(_condJoin == SMTJsonFlowNodeCondType.OR) { for(SMTJsonFlowNodeCondExpr condExpr : _listCondExpr) { SMTJavaAIError error = condExpr.executeFlowNode(execArg, r_eq); if(error != null) return error; if(r_eq[0]) return null; } return null; } else if(_condJoin == SMTJsonFlowNodeCondType.AND) { for(SMTJsonFlowNodeCondExpr condExpr : _listCondExpr) { SMTJavaAIError error = condExpr.executeFlowNode(execArg, r_eq); if(error != null) return error; if(!r_eq[0]) return null; } } return null; } } private static class OgnlRecMap implements Map { private SMTJsonFlowExecArg _execArg; public OgnlRecMap(SMTJsonFlowExecArg execArg) { _execArg = execArg; } @Override public int size() { return 0; } @Override public boolean isEmpty() { return false; } @Override public boolean containsKey(Object key) { return false; } @Override public boolean containsValue(Object value) { return false; } @Override public Object get(Object key) { try { String value = _execArg._jsonArgs.safeGetStr((String)key, null); return value; } catch (Exception e) { throw new RuntimeException("get record value error : " + key, e); } } @Override public Object put(String key, Object value) { return null; } @Override public Object remove(Object key) { return null; } @Override public void putAll(Map m) { } @Override public void clear() { } @Override public Set keySet() { return null; } @Override public Collection values() { return null; } @Override public Set> entrySet() { return null; } } /////////////////////////////////////////////////////////////////////////////// protected String _sourceHandleElse; protected List _listDownNodesElse = new ArrayList<>(); protected List _listCondItem = new ArrayList<>(); @Override public void initInstane(SMTJsonFlowManager manager, Json jsonNode) throws Exception { super.initInstane(manager, jsonNode); Json jsonConds = jsonNode.getJsonPath("data|group_params|0|params|0|value", false); for(Json jsonCond : jsonConds.asJsonList()) { // 如果存在操作符则代表需要做条件判断 if(jsonCond.has("operator")) { SMTJsonFlowNodeCondItem condItem = new SMTJsonFlowNodeCondItem(jsonCond); _listCondItem.add(condItem); } // 如果不存在操作符则代表else结果 else { _sourceHandleElse = jsonCond.getJson("id").asString(); } } } @Override public void initEdge(SMTJsonFlowNode tagNode, Json jsonEdge) { super.initEdge(tagNode, jsonEdge); String sourceHandle = jsonEdge.safeGetStr("sourceHandle", ""); // 扫描并加入所有条件节点 for(SMTJsonFlowNodeCondItem condItem : _listCondItem) { condItem.initEdge(tagNode, sourceHandle); } // 如果else节点存在匹配关系,则加入else节点 if(_sourceHandleElse.equals(sourceHandle)) _listDownNodesElse.add(tagNode); } @Override public void afterInstance() throws Exception { super.afterInstance(); if(_listCondItem.size() == 0) throw new Exception("condtion is empty : " + this.getId()); for(SMTJsonFlowNodeCondItem condItem : _listCondItem) { if(condItem._listDownNodes.size() == 0) throw new Exception("condtion node not any source handle : " + this.getId()); } if(this._listDownNodesElse.size() == 0) throw new Exception("condtion node not else source handle : " + this.getId()); } @Override public SMTJavaAIError executeFlowNode(SMTJsonFlowExecArg execArg) throws Exception { List listDownNodes = null; // 判断条件是否成立 boolean[] condEQ = new boolean[1]; for(SMTJsonFlowNodeCondItem condItem : _listCondItem) { SMTJavaAIError error = condItem.executeFlowNode(execArg, condEQ); if(error != null) return error; if(condEQ[0]) { listDownNodes = condItem._listDownNodes; break; } } // 如果前面的条件都不符合,则自动跳转到else节点 if(listDownNodes == null) listDownNodes = _listDownNodesElse; if(listDownNodes != null) { for(SMTJsonFlowNode flowNode : listDownNodes) { execArg._stackNodeExec.addLast(flowNode.createFlowNodeExec()); } } return null; } }