Java 如何向 GHCi 写入命令?

问题描述 投票:0回答:1

我正在制作一个应用程序(Windows),使用 Haskell 来实现数值方法来解决问题,使用 Java 来实现 GUI 并处理用户输入。对于进程间通信,我让它们读取和写入共同的 JSON 文件。这一切都有效,但是当尝试通过让 Java 与 cmd 通信并运行 GHCi 来执行程序时,一旦编译器运行,我似乎无法加载和运行 Haskell 文件。没有错误消息,但我的 Java 程序似乎经常陷入我认为的死锁。有任何想法吗? 这是我的代码:

        try {
            // Serialize dataMap to JSON and write to file
            String jsonString = objectMapper.writeValueAsString(dataMap);
            FileWriter fileWriter = new FileWriter("newtonRaphson.json");
            fileWriter.write(jsonString);
            fileWriter.close();

            // Read JSON file
            File jsonFile = new File("newtonRaphson.json");
            JsonNode rootNode = objectMapper.readTree(jsonFile);

            // Extract solution from JSON
            Iterator<Map.Entry<String, JsonNode>> fields = rootNode.fields();
            Map.Entry<String, JsonNode> secondEntry = fields.next();
            JsonNode solutionOut = secondEntry.getValue();
            String solutionText = solutionOut.asText();

            // Display equation on GUI
            Text messageTextFxn = new Text(equation);
            messageTextFxn.setFont(new Font("System", 15));
            fxnPane.getChildren().add(messageTextFxn);

            // Display solution on GUI
            Text messageTextSol = new Text(solutionText);
            messageTextSol.setFont(new Font("System", 15));
            solPane.getChildren().add(messageTextSol);

            // Launch Haskell REPL and interact with it
            String haskellPath = "path\\to\\hsFile";
            ProcessBuilder builder = new ProcessBuilder("stack", "repl");
            builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
            builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
            builder.redirectError(ProcessBuilder.Redirect.INHERIT);
            Process process = builder.start();

            OutputStream outputStream = process.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream);
            
            // Load Haskell module
            writer.println(":l " + haskellPath + "\\newtonRaphson");
            writer.flush();
            System.out.println("Haskell module loaded successfully.");

            // Execute Haskell function
            writer.println("nr");
            writer.flush();
            System.out.println("Haskell function executed successfully.");

            // Close writer and wait for process to terminate
            writer.close();
            int exitCode = process.waitFor();

            if (exitCode == 0) {
                System.out.println("Process completed successfully.");
            } else {
                System.out.println("Process terminated with an error.");
            }

我尝试过使用不同的方式与终端实际通信,包括 processBuilder 和 Runtime exec(),但我认为这可能是错误的方法,只是不确定此时要做什么。

java haskell ipc communication
1个回答
0
投票

有关详细信息,请参阅 OP 的讨论,但消除使用 INHERIT 的方法并使用 bufferedWriter 是有效的:

 try {
        // Serialize dataMap to JSON and write to file
        String jsonString = objectMapper.writeValueAsString(dataMap);
        FileWriter fileWriter = new FileWriter("newtonRaphson.json");
        fileWriter.write(jsonString);
        fileWriter.close();

        // Read JSON file
        File jsonFile = new File("newtonRaphson.json");
        JsonNode rootNode = objectMapper.readTree(jsonFile);

        // Extract solution from JSON
        Iterator<Map.Entry<String, JsonNode>> fields = rootNode.fields();
        Map.Entry<String, JsonNode> secondEntry = fields.next();
        JsonNode solutionOut = secondEntry.getValue();
        String solutionText = solutionOut.asText();

        // Display equation on GUI
        Text messageTextFxn = new Text(equation);
        messageTextFxn.setFont(new Font("System", 15));
        fxnPane.getChildren().add(messageTextFxn);

        // Display solution on GUI
        Text messageTextSol = new Text(solutionText);
        messageTextSol.setFont(new Font("System", 15));
        solPane.getChildren().add(messageTextSol);

        // Launch Haskell REPL and interact with it
        String haskellPath = "C:\\Users\\cezar\\Desktop\\AcademicResources\\Engineering\\Java\\JavaFX_VS\\chemesolver\\haskell\\nr\\src";
        ProcessBuilder builder = new ProcessBuilder("stack", "repl");
        Process process = builder.start();

        OutputStream outputStream = process.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        
        // Load Haskell module
        writer.write(":l " + haskellPath + "\\newtonRaphson");
        writer.flush();
        System.out.println("Haskell module loaded successfully.");

        // Execute Haskell function
        writer.write("nr");
        writer.flush();
        System.out.println("Haskell function executed successfully.");

        // Close writer and wait for process to terminate
        writer.close();
        int exitCode = process.waitFor();
© www.soinside.com 2019 - 2024. All rights reserved.