李春阳 1 سال پیش
والد
کامیت
3fcf6896ca

+ 3 - 2
api-common/src/main/java/api/common/pojo/param/scene/SceneEvaluationComputeParam.java

@@ -1,6 +1,5 @@
 package api.common.pojo.param.scene;
 
-import api.common.pojo.common.PageVO;
 import lombok.*;
 
 import java.io.Serializable;
@@ -20,9 +19,11 @@ public class SceneEvaluationComputeParam implements Serializable {
     // 场景评价规则 Id
     private String sceneId;
     private String sceneXOSCPath;
-    private String sceneXDRCPath;
+    private String sceneXODRPath;
     private String sceneType;
     private String taskId;
     private String computeType;
     private String evaluationPath;
+    private String vehicleId;
+    private String algorithmId;
 }

+ 7 - 0
api-common/src/main/java/api/common/pojo/po/scene/SceneComplexityPO.java

@@ -3,6 +3,7 @@ package api.common.pojo.po.scene;
 import lombok.*;
 
 import java.io.Serializable;
+import java.sql.Timestamp;
 import java.util.List;
 
 /**
@@ -23,8 +24,14 @@ public class SceneComplexityPO implements Serializable {
     private String taskId;
     private String complexity;
     private String complexityLevel;
+    private String isDeleted;
     // 复杂度下限
     private String minComplexity;
     // 复杂度上限
     private String maxComplexity;
+    public Timestamp createTime;
+    /**
+     * 记录创建人(用户id)
+     */
+    public String createUserId;
 }

+ 7 - 0
api-common/src/main/java/api/common/pojo/po/scene/SceneRiskPO.java

@@ -3,6 +3,7 @@ package api.common.pojo.po.scene;
 import lombok.*;
 
 import java.io.Serializable;
+import java.sql.Timestamp;
 import java.util.List;
 
 /**
@@ -26,4 +27,10 @@ public class SceneRiskPO implements Serializable {
     private String minRisk;
     private String maxRisk;
     private List<String> sceneIdList;
+    private String isDeleted;
+    public Timestamp createTime;
+    /**
+     * 记录创建人(用户id)
+     */
+    public String createUserId;
 }

+ 188 - 3
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/domain/service/TaskDomainService.java

@@ -1,8 +1,15 @@
 package com.css.simulation.resource.scheduler.domain.service;
 
 import api.common.pojo.constants.DictConstants;
+import api.common.pojo.param.scene.SceneEvaluationComputeParam;
+import api.common.pojo.param.scene.SceneImportParam;
+import api.common.pojo.po.scene.SceneComplexityPO;
+import api.common.pojo.po.scene.SceneEvaluationRulePO;
+import api.common.pojo.po.scene.SceneRiskPO;
 import api.common.util.*;
+import com.alibaba.druid.util.StringUtils;
 import com.css.simulation.resource.scheduler.app.repository.TaskIndexRepository;
+import com.css.simulation.resource.scheduler.app.service.ProjectApplicationService;
 import com.css.simulation.resource.scheduler.infra.configuration.custom.CustomConfiguration;
 import com.css.simulation.resource.scheduler.infra.db.entity.ScoringRuleEntity;
 import com.css.simulation.resource.scheduler.infra.db.mysql.mapper.*;
@@ -24,11 +31,10 @@ import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.InputStreamReader;
+import java.io.*;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 @Component
@@ -65,6 +71,8 @@ public class TaskDomainService {
     private Admin admin;
     @Resource
     private CustomConfiguration customConfiguration;
+    @Resource
+    private ProjectApplicationService projectApplicationService;
 
     public void batchInsertTask(List<TaskEntity> taskEntityList) {
         try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
@@ -112,6 +120,8 @@ 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);
+        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++) {
             String scoreExplain = null; // 每个叶子指标下的任务的得分说明一样和叶子指标一致
@@ -226,6 +236,44 @@ public class TaskDomainService {
                         taskOfLeaf.setScored(false);
                         simulationManualProjectTaskMapper.updateFailStateWithStopTime(task2Id, DictConstants.TASK_ABORTED, TimeUtil.getNowForMysql(), DictConstants.TASK_ERROR_REASON_5);
                     }
+
+                    // 计算复杂度和危险度
+                    TaskEntity taskEntity = simulationManualProjectTaskMapper.selectById(task2Id);
+                    String sceneId = taskEntity.getSceneId();
+                    if (!StringUtils.isEmpty(projectEntity.getComplexityEvaluationRuleId())) {
+                        SceneEvaluationComputeParam sceneEvaluationComputeParam = new SceneEvaluationComputeParam();
+                        sceneEvaluationComputeParam.setSceneId(sceneId);
+                        sceneEvaluationComputeParam.setSceneXOSCPath(sceneEntityMap.get(sceneId).getScenarioOsc());
+                        sceneEvaluationComputeParam.setSceneXODRPath(sceneEntityMap.get(sceneId).getScenarioOdr());
+                        sceneEvaluationComputeParam.setSceneType(sceneEntityMap.get(sceneId).getType());
+                        sceneEvaluationComputeParam.setTaskId(projectId);
+                        sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
+                        try {
+                            SceneImportParam sceneImportParam = new SceneImportParam();
+                            sceneImportParam.setSceneEvaluationRuleId(projectEntity.getComplexityEvaluationRuleId());
+                            computeSceneReference(sceneImportParam, sceneEvaluationComputeParam);
+                        } catch (Exception e) {
+                            log.error("计算复杂度失败", e);
+                        }
+                    }
+                    // 计算复杂度和危险度
+                    if (!StringUtils.isEmpty(projectEntity.getRiskEvaluationRuleId())) {
+                        SceneEvaluationComputeParam sceneEvaluationComputeParam = new SceneEvaluationComputeParam();
+                        sceneEvaluationComputeParam.setSceneId(sceneId);
+                        sceneEvaluationComputeParam.setEvaluationPath(runResultFilePath);
+                        sceneEvaluationComputeParam.setSceneType(sceneEntityMap.get(sceneId).getType());
+                        sceneEvaluationComputeParam.setTaskId(projectId);
+                        sceneEvaluationComputeParam.setComputeType(DictConstants.RISK);
+                        sceneEvaluationComputeParam.setAlgorithmId(projectEntity.getAlgorithm());
+                        sceneEvaluationComputeParam.setVehicleId(projectEntity.getVehicle());
+                        try {
+                            SceneImportParam sceneImportParam = new SceneImportParam();
+                            sceneImportParam.setSceneEvaluationRuleId(projectEntity.getRiskEvaluationRuleId());
+                            computeSceneReference(sceneImportParam, sceneEvaluationComputeParam);
+                        } catch (Exception e) {
+                            log.error("计算复杂度失败", e);
+                        }
+                    }
                 }
             }
 
@@ -384,4 +432,141 @@ public class TaskDomainService {
     }
 
 
+    /**
+     * 场景上传计算复杂度,计算复杂度需要用到 osc 和 odr 路径,计算危险度用 evaluationPath
+     *
+     * @param param
+     */
+    public boolean computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
+        String ruleId = param.getSceneEvaluationRuleId();
+        // 获取场景评价规则
+//        SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
+        SceneEvaluationRulePO sceneEvaluationRulePO = new SceneEvaluationRulePO();
+        if (sceneEvaluationRulePO == null) {
+            log.error(ruleId + " 的场景评价规则已删除");
+            return false;
+        }
+        // 1 判断有没有用户目录,没有则复制
+        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParam.getTaskId() + "/";
+        String scriptsPath = evaluationDirectoryOfUser + "scripts/";
+        if (!new File(evaluationDirectoryOfUser).exists()) {
+            // 1 将场景评价规则脚本保存到 script 目录
+            FileUtil.createDirectory(scriptsPath);
+            final ArrayList<String> scriptFilePath = CollectionUtil.createArrayList(
+                    "/data_preprocessing.py",
+                    "/elevation.py",
+                    "/scenario_criticality_algorithm.py",
+                    "/scenario_evaluation_main.py",
+                    "/scenario_evaluation_utils52.py"
+            );
+            for (String pyFilePath : scriptFilePath) {
+//                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationScript + pyFilePath, scriptsPath + pyFilePath);
+            }
+        }
+        // 下载场景评价脚本到脚本目录
+        if (sceneEvaluationRulePO.getScriptPath() == null) {
+            return false;
+        }
+        String pyMainPath = scriptsPath + "/" + sceneEvaluationRulePO.getRuleId() + ".py";
+        if (!new File(pyMainPath).exists()) {
+            MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationRulePO.getScriptPath(), pyMainPath);
+        }
+        // 创建场景路径
+        String scenePath = evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId();
+        if (!new File(scenePath).exists()) {
+            FileUtil.createDirectory(scenePath);
+        } else {
+            // 一个场景只计算一次
+            return false;
+        }
+        try {
+            if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.COMPLEXITY)) {
+                // 计算复杂度,根据场景 id 获取场景信息,下载 osc odr
+                String scenarioOsc = sceneEvaluationComputeParam.getSceneXOSCPath();
+                String[] splitXosc = scenarioOsc.split("/");
+                String xoscName = splitXosc[splitXosc.length - 1];
+                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationComputeParam.getSceneXOSCPath(), scenePath + "/" + xoscName);
+                String scenarioOdr = sceneEvaluationComputeParam.getSceneXODRPath();
+                String[] splitXodr = scenarioOdr.split("/");
+                String xodrName = splitXodr[splitXodr.length - 1];
+                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationComputeParam.getSceneXODRPath(), scenePath + "/" + xodrName);
+            } else if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.RISK)) {
+                // 计算危险度 从 minio path 下载 csv (ego 和 sensors)
+                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationComputeParam.getEvaluationPath() + "/Ego.csv", scenePath + "/Ego.csv");
+                MinioUtil.downloadToFile(minioClient, bucketName, sceneEvaluationComputeParam.getEvaluationPath() + "/sensors.csv", scenePath + "/sensors.csv");
+            } else {
+                return false;
+            }
+        } catch (Exception e) {
+            log.error("文件下载失败", e);
+            return false;
+        }
+        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
+        String sceneEvaluationResult;
+        log.info("开始执行场景评价命令:" + sceneEvaluationCommand);
+        Runtime r = Runtime.getRuntime();
+        Process p = null;
+        try {
+            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId()));
+        } catch (IOException e) {
+            log.error("执行场景评价脚本失败,脚本命令为: " + sceneEvaluationCommand, e);
+            return false;
+        }
+        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        StringBuilder sb = new StringBuilder();
+        String inline;
+        while (true) {
+            try {
+                if (null == (inline = br.readLine())) break;
+            } catch (IOException e) {
+                log.error("获取脚本返回内容失败", e);
+                return false;
+            }
+            sb.append(inline).append("\n");
+        }
+        sceneEvaluationResult = sb.toString();
+        log.info("场景" + sceneEvaluationComputeParam.getSceneId() + " 的场景评价结束,结果为:" + sceneEvaluationResult);
+        String replace = StringUtil.replace(sceneEvaluationResult, "'", "\"");
+        JsonNode rootNode;
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            //JSON ----> JsonNode
+            rootNode = mapper.readTree(replace);
+        } catch (Exception e) {
+            log.error("场景" + sceneEvaluationComputeParam.getSceneId() + " 的场景评价失败:", e);
+            return false;
+        }
+        if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.COMPLEXITY)) {
+            String complexity = rootNode.path("复杂度").asText();
+            String complexityLevel = rootNode.path("复杂度等级").asText();
+            SceneComplexityPO sceneComplexityPO = new SceneComplexityPO();
+            sceneComplexityPO.setSceneId(sceneEvaluationComputeParam.getSceneId());
+            sceneComplexityPO.setComplexityId(StringUtil.getRandomUUID());
+            sceneComplexityPO.setSceneType(sceneEvaluationComputeParam.getSceneType());
+            sceneComplexityPO.setRuleId(ruleId);
+            sceneComplexityPO.setTaskId(sceneEvaluationComputeParam.getTaskId());
+            sceneComplexityPO.setComplexity(complexity);
+            sceneComplexityPO.setComplexityLevel(complexityLevel);
+            sceneComplexityPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+            sceneComplexityPO.setCreateUserId(null);
+            sceneComplexityPO.setCreateTime(TimeUtil.getNowForMysql());
+//            sceneComplexityMapper.saveSceneComplexity(sceneComplexityPO);
+        } else {
+            String risk = rootNode.path("危险度").asText();
+            String riskLevel = rootNode.path("危险度等级").asText();
+            SceneRiskPO sceneRiskPO = new SceneRiskPO();
+            sceneRiskPO.setSceneId(sceneEvaluationComputeParam.getSceneId());
+            sceneRiskPO.setRuleId(StringUtil.getRandomUUID());
+            sceneRiskPO.setSceneType(sceneEvaluationComputeParam.getSceneType());
+            sceneRiskPO.setRuleId(ruleId);
+            sceneRiskPO.setTaskId(sceneEvaluationComputeParam.getTaskId());
+            sceneRiskPO.setRisk(risk);
+            sceneRiskPO.setRiskLevel(riskLevel);
+            sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+            sceneRiskPO.setCreateUserId(null);
+            sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
+//            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+        }
+        return true;
+    }
 }

+ 4 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/infra/entity/ProjectEntity.java

@@ -18,4 +18,8 @@ public class ProjectEntity {
     private String projectType;
     private String maxSimulationTime;
     private String isChoiceGpu;
+    private String algorithm;
+    private String vehicle;
+    private String complexityEvaluationRuleId;
+    private String riskEvaluationRuleId;
 }

+ 76 - 39
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneEvaluationRuleService.java

@@ -10,13 +10,14 @@ import api.common.pojo.po.scene.SceneEvaluationRulePO;
 import api.common.pojo.po.scene.SceneRiskPO;
 import api.common.util.*;
 import com.alibaba.druid.util.StringUtils;
-import com.css.simulation.resource.server.infra.db.entity.SceneEvaluationResultEntity;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneComplexityMapper;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneEvaluationRuleMapper;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.ScenePackageMapper;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneRiskMapper;
 import com.css.simulation.resource.server.infra.feign.service.FileDownService;
 import com.css.simulation.resource.server.infra.util.AuthUtil;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import feign.Response;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
@@ -216,16 +217,13 @@ public class SceneEvaluationRuleService {
      *
      * @param param
      */
-    public void computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
-        if (StringUtils.isEmpty(param.getSceneEvaluationRuleId())) {
-            return;
-        }
+    public boolean computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
         String ruleId = param.getSceneEvaluationRuleId();
         // 获取场景评价规则
         SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
         if (sceneEvaluationRulePO == null) {
             log.error(ruleId + " 的场景评价规则已删除");
-            return;
+            return false;
         }
         // 1 判断有没有用户目录,没有则复制
         String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParam.getTaskId() + "/";
@@ -249,7 +247,7 @@ public class SceneEvaluationRuleService {
         }
         // 下载场景评价脚本到脚本目录
         if (sceneEvaluationRulePO.getScriptPath() == null) {
-            return;
+            return false;
         }
         String pyMainPath = scriptsPath + "/" + sceneEvaluationRulePO.getRuleId() + ".py";
         if (!new File(pyMainPath).exists()) {
@@ -259,35 +257,46 @@ public class SceneEvaluationRuleService {
             FileUtil.writeStringToLocalFile(pyStr, pyMainPath);
         }
         // 创建场景路径
-        if (!new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId()).exists()) {
-            FileUtil.createDirectory(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId());
+        String scenePath = evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId();
+        if (!new File(scenePath).exists()) {
+            FileUtil.createDirectory(scenePath);
         } else {
             // 一个场景只计算一次
-            return;
+            return false;
         }
-
-
-        if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), "1")) {
-            // 计算复杂度,根据场景 id 获取场景信息,下载 osc odr
-
-        } else if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), "2")) {
-            // 计算危险度 从 minio path 下载 csv (ego 和 sensors)
-
-        } else {
-            return;
+        try {
+            if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.COMPLEXITY)) {
+                // 计算复杂度,根据场景 id 获取场景信息,下载 osc odr
+                String scenarioOsc = sceneEvaluationComputeParam.getSceneXOSCPath();
+                String[] splitXosc = scenarioOsc.split("/");
+                String xoscName = splitXosc[splitXosc.length - 1];
+                downloadDependFile(sceneEvaluationComputeParam.getSceneXOSCPath(), scenePath + "/" + xoscName);
+
+                String scenarioOdr = sceneEvaluationComputeParam.getSceneXODRPath();
+                String[] splitXodr = scenarioOdr.split("/");
+                String xodrName = splitXodr[splitXodr.length - 1];
+                downloadDependFile(sceneEvaluationComputeParam.getSceneXODRPath(), scenePath + "/" + xodrName);
+            } else if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.RISK)) {
+                // 计算危险度 从 minio path 下载 csv (ego 和 sensors)
+                downloadDependFile(sceneEvaluationComputeParam.getEvaluationPath() + "/Ego.csv", scenePath + "/Ego.csv");
+                downloadDependFile(sceneEvaluationComputeParam.getEvaluationPath() + "/sensors.csv", scenePath + "/sensors.csv");
+            } else {
+                return false;
+            }
+        } catch (IOException e) {
+            log.error("文件下载失败", e);
+            return false;
         }
-
-
-        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId());
+        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
         String sceneEvaluationResult;
-        String evaluationScore = null;
         log.info("开始执行场景评价命令:" + sceneEvaluationCommand);
         Runtime r = Runtime.getRuntime();
         Process p = null;
         try {
             p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId()));
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            log.error("执行场景评价脚本失败,脚本命令为: " + sceneEvaluationCommand, e);
+            return false;
         }
         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
         StringBuilder sb = new StringBuilder();
@@ -296,43 +305,71 @@ public class SceneEvaluationRuleService {
             try {
                 if (null == (inline = br.readLine())) break;
             } catch (IOException e) {
-                throw new RuntimeException(e);
+                log.error("获取脚本返回内容失败", e);
+                return false;
             }
             sb.append(inline).append("\n");
         }
         sceneEvaluationResult = sb.toString();
         log.info("场景" + sceneEvaluationComputeParam.getSceneId() + " 的场景评价结束,结果为:" + sceneEvaluationResult);
         String replace = StringUtil.replace(sceneEvaluationResult, "'", "\"");
+        JsonNode rootNode;
         try {
-            evaluationScore = JsonUtil.stringToJson(replace);
+            ObjectMapper mapper = new ObjectMapper();
+            //JSON ----> JsonNode
+            rootNode = mapper.readTree(replace);
         } catch (Exception e) {
             log.error("场景" + sceneEvaluationComputeParam.getSceneId() + " 的场景评价失败:", e);
-            return;
+            return false;
         }
-
-
+        if (StringUtils.equals(sceneEvaluationComputeParam.getComputeType(), DictConstants.COMPLEXITY)) {
+            String complexity = rootNode.path("复杂度").asText();
+            String complexityLevel = rootNode.path("复杂度等级").asText();
+            SceneComplexityPO sceneComplexityPO = new SceneComplexityPO();
+            sceneComplexityPO.setSceneId(sceneEvaluationComputeParam.getSceneId());
+            sceneComplexityPO.setComplexityId(StringUtil.getRandomUUID());
+            sceneComplexityPO.setSceneType(sceneEvaluationComputeParam.getSceneType());
+            sceneComplexityPO.setRuleId(ruleId);
+            sceneComplexityPO.setTaskId(sceneEvaluationComputeParam.getTaskId());
+            sceneComplexityPO.setComplexity(complexity);
+            sceneComplexityPO.setComplexityLevel(complexityLevel);
+            sceneComplexityPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+            sceneComplexityPO.setCreateUserId(AuthUtil.getCurrentUserId());
+            sceneComplexityPO.setCreateTime(TimeUtil.getNowForMysql());
+            sceneComplexityMapper.saveSceneComplexity(sceneComplexityPO);
+        } else {
+            String risk = rootNode.path("危险度").asText();
+            String riskLevel = rootNode.path("危险度等级").asText();
+            SceneRiskPO sceneRiskPO = new SceneRiskPO();
+            sceneRiskPO.setSceneId(sceneEvaluationComputeParam.getSceneId());
+            sceneRiskPO.setRuleId(StringUtil.getRandomUUID());
+            sceneRiskPO.setSceneType(sceneEvaluationComputeParam.getSceneType());
+            sceneRiskPO.setRuleId(ruleId);
+            sceneRiskPO.setTaskId(sceneEvaluationComputeParam.getTaskId());
+            sceneRiskPO.setRisk(risk);
+            sceneRiskPO.setRiskLevel(riskLevel);
+            sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+            sceneRiskPO.setCreateUserId(AuthUtil.getCurrentUserId());
+            sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
+            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+        }
+        return true;
     }
 
     private void downloadDependFile(String minioPath, String localPath) throws IOException {
-//        Response egoDownload = fileDownService.download(MinioParameter.builder().objectName(sceneEvaluationComputeParam.getEvaluationPath() + "/Ego.csv").build());
         Response download = fileDownService.download(MinioParameter.builder().objectName(minioPath).build());
         Response.Body body = download.body();
-        InputStream inputStream = null;
+        InputStream inputStream;
         inputStream = body.asInputStream();
-//            File file = new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId() + "/Ego.csv");
         File file = new File(localPath);
         OutputStream outputStream = Files.newOutputStream(file.toPath());
-
-//        log.error("文件 " + evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId() + "/Ego.csv 下载失败", e);
-
         byte[] buffer = new byte[1024];
         int bytesRead;
         while ((bytesRead = inputStream.read(buffer)) != -1) {
             outputStream.write(buffer, 0, bytesRead);
-            inputStream.close();
-            outputStream.close();
         }
+        inputStream.close();
+        outputStream.close();
         download.close();
     }
-
 }

+ 19 - 12
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneReferenceLibService.java

@@ -216,7 +216,7 @@ public class SceneReferenceLibService {
                             MI.setObjectName(scenePath);
                             List<String> addressList = fileDownService.listDeepOne(MI).getInfo();
                             SceneReferenceLibPO sceneReferenceLibPO = new SceneReferenceLibPO();
-                            boolean isupdate = false;
+                            boolean isUpdate = false;
                             List<SceneReferenceLibPO> scenes = new ArrayList<>();
                             for (String address : addressList) {
                                 if (address.contains(".json")) {
@@ -244,7 +244,7 @@ public class SceneReferenceLibService {
                                     scenes = sceneReferenceLibMapper.selectAllByName(sceneReferenceLibPO);
                                     // 新增数据
                                     if (CollectionUtil.isNotEmpty(scenes)) {
-                                        isupdate = true;
+                                        isUpdate = true;
                                     }
                                     String mainBehavior = root.path("ego_action").asText();
                                     String otherBehavior = root.path("target_action").asText();
@@ -256,11 +256,7 @@ public class SceneReferenceLibService {
                                         JsonNode node = weather.next();
                                         weatherList.add(node.asText());
                                     }
-//                                    String scenarioWeather = root.withArray("scenario_weather").asText();
                                     String rodeType = root.path("rode_type").asText();
-//                                    String roadPlaneGeometry = root.path("road_plane_geometry").asText();
-
-
                                     Iterator<JsonNode> roadPlaneGeometry = root.path("road_plane_geometry").elements();
                                     List<String> roadPlaneGeometryList = new ArrayList<>();
                                     if (roadPlaneGeometry.hasNext()) {
@@ -268,14 +264,12 @@ public class SceneReferenceLibService {
                                         roadPlaneGeometryList.add(node.asText());
                                     }
 
-
                                     Iterator<JsonNode> roadProfileGeometry = root.path("road_profile_geometry").elements();
                                     List<String> roadProfileGeometryList = new ArrayList<>();
                                     if (roadProfileGeometry.hasNext()) {
                                         JsonNode node = roadProfileGeometry.next();
                                         roadProfileGeometryList.add(node.asText());
                                     }
-//                                    String roadProfileGeometry = root.path("road_profile_geometry").asText();
                                     String autonomousDrivingFunction = root.path("autonomous_driving_function").asText();
                                     String drivingArea = root.path("driving_area").asText();
                                     String sceneDescription = root.path("scene_description").asText();
@@ -311,7 +305,7 @@ public class SceneReferenceLibService {
                                     sceneReferenceLibPO.setVideoPreview(address);
                                 }
                             }
-                            if (isupdate) {
+                            if (isUpdate) {
                                 // -------------------------------- 修改数据到 mysql --------------------------------
                                 scenes.forEach(scene -> {
                                     sceneReferenceLibPO.setSceneId(scene.getSceneId());
@@ -331,10 +325,23 @@ public class SceneReferenceLibService {
                             successNum += 1;
                             // 计算复杂度
                             if (!StringUtils.isEmpty(params.getSceneEvaluationRuleId())) {
-
-
+                                String tempSceneId = scenes.get(0).getSceneId();
+                                SceneEvaluationComputeParam sceneEvaluationComputeParam = new SceneEvaluationComputeParam();
+                                sceneEvaluationComputeParam.setSceneId(isUpdate ? tempSceneId : sceneReferenceLibPO.getSceneId());
+                                sceneEvaluationComputeParam.setSceneXOSCPath(sceneReferenceLibPO.getXmlAddress());
+                                sceneEvaluationComputeParam.setSceneXODRPath(sceneReferenceLibPO.getXodrAddress());
+                                sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_REFERENCE_LIB);
+                                sceneEvaluationComputeParam.setTaskId(StringUtil.getRandomUUID());
+                                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);
+                                    }
+                                } catch (Exception e) {
+                                    log.error("计算复杂度失败", e);
+                                }
                             }
-
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
                             log.error("parse scene reference lib file failed.", e);

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

@@ -18,4 +18,6 @@ public interface SceneComplexityMapper {
             "where sc.scene_id = #{sceneId} order by sc.create_time desc" )
     List<SceneComplexityPO> querySceneEvaluationComplexityDetail(@Param("sceneId") String sceneId);
 
+    void saveSceneComplexity(SceneComplexityPO params);
+
 }

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

@@ -1,5 +1,6 @@
 package com.css.simulation.resource.server.infra.db.mysql.mapper;
 
+import api.common.pojo.po.scene.SceneComplexityPO;
 import api.common.pojo.po.scene.SceneRiskPO;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
@@ -20,4 +21,6 @@ public interface SceneRiskMapper {
             "where sr.scene_id = #{sceneId} order by sr.create_time desc" )
     List<SceneRiskPO> querySceneEvaluationRiskDetail(@Param("sceneId") String sceneId);
 
+    void saveSceneRisk(SceneRiskPO params);
+
 }

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

@@ -39,4 +39,18 @@
         ) srlsc GROUP BY scene_id;
     </select>
 
+    <insert id="saveSceneComplexity" 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 (#{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})
+    </insert>
+
 </mapper>

+ 2 - 2
simulation-resource-server/src/main/resources/mysql/mapper/ScenePackageSublistMapper.xml

@@ -25,14 +25,14 @@
 
         insert into simulation.scene_package_sublist
         (sublist_id,sublist_name,weight,rule_name,package_and_rules,
-        scene_natural_ids,scene_traffic_ids,scene_statue_ids,scene_generalization_ids,
+        scene_natural_ids,scene_traffic_ids,scene_statue_ids,scene_generalization_ids,scene_reference_lib_ids,
         scene_num,parent_id,root_id,seq,package_level,remarks,
         create_user_id,create_time,modify_user_id,modify_time,
         is_deleted,share,tree_node,template_id,example_id,rule_name_array) values
         <foreach collection="list" index="index" item="item" separator=",">
             (#{item.id,jdbcType=VARCHAR},#{item.sublistName,jdbcType=VARCHAR},#{item.weight,jdbcType=VARCHAR},#{item.ruleName,jdbcType=VARCHAR},
             #{item.packageAndRules,jdbcType=VARCHAR},
-            #{item.sceneNaturalIds}, #{item.sceneTrafficIds}, #{item.sceneStatueIds}, #{item.sceneGeneralizationIds},
+            #{item.sceneNaturalIds}, #{item.sceneTrafficIds}, #{item.sceneStatueIds}, #{item.sceneGeneralizationIds},#{item.sceneReferenceLibIds},
             #{item.sceneNum},#{item.parentId,jdbcType=VARCHAR},#{item.rootId,jdbcType=VARCHAR},#{index}+1,
             #{item.packageLevel},#{item.remarks},
             #{item.createUserId,jdbcType=VARCHAR},#{item.createTime},#{item.modifyUserId,jdbcType=VARCHAR},#{item.modifyTime},

+ 13 - 0
simulation-resource-server/src/main/resources/mysql/mapper/SceneRiskMapper.xml

@@ -39,4 +39,17 @@
         ) srlsc GROUP BY scene_id;
     </select>
 
+    <insert id="saveSceneRisk" parameterType="api.common.pojo.po.scene.SceneRiskPO">
+        insert into simulation.scene_risk
+        (risk_id, scene_id, scene_type, rule_id,algorithm_id,vehicle_id
+         task_id, risk, risk_level,
+         is_deleted, create_user_id, create_time
+        )
+        values (#{riskId,jdbcType=VARCHAR}, #{sceneId,jdbcType=VARCHAR}, #{sceneType,jdbcType=VARCHAR},
+                #{ruleId,jdbcType=VARCHAR},#{algorithmId,jdbcType=VARCHAR},#{vehicleId,jdbcType=VARCHAR},
+                #{taskId,jdbcType=VARCHAR}, #{risk,jdbcType=VARCHAR},
+                #{riskLevel,jdbcType=VARCHAR},
+                #{isDeleted,jdbcType=VARCHAR},
+                #{createUserId,jdbcType=VARCHAR}, #{createTime})
+    </insert>
 </mapper>