diff --git a/auto-solution-admin/src/main/java/com/solution/web/controller/algo/AlgorithmController.java b/auto-solution-admin/src/main/java/com/solution/web/controller/algo/AlgorithmController.java index 5319e22..a6cb041 100644 --- a/auto-solution-admin/src/main/java/com/solution/web/controller/algo/AlgorithmController.java +++ b/auto-solution-admin/src/main/java/com/solution/web/controller/algo/AlgorithmController.java @@ -1,8 +1,14 @@ package com.solution.web.controller.algo; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; +import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; @@ -37,6 +43,51 @@ public class AlgorithmController extends BaseController { @Autowired private IAlgorithmService algorithmService; + /** + * 导出规则列表 + */ + @ApiOperation("运行python程序") + @PostMapping("/run") + public Object run(@RequestBody Algorithm algorithm) { + List list = algorithmService.selectAlgorithmList(algorithm); + ObjectMapper objectMapper = new ObjectMapper(); + + for (Algorithm al : list) { + String codePath = al.getCodePath(); + + try { + String jsonInput = objectMapper.writeValueAsString(al); + String pythonExe = "E:\\Apps\\anaconda3\\python.exe"; + ProcessBuilder pb = new ProcessBuilder(pythonExe, codePath); + Process process = pb.start(); + + try (OutputStream os = process.getOutputStream()) { + os.write(jsonInput.getBytes(StandardCharsets.UTF_8)); + os.flush(); + } + + String result; + // 修改你 Java 代码中的这一段 + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { + result = reader.lines().collect(Collectors.joining("\n")); + } + + int exitCode = process.waitFor(); + if (exitCode != 0) { + return "Python执行失败,错误码:" + exitCode; + } + + return objectMapper.readTree(result); + } catch (Exception e) { + e.printStackTrace(); + return "执行异常: " + e.getMessage(); + } + } + return "未找到算法"; + } + + /** * 查询规则列表 */ @@ -56,7 +107,7 @@ public class AlgorithmController extends BaseController { @PreAuthorize("@ss.hasPermi('algo:algorithm:export')") @Log(title = "规则", businessType = BusinessType.EXPORT) @PostMapping("/export") - public void export(HttpServletResponse response, Algorithm algorithm) { + public void export(HttpServletResponse response, @RequestBody Algorithm algorithm) { List list = algorithmService.selectAlgorithmList(algorithm); ExcelUtil util = new ExcelUtil(Algorithm.class); util.exportExcel(response, list, "规则数据"); diff --git a/auto-solution-algo/src/main/java/com/solution/algo/domain/Algorithm.java b/auto-solution-algo/src/main/java/com/solution/algo/domain/Algorithm.java index 07d2755..f18656d 100644 --- a/auto-solution-algo/src/main/java/com/solution/algo/domain/Algorithm.java +++ b/auto-solution-algo/src/main/java/com/solution/algo/domain/Algorithm.java @@ -34,6 +34,10 @@ public class Algorithm @Excel(name = "算法描述") private String description; + /** 算法配置 */ + @Excel(name = "算法配置") + private String algoConfig; + /** 算法参数定义信息 */ private List algorithmParamList; @@ -87,6 +91,16 @@ public class Algorithm return description; } + public void setAlgoConfig(String algoConfig) + { + this.algoConfig = algoConfig; + } + + public String getAlgoConfig() + { + return algoConfig; + } + public List getAlgorithmParamList() { return algorithmParamList; @@ -105,6 +119,7 @@ public class Algorithm .append("type", getType()) .append("codePath", getCodePath()) .append("description", getDescription()) + .append("algoConfig", getAlgoConfig()) .append("algorithmParamList", getAlgorithmParamList()) .toString(); } diff --git a/auto-solution-algo/src/main/resources/mapper/algo/AlgorithmMapper.xml b/auto-solution-algo/src/main/resources/mapper/algo/AlgorithmMapper.xml index 4d1afdb..22cc9f1 100644 --- a/auto-solution-algo/src/main/resources/mapper/algo/AlgorithmMapper.xml +++ b/auto-solution-algo/src/main/resources/mapper/algo/AlgorithmMapper.xml @@ -10,6 +10,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -25,7 +26,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select id, name, type, code_path, description from algorithm + select id, name, type, code_path, description, algo_config from algorithm @@ -57,12 +59,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" type, code_path, description, + algo_config, #{name}, #{type}, #{codePath}, #{description}, + #{algoConfig}, @@ -73,6 +77,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" type = #{type}, code_path = #{codePath}, description = #{description}, + algo_config = #{algoConfig}, where id = #{id}