By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,226 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,226 IT Pros & Developers. It's quick & easy.

Java Prolog Sockets

P: n/a
I am trying to communicate to a prolog server from a java client,
however even though the connection is successfully made every time I
try to perform QueryExecute I get an error, either Socket closed or
null etc. Can anyone help? I am using SICStus prolog.

Currently my prolog server looks like:
:- write('starting'),nl.
:- use_module(library(sockets)).
:- use_module(library(prologbeans)).
:- use_module(library(charsio)).
start_server(Port, QueueLength) :-
socket('AF_INET', Socket),
socket_bind(Socket, 'AF_INET'(localhost, Port)),
socket_listen(Socket, QueueLength),
loop(Socket).

loop(Socket) :-
socket_accept(Socket, Client, Stream),
on_exception(RE, get_request(Stream, Request),
h1(Stream,Client,RE)),
on_exception(PE, process(Request, Stream, Client), h1(Stream,
Client, PE)),
loop(Socket).

get_request(Stream, Request):-
read(Stream, Request).

process(bye, Stream, _Client) :-
close(Stream).
process(Request, Stream, Client) :-
on_exception(E, user:application_handle(Request,Result),
h2(Stream, no, E)),
return(Stream,Result),
on_exception(RE, get_request(Stream, Request1),
h1(Stream,Client,RE)),
on_exception(PE, process(Request1, Stream, Client), h1(Stream,
Client, PE)).

return(Stream,Result) :-
nonvar(Result), !,
format(Stream, "~w~n", [Result]),
flush_output(Stream).
return(_, _).

h1(Stream, C, E) :-
format(Stream, "error~n",[]),
format(user_output, "Client: ~w~nError: ~w",[C, E]).

h2(Stream, Ack, E) :-
format(Stream, "~w~n",[Ack]),
format(user_output, "Application Error: ~w~n",[E]).
evaluate(C,R):- R is C.

and my java client looks like:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Object.*;
import java.lang.Runtime.*;
import java.lang.Process.*;
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.util.Stack;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;
import java.net.*;
import java.beans.*;
import se.sics.prologbeans.*;
import java.nio.channels.SocketChannel;

public class EvaluateGUI implements ActionListener {

private JTextArea text = new JTextArea(20, 40);
private JTextField input = new JTextField(36);
private JButton evaluate = new JButton("Evaluate");
private PrologSession session = new PrologSession();

public ServerSocket srv;
public Socket soc;

public EvaluateGUI() {
JFrame frame = new JFrame("Prolog Evaluator");
Container panel = frame.getContentPane();
panel.add(new JScrollPane(text), BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(input, BorderLayout.CENTER);
inputPanel.add(evaluate, BorderLayout.EAST);
panel.add(inputPanel, BorderLayout. SOUTH);
text.setEditable(false);
evaluate.addActionListener(this);
input.addActionListener(this);
session.setPort(1363);
session.setHost("192.168.0.84");
session.setAutoConnect(true);
/*try{

srv = new ServerSocket(1363,10);
soc = srv.accept();
}
catch(Exception e)
{
System.out.println("Socket exception");
}
*/
try{
session.connect();
System.out.println("connected");
System.out.println(session.getTimeout() + " time out");
}
catch(Exception e)
{
System.out.println("Exception thrown");
}
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack();
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
try {
//session.setAutoConnect(true);input.getText()

Bindings bindings = new Bindings().bind("E",
input.getText() + ".");
System.out.println("after bindings " + bindings);

if (session.isConnected())
System.out.println("IT IS CONNECTED");
else
System.out.println("IT IS NOT CONNECTED");

String myquery = "evaluate(E,R)";

QueryAnswer answer = session.executeQuery(myquery,
bindings);
System.out.println("after answer " + bindings);

if (session.isConnected())
System.out.println("IT IS CONNECTED");
else
System.out.println("IT IS NOT CONNECTED");
/*
Term result = answer.getValue("R");
System.out.println("after term");
if (result != null) {
text.append(input.getText() + " = " + result + '\n');
input.setText("");
} else {
text.append("Error: " + answer.getError() + "\n");
}
*/
} catch (Exception e) {
text.append("Error when querying Prolog Server: " +
e.getMessage() + '\n');
}
finally{

session.disconnect();
}
}

public static void main(String[] args) {
new EvaluateGUI();
}
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.