From cebc92ea837c5cf8a3e3e9683a2448db10e7927b Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Fri, 27 Sep 2024 14:57:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96yaml=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-modules/management-platform/pom.xml | 6 - .../service/impl/ExperimentServiceImpl.java | 17 ++- .../com/ruoyi/platform/utils/YamlUtils.java | 108 ++++++++++++------ 3 files changed, 84 insertions(+), 47 deletions(-) diff --git a/ruoyi-modules/management-platform/pom.xml b/ruoyi-modules/management-platform/pom.xml index 116809d2..ba689d0e 100644 --- a/ruoyi-modules/management-platform/pom.xml +++ b/ruoyi-modules/management-platform/pom.xml @@ -242,12 +242,6 @@ jedis 3.6.0 - - - org.jyaml - jyaml - 1.3 - diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java index 52ac55da..f91e1a2f 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentServiceImpl.java @@ -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 targetMap : target){ - String targetTaskId = (String)targetMap.get("task_id"); - Map datasetExportMap = (Map)datasetExport.get(targetTaskId); + if (target != null && target.size() > 0) { + for (Map targetMap : target) { + String targetTaskId = (String) targetMap.get("task_id"); + Map datasetExportMap = (Map) datasetExport.get(targetTaskId); List datasets = (List) datasetExportMap.get("datasets"); if (datasets != null) { for (Map 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); } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java index cdf022c8..47341e79 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/YamlUtils.java @@ -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 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 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 * @@ -72,11 +107,14 @@ public class YamlUtils { public static Map loadYamlFile(String filePath) { Yaml yaml = new Yaml(); try (InputStream inputStream = new FileInputStream(new File(filePath))) { - HashMap load = (HashMap) 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+)?"); // 匹配整数或小数,包括负数 + } }