package com.changhong.epc.parsing.service.transition.billInfo.function.impl; import com.changhong.epc.parsing.service.transition.billInfo.function.FunConst; import com.changhong.epc.parsing.service.transition.billInfo.function.IFuncActuator; import com.changhong.epc.parsing.service.transition.billInfo.function.IFuncModel; import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.StringUtil; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @Slf4j public class FuncActuatorImpl implements IFuncActuator, FunConst { private IFuncModel simpleFuncModel = new FuncModelImpl(); private List result = new ArrayList<>(); private Map matchingVal; @SuppressWarnings("unchecked") public Object SimpleFuncModel(String funMethodName, Object[] params) throws Exception{ log.debug("开始执行函数:"+ funMethodName + "\t" + JSONTool.toJson(params)); Class clazz = simpleFuncModel.getClass(); Method m = clazz.getDeclaredMethod(funMethodName, String.class , String.class); Object invoke = m.invoke(simpleFuncModel, StringUtil.trim(Objects.toString(params[0])), StringUtil.trim(Objects.toString(params[1]))); log.debug("函数执行完成:" + invoke); return invoke; } @Override public Object exeFun(String funMethodName, Object[] params) { try { return SimpleFuncModel(funMethodName,params); } catch (Exception e) { } log.debug("函数执行发生异常!"); return false; } public static String regularFind(String src, String regex){ Pattern p = Pattern.compile(regex); Matcher m = p.matcher(src); while (m.find()) { return m.group(); } return null; } /** * @throws Exception * @return * * @Title: funBodysParator * @param @param src 设定文件 * @return void 返回类型 * @throws */ public Object funBodysParator(String src) throws Exception{ String regex1 = "(?<=[\\((]).*(?=[\\))])"; String regex2 = "^.*?(?=[\\((])"; // 1. 取得函数名字 String funName = regularFind(src,regex2); // 2. 取得函数体 String funBody = regularFind(src,regex1); log.debug("funName: "+ funName+"\n"); log.debug("funBody: "+funBody+"\n"); return exeFun(funName, paramParsing(funBody)); } /** * @throws Exception * 函数参数解析 * @Title: paramParsing * @param @param funBody * @param @return 设定文件 * @return Object[] 返回类型 * @throws */ private Object[] paramParsing(String funBody) throws Exception { String[] split = funBody.split(","); List pars = new ArrayList<>(); for (String s : split) { s = s.trim(); if(s.charAt(0) == '['){ s = s.replaceAll("^[\\[]+|[\\]]+$", ""); // 元素数据 进行匹配 log.debug("查找元素:"+s); s = findMatchingVal(s); if (s == null) { throw new IEMRuntimeException("元素不存在"); } } pars.add(s); } return pars.toArray(); } private String findMatchingVal(String s){ return String.valueOf( matchingVal.get(s) ); } @Override public List exeFunList(String sourceFuncList) throws Exception { sourceFuncList = sourceFuncList.trim(); String[] funcList = sourceFuncList.split(FUNC_SEPARATOR); for (String funStr : funcList) { log.debug(funStr); result.add((Boolean)funBodysParator(funStr.trim())); } return result; } @Override public void setDate(Map data) { matchingVal = data; } }