Re: SWI-Prolog Query

Jan Wielemaker <[email protected]> Tue, 15 Apr 2014 20:56:12 +0200
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 15-04-14 20:46, Pavan Kumar wrote:
> I see a http://www.swi-prolog.org/packages/jpl/java_api/ Interface for
> Java. Is this the recommended and better way to call SWI-Prolog from Java
> Programs?

It depends.  JPL is good if you need Prolog to be around and want to ask
it many small questions and/or incrementally build state in Prolog.
However,
it is sometimes hard to get working and various platforms have issues
because
both Prolog and Java are designed to have control over the process.  This is
notably a problem on Unix based platforms, where both use signals to make
threads cooperate on issues such as garbage collection.

So, if you need a clean one-shot interaction, calling Prolog as a process
and communicate the I/O using files and/or read/write of standard I/O can
be a nice and simple solution.

Another good way out is to run Prolog as a web service and use e.g.,
JSON based POST requests to exchange data.   This is in a sense an
intermediate.  The calling overhead is larger than JPL, but in the
few millisecond region, while avoid starting Prolog and can maintain
state in the Prolog process.

	Cheers --- Jan

> 
> I just have a Query generated by java Program which I need to pass on to
> SWI-Prolog, and make swi-Prolog to write the Output to a file and then
> another Java function uses that file for further processing.
> 
> Hope I am clear now. Kindly comment.
> 
> Thanks,
> Pavan.
> 
> 
> On Tue, Apr 15, 2014 at 12:00 PM, Grzegorz Jaƛkiewicz <
> [email protected]> wrote:
> 
>> As far as i understand you are running Prolog interpreter as OS command,
>> eg. "Runtime.getRuntime().exec("swipl");"
>> Am i right?
>>
>> if so, you could use -s flag in the call to specify the source file to be
>> loaded by interpreter and -t for toplevel goal, eg.
>> Runtime.getRuntime().exec("swipl -s blah.pl -t somegoal");
>>
>> to output to file use "tell" predicate in goal specified by -t
>> eg. -t "tell('out.txt'), my_predicate(X), write(X), told"
>>
>> "my_predicate" is a predicate you want to call.
>>
>> HTH
>>
>>
>>
>>
>>
>> 2014-04-15 16:49 GMT+02:00 Pavan Kumar <[email protected]>:
>>
>>> I have a Java Program that generates a SWI-Prolog Query and writes to a
>>> file, I need to Invoke SWI-Prolog from my Java Program and SWI-Prolog
>>> should take the file having the Query as Input and write the Output to
>>> another file.
>>> So, I found Open(/4) in SWI-Prolog but didn't understand it, its usage and
>>> how to make SWI-Prolog execute the Command in that file and also specify
>>> the Target Output file that SWI-Prolog should write to.
>>>
>>> Also Is it possible to call SWI-Prolog from my Java Program and make it
>>> execute the Query in file? if yes, can anyone let me know how to do it?
>>> Also, There can be one or more commands in the Input file for SWI-Prolog
>>> It
>>> should execute the commands in that file and write output to a Target file
>>> mentioned.
>>> -------------- next part --------------
>>> HTML attachment scrubbed and removed
>>> _______________________________________________
>>> SWI-Prolog mailing list
>>> [email protected]
>>> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>>
>>
>>
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
> 
_______________________________________________
SWI-Prolog mailing list
[email protected]
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog