|
@@ -0,0 +1,143 @@
|
|
|
+package com.ips.system.utils;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class AlgorithmCaller {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AlgorithmCaller.class);
|
|
|
+ private static final String DEFAULT_PYTHON = "python";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取算法的根目录
|
|
|
+ * @param pythonFilePath Python文件的完整路径,如"d:/xxx/yyy/zzz/dd.py"
|
|
|
+ * @return 返回根目录路径,如"d:/xxx/yyy/zzz/"
|
|
|
+ */
|
|
|
+ public static String getAlgorithmRootPath(String pythonFilePath) {
|
|
|
+ if (pythonFilePath == null || pythonFilePath.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("Python file path cannot be null or empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ Path path = Paths.get(pythonFilePath);
|
|
|
+ Path parentPath = path.getParent();
|
|
|
+
|
|
|
+ if (parentPath != null) {
|
|
|
+ return parentPath.toString() + File.separator;
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("Invalid python file path: " + pythonFilePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据json字符串生成input.json文件到指定目录
|
|
|
+ * @param directory 目标目录
|
|
|
+ * @param jsonContent JSON内容字符串
|
|
|
+ * @return 生成的JSON文件路径
|
|
|
+ */
|
|
|
+ public static String generateInputJsonFile(String directory, String jsonContent) throws IOException {
|
|
|
+ if (directory == null || directory.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("Directory cannot be null or empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonContent == null || jsonContent.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("JSON content cannot be null or empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保目录存在
|
|
|
+ File dir = new File(directory);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ boolean created = dir.mkdirs();
|
|
|
+ if (!created) {
|
|
|
+ throw new IOException("Failed to create directory: " + directory);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建input.json文件
|
|
|
+ File jsonFile = new File(directory, "input.json");
|
|
|
+ try (FileWriter writer = new FileWriter(jsonFile)) {
|
|
|
+ writer.write(jsonContent);
|
|
|
+ }
|
|
|
+
|
|
|
+ return jsonFile.getAbsolutePath();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用Python代码
|
|
|
+ * @param pythonScriptPath Python脚本路径
|
|
|
+ * @param workingDirectory 工作目录
|
|
|
+ * @return 执行成功返回"成功",失败返回错误信息
|
|
|
+ */
|
|
|
+ public static String callPythonScript(String pythonScriptPath, String workingDirectory) {
|
|
|
+ if (pythonScriptPath == null || pythonScriptPath.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("Python script path cannot be null or empty");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(DEFAULT_PYTHON, pythonScriptPath);
|
|
|
+ processBuilder.directory(new File(workingDirectory));
|
|
|
+ processBuilder.redirectErrorStream(true);
|
|
|
+
|
|
|
+ try {
|
|
|
+ Process process = processBuilder.start();
|
|
|
+
|
|
|
+ // 读取输出
|
|
|
+ StringBuilder output = new StringBuilder();
|
|
|
+ try (BufferedReader reader = new BufferedReader(
|
|
|
+ new InputStreamReader(process.getInputStream()))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ output.append(line).append("\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待进程结束
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+
|
|
|
+ if (exitCode == 0) {
|
|
|
+ logger.info("Python script executed successfully: {}", pythonScriptPath);
|
|
|
+ return "";
|
|
|
+ } else {
|
|
|
+ String errorMsg = String.format("Python script execution failed with exit code %d. Output: %s",
|
|
|
+ exitCode, output.toString());
|
|
|
+ logger.error(errorMsg);
|
|
|
+ return errorMsg;
|
|
|
+ }
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ String errorMsg = String.format("Failed to execute Python script: %s. Error: %s",
|
|
|
+ pythonScriptPath, e.getMessage());
|
|
|
+ logger.error(errorMsg, e);
|
|
|
+ return errorMsg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 完整调用流程
|
|
|
+ * @param pythonScriptPath Python脚本路径
|
|
|
+ * @param jsonContent 输入JSON内容
|
|
|
+ * @return 执行结果
|
|
|
+ */
|
|
|
+ public static String executeAlgorithm(String pythonScriptPath, String jsonContent) {
|
|
|
+ try {
|
|
|
+ // 1. 获取工作目录
|
|
|
+ String workingDirectory = getAlgorithmRootPath(pythonScriptPath);
|
|
|
+
|
|
|
+ // 2. 生成输入文件
|
|
|
+ generateInputJsonFile(workingDirectory, jsonContent);
|
|
|
+
|
|
|
+ // 3. 调用Python脚本
|
|
|
+ return callPythonScript(pythonScriptPath, workingDirectory);
|
|
|
+ } catch (Exception e) {
|
|
|
+ String errorMsg = String.format("Algorithm execution failed: %s", e.getMessage());
|
|
|
+ logger.error(errorMsg, e);
|
|
|
+ return errorMsg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|