简体   繁体   English

从Java程序传递SWI-Prolog查询

[英]Passing SWI-Prolog Query from Java Program

I have a Query generated by my Java Program in Query Variable. 我在查询变量中有一个由Java程序生成的查询。 I want to send this to SWI-Prolog to get the solution of this. 我想将其发送到SWI-Prolog以获得解决方案。 The following shows the subcode I wrote in Java to communicate with SWI-Prolog. 下面显示了我用Java编写的用于与SWI-Prolog通信的子代码。

  Process P= new ProcessBuilder("C:\\Program Files\\swipl\\bin\\swipl-win.exe").start();
        BufferedReader processOutput = new BufferedReader(new InputStreamReader(P.getInputStream()));
        BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(P.getOutputStream()));

        String commandToSend = Query;
         processInput.write(commandToSend);

But the SWI-Prolog Window opens up but the comand I am writing to ProcessInput is not being sent to SWI-Prolog. 但是SWI-Prolog窗口打开,但是我正在写给ProcessInput的命令没有发送到SWI-Prolog。 Can someoe suggest me the best way to do this? 可以向我建议最好的方法吗? I need the output of the Query Execution from SWI-Prolog in a File as well. 我还需要文件中SWI-Prolog的查询执行的输出。

Save your query to external text file and execute swipl from command line (with appropriate options set) and redirect output to another text file. 将查询保存到外部文本文件, swipl从命令行(已设置适当的选项)执行swipl ,然后将输出重定向到另一个文本文件。 Then, read that output file. 然后,读取该输出文件。

Example: 例:

  1. Save your query to QUERY.TXT as :- query. 将您的查询另存为QUERY.TXT :- query.
  2. Run swipl with redirection to OUTPUT.TXT: swipl -s QUERY.TXT -g halt --quiet > OUTPUT.TXT 运行swipl并重定向到OUTPUT.TXT: swipl -s QUERY.TXT -g halt --quiet > OUTPUT.TXT
  3. Read contents of OUTPUT.TXT 读取OUTPUT.TXT的内容

Also, I suspect you should use combination of Runtime.getRuntime().exec(...) and waitFor(...) to wait for swipl for generating output. 另外,我怀疑您应该结合使用Runtime.getRuntime().exec(...)waitFor(...)来等待swipl生成输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM