package com.changhong.epc.parsing.service.transition.billInfo.formula;
|
|
import com.changhong.epc.parsing.service.transition.billInfo.function.IFuncActuator;
|
import com.changhong.epc.parsing.service.transition.billInfo.function.impl.FuncActuatorImpl;
|
import com.changhong.epc.parsing.service.transition.billInfo.models.TransverterBillInfoPre;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
|
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 转换票据 公式解析器
|
* @ClassName: ExpressionOarser
|
* @author [九鼎联合科技]
|
* @date 2017年7月23日 下午4:33:18
|
*/
|
@Slf4j
|
public class ExpressionOarser {
|
|
//条件
|
private String condition;
|
//元素函数表
|
private String elementList;
|
|
private IFuncActuator funcFactory = new FuncActuatorImpl();
|
private List<Boolean> funResultList = new ArrayList<>();
|
|
public ExpressionOarser(TransverterBillInfoPre pre) {
|
condition = pre.getCondition();
|
elementList = pre.getElementList();
|
funcFactory.setDate(pre.getMatchingVal());
|
}
|
|
|
// ----- js
|
ScriptEngineManager manager = new ScriptEngineManager();
|
ScriptEngine engine = manager.getEngineByName("javascript");
|
|
/**
|
* @throws Exception
|
* 执行表达式
|
* @Title: execute
|
* @param
|
* @return void 返回类型
|
* @throws
|
*/
|
public Boolean execute(){
|
if(StringUtils.isBlank(condition)){
|
return true;
|
}
|
|
try {
|
log.debug("----------计算函数--------");
|
// 1. 计算函数 ok
|
funResultList = funcFactory.exeFunList(elementList);
|
// 2. 填充表达式
|
log.debug("----------填充表达式 --------");
|
String fillParam = fillParam();
|
// 3. 执行表达式
|
log.debug("----------执行表达式 --------");
|
return exeJS(fillParam);
|
} catch (Exception e) {
|
log.debug("----------匹配发生异常 --------");
|
log.error(e.toString());
|
return false;
|
}
|
}
|
|
|
/**
|
* 为表达式填充参数
|
* @Title: addParam
|
* @param @throws Exception 设定文件
|
* @return void 返回类型
|
* @throws
|
*/
|
public String fillParam(){
|
Pattern pattern = Pattern.compile("[\\d]+");
|
Map<String,String> indexRes = new HashMap<>();
|
Matcher matcher = pattern.matcher(condition);
|
int c = 97;
|
while(matcher.find()){
|
String group = matcher.group(),
|
key = String.format("{%c}", c++);
|
condition = condition.replaceFirst(group, key);
|
indexRes.put(key, group);
|
|
}
|
// System.out.println(condition);
|
|
for (String key : indexRes.keySet()) {
|
// System.out.println();
|
condition = condition.replace(key,
|
" "+String.valueOf(
|
funResultList.get(
|
Integer.valueOf(indexRes.get(key))
|
)) +" "
|
);
|
|
}
|
condition = condition.replaceAll(" and ", " && ");
|
condition = condition.replaceAll(" or ", " || ");
|
return condition;
|
|
}
|
|
|
|
private Boolean exeJS(String exeStr){
|
log.debug(exeStr);
|
Boolean eval = false;
|
try {
|
eval = (Boolean) engine.eval(exeStr);
|
} catch (ScriptException e) {
|
// System.err.println(e.getMessage());
|
}
|
return eval;
|
}
|
|
|
}
|