开发者

Prolog embedding in java help [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 months ago.

Improve this question

| have a filter written in prolog which i would like to embed in my web service. I am using JAVA at my back end, I would like the filter to be added here. I am using tomcat a开发者_开发百科s my applicaton server. Can anyone suggest a good way. I am aware of JPL, the sad part is I am not able to get it working.


There's always tuProlog if you want to embed your Prolog engine into Java itself. Other alternatives would include the similar-in-concept JLog or the more compilerish Prolog Cafe.

Another alternative, perhaps a bit more distant (it will certainly involve a bit of porting effort on your part) is to use the Mercury language – it has a very Prolog-ish syntax, but is not actually Prolog – which features, among other things, a Java back-end and commensurate Java FFI.


It's not immediately obvious what you're trying to do so I'm going to make the assumption that you're really just trying to puzzle out how to launch your Prolog code and get the results back.

If that's the case, Java provides the Process classes:

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

Perhaps more immediately useful is the ProcessBuilder: this allows you to create a full working environment (including the environment variables) that closely simulates the command line that you're probably testing with now. Quoting from the Javadoc:

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();

Remember, if you want to pass information to the Process that you created, you need to use the Process.getOutputStream() (that would be your output to the Prolog's input). Likewise, if you want to get the Prolog's output and errors, you need to use Process.getInputStream() and Process.getErrorStream().

I've confused those input and output streams in the past and still wish that I could have those hours of my life back....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜