李春阳 1 年間 前
コミット
4072d547b6

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

@@ -3,6 +3,7 @@ package api.common.pojo.param.scene;
 import lombok.*;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * 场景评价规则
@@ -18,6 +19,7 @@ public class SceneEvaluationComputeParam implements Serializable {
 
     // 场景评价规则 Id
     private String sceneId;
+    private List<String> copySceneId;
     private String sceneXOSCPath;
     private String sceneXODRPath;
     private String sceneType;

+ 125 - 96
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/domain/service/TaskDomainService.java

@@ -135,6 +135,8 @@ public class TaskDomainService {
         List<SceneEntity> sceneEntityList = getSceneList(projectId, packageId);
         Map<String, SceneEntity> sceneEntityMap = sceneEntityList.stream().collect(Collectors.toMap(SceneEntity::getId, Function.identity()));
         List<LeafIndexEntity> leafIndexList = new ArrayList<>();
+        List<SceneEvaluationComputeParam> sceneComplexityEvaluationComputeParamList = new ArrayList<>();
+        List<SceneEvaluationComputeParam> sceneRiskEvaluationComputeParamList = new ArrayList<>();
         for (int i = 0; i < leafIndexTemplateList.size(); i++) {
             String scoreExplain = null; // 每个叶子指标下的任务的得分说明一样和叶子指标一致
             IndexTemplateEntity leafIndexTemplate = leafIndexTemplateList.get(i);
@@ -260,13 +262,7 @@ public class TaskDomainService {
                         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);
-                        }
+                        sceneComplexityEvaluationComputeParamList.add(sceneEvaluationComputeParam);
                     }
                     // 计算复杂度和危险度
                     if (!StringUtils.isEmpty(projectEntity.getRiskEvaluationRuleId())) {
@@ -278,13 +274,7 @@ public class TaskDomainService {
                         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);
-                        }
+                        sceneRiskEvaluationComputeParamList.add(sceneEvaluationComputeParam);
                     }
                 }
             }
@@ -332,6 +322,24 @@ public class TaskDomainService {
 
             leafIndexList.add(leafIndex);
         }
+        if (!StringUtils.isEmpty(projectEntity.getComplexityEvaluationRuleId())) {
+            try {
+                SceneImportParam sceneImportParam = new SceneImportParam();
+                sceneImportParam.setSceneEvaluationRuleId(projectEntity.getComplexityEvaluationRuleId());
+                computeSceneReference(sceneImportParam, sceneComplexityEvaluationComputeParamList);
+            } catch (Exception e) {
+                log.error("计算复杂度失败", e);
+            }
+        }
+        if (!StringUtils.isEmpty(projectEntity.getRiskEvaluationRuleId())) {
+            try {
+                SceneImportParam sceneImportParam = new SceneImportParam();
+                sceneImportParam.setSceneEvaluationRuleId(projectEntity.getRiskEvaluationRuleId());
+                computeSceneReference(sceneImportParam, sceneRiskEvaluationComputeParamList);
+            } catch (Exception e) {
+                log.error("计算复杂度失败", e);
+            }
+        }
         // 删除临时文件
         FileUtil.rm(linuxTempPath + "scene/evaluation/" + projectId + "/");   // 删除临时文件
         // 保存叶子指标得分
@@ -343,7 +351,6 @@ public class TaskDomainService {
     }
 
     public void computeFirst(List<LeafIndexEntity> leafIndexList, List<IndexTemplateEntity> allIndexTemplateList, String projectId, int maxLevel) {
-
         log.debug("计算父指标得分:" + leafIndexList);
         Iterator<LeafIndexEntity> leafTaskIndexIterator = leafIndexList.iterator();
         // 把 1 级的指标得分直接保存
@@ -451,7 +458,7 @@ public class TaskDomainService {
      *
      * @param param
      */
-    public boolean computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
+    public boolean computeSceneReference(SceneImportParam param, List<SceneEvaluationComputeParam> sceneEvaluationComputeParams) {
         String ruleId = param.getSceneEvaluationRuleId();
         // 获取场景评价规则
         SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
@@ -460,7 +467,7 @@ public class TaskDomainService {
             return false;
         }
         // 1 判断有没有用户目录,没有则复制
-        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParam.getTaskId() + "/";
+        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParams.get(0).getTaskId() + "/";
         String scriptsPath = evaluationDirectoryOfUser + "scripts/";
         if (!new File(evaluationDirectoryOfUser).exists()) {
             // 1 将场景评价规则脚本保存到 script 目录
@@ -468,8 +475,6 @@ public class TaskDomainService {
             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) {
@@ -484,102 +489,126 @@ public class TaskDomainService {
         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");
+        String scenePathFather = evaluationDirectoryOfUser + "scene/";
+        for (SceneEvaluationComputeParam sceneEvaluationComputeParam : sceneEvaluationComputeParams) {
+            // 创建场景路径
+            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;
             }
-        } catch (Exception e) {
-            log.error("文件下载失败", e);
-            return false;
         }
-        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
+        //        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
+        String sceneEvaluationCommand = "python " + pyMainPath + " -root_path  " + (scenePathFather);
         String sceneEvaluationResult;
         log.info("开始执行场景评价命令:" + sceneEvaluationCommand);
         Runtime r = Runtime.getRuntime();
         Process p = null;
         try {
-            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId()));
+            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + "result"));
         } 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 {
+            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+            String inline;
+            while ((inline = br.readLine()) != null) {
+                sb.append(inline).append("\n");
+            }
+            br.close();
+        } catch (IOException e) {
+            log.error("获取场景评价脚本返回内容失败", e);
+            return false;
+        }
+        sceneEvaluationResult = sb.toString();
+        log.info("场景评价结束,结果为:" + sceneEvaluationResult);
+        for (SceneEvaluationComputeParam sceneEvaluationComputeParam : sceneEvaluationComputeParams) {
+            // 读文件
+            StringBuilder result = new StringBuilder();
             try {
-                if (null == (inline = br.readLine())) break;
+                FileInputStream fileInputStream = new FileInputStream(scenePathFather + sceneEvaluationComputeParam.getSceneId() + "/scenario_evaluation.json");
+                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
+
+                String line;
+                while ((line = bufferedReader.readLine()) != null) {
+                    result.append(line).append("\n");
+                }
+                bufferedReader.close();
             } catch (IOException e) {
-                log.error("获取脚本返回内容失败", 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(matchLevelEnumByLevel(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(matchLevelEnumByLevel(riskLevel));
-            sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
-            sceneRiskPO.setCreateUserId(null);
-            sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
-            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+            String resultStr = result.toString();
+            String replace = StringUtil.replace(resultStr, "'", "\"");
+            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(matchLevelEnumByLevel(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(matchLevelEnumByLevel(riskLevel));
+                sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+                sceneRiskPO.setCreateUserId(null);
+                sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
+                sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+            }
         }
+        // 删除临时文件
+        FileUtil.deleteFolder(linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParams.get(0).getTaskId());   // 删除临时文件
         return true;
     }
 

+ 9 - 7
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneAccidentService.java

@@ -773,6 +773,7 @@ public class SceneAccidentService {
                     List<String> listScene = fileDownService.listDeepOne(MI).getInfo();
                     log.info("共需要上传 " + listScene.size() + " 个交通事故场景");
                     String taskId = api.common.util.StringUtil.getRandomUUID();
+                    List<SceneEvaluationComputeParam> paramList = new ArrayList<>();
                     for (String scenePath : listScene) {
                         try {
                             MI.setObjectName(scenePath);
@@ -881,14 +882,10 @@ public class SceneAccidentService {
                                 sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_ACCIDENT);
                                 sceneEvaluationComputeParam.setTaskId(taskId);
                                 sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
-                                try {
-                                    boolean sceneEvaluationResult = sceneEvaluationRuleService.computeSceneReference(params, sceneEvaluationComputeParam);
-                                    if (isupdate.equals("0") && sceneEvaluationResult) {
-                                        sceneEvaluationRuleService.copySceneComplexityResult(taskId, tempSceneId, scenes.stream().map(SceneAccidentPO::getAccidentId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
-                                    }
-                                } catch (Exception e) {
-                                    log.error("计算复杂度失败", e);
+                                if (isupdate.equals("0")) {
+                                    sceneEvaluationComputeParam.setCopySceneId(scenes.stream().map(SceneAccidentPO::getAccidentId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
                                 }
+                                paramList.add(sceneEvaluationComputeParam);
                             }
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
@@ -896,6 +893,11 @@ public class SceneAccidentService {
                             falseNum = falseNum + 1;
                         }
                     }
+                    try {
+                        sceneEvaluationRuleService.computeSceneReference(params, paramList);
+                    } catch (Exception e) {
+                        log.error("计算复杂度失败", e);
+                    }
                 }
             }
             sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_4);

+ 110 - 83
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneEvaluationRuleService.java

@@ -225,7 +225,7 @@ public class SceneEvaluationRuleService {
      *
      * @param param
      */
-    public boolean computeSceneReference(SceneImportParam param, SceneEvaluationComputeParam sceneEvaluationComputeParam) {
+    public boolean computeSceneReference(SceneImportParam param, List<SceneEvaluationComputeParam> sceneEvaluationComputeParams) {
         String ruleId = param.getSceneEvaluationRuleId();
         // 获取场景评价规则
         SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
@@ -234,7 +234,7 @@ public class SceneEvaluationRuleService {
             return false;
         }
         // 1 判断有没有用户目录,没有则复制
-        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParam.getTaskId() + "/";
+        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParams.get(0).getTaskId() + "/";
         String scriptsPath = evaluationDirectoryOfUser + "scripts/";
         if (!new File(evaluationDirectoryOfUser).exists()) {
             // 1 将场景评价规则脚本保存到 script 目录
@@ -242,8 +242,6 @@ public class SceneEvaluationRuleService {
             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) {
@@ -255,6 +253,7 @@ public class SceneEvaluationRuleService {
                 }
             }
         }
+
         // 下载场景评价脚本到脚本目录
         if (sceneEvaluationRulePO.getScriptPath() == null) {
             return false;
@@ -268,103 +267,131 @@ public class SceneEvaluationRuleService {
                 return false;
             }
         }
-        // 创建场景路径
-        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];
-                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");
+        String scenePathFather = evaluationDirectoryOfUser + "scene/";
+        for (SceneEvaluationComputeParam sceneEvaluationComputeParam : sceneEvaluationComputeParams) {
+            // 创建场景路径
+            String scenePath = scenePathFather + 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];
+                    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() + "/sensor.csv", scenePath + "/sensor.csv");
+                } else {
+                    return false;
+                }
+            } catch (IOException e) {
+                log.error("文件下载失败", e);
                 return false;
             }
-        } catch (IOException e) {
-            log.error("文件下载失败", e);
-            return false;
         }
-        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
+//        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (scenePath);
+        String sceneEvaluationCommand = "python " + pyMainPath + " -root_path  " + (scenePathFather);
         String sceneEvaluationResult;
         log.info("开始执行场景评价命令:" + sceneEvaluationCommand);
         Runtime r = Runtime.getRuntime();
-        Process p = null;
+        Process p;
         try {
-            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + sceneEvaluationComputeParam.getSceneId()));
+            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + "result"));
         } 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 {
+            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+            String inline;
+            while ((inline = br.readLine()) != null) {
+                sb.append(inline).append("\n");
+            }
+            br.close();
+        } catch (IOException e) {
+            log.error("获取场景评价脚本返回内容失败", e);
+            return false;
+        }
+        sceneEvaluationResult = sb.toString();
+        log.info("场景评价结束,结果为:" + sceneEvaluationResult);
+        for (SceneEvaluationComputeParam sceneEvaluationComputeParam : sceneEvaluationComputeParams) {
+            // 读文件
+            StringBuilder result = new StringBuilder();
             try {
-                if (null == (inline = br.readLine())) break;
+                FileInputStream fileInputStream = new FileInputStream(scenePathFather + sceneEvaluationComputeParam.getSceneId() + "/scenario_evaluation.json");
+                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
+
+                String line;
+                while ((line = bufferedReader.readLine()) != null) {
+                    result.append(line).append("\n");
+                }
+                bufferedReader.close();
             } catch (IOException e) {
-                log.error("获取脚本返回内容失败", 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(matchLevelEnumByLevel(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(matchLevelEnumByLevel(riskLevel));
-            sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
-            sceneRiskPO.setCreateUserId(AuthUtil.getCurrentUserId());
-            sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
-            sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+            String resultStr = result.toString();
+            String replace = StringUtil.replace(resultStr, "'", "\"");
+            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(matchLevelEnumByLevel(complexityLevel));
+                sceneComplexityPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+                sceneComplexityPO.setCreateUserId(AuthUtil.getCurrentUserId());
+                sceneComplexityPO.setCreateTime(TimeUtil.getNowForMysql());
+                sceneComplexityMapper.saveSceneComplexity(sceneComplexityPO);
+                if (CollectionUtil.isNotEmpty(sceneEvaluationComputeParam.getCopySceneId())) {
+                    copySceneComplexityResult(sceneEvaluationComputeParam.getTaskId(), sceneEvaluationComputeParam.getSceneId(), sceneEvaluationComputeParam.getCopySceneId());
+                }
+            } 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(matchLevelEnumByLevel(riskLevel));
+                sceneRiskPO.setIsDeleted(DictConstants.IS_NOT_DELETED);
+                sceneRiskPO.setCreateUserId(AuthUtil.getCurrentUserId());
+                sceneRiskPO.setCreateTime(TimeUtil.getNowForMysql());
+                sceneRiskMapper.saveSceneRisk(sceneRiskPO);
+            }
         }
+        // 删除临时文件
+        FileUtil.deleteFolder(linuxTempPath + "scene/evaluation/" + sceneEvaluationComputeParams.get(0).getTaskId());   // 删除临时文件
         return true;
     }
 

+ 10 - 7
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneNaturalService.java

@@ -548,6 +548,7 @@ public class SceneNaturalService {
                     List<String> listScene = fileDownService.listDeepOne(MI).getInfo();
                     log.info("共需要上传 " + listScene.size() + " 个自然驾驶场景");
                     String taskId = api.common.util.StringUtil.getRandomUUID();
+                    List<SceneEvaluationComputeParam> paramList = new ArrayList<>();
                     HashSet<String> strings = new HashSet<>(listScene);
                     log.info("去重之后还剩 " + strings.size() + " 个自然驾驶场景");
                     int addNumber = 0;
@@ -705,14 +706,11 @@ public class SceneNaturalService {
                                 sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_NATURAL);
                                 sceneEvaluationComputeParam.setTaskId(taskId);
                                 sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
-                                try {
-                                    boolean sceneEvaluationResult = sceneEvaluationRuleService.computeSceneReference(parms, sceneEvaluationComputeParam);
-                                    if (update && sceneEvaluationResult) {
-                                        sceneEvaluationRuleService.copySceneComplexityResult(taskId, tempSceneId, scenes.stream().map(SceneNaturalPO::getNaturalId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
-                                    }
-                                } catch (Exception e) {
-                                    log.error("计算复杂度失败", e);
+                                if (update) {
+                                    sceneEvaluationComputeParam.setCopySceneId(scenes.stream().map(SceneNaturalPO::getNaturalId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
                                 }
+                                paramList.add(sceneEvaluationComputeParam);
+
                             }
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
@@ -721,6 +719,11 @@ public class SceneNaturalService {
                         }
                     }
                     log.info("共新增 " + addNumber + " 个自然驾驶场景,修改 " + updateNumber + "个场景。");
+                    try {
+                        sceneEvaluationRuleService.computeSceneReference(parms, paramList);
+                    } catch (Exception e) {
+                        log.error("计算复杂度失败", e);
+                    }
                 }
             }
             sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_4);

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

@@ -30,8 +30,6 @@ import java.util.stream.Collectors;
 @Service
 public class SceneReferenceLibService {
 
-    @Value("${server.linux-path.temp}")
-    private String linuxTempPath;
     @Resource
     private SceneReferenceLibMapper sceneReferenceLibMapper;
 
@@ -215,6 +213,7 @@ public class SceneReferenceLibService {
                     List<String> listScene = fileDownService.listDeepOne(MI).getInfo();
                     log.info("importMinio() 共需要上传 " + listScene.size() + " 个基准场景");
                     String taskId = StringUtil.getRandomUUID();
+                    List<SceneEvaluationComputeParam> paramList = new ArrayList<>();
                     for (String scenePath : listScene) {
                         try {
                             MI.setObjectName(scenePath);
@@ -327,7 +326,6 @@ public class SceneReferenceLibService {
 
                             }
                             successNum += 1;
-                            // 计算复杂度
                             if (!StringUtils.isEmpty(params.getSceneEvaluationRuleId())) {
                                 String tempSceneId = scenes.get(0).getSceneId();
                                 SceneEvaluationComputeParam sceneEvaluationComputeParam = new SceneEvaluationComputeParam();
@@ -337,14 +335,10 @@ public class SceneReferenceLibService {
                                 sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_REFERENCE_LIB);
                                 sceneEvaluationComputeParam.setTaskId(taskId);
                                 sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
-                                try {
-                                    boolean sceneEvaluationResult = sceneEvaluationRuleService.computeSceneReference(params, sceneEvaluationComputeParam);
-                                    if (isUpdate && sceneEvaluationResult) {
-                                        sceneEvaluationRuleService.copySceneComplexityResult(taskId, tempSceneId, scenes.stream().map(SceneReferenceLibPO::getSceneId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
-                                    }
-                                } catch (Exception e) {
-                                    log.error("计算复杂度失败", e);
+                                if (isUpdate) {
+                                    sceneEvaluationComputeParam.setCopySceneId(scenes.stream().map(SceneReferenceLibPO::getSceneId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
                                 }
+                                paramList.add(sceneEvaluationComputeParam);
                             }
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
@@ -352,8 +346,14 @@ public class SceneReferenceLibService {
                             falseNum += 1;
                         }
                     }
-                    // 删除临时文件
-                    FileUtil.deleteFolder(linuxTempPath + "scene/evaluation/" + taskId);   // 删除临时文件
+                    // 计算复杂度
+                    if (!StringUtils.isEmpty(params.getSceneEvaluationRuleId())) {
+                        try {
+                            sceneEvaluationRuleService.computeSceneReference(params, paramList);
+                        } catch (Exception e) {
+                            log.error("计算复杂度失败", e);
+                        }
+                    }
                 }
             }
             sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_4);

+ 9 - 9
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneStandardsService.java

@@ -335,6 +335,7 @@ public class SceneStandardsService {
                     List<String> listScene = fileDownService.listDeepOne(MI).getInfo();
                     log.info("importMinio() 共需要上传 " + listScene.size() + " 个标准法规场景");
                     String taskId = api.common.util.StringUtil.getRandomUUID();
+                    List<SceneEvaluationComputeParam> paramList = new ArrayList<>();
                     for (String scenePath : listScene) {
                         try {
                             MI.setObjectName(scenePath);
@@ -432,23 +433,22 @@ public class SceneStandardsService {
                                 sceneEvaluationComputeParam.setSceneType(DictConstants.SCENE_STANDARD);
                                 sceneEvaluationComputeParam.setTaskId(taskId);
                                 sceneEvaluationComputeParam.setComputeType(DictConstants.COMPLEXITY);
-                                try {
-                                    boolean sceneEvaluationResult = sceneEvaluationRuleService.computeSceneReference(parms, sceneEvaluationComputeParam);
-                                    if (isupdate.equals("0") && sceneEvaluationResult) {
-                                        sceneEvaluationRuleService.copySceneComplexityResult(taskId, tempSceneId, scenes.stream().map(StandardsRegulationsPO::getRegulationsId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
-                                    }
-                                } catch (Exception e) {
-                                    log.error("计算复杂度失败", e);
+                                if (isupdate.equals("0")) {
+                                    sceneEvaluationComputeParam.setCopySceneId(scenes.stream().map(StandardsRegulationsPO::getRegulationsId).filter(sceneId -> !StringUtils.equals(tempSceneId, sceneId)).collect(Collectors.toList()));
                                 }
+                                paramList.add(sceneEvaluationComputeParam);
                             }
-
-
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
                             e.printStackTrace();
                             falseNum = falseNum + 1;
                         }
                     }
+                    try {
+                        sceneEvaluationRuleService.computeSceneReference(parms, paramList);
+                    } catch (Exception e) {
+                        log.error("计算复杂度失败", e);
+                    }
                 }
             }
             sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_4);