فهرست منبع

添加规则引擎,实现计算公式的执行

Gaokun Wang 4 ماه پیش
والد
کامیت
a9dfa69848

+ 6 - 1
als-modules/agile-assurance/pom.xml

@@ -23,7 +23,6 @@
             <artifactId>hutool-all</artifactId>
         </dependency>
 
-
         <!--es配置-->
         <dependency>
             <groupId>org.elasticsearch.client</groupId>
@@ -44,5 +43,11 @@
             <groupId>org.dromara.easy-es</groupId>
             <artifactId>easy-es-boot-starter</artifactId>
         </dependency>
+
+        <!-- QLExpress规则引擎的依赖配置-->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>QLExpress</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 20 - 0
als-modules/agile-assurance/src/main/java/org/eco/als/controller/AlgorithmController.java

@@ -5,14 +5,17 @@ import cn.hutool.core.util.StrUtil;
 import jakarta.annotation.Resource;
 import lombok.RequiredArgsConstructor;
 import org.eco.als.domain.bo.AlgorithmBo;
+import org.eco.als.domain.bo.ExpressBo;
 import org.eco.als.domain.bo.QaBo;
 import org.eco.als.domain.bo.TaskBo;
 import org.eco.als.service.IAlgorithmService;
+import org.eco.als.service.IFormulaService;
 import org.eco.common.core.core.domain.CommonResult;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 算法相关控制
@@ -27,6 +30,8 @@ import java.util.List;
 public class AlgorithmController {
     @Resource
     private IAlgorithmService algorithmService;
+    @Resource
+    private IFormulaService formulaService;
 
     /**
      * 执行预处理
@@ -133,4 +138,19 @@ public class AlgorithmController {
     public CommonResult<String> executeQa(@Validated @RequestBody QaBo QaBo) {
         return CommonResult.success(algorithmService.executeQa(QaBo), "");
     }
+
+    /**
+     * 执行公式-规则引擎
+     *
+     * @param expressBo 入参
+     * @return org.eco.common.core.core.domain.CommonResult<java.lang.String> 结果
+     **/
+    @PostMapping("/execute/express")
+    public CommonResult<String> executeExpress(@RequestBody ExpressBo expressBo) throws Exception {
+        String express = expressBo.getExpression();
+        Map<String, Object> variables = expressBo.getVariables();
+        // 执行表达式求值
+        Object result = formulaService.evaluateExpression(express, variables);
+        return CommonResult.success(result.toString(), "");
+    }
 }

+ 8 - 0
als-modules/agile-assurance/src/main/java/org/eco/als/controller/WarningController.java

@@ -1,6 +1,8 @@
 package org.eco.als.controller;
 
 import cn.dev33.satoken.annotation.SaCheckPermission;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import jakarta.annotation.Resource;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
@@ -157,6 +159,12 @@ public class WarningController extends BaseController {
     @GetMapping(value = "/{code}/{sortieNo}")
     public CommonResult<String> getInfoByHmc(@PathVariable String code, @PathVariable String sortieNo) {
         JudgeFaultLogicVo judgeFaultLogicVo = judgeFaultLogicService.selectByHmc(code);
+        if (ObjectUtil.isEmpty(judgeFaultLogicVo)) {
+            return CommonResult.fail("判故逻辑不能为空,请配置.");
+        }
+        if (StrUtil.isBlank(judgeFaultLogicVo.getParameterColumn())) {
+            return CommonResult.fail("判故逻辑中参数列表不能为空,请配置.");
+        }
         String columnData = judgeFaultLogicVo.getParameterColumn();
         DataImportVo dataImportVo = dataImportService.selectBySortieNo(sortieNo);
         SysOssVo ossVo = sysOssService.getById(dataImportVo.getOssId());

+ 11 - 0
als-modules/agile-assurance/src/main/java/org/eco/als/domain/bo/ExpressBo.java

@@ -0,0 +1,11 @@
+package org.eco.als.domain.bo;
+
+import lombok.Data;
+
+import java.util.Map;
+
+@Data
+public class ExpressBo {
+    private String expression;
+    private Map<String, Object> variables;
+}

+ 7 - 0
als-modules/agile-assurance/src/main/java/org/eco/als/service/IFormulaService.java

@@ -0,0 +1,7 @@
+package org.eco.als.service;
+
+import java.util.Map;
+
+public interface IFormulaService {
+    Object evaluateExpression(String expression, Map<String, Object> variables) throws Exception;
+}

+ 61 - 0
als-modules/agile-assurance/src/main/java/org/eco/als/service/impl/FormulaService.java

@@ -0,0 +1,61 @@
+package org.eco.als.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import com.ql.util.express.DefaultContext;
+import com.ql.util.express.ExpressRunner;
+import org.eco.als.service.IFormulaService;
+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;
+
+@Service
+public class FormulaService implements IFormulaService {
+    private final ExpressRunner runner = new ExpressRunner();
+
+    @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);
+        return runner.execute(expression, context, null, true, false);
+    }
+
+    private Map<String, Object> buildVariableContext(String expression) {
+        String[] variableNames = parseVariableNames(expression);
+        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);
+            }
+        }
+        return variables;
+    }
+
+    /**
+     * 解析表达式中的变量名。
+     */
+    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);
+        }
+        return variables.keySet().toArray(new String[0]);
+    }
+}

+ 7 - 0
pom.xml

@@ -65,6 +65,7 @@
         <flatten-maven-plugin.version>1.3.0</flatten-maven-plugin.version>
         <easy-es.version>2.0.0-beta4</easy-es.version>
         <es.version>7.12.1</es.version>
+        <ql.version>3.3.4</ql.version>
     </properties>
 
     <profiles>
@@ -108,6 +109,12 @@
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <!-- QLExpress规则引擎的依赖配置-->
+            <dependency>
+                <groupId>com.alibaba</groupId>
+                <artifactId>QLExpress</artifactId>
+                <version>${ql.version}</version>
+            </dependency>
 
             <!--达梦数据库驱动-->
             <dependency>