Re: Networking in Prolog - a survey
- From: "rupertlssmith@xxxxxxxxxxxxxx" <rupertlssmith@xxxxxxxxxxxxxx>
- Date: Wed, 21 May 2008 02:24:23 -0700 (PDT)
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
.
- Follow-Ups:
- Re: Networking in Prolog - a survey
- From: Alessio Stalla
- Re: Networking in Prolog - a survey
- References:
- Networking in Prolog - a survey
- From: Alessio Stalla
- Networking in Prolog - a survey
- Prev by Date: Re: Why Prolog code from Wikipedia doesn't run?
- Next by Date: Re: Why Prolog code from Wikipedia doesn't run?
- Previous by thread: Re: Networking in Prolog - a survey
- Next by thread: Re: Networking in Prolog - a survey
- Index(es):
Relevant Pages
|