Re: [clisp-list] connecting CLISP and Java

"santi" <scarbonell@...> writes:
> Hi Pascal,
>
> Finally I've tried to use sockets and repl so you said to me. I can
> connect with CLISP using sockets with Java and can execute some
> functions!!!. In LISP I implement this server code:
>
> http://clisp.org/impnotes/socket.html
>
> But I've got one problem. In the functions of my LISP program, there
> are some functions that use the LISP format function and when I try to
> call from Java, the response appears in the LISP console but no in the
> Java console. I've thought that I need to redirect the default output
> to the sockets, so the format responses must appear in the Java
> console despite LISP console. I've tried this:
>
> (let ((*standard-output* <SOCKET>))
>
> I've tried this too:
>
> (let ((*standard-output* (LET ((server (SOCKET:SOCKET-SERVER)))
> (FORMAT t "~&Waiting for a connection on ~S:~D~%"
> (SOCKET:SOCKET-SERVER-HOST server) (SOCKET:SOCKET-SERVER-PORT server))
> (UNWIND-PROTECT
> ;; infinite loop, terminate with Control+C
> (LOOP (WITH-OPEN-STREAM (socket (SOCKET:SOCKET-ACCEPT server))
> (MULTIPLE-VALUE-BIND (local-host local-port) (SOCKET:SOCKET-STREAM-LOCAL socket)
> (MULTIPLE-VALUE-BIND (remote-host remote-port) (SOCKET:SOCKET-STREAM-PEER socket)
> (FORMAT T "~&Connection: ~S:~D -- ~S:~D~%"
> remote-host remote-port local-host local-port)))
> ;; loop is terminated when the remote host closes the connection or on EXT:EXIT
> (LOOP (WHEN (EQ :eof (SOCKET:SOCKET-STATUS (cons socket :input))) (RETURN))
> (PRINT (EVAL (READ socket)) socket)
> ;; flush everything left in socket
> (LOOP :for c = (READ-CHAR-NO-HANG socket nil nil) :while c)
> (TERPRI socket))))
> ;; make sure server is closed
> (SOCKET:SOCKET-SERVER-CLOSE server)))
> )))
>
> But doesn't works.
>
> Do you know, who can I redirect the LISP output console to the Java console
I would redirect the lisp streams to some string stream (or if you
expect large output, redirect to a temporary file stream, but an in
memory string enough for normal usage), and when evaluation is finished,
I would send those strings to the java application to do whatever it
wants to do with them (such as outputting them to the Java console).
Something like this:
(defun server-repl (socket)
(loop ; Loop
(let ((request (read-request socket))) ; Read
(let ((results)
(error)
(errout)
(stdout
(with-output-to-string (*standard-output*)
(setf errout
(with-output-to-string (*error-output*)
(let* ((*standard-input* (make-concatenated-stream))
(*trace-output* *standard-output*)
(*terminal-io* (make-two-way-stream *standard-input*
*standard-output*))
(*query-io* *terminal-io*)
(*debug-io* *terminal-io*))
(handler-case
(setf results (multiple-value-list
(execute-request request))) ; Eval
(error (err)
(setf error (princ-to-string err))))))))))
(send-answer socket (make-answer :error error ; Print
:result results
:stdout stdout
:stderr errout))))))
You have to implement the missing functions, read-request,
execute-request, send-answer. You may want to redirect differently the
streams. Eg. you may want to send errors and *error-output* or
*trace-output* messages to a log file. You may want to not redirect
*debug-io*, so that you can still debug the lisp process on the terminal
you launched it. Or you may want to redirect *query-io* to your own
subclass of Gray streams so that you can send queries back to the Java
application for user interaction and send back the answers to the
*query-io* stream for reading.
But the idea is that: you bind the stream variables to whatever stream
you want, such as string streams, to collect the data, and when done,
you send that data along with the result to the client application.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Thread view

Hi,
I'm new to CLISP and I'd like if someone can advise me.
I'm trying to put a java interface to a lisp application. This lisp application is compatible with CLISP, so I've made a mem image and load some cl files and can call some functions and works fine. My problem is that I need to create an interfaz with Java but I don't know how to do it.
The lisp code is an AI application called Babylon developed in 90's and I need to put some interface with Java. Because the application works fine in CLISP, I'd like to do a Java application, and If I do some call to a function, the response from the console appears in Java and if I need to send some parameters, that this parameter appears in the console.
I don't if there are some application, interface or function to call the lisp console from webservices and the webservices try to call it from Java. I need advice.
Thanks

"scarbonell@..." <scarbonell@...> writes:
> I'm new to CLISP and I'd like if someone can advise me.
Meaning you've never used clisp, the Common Lisp implementation written
in C (hence C lisp, clisp), before.
Or meaning you've never used Common Lisp, the programming language
before?
There are a lot of Common Lisp implementations, clisp is just one of
them.
Go to http://cliki.net/ for more pointers about Common Lisp.
> I'm trying to put a java interface to a lisp application. This lisp
> application is compatible with CLISP, so I've made a mem image and
> load some cl files and can call some functions and works fine. My
> problem is that I need to create an interfaz with Java but I don't
> know how to do it.
Nonetheless, it may be conforming or largely conforming, so you should
not have much problem to make it run with another CL implementation.
> The lisp code is an AI application called Babylon developed in 90's
> and I need to put some interface with Java. Because the application
> works fine in CLISP, I'd like to do a Java application, and If I do
> some call to a function, the response from the console appears in Java
> and if I need to send some parameters, that this parameter appears in
> the console.
>
> I don't if there are some application, interface or function to call
> the lisp console from webservices and the webservices try to call it
> from Java. I need advice.
Well, that sounds rather like a Java question. I suppose it's possible
in Java to fork a program (whatever language it's written in!), and to
send lines to it and read responses from it. In C you'd do that with
pipe, dup, fork and exec. With clisp you'd do that using
EXT:MAKE-PIPE-IO-STREAM http://www.clisp.org/impnotes/shell.html I guess
something similar exists in Java?
But consider ABCL instead of clisp. It'll be much easier to integrate
Java with Lisp using ABCL.
With clisp, the best you can do is to provide a tcp/ip or unix socket
server on one side and a connect to it on the other side, implementing
some kind of protocol to communicate between the Java program and the
Lisp program (or using the REPL with a pair of pipes like above).
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Hi Pascal,
This is the CLISP that I'm using: http://www.clisp.org/
I've downloaded the windows version and make a mem image with Babylon code and works fine, that is why I'm trying to communicate with the clisp console.
About ABCL, I've tried to compile my lisp application using it but appears a lot of errors, because it has some implementation-specific extensions that doesn’t work with ABCL that's why I stopped using.
About this url http://www.clisp.org/impnotes/shell.html it's look like very interesting. It's supposed that with winehq can send message to the lisp console. I'll try it. If Java it's a problem, I could consider to change my future interfaz from java to c, to communicate with CLISP.
But, I think that there are some way to use something like webservices with CLISP. WebServices it's interplatform, so I think that it's possible to communicate with Java. What do you think? What's REPL
Thanks
-----Mensaje original-----
De: Pascal J. Bourguignon [mailto:pjb@...]
Enviado el: lunes, 05 de marzo de 2012 15:47
Para: scarbonell@...
CC: clisp-list@...
Asunto: Re: [clisp-list] connecting CLISP and Java
"scarbonell@..." <scarbonell@...> writes:
> I'm new to CLISP and I'd like if someone can advise me.
Meaning you've never used clisp, the Common Lisp implementation written
in C (hence C lisp, clisp), before.
Or meaning you've never used Common Lisp, the programming language
before?
There are a lot of Common Lisp implementations, clisp is just one of
them.
Go to http://cliki.net/ for more pointers about Common Lisp.
> I'm trying to put a java interface to a lisp application. This lisp
> application is compatible with CLISP, so I've made a mem image and
> load some cl files and can call some functions and works fine. My
> problem is that I need to create an interfaz with Java but I don't
> know how to do it.
Nonetheless, it may be conforming or largely conforming, so you should
not have much problem to make it run with another CL implementation.
> The lisp code is an AI application called Babylon developed in 90's
> and I need to put some interface with Java. Because the application
> works fine in CLISP, I'd like to do a Java application, and If I do
> some call to a function, the response from the console appears in Java
> and if I need to send some parameters, that this parameter appears in
> the console.
>
> I don't if there are some application, interface or function to call
> the lisp console from webservices and the webservices try to call it
> from Java. I need advice.
Well, that sounds rather like a Java question. I suppose it's possible
in Java to fork a program (whatever language it's written in!), and to
send lines to it and read responses from it. In C you'd do that with
pipe, dup, fork and exec. With clisp you'd do that using
EXT:MAKE-PIPE-IO-STREAM http://www.clisp.org/impnotes/shell.html I guess
something similar exists in Java?
But consider ABCL instead of clisp. It'll be much easier to integrate
Java with Lisp using ABCL.
With clisp, the best you can do is to provide a tcp/ip or unix socket
server on one side and a connect to it on the other side, implementing
some kind of protocol to communicate between the Java program and the
Lisp program (or using the REPL with a pair of pipes like above).
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

"santi" <scarbonell@...> writes:
> Hi Pascal,
>
> This is the CLISP that I'm using: http://www.clisp.org/
There's a single CLISP, this is the one.
> About ABCL, I've tried to compile my lisp application using it but
> appears a lot of errors, because it has some implementation-specific
> extensions that doesn’t work with ABCL that's why I stopped using.
Perhaps you could update your application to use the extensions of ABCL,
another CL implementation, different from CLISP, the CL implementation
mentionned above.
> What do you think?
I think that I said all I had to say in my first answer. Read it again.
Otherwise you must answer first the question whether you want to call
lisp from java or java from lisp. From what you said I infered you
wanted to call lisp from java, and therefore you have a java problem. I
don't know anything about it, you should ask your question on the java
newsgroups or forum or whatever they use.
> What's REPL
REPL = Read Eval Print Loop.
> -----Mensaje original-----
> De: Pascal J. Bourguignon [mailto:pjb@...]
> Enviado el: lunes, 05 de marzo de 2012 15:47
> Para: scarbonell@...
> CC: clisp-list@...
> Asunto: Re: [clisp-list] connecting CLISP and Java
>
> "scarbonell@..." <scarbonell@...> writes:
>
>> I'm new to CLISP and I'd like if someone can advise me.
>
> Meaning you've never used clisp, the Common Lisp implementation written
> in C (hence C lisp, clisp), before.
>
> Or meaning you've never used Common Lisp, the programming language
> before?
>
> There are a lot of Common Lisp implementations, clisp is just one of
> them.
>
> Go to http://cliki.net/ for more pointers about Common Lisp.
>
>
>> I'm trying to put a java interface to a lisp application. This lisp
>> application is compatible with CLISP, so I've made a mem image and
>> load some cl files and can call some functions and works fine. My
>> problem is that I need to create an interfaz with Java but I don't
>> know how to do it.
>
> Nonetheless, it may be conforming or largely conforming, so you should
> not have much problem to make it run with another CL implementation.
>
>
>> The lisp code is an AI application called Babylon developed in 90's
>> and I need to put some interface with Java. Because the application
>> works fine in CLISP, I'd like to do a Java application, and If I do
>> some call to a function, the response from the console appears in Java
>> and if I need to send some parameters, that this parameter appears in
>> the console.
>>
>> I don't if there are some application, interface or function to call
>> the lisp console from webservices and the webservices try to call it
>> from Java. I need advice.
>
> Well, that sounds rather like a Java question. I suppose it's possible
> in Java to fork a program (whatever language it's written in!), and to
> send lines to it and read responses from it. In C you'd do that with
> pipe, dup, fork and exec. With clisp you'd do that using
> EXT:MAKE-PIPE-IO-STREAM http://www.clisp.org/impnotes/shell.html I guess
> something similar exists in Java?
>
>
> But consider ABCL instead of clisp. It'll be much easier to integrate
> Java with Lisp using ABCL.
>
>
> With clisp, the best you can do is to provide a tcp/ip or unix socket
> server on one side and a connect to it on the other side, implementing
> some kind of protocol to communicate between the Java program and the
> Lisp program (or using the REPL with a pair of pipes like above).
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Hi Kaz,
I've tried to compile my lisp application using ABCL but a appears a lot of errors, because it has some implementation-specific extensions that doesn’t work with ABCL that's why I stopped using.
The only two implementations that works fine with my lisp application are Allegro CL and CLISP (http://www.clisp.org/)
Any Ideas how to communicate CLISP with Java?
Thanks
-----Mensaje original-----
De: Kaz Kylheku [mailto:kaz@...]
Enviado el: lunes, 05 de marzo de 2012 17:12
Para: scarbonell@...
CC: clisp-list@...
Asunto: Re: [clisp-list] connecting CLISP and Java
On Mon, 5 Mar 2012 15:28:32 +0100 (CET), "scarbonell@..."
<scarbonell@...> wrote:
> Hi,
>
> I'm new to CLISP and I'd like if someone can advise me.
>
> I'm trying to put a java interface to a lisp application. This lisp
> application is compatible with CLISP, so I've made a mem image and
> load some cl files and can call some functions and works fine. My
> problem is that I need to create an interfaz with Java but I don't
> know how to do it.
The no-brainer approach would be to use a JVM-based Common Lisp
for this (Armed Bear Common Lisp or ABCL).
http://common-lisp.net/project/armedbear/

Hi Pascal,
Finally I've tried to use sockets and repl so you said to me. I can connect with CLISP using sockets with Java and can execute some functions!!!. In LISP I implement this server code:
http://clisp.org/impnotes/socket.html
But I've got one problem. In the functions of my LISP program, there are some functions that use the LISP format function and when I try to call from Java, the response appears in the LISP console but no in the Java console. I've thought that I need to redirect the default output to the sockets, so the format responses must appear in the Java console despite LISP console. I've tried this:
(let ((*standard-output* <SOCKET>))
I've tried this too:
(let ((*standard-output* (LET ((server (SOCKET:SOCKET-SERVER)))
(FORMAT t "~&Waiting for a connection on ~S:~D~%"
(SOCKET:SOCKET-SERVER-HOST server) (SOCKET:SOCKET-SERVER-PORT server))
(UNWIND-PROTECT
;; infinite loop, terminate with Control+C
(LOOP (WITH-OPEN-STREAM (socket (SOCKET:SOCKET-ACCEPT server))
(MULTIPLE-VALUE-BIND (local-host local-port) (SOCKET:SOCKET-STREAM-LOCAL socket)
(MULTIPLE-VALUE-BIND (remote-host remote-port) (SOCKET:SOCKET-STREAM-PEER socket)
(FORMAT T "~&Connection: ~S:~D -- ~S:~D~%"
remote-host remote-port local-host local-port)))
;; loop is terminated when the remote host closes the connection or on EXT:EXIT
(LOOP (WHEN (EQ :eof (SOCKET:SOCKET-STATUS (cons socket :input))) (RETURN))
(PRINT (EVAL (READ socket)) socket)
;; flush everything left in socket
(LOOP :for c = (READ-CHAR-NO-HANG socket nil nil) :while c)
(TERPRI socket))))
;; make sure server is closed
(SOCKET:SOCKET-SERVER-CLOSE server)))
)))
But doesn't works.
Do you know, who can I redirect the LISP output console to the Java console
Thanks
-----Mensaje original-----
De: Pascal J. Bourguignon [mailto:pjb@...]
Enviado el: martes, 06 de marzo de 2012 21:08
Para: santi
CC: clisp-list@...
Asunto: Re: [clisp-list] connecting CLISP and Java
"santi" <scarbonell@...> writes:
> Hi Pascal,
>
> This is the CLISP that I'm using: http://www.clisp.org/
There's a single CLISP, this is the one.
> About ABCL, I've tried to compile my lisp application using it but
> appears a lot of errors, because it has some implementation-specific
> extensions that doesn’t work with ABCL that's why I stopped using.
Perhaps you could update your application to use the extensions of ABCL,
another CL implementation, different from CLISP, the CL implementation
mentionned above.
> What do you think?
I think that I said all I had to say in my first answer. Read it again.
Otherwise you must answer first the question whether you want to call
lisp from java or java from lisp. From what you said I infered you
wanted to call lisp from java, and therefore you have a java problem. I
don't know anything about it, you should ask your question on the java
newsgroups or forum or whatever they use.
> What's REPL
REPL = Read Eval Print Loop.
> -----Mensaje original-----
> De: Pascal J. Bourguignon [mailto:pjb@...]
> Enviado el: lunes, 05 de marzo de 2012 15:47
> Para: scarbonell@...
> CC: clisp-list@...
> Asunto: Re: [clisp-list] connecting CLISP and Java
>
> "scarbonell@..." <scarbonell@...> writes:
>
>> I'm new to CLISP and I'd like if someone can advise me.
>
> Meaning you've never used clisp, the Common Lisp implementation written
> in C (hence C lisp, clisp), before.
>
> Or meaning you've never used Common Lisp, the programming language
> before?
>
> There are a lot of Common Lisp implementations, clisp is just one of
> them.
>
> Go to http://cliki.net/ for more pointers about Common Lisp.
>
>
>> I'm trying to put a java interface to a lisp application. This lisp
>> application is compatible with CLISP, so I've made a mem image and
>> load some cl files and can call some functions and works fine. My
>> problem is that I need to create an interfaz with Java but I don't
>> know how to do it.
>
> Nonetheless, it may be conforming or largely conforming, so you should
> not have much problem to make it run with another CL implementation.
>
>
>> The lisp code is an AI application called Babylon developed in 90's
>> and I need to put some interface with Java. Because the application
>> works fine in CLISP, I'd like to do a Java application, and If I do
>> some call to a function, the response from the console appears in Java
>> and if I need to send some parameters, that this parameter appears in
>> the console.
>>
>> I don't if there are some application, interface or function to call
>> the lisp console from webservices and the webservices try to call it
>> from Java. I need advice.
>
> Well, that sounds rather like a Java question. I suppose it's possible
> in Java to fork a program (whatever language it's written in!), and to
> send lines to it and read responses from it. In C you'd do that with
> pipe, dup, fork and exec. With clisp you'd do that using
> EXT:MAKE-PIPE-IO-STREAM http://www.clisp.org/impnotes/shell.html I guess
> something similar exists in Java?
>
>
> But consider ABCL instead of clisp. It'll be much easier to integrate
> Java with Lisp using ABCL.
>
>
> With clisp, the best you can do is to provide a tcp/ip or unix socket
> server on one side and a connect to it on the other side, implementing
> some kind of protocol to communicate between the Java program and the
> Lisp program (or using the REPL with a pair of pipes like above).
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

"santi" <scarbonell@...> writes:
> Hi Pascal,
>
> Finally I've tried to use sockets and repl so you said to me. I can
> connect with CLISP using sockets with Java and can execute some
> functions!!!. In LISP I implement this server code:
>
> http://clisp.org/impnotes/socket.html
>
> But I've got one problem. In the functions of my LISP program, there
> are some functions that use the LISP format function and when I try to
> call from Java, the response appears in the LISP console but no in the
> Java console. I've thought that I need to redirect the default output
> to the sockets, so the format responses must appear in the Java
> console despite LISP console. I've tried this:
>
> (let ((*standard-output* <SOCKET>))
>
> I've tried this too:
>
> (let ((*standard-output* (LET ((server (SOCKET:SOCKET-SERVER)))
> (FORMAT t "~&Waiting for a connection on ~S:~D~%"
> (SOCKET:SOCKET-SERVER-HOST server) (SOCKET:SOCKET-SERVER-PORT server))
> (UNWIND-PROTECT
> ;; infinite loop, terminate with Control+C
> (LOOP (WITH-OPEN-STREAM (socket (SOCKET:SOCKET-ACCEPT server))
> (MULTIPLE-VALUE-BIND (local-host local-port) (SOCKET:SOCKET-STREAM-LOCAL socket)
> (MULTIPLE-VALUE-BIND (remote-host remote-port) (SOCKET:SOCKET-STREAM-PEER socket)
> (FORMAT T "~&Connection: ~S:~D -- ~S:~D~%"
> remote-host remote-port local-host local-port)))
> ;; loop is terminated when the remote host closes the connection or on EXT:EXIT
> (LOOP (WHEN (EQ :eof (SOCKET:SOCKET-STATUS (cons socket :input))) (RETURN))
> (PRINT (EVAL (READ socket)) socket)
> ;; flush everything left in socket
> (LOOP :for c = (READ-CHAR-NO-HANG socket nil nil) :while c)
> (TERPRI socket))))
> ;; make sure server is closed
> (SOCKET:SOCKET-SERVER-CLOSE server)))
> )))
>
> But doesn't works.
>
> Do you know, who can I redirect the LISP output console to the Java console
I would redirect the lisp streams to some string stream (or if you
expect large output, redirect to a temporary file stream, but an in
memory string enough for normal usage), and when evaluation is finished,
I would send those strings to the java application to do whatever it
wants to do with them (such as outputting them to the Java console).
Something like this:
(defun server-repl (socket)
(loop ; Loop
(let ((request (read-request socket))) ; Read
(let ((results)
(error)
(errout)
(stdout
(with-output-to-string (*standard-output*)
(setf errout
(with-output-to-string (*error-output*)
(let* ((*standard-input* (make-concatenated-stream))
(*trace-output* *standard-output*)
(*terminal-io* (make-two-way-stream *standard-input*
*standard-output*))
(*query-io* *terminal-io*)
(*debug-io* *terminal-io*))
(handler-case
(setf results (multiple-value-list
(execute-request request))) ; Eval
(error (err)
(setf error (princ-to-string err))))))))))
(send-answer socket (make-answer :error error ; Print
:result results
:stdout stdout
:stderr errout))))))
You have to implement the missing functions, read-request,
execute-request, send-answer. You may want to redirect differently the
streams. Eg. you may want to send errors and *error-output* or
*trace-output* messages to a log file. You may want to not redirect
*debug-io*, so that you can still debug the lisp process on the terminal
you launched it. Or you may want to redirect *query-io* to your own
subclass of Gray streams so that you can send queries back to the Java
application for user interaction and send back the answers to the
*query-io* stream for reading.
But the idea is that: you bind the stream variables to whatever stream
you want, such as string streams, to collect the data, and when done,
you send that data along with the result to the client application.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.