Re: Networking in Prolog - a survey



On May 13, 9:56 pm, Alessio Stalla <alessiosta...@xxxxxxxxx> wrote:
Hello, for my dissertation I developed a little Prolog interpreter
with didactical purposes (that was in late 2006). I coded it in Java
because it was the language I knew better. These days my professor
kindly asked if it was possible to extend the interpreter with various
facilities, among which basic networking support (thus allowing
different instances of the system to share knowledge). I think the
task is not difficult implementation-wise, since I can leverage Java's
libraries. However, before starting writing code, I wanted to check
the state of the art in Prolog networking libraries to see if I could
get any inspiration. Specifically, I'd like to make it possible to
write in Prolog a simple server capable of handling multiple
connections "simultaneously" or better asynchronously, but adding
multithreading to my interpreter is not going to be easy, so I'd avoid
that if possible; instead, I thought of using an event-based approach
(the programmer registers a goal with two free variables, Connection
and Message, as an "event handler" that will be automatically invoked
by the system each time a "message" arrives on an open connection.
What a message is concretely is rather vague at the moment, but I
digress. This way I will probably still have to manage threads, but
I'll be able to do it from the internals of the interpreter, without
exposing a threading API on the prolog side). I read about SWI
Prolog's clib networking support, which as far as I can tell is a
Prolog skin over the standard Unix socket library; it even mentions
fork! Besides that, I found no other convincing example of networking
libraries in Prolog. Has anyone on this group any insight into the
problem?

Thanks in advance to anyone who answers!
Kind regards,

Alessio Stalla

Reading this, one thing that occurs to me is that you can protect your
main prolog interpreter loop from needing to be multi-threaded by
putting a 'synchronized' block around it.

So if the user submits a query on the command line it gets passed to
the interpreter. If at the same time some data arrives on a socket and
invokes your event handler predicate which in turn triggers the
interpreter loop to run, it will have to wait for the users query to
complete before it gets processed. Perhaps not ideal for super
responsive io, but maybe good enough for your purposes. Also, only one
incoming packet at a time will be able to be processed. Once you
figure out how to make it multi-threaded, one day when you find the
time ;), you can remove this limitation.

I think 'asynchronous' and 'event driven' io are pretty much the same
concept. Worth bearing in mind that computers are essentially event
driven machines at the hardware level (hardware devices create
interrupts when hardware events occurr). You can also simulate real
asynchronous io in Java by using threads. Basically you encode all
your io events as objects, then pass them into a concurrent producer/
consumer queue, then have a thread (or more) consuming and processing
the io events as they occurr. You need a reasonable amount of code on
top of the java.io blocking sockets or java.nio non-blocking sockets
to set up an event driven system like this. Fortunately there are such
libraries already in existence for you to use. You could look at:

http://www.eecs.harvard.edu/~mdw/proj/seda/

or

http://mina.apache.org/

for example.

Using these libraries mostly what you will have to do is to write an
event handler for incoming data that invokes you prolog interpreter
loop in an appropriate way, and to encode a library call in Java that
is callable from your prolog to output data to a socket.

Have you any plans to release the fruits of your labour as open
source? I am working in both of these areas myself at the moment,
logic programming and asynchronous io in Java, that is. Although I am
still a long way off attempting to marry them together. I have a
sourceforge project for real asynchronous io in Java:

https://sourceforge.net/projects/jsr203nio2

Which is just a stub at the moment. I have some code to upload into it
for asynchronous file io, asynchronous socket io is not done yet but
is planned in the coming months. I also have code for a prolog-like
logic engine, which has a byte code interpreter in Java, one in C, and
also compiles down to native using the LLVM back-end. I intend to
realease that as open source too. Yes, once I find the time to
complete it!

Good luck with your project,

Rupert
.



Relevant Pages

  • Re: Networking in Prolog - a survey
    ... main prolog interpreter loop from needing to be multi-threaded by ... interpreter loop to run, it will have to wait for the users query to ... allow to run the event handler code with a separate prolog stack, ... If I limit the handling of threads to be only on the Java side, ...
    (comp.lang.prolog)
  • Networking in Prolog - a survey
    ... for my dissertation I developed a little Prolog interpreter ... among which basic networking support (thus allowing ... the state of the art in Prolog networking libraries to see if I could ...
    (comp.lang.prolog)
  • Re: Networking in Prolog - a survey
    ... If Prolog is embedded into ... Java, then outside world knows only about Java. ... All networking ca nbe ... I want two instances of my Prolog interpreter to ...
    (comp.lang.prolog)
  • Re: Differences between C++ and Java
    ... > are *implementation* defined and the standard also specifies the ... > "In Java, garbage collection of unreferenced objects is automatic. ... > C++, you manually manage memory. ... > provide third party libraries that work just as well, ...
    (comp.lang.java.programmer)
  • Re: Ein Python-Bytecode-Interpreter
    ... Python bauen. ... Wenn man hier zum Beispiel von einem Servlet pro Thread ausgeht und am Ende des Servlets dein Pythonprogramm steht, dann kann man eher mit einem Interpreter pro Thread arbeiten, der auch fast gar keinen Javacode alleine aufrufen müsste. ... Immerhin musst du auf diese Art überlegen wie die beliebige Objekte in Java auf Python mapst... ... class PyString extends PyValue { ...
    (de.comp.lang.java)