李春阳 1 tahun lalu
induk
melakukan
3e069fc225

+ 62 - 7
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/domain/service/TaskDomainService.java

@@ -47,6 +47,8 @@ public class TaskDomainService {
     private String pyPath;
     @Value("${scheduler.linux-path.temp}")
     private String linuxTempPath;
+    @Value("${scheduler.scene-evaluation.script}")
+    private String sceneEvaluationScript;
     @Resource
     private StringRedisTemplate stringRedisTemplate;
     @Resource
@@ -72,7 +74,15 @@ public class TaskDomainService {
     @Resource
     private CustomConfiguration customConfiguration;
     @Resource
-    private ProjectApplicationService projectApplicationService;
+    private SceneEvaluationRuleMapper sceneEvaluationRuleMapper;
+    @Resource
+    private SceneComplexityMapper sceneComplexityMapper;
+    @Resource
+    private SceneRiskMapper sceneRiskMapper;
+    @Resource
+    private SceneMapper sceneMapper;
+    @Resource
+    private IndexTemplateMapper indexTemplateMapper;
 
     public void batchInsertTask(List<TaskEntity> taskEntityList) {
         try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
@@ -120,7 +130,7 @@ public class TaskDomainService {
         List<IndexTemplateEntity> leafIndexTemplateList = JsonUtil.jsonToList(leafIndexTemplateListJson, IndexTemplateEntity.class);
         log.debug("共有 " + leafIndexTemplateList.size() + "个叶子节点:" + leafIndexTemplateListJson);
         int maxLevel = 1; // 用于计算指标得分
-        List<SceneEntity> sceneEntityList = projectApplicationService.getSceneList(projectId, packageId);
+        List<SceneEntity> sceneEntityList = getSceneList(projectId, packageId);
         Map<String, SceneEntity> sceneEntityMap = sceneEntityList.stream().collect(Collectors.toMap(SceneEntity::getId, Function.identity()));
         List<LeafIndexEntity> leafIndexList = new ArrayList<>();
         for (int i = 0; i < leafIndexTemplateList.size(); i++) {
@@ -320,6 +330,8 @@ public class TaskDomainService {
 
             leafIndexList.add(leafIndex);
         }
+        // 删除临时文件
+        FileUtil.rm(linuxTempPath + "scene/evaluation/" + projectId + "/");   // 删除临时文件
         // 保存叶子指标得分
         taskIndexRepository.batchInsertLeafIndex(leafIndexList);
         // 保存一级指标分数
@@ -440,8 +452,7 @@ public class TaskDomainService {
     public boolean computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
         String ruleId = param.getSceneEvaluationRuleId();
         // 获取场景评价规则
-//        SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
-        SceneEvaluationRulePO sceneEvaluationRulePO = new SceneEvaluationRulePO();
+        SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
         if (sceneEvaluationRulePO == null) {
             log.error(ruleId + " 的场景评价规则已删除");
             return false;
@@ -460,7 +471,7 @@ public class TaskDomainService {
                     "/scenario_evaluation_utils52.py"
             );
             for (String pyFilePath : scriptFilePath) {
-//                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationScript + pyFilePath, scriptsPath + pyFilePath);
+                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationScript + pyFilePath, scriptsPath + pyFilePath);
             }
         }
         // 下载场景评价脚本到脚本目录
@@ -550,7 +561,7 @@ public class TaskDomainService {
             sceneComplexityPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
             sceneComplexityPO.setCreateUserId(null);
             sceneComplexityPO.setCreateTime(TimeUtil.getNowForMysql());
-//            sceneComplexityMapper.saveSceneComplexity(sceneComplexityPO);
+            sceneComplexityMapper.saveSceneComplexity(sceneComplexityPO);
         } else {
             String risk = rootNode.path("危险度").asText();
             String riskLevel = rootNode.path("危险度等级").asText();
@@ -565,8 +576,52 @@ public class TaskDomainService {
             sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
             sceneRiskPO.setCreateUserId(null);
             sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
-//            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
         }
         return true;
     }
+
+    @SneakyThrows
+    public List<SceneEntity> getSceneList(String projectId, String packageId) {
+
+        String allIndexPrefix = "project:" + projectId + ":package:" + packageId + ":all";
+        String leafIndexPrefix = "project:" + projectId + ":package:" + packageId + ":leaf";
+        //1 查询该场景包的所有指标列表,包删了无所谓,但要过滤删掉的指标。并保存成 json 文件。
+        List<IndexTemplateEntity> allIndexList = indexTemplateMapper.selectByPackageIdIncludeDeleted(packageId);
+        stringRedisTemplate.opsForValue().set(allIndexPrefix, JsonUtil.listToJson(allIndexList));
+        //2 查询场景包叶子指标
+        List<IndexTemplateEntity> leafIndexList = allIndexList.stream().filter(index -> StringUtil.isNotEmpty(index.getRuleId())).collect(Collectors.toList());
+        log.info("项目 " + projectId + " 的叶子指标为:" + leafIndexList);
+        stringRedisTemplate.opsForValue().set(leafIndexPrefix, JsonUtil.listToJson(leafIndexList));
+        List<SceneEntity> sceneList = new ArrayList<>();
+        leafIndexList.forEach(leafIndex -> {
+            String naturalIds = leafIndex.getSceneNaturalIds();
+            String standardIds = leafIndex.getSceneStatueIds();
+            String accidentIds = leafIndex.getSceneTrafficIds();
+            String generalizationIds = leafIndex.getSceneGeneralizationIds();
+            String sceneReferenceIds = leafIndex.getSceneReferenceLibIds();
+            if (StringUtil.isNotEmpty(naturalIds)) {
+                List<String> naturalIdList = new ArrayList<>(Arrays.asList(naturalIds.split(",")));
+                sceneList.addAll(sceneMapper.selectNaturalByIdList(naturalIdList));
+            }
+            if (StringUtil.isNotEmpty(standardIds)) {
+                List<String> standardIdList = new ArrayList<>(Arrays.asList(standardIds.split(",")));
+                sceneList.addAll(sceneMapper.selectStandardByIdList(standardIdList));
+            }
+            if (StringUtil.isNotEmpty(accidentIds)) {
+                List<String> accidentIdList = new ArrayList<>(Arrays.asList(accidentIds.split(",")));
+                sceneList.addAll(sceneMapper.selectAccidentByIdList(accidentIdList));
+            }
+            if (StringUtil.isNotEmpty(generalizationIds)) {
+                List<String> generalizationIdList = new ArrayList<>(Arrays.asList(generalizationIds.split(",")));
+                sceneList.addAll(sceneMapper.selectGeneralizationByIdList(generalizationIdList));
+            }
+            if (StringUtil.isNotEmpty(sceneReferenceIds)) {
+                List<String> sceneReferenceIdList = new ArrayList<>(Arrays.asList(sceneReferenceIds.split(",")));
+                sceneList.addAll(sceneMapper.selectReferenceLibByIdList(sceneReferenceIdList));
+            }
+        });
+        log.info("项目" + projectId + " 共有 " + sceneList.size() + " 个任务:" + sceneList);
+        return sceneList;
+    }
 }

+ 11 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/infra/db/mysql/mapper/SceneComplexityMapper.java

@@ -0,0 +1,11 @@
+package com.css.simulation.resource.scheduler.infra.db.mysql.mapper;
+
+import api.common.pojo.po.scene.SceneComplexityPO;
+import org.apache.ibatis.annotations.Mapper;
+
+
+@Mapper
+public interface SceneComplexityMapper {
+    void saveSceneComplexity(SceneComplexityPO params);
+
+}

+ 19 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/infra/db/mysql/mapper/SceneEvaluationRuleMapper.java

@@ -0,0 +1,19 @@
+package com.css.simulation.resource.scheduler.infra.db.mysql.mapper;
+
+import api.common.pojo.po.scene.SceneEvaluationRulePO;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.springframework.stereotype.Repository;
+
+
+@Mapper
+@Repository
+public interface SceneEvaluationRuleMapper {
+
+    @Select("select rule_id, rule_name, rule_type,script_name, script_path\n" +
+            "from scene_evaluation_rule\n" +
+            "where is_deleted = '0' AND rule_id =  #{ruleId} \n")
+    SceneEvaluationRulePO querySceneEvaluationPyById(@Param("ruleId") String ruleId);
+
+}

+ 11 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/infra/db/mysql/mapper/SceneRiskMapper.java

@@ -0,0 +1,11 @@
+package com.css.simulation.resource.scheduler.infra.db.mysql.mapper;
+
+import api.common.pojo.po.scene.SceneRiskPO;
+import org.apache.ibatis.annotations.Mapper;
+
+
+@Mapper
+public interface SceneRiskMapper {
+    void saveSceneRisk(SceneRiskPO params);
+
+}

+ 2 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/infra/db/mysql/mapper/SimulationManualProjectMapper.java

@@ -28,6 +28,8 @@ public interface SimulationManualProjectMapper {
             "       parallelism,\n" +
             "       now_run_state,\n" +
             "       is_choice_gpu,\n" +
+            "       risk_evaluation_rule_id,\n" +
+            "       complexity_evaluation_rule_id,\n" +
             "       '1' project_type,\n" +
             "       max_simulation_time\n" +
             "from simulation_manual_project\n" +

+ 8 - 5
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/impl/SimulationProjectServiceImpl.java

@@ -3157,12 +3157,16 @@ public class SimulationProjectServiceImpl implements SimulationProjectService {
                 }
             }
             document.add(pdfPTable2);
+            List<SceneScListVo> sceneScoreLi = vo.getSceneScoreLi();
 
+            if ("1".equals(param.getProjectType())) {
+                // 场景评价
+                addElement(document, "场景评价", null, bf3, 15, false, 30, true);
+                addElement(document, "       场景测试包共包含" + totalSceneNum + "个场景.", null, bf3, 15, false, 30, false);
+//                projectId
+//                sceneScoreLi
 
-            // 场景评价
-            addElement(document, "场景评价", null, bf3, 15, false, 30, true);
-            addElement(document, "       场景测试包共包含" + totalSceneNum + "个场景.", null, bf3, 15, false, 30, false);
-
+            }
 
             // ------- 4.详细得分说明 -------
 
@@ -3173,7 +3177,6 @@ public class SimulationProjectServiceImpl implements SimulationProjectService {
              */
             // lineFeed(document, 2, 2);
             List<Map> scoreLiTitle = vo.getSceneScoreLiTitle();
-            List<SceneScListVo> sceneScoreLi = vo.getSceneScoreLi();
             addElement(document, "4. 详细场景得分说明", null, bf4, 16, false, 40, false);
             StringBuffer s1 = new StringBuffer("       " + vo.getAlgorithmName());
             /*

+ 38 - 13
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneEvaluationRuleService.java

@@ -21,6 +21,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import feign.Response;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -40,11 +42,11 @@ import java.util.stream.Collectors;
 public class SceneEvaluationRuleService {
 
 
-    //    @Value("${server.linux-path.temp}")
-    private String linuxTempPath = "";
-    //
-//    @Value("${server.scene-evaluation.script}")
-    private String sceneEvaluationScript = "";
+    @Value("${server.linux-path.temp}")
+    private String linuxTempPath;
+
+    @Value("${server.scene-evaluation.script}")
+    private String sceneEvaluationScript;
 
     @Resource
     private ScenePackageMapper scenePackageMapper;
@@ -239,10 +241,12 @@ public class SceneEvaluationRuleService {
                     "/scenario_evaluation_utils52.py"
             );
             for (String pyFilePath : scriptFilePath) {
-                Response download = fileDownService.download(MinioParameter.builder().objectName(sceneEvaluationScript + pyFilePath).build());
-                String pyStr = download.body().toString();
-                download.close();
-                FileUtil.writeStringToLocalFile(pyStr, scriptsPath + pyFilePath);
+                try {
+                    downloadDependFile(sceneEvaluationScript + pyFilePath, scriptsPath + pyFilePath);
+                } catch (IOException e) {
+                    log.error("下载 py 依赖文件失败: " + sceneEvaluationScript + pyFilePath, e);
+                    return false;
+                }
             }
         }
         // 下载场景评价脚本到脚本目录
@@ -251,10 +255,12 @@ public class SceneEvaluationRuleService {
         }
         String pyMainPath = scriptsPath + "/" + sceneEvaluationRulePO.getRuleId() + ".py";
         if (!new File(pyMainPath).exists()) {
-            Response download = fileDownService.download(MinioParameter.builder().objectName(sceneEvaluationRulePO.getScriptPath()).build());
-            String pyStr = download.body().toString();
-            download.close();
-            FileUtil.writeStringToLocalFile(pyStr, pyMainPath);
+            try {
+                downloadDependFile(sceneEvaluationRulePO.getScriptPath(), pyMainPath);
+            } catch (IOException e) {
+                log.error("下载 py 执行文件失败: " + sceneEvaluationRulePO.getScriptPath(), e);
+                return false;
+            }
         }
         // 创建场景路径
         String scenePath = evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId();
@@ -372,4 +378,23 @@ public class SceneEvaluationRuleService {
         outputStream.close();
         download.close();
     }
+
+    public void copySceneComplexityResult(String taskId, String tempSceneId, List<String> copySceneIds) {
+        List<SceneComplexityPO> sceneComplexityPOS = sceneComplexityMapper.selectSceneComplexityEvaluation(taskId, tempSceneId);
+        List<SceneComplexityPO> saveList = new ArrayList<>();
+        if (CollectionUtil.isNotEmpty(sceneComplexityPOS)) {
+            for (SceneComplexityPO sceneComplexityPO : sceneComplexityPOS) {
+                for (String copySceneId : copySceneIds) {
+                    SceneComplexityPO tempComplexityPO = new SceneComplexityPO();
+                    BeanUtils.copyProperties(sceneComplexityPO, tempComplexityPO);
+                    tempComplexityPO.setSceneId(copySceneId);
+                    tempComplexityPO.setComplexityId(StringUtil.getRandomUUID());
+                    saveList.add(tempComplexityPO);
+                }
+            }
+            if (CollectionUtil.isNotEmpty(saveList)) {
+                sceneComplexityMapper.saveSceneComplexityS(saveList);
+            }
+        }
+    }
 }

+ 8 - 2
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneReferenceLibService.java

@@ -17,6 +17,7 @@ import feign.Response;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -29,6 +30,8 @@ import java.util.stream.Collectors;
 @Service
 public class SceneReferenceLibService {
 
+    @Value("${server.linux-path.temp}")
+    private String linuxTempPath;
     @Resource
     private SceneReferenceLibMapper sceneReferenceLibMapper;
 
@@ -211,6 +214,7 @@ public class SceneReferenceLibService {
                     MI.setObjectName(filePath);
                     List<String> listScene = fileDownService.listDeepOne(MI).getInfo();
                     log.info("importMinio() 共需要上传 " + listScene.size() + " 个基准场景");
+                    String taskId = StringUtil.getRandomUUID();
                     for (String scenePath : listScene) {
                         try {
                             MI.setObjectName(scenePath);
@@ -331,12 +335,12 @@ public class SceneReferenceLibService {
                                 sceneEvaluationComputeParam.setSceneXOSCPath(sceneReferenceLibPO.getXmlAddress());
                                 sceneEvaluationComputeParam.setSceneXODRPath(sceneReferenceLibPO.getXodrAddress());
                                 sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_REFERENCE_LIB);
-                                sceneEvaluationComputeParam.setTaskId(StringUtil.getRandomUUID());
+                                sceneEvaluationComputeParam.setTaskId(taskId);
                                 sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
                                 try {
                                     boolean sceneEvaluationResult = sceneEvaluationRuleService.computeSceneReference(params, sceneEvaluationComputeParam);
                                     if (isUpdate && sceneEvaluationResult) {
-//                                        sceneEvaluationRuleService.copySceneComplexityResult(tempSceneId, scenes.stream().map(SceneReferenceLibPO::getSceneId).collect(Collectors.toList()), DictConstants.SCENE_REFERENCE_LIB);
+                                        sceneEvaluationRuleService.copySceneComplexityResult(taskId, tempSceneId, scenes.stream().map(SceneReferenceLibPO::getSceneId).collect(Collectors.toList()));
                                     }
                                 } catch (Exception e) {
                                     log.error("计算复杂度失败", e);
@@ -348,6 +352,8 @@ public class SceneReferenceLibService {
                             falseNum += 1;
                         }
                     }
+                    // 删除临时文件
+                    FileUtil.rm(linuxTempPath + "scene/evaluation/" + taskId + "/");   // 删除临时文件
                 }
             }
             sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_4);

+ 5 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/server/infra/db/mysql/mapper/SceneComplexityMapper.java

@@ -20,4 +20,9 @@ public interface SceneComplexityMapper {
 
     void saveSceneComplexity(SceneComplexityPO params);
 
+    void saveSceneComplexityS(@Param("list") List<SceneComplexityPO> params);
+
+    @Select("SELECT * FROM scene_complexity WHERE scene_id = #{sceneId} AND task_id = #{taskId}" )
+    List<SceneComplexityPO> selectSceneComplexityEvaluation(@Param("taskId") String taskId, @Param("sceneId") String sceneId);
+
 }

+ 17 - 0
simulation-resource-server/src/main/resources/mysql/mapper/SceneComplexityMapper.xml

@@ -53,4 +53,21 @@
                 #{createUserId,jdbcType=VARCHAR}, #{createTime})
     </insert>
 
+    <insert id="saveSceneComplexityS" parameterType="api.common.pojo.po.scene.SceneComplexityPO">
+        insert into simulation.scene_complexity
+        (complexity_id, scene_id, scene_type, rule_id,
+        task_id, complexity, complexity_level,
+        is_deleted, create_user_id, create_time
+        )
+        values
+        <foreach collection="list" item="item" index="index" separator=",">
+            (#{complexityId,jdbcType=VARCHAR}, #{sceneId,jdbcType=VARCHAR}, #{sceneType,jdbcType=VARCHAR},
+            #{ruleId,jdbcType=VARCHAR},
+            #{taskId,jdbcType=VARCHAR}, #{complexity,jdbcType=VARCHAR},
+            #{complexityLevel,jdbcType=VARCHAR},
+            #{isDeleted,jdbcType=VARCHAR},
+            #{createUserId,jdbcType=VARCHAR}, #{createTime})
+        </foreach>
+    </insert>
+
 </mapper>