Browse Source

优化yaml导出测试

dev-lhz
chenzhihang 1 year ago
parent
commit
f7b89ded5a
2 changed files with 44 additions and 25 deletions
  1. +4
    -4
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java
  2. +40
    -21
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java

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

@@ -13,13 +13,11 @@ import com.ruoyi.platform.mapper.ExperimentDao;
import com.ruoyi.platform.mapper.ExperimentInsDao;
import com.ruoyi.platform.mapper.ModelDependency1Dao;
import com.ruoyi.platform.service.*;
import com.ruoyi.platform.utils.FileUtil;
import com.ruoyi.platform.utils.HttpUtils;
import com.ruoyi.platform.utils.JacksonUtil;
import com.ruoyi.platform.utils.JsonUtils;
import com.ruoyi.platform.vo.ModelsVo;
import com.ruoyi.platform.vo.NewDatasetVo;
import com.ruoyi.platform.vo.VersionVo;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
@@ -534,8 +532,10 @@ public class ExperimentServiceImpl implements ExperimentService {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String paramName = jsonObject.getString("param_name");
Object paramValue = jsonObject.get("param_value");
trainParam.put(paramName, paramValue);
if (!"model_version".equals(paramName)) {
Object paramValue = jsonObject.get("param_value");
trainParam.put(paramName, paramValue);
}
}
modelMetaVo.setParams(trainParam);



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

@@ -1,32 +1,49 @@
package com.ruoyi.platform.utils;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;
//import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.Iterator;
import java.util.Map;
import org.yaml.snakeyaml.nodes.Tag;


import org.ho.yaml.Yaml;

public class YamlUtils {

/**
* 将Map对象转换为YAML格式并写入指定路径的文件中
*
* @param data Map对象
* @param path 文件路径
* @param fileName 文件名
* @param data Map对象
* @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();
@@ -36,11 +53,12 @@ public class YamlUtils {
}

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

}
}

@@ -53,7 +71,8 @@ public class YamlUtils {
public static Map<String, Object> loadYamlFile(String filePath) {
Yaml yaml = new Yaml();
try (InputStream inputStream = new FileInputStream(new File(filePath))) {
return yaml.load(inputStream);
Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
return load;
} catch (IOException e) {
e.printStackTrace();
return null;


Loading…
Cancel
Save