Browse Source

运行matlab算法功能已完成

twzydn20000928 2 years ago
parent
commit
1fe758552c

+ 100 - 9
pdaaphm-admin/src/main/java/com/pdaaphm/biz/service/impl/RunMatlabImpl.java

@@ -16,7 +16,9 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -72,7 +74,7 @@ public class RunMatlabImpl implements RunAlgorithmService {
         }
 
         //run matlab exe
-//        runMatExe(url);
+        runMatExe(url);
 
         //将matlab输出的文件从matlabOutputPathList里copy到文件系统outputPath
 
@@ -83,10 +85,12 @@ public class RunMatlabImpl implements RunAlgorithmService {
             int index = matlabOutputPath.lastIndexOf("\\");
             String dir = matlabOutputPath.substring(0, index);
             String prefixName = matlabOutputPath.substring(index + 1);
-            Map<String,String> map = getFinalNameAndPath(dir,prefixName);
-            String outputPath = map.get("path");
+            String matFinalOutPath = getABSFinalPath(dir, prefixName);
+            int index2 = matFinalOutPath.lastIndexOf('.');
+            String extension = matFinalOutPath.substring(index2 + 1);
+            String outputPath = resultList.get(q) + '.' + extension;
             try {
-                copyFile(matlabOutputPath, outputPath);
+                copyFile(matFinalOutPath, outputPath);
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
@@ -141,6 +145,28 @@ public class RunMatlabImpl implements RunAlgorithmService {
         return null;
     }
 
+    public String getABSFinalPath(String dirs, String prefixName) {
+        // 指定目录路径
+        java.io.File directory = new java.io.File(dirs);
+
+        // 使用FilenameFilter筛选与已知文件名开头的文件
+        String[] matchingFiles = directory.list((dir, name) -> name.startsWith(prefixName));
+
+        String res;
+
+        if (matchingFiles != null && matchingFiles.length == 1) {
+            System.out.println("找到匹配的文件:");
+            res = dirs + '\\' + matchingFiles[0];
+            return res;
+        } else if (matchingFiles != null && matchingFiles.length > 1) {
+            logger.error("找到多个匹配的文件,这不应该发生。");
+            return null;
+        } else {
+            logger.error("没有找到匹配的文件。");
+            return null;
+        }
+    }
+
     // todo return type is Map<String,String>
     public Map getFinalNameAndPath(String dirs, String prefixName) {
         // 指定目录路径
@@ -157,11 +183,9 @@ public class RunMatlabImpl implements RunAlgorithmService {
             map.put("path", dirs + '/' + matchingFiles[0]);
             return map;
         } else if (matchingFiles != null && matchingFiles.length > 1) {
-            // todo 工程内禁止使用System.out.print 日志统一使用log
             logger.error("找到多个匹配的文件,这不应该发生。");
             return null;
         } else {
-            // todo 工程内禁止使用System.out.print 日志统一使用log
             logger.error("没有找到匹配的文件。");
             return null;
         }
@@ -185,23 +209,90 @@ public class RunMatlabImpl implements RunAlgorithmService {
             ProcessBuilder processBuilder = new ProcessBuilder(url);
             // 启动外部程序
             Process process = processBuilder.start();
-            // 等待外部程序执行完成
+
+            // Thread to handle the output stream.
+            Thread outputThread = new Thread(() -> {
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        logger.info(line);
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            });
+
+            // Thread to handle the error stream.
+            Thread errorThread = new Thread(() -> {
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        logger.error(line);
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            });
+
+            outputThread.start();
+            errorThread.start();
+
+            // Wait for the external process to finish.
             int exitCode = process.waitFor();
+
+            // Optionally, wait for the threads to finish reading.
+            outputThread.join();
+            errorThread.join();
+
             System.out.println("外部程序执行完成,退出码:" + exitCode);
         } catch (IOException | InterruptedException e) {
             e.printStackTrace();
         }
+        return;
     }
 
     public static void main(String args[]) {
-        String url = "D:\\pdaaphm\\matlabexe\\GWO_SVM_exmp2.exe";
+        String url = "D:\\pdaaphm\\matlabexefu\\NARX_main.exe";
         try {
             // 指定要执行的外部程序和参数
             ProcessBuilder processBuilder = new ProcessBuilder(url);
             // 启动外部程序
             Process process = processBuilder.start();
-            // 等待外部程序执行完成
+
+            // Thread to handle the output stream.
+            Thread outputThread = new Thread(() -> {
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        System.out.println(line);
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            });
+
+            // Thread to handle the error stream.
+            Thread errorThread = new Thread(() -> {
+                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        System.err.println(line);
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            });
+
+            outputThread.start();
+            errorThread.start();
+
+            // Wait for the external process to finish.
             int exitCode = process.waitFor();
+
+            // Optionally, wait for the threads to finish reading.
+            outputThread.join();
+            errorThread.join();
+
             System.out.println("外部程序执行完成,退出码:" + exitCode);
         } catch (IOException | InterruptedException e) {
             e.printStackTrace();