|
@@ -1,58 +1,72 @@
|
|
|
package org.eco.als.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.ql.util.express.DefaultContext;
|
|
|
import com.ql.util.express.ExpressRunner;
|
|
|
+import com.ql.util.express.InstructionSet;
|
|
|
+import com.ql.util.express.exception.QLBizException;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eco.als.domain.bo.ExpressBo;
|
|
|
+import org.eco.als.service.IAlgorithmService;
|
|
|
import org.eco.als.service.IFormulaService;
|
|
|
+import org.eco.als.utils.CheckVibrationDuration;
|
|
|
import org.eco.common.core.exception.BusinessException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
|
|
|
+/**
|
|
|
+ * @author wanggaokun
|
|
|
+ */
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class FormulaService implements IFormulaService {
|
|
|
- private final ExpressRunner runner = new ExpressRunner();
|
|
|
+ @Resource
|
|
|
+ private IAlgorithmService algorithmService;
|
|
|
+ private ExpressRunner runner;
|
|
|
|
|
|
@Override
|
|
|
- public Object evaluateExpression(String expression, Map<String, Object> variablesMap) throws Exception {
|
|
|
- DefaultContext<String, Object> context = new DefaultContext<>();
|
|
|
-// Map<String, Object> variables = CollUtil.isEmpty(variablesMap) ? buildVariableContext(expression) : variablesMap;
|
|
|
-// context.putAll(variables);
|
|
|
- // 设置用户参数
|
|
|
- context.put("age", 21);
|
|
|
- context.put("monthlyIncome", 8000);
|
|
|
- context.put("collateralValue", 600000);
|
|
|
- context.put("creditScore", 680);
|
|
|
- context.put("hasGuarantor", true);
|
|
|
-
|
|
|
- expression =
|
|
|
- "age >= 25 " +
|
|
|
- "&& (monthlyIncome >= 10000 || collateralValue >= 500000) " +
|
|
|
- "&& (creditScore >= 700 || hasGuarantor == true)";
|
|
|
- return runner.execute(expression, context, null, true, false);
|
|
|
+ public Object evaluateExpression(ExpressBo expressBo) throws Exception {
|
|
|
+ runner = new ExpressRunner();
|
|
|
+ runner.addOperatorWithAlias("≠", "!=", null);
|
|
|
+ runner.addOperatorWithAlias("≥", ">=", null);
|
|
|
+ runner.addOperatorWithAlias("≤", "<=", null);
|
|
|
+ // // 注册自定义(持续时间计算)函数到QLExpress上下文
|
|
|
+ runner.addFunction("checkVibrationDuration", new CheckVibrationDuration());
|
|
|
+ runner.setShortCircuit(true);
|
|
|
+ Object result = null;
|
|
|
+ try {
|
|
|
+ runner.parseInstructionSet(expressBo.getExpression());
|
|
|
+ JSONArray arr = algorithmService.getArrayByOssId(expressBo.getOssId());
|
|
|
+ for (Object object : arr) {
|
|
|
+ DefaultContext<String, Object> context = new DefaultContext<>();
|
|
|
+ Map<String, Object> variables = CollUtil.isEmpty(expressBo.getVariables()) ? buildVariableContext(expressBo, (JSONObject) object) : expressBo.getVariables();
|
|
|
+ context.putAll(variables);
|
|
|
+ Boolean isMatch = (Boolean) runner.execute(expressBo.getExpression(), context, null, true, false);
|
|
|
+ log.info("isMatch==========================:{}", isMatch);
|
|
|
+ if (isMatch) {
|
|
|
+ result = object;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (QLBizException e) {
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- private Map<String, Object> buildVariableContext(String expression) {
|
|
|
- String[] variableNames = parseVariableNames(expression);
|
|
|
+ private Map<String, Object> buildVariableContext(ExpressBo expressBo, JSONObject jsonObject) {
|
|
|
+ String[] variableNames = parseVariableNames(expressBo.getExpression());
|
|
|
Map<String, Object> variables = new HashMap<>();
|
|
|
// 字段属性自定义值
|
|
|
for (String fieldName : variableNames) {
|
|
|
- switch (fieldName) {
|
|
|
- case "ratedLife":
|
|
|
- variables.put(fieldName, 14);
|
|
|
- break;
|
|
|
- case "usedLife":
|
|
|
- variables.put(fieldName, 8);
|
|
|
- break;
|
|
|
- case "uu":
|
|
|
- variables.put(fieldName, 2);
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw new BusinessException("无此属性公式的配置,请检查: {0}", fieldName);
|
|
|
- }
|
|
|
+ variables.put(fieldName, jsonObject.get(fieldName));
|
|
|
}
|
|
|
+ variables.put("时间", jsonObject.get("时间"));
|
|
|
return variables;
|
|
|
}
|
|
|
|
|
@@ -60,12 +74,11 @@ public class FormulaService implements IFormulaService {
|
|
|
* 解析表达式中的变量名。
|
|
|
*/
|
|
|
private String[] parseVariableNames(String expression) {
|
|
|
- Pattern pattern = Pattern.compile("[a-zA-Z][a-zA-Z0-9_]*");
|
|
|
- Matcher matcher = pattern.matcher(expression);
|
|
|
- Map<String, Boolean> variables = new HashMap<>();
|
|
|
- while (matcher.find()) {
|
|
|
- variables.put(matcher.group(), true);
|
|
|
+ try {
|
|
|
+ InstructionSet instructions = runner.parseInstructionSet(expression);
|
|
|
+ return instructions.getOutAttrNames();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- return variables.keySet().toArray(new String[0]);
|
|
|
}
|
|
|
}
|