|
@@ -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;
|
|
|
+ }
|
|
|
}
|