473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Java Prolog Sockets

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
0 2687

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Charles Rush | last post by:
I have some bench time and want to pick up a new language to, perhaps, reduce the amount of bench time in the future. To that end, I've been looking at either the C#/.NET (VB/.NET?) thing with the...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: Jay | last post by:
Hi, I implemeneted an FTP client and server long time back using Java. I found sockets porgramming in java quite useful and easy to handle. I now wanted to implement a similar program using C++....
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
5
by: Sophia | last post by:
Hi, My question is pretty much what the title describes. My situation is: The essential task is to provide a service to web user. This service can only be accessed from a java program on the...
13
by: RK | last post by:
I apologize if this is a stupid question, I'm asking Python group for perspective on Ruby, but I don't see how the alternative of going to a ruby group for a perspective on Ruby is going to do me...
4
by: dmvsatyakumar | last post by:
Hi Alll, When i try to parse the string.xml which is in UTF-16LE, its working fine with JDK 1.4 but gives the following error in JDK 1.5. Error : "Content is not allowed in prolog". Can any one of...
3
by: Ian Semmel | last post by:
I am not new to computing, but I am to Java and wonder if anyone could point me in the right (or a) direction. Specifically, how are multi-tiered applications put together. There appears to be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.