Browse Source

优化yaml导出测试

dev-lhz
chenzhihang 1 year ago
parent
commit
cebc92ea83
3 changed files with 84 additions and 47 deletions
  1. +0
    -6
      ruoyi-modules/management-platform/pom.xml
  2. +11
    -6
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java
  3. +73
    -35
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java

+ 0
- 6
ruoyi-modules/management-platform/pom.xml View File

@@ -242,12 +242,6 @@
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>

<dependency>
<groupId>org.jyaml</groupId>
<artifactId>jyaml</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

<build>


+ 11
- 6
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java View File

@@ -16,6 +16,7 @@ import com.ruoyi.platform.service.*;
import com.ruoyi.platform.utils.HttpUtils;
import com.ruoyi.platform.utils.JacksonUtil;
import com.ruoyi.platform.utils.JsonUtils;
import com.ruoyi.platform.utils.YamlUtils;
import com.ruoyi.platform.vo.ModelsVo;
import com.ruoyi.platform.vo.NewDatasetVo;
import com.ruoyi.system.api.model.LoginUser;
@@ -32,6 +33,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
@@ -477,10 +479,10 @@ public class ExperimentServiceImpl implements ExperimentService {
sourceParams.put("train_name", sourceTaskId);
sourceParams.put("preprocess_code", projectMap);

if (target != null && target.size() > 0){
for (Map<String, Object> targetMap : target){
String targetTaskId = (String)targetMap.get("task_id");
Map<String, Object> datasetExportMap = (Map<String, Object>)datasetExport.get(targetTaskId);
if (target != null && target.size() > 0) {
for (Map<String, Object> targetMap : target) {
String targetTaskId = (String) targetMap.get("task_id");
Map<String, Object> datasetExportMap = (Map<String, Object>) datasetExport.get(targetTaskId);
List<Map> datasets = (List<Map>) datasetExportMap.get("datasets");
if (datasets != null) {
for (Map<String, Object> dataset : datasets) {
@@ -543,8 +545,11 @@ public class ExperimentServiceImpl implements ExperimentService {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String paramName = jsonObject.getString("param_name");
if (!"model_version".equals(paramName)) {
Object paramValue = jsonObject.get("param_value");
String paramValue = jsonObject.getString("param_value");
if (YamlUtils.isNumeric(paramValue)) {
BigDecimal bigDecimal = new BigDecimal(paramValue);
trainParam.put(paramName, bigDecimal);
} else {
trainParam.put(paramName, paramValue);
}
}


+ 73
- 35
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java View File

@@ -1,15 +1,22 @@
package com.ruoyi.platform.utils;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.DumperOptions;
//import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


import org.ho.yaml.Yaml;
//import org.ho.yaml.Yaml;

@Component
public class YamlUtils {

/**
@@ -19,32 +26,15 @@ public class YamlUtils {
* @param path 文件路径
* @param fileName 文件名
*/
// public static void generateYamlFile(Map<String, Object> data, String path, String fileName) {
// DumperOptions options = new DumperOptions();
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
// options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
//
//
// // 创建Yaml实例
// Yaml yaml = new Yaml(options);
//
// File directory = new File(path);
// if (!directory.exists()) {
// boolean isCreated = directory.mkdirs();
// if (!isCreated) {
// throw new RuntimeException("创建路径失败: " + path);
// }
// }
//
// String fullPath = path + "/" + fileName + ".yaml";
//
// try (FileWriter writer = new FileWriter(fullPath)) {
// yaml.dump(data, writer);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
public static void generateYamlFile(Map<String, Object> data, String path, String fileName) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);


// 创建Yaml实例
Yaml yaml = new Yaml(options);

File directory = new File(path);
if (!directory.exists()) {
boolean isCreated = directory.mkdirs();
@@ -54,15 +44,60 @@ public class YamlUtils {
}

String fullPath = path + "/" + fileName + ".yaml";
File dumpFile = new File(fullPath);
try {
Yaml.dump(data, dumpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();

try (FileWriter writer = new FileWriter(fullPath)) {
String dump = yaml.dump(data);

yaml.dump(data, writer);
} catch (IOException e) {
e.printStackTrace();
}
}


// public static void generateYamlFile1(Object data, String path, String fileName) {
// try {
// YAMLFactory yamlFactory = new YAMLFactory();
// yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
// yamlFactory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
////
// ObjectMapper objectMapper = new ObjectMapper(yamlFactory);
//// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
// String s = objectMapper.writeValueAsString(data);
//
// File directory = new File(path);
// if (!directory.exists()) {
// boolean isCreated = directory.mkdirs();
// if (!isCreated) {
// throw new RuntimeException("创建路径失败: " + path);
// }
// }
//
// try {
// DumperOptions options = new DumperOptions();
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
// // 创建Yaml实例
// Yaml yaml = new Yaml(options);
//
// String fullPath = path + "/" + fileName + ".yaml";
// FileWriter writer = new FileWriter(fullPath);
//
// yaml.dump(s, writer);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
//
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
//
//
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// }
// }


/**
* 读取YAML文件并将其内容转换为Map<String, Object>
*
@@ -72,11 +107,14 @@ public class YamlUtils {
public static Map<String, Object> loadYamlFile(String filePath) {
Yaml yaml = new Yaml();
try (InputStream inputStream = new FileInputStream(new File(filePath))) {
HashMap<String, Object> load = (HashMap<String, Object>) yaml.load(inputStream);
return load;
return yaml.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

public static boolean isNumeric(String str) {
return str.matches("-?\\\\d+(\\\\.\\\\d+)?"); // 匹配整数或小数,包括负数
}
}

Loading…
Cancel
Save