473,756 Members | 5,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(libr ary(sockets)).
:- use_module(libr ary(prologbeans )).
:- use_module(libr ary(charsio)).
start_server(Po rt, QueueLength) :-
socket('AF_INET ', Socket),
socket_bind(Soc ket, 'AF_INET'(local host, Port)),
socket_listen(S ocket, QueueLength),
loop(Socket).

loop(Socket) :-
socket_accept(S ocket, Client, Stream),
on_exception(RE , get_request(Str eam, Request),
h1(Stream,Clien t,RE)),
on_exception(PE , process(Request , Stream, Client), h1(Stream,
Client, PE)),
loop(Socket).

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

process(bye, Stream, _Client) :-
close(Stream).
process(Request , Stream, Client) :-
on_exception(E, user:applicatio n_handle(Reques t,Result),
h2(Stream, no, E)),
return(Stream,R esult),
on_exception(RE , get_request(Str eam, Request1),
h1(Stream,Clien t,RE)),
on_exception(PE , process(Request 1, Stream, Client), h1(Stream,
Client, PE)).

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

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

h2(Stream, Ack, E) :-
format(Stream, "~w~n",[Ack]),
format(user_out put, "Applicatio n 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.Objec t.*;
import java.lang.Runti me.*;
import java.lang.Proce ss.*;
import java.lang.*;
import java.awt.*;
import java.applet.*;
import java.util.Stack ;
import java.awt.event. *;
import java.io.*;
import java.util.Strin gTokenizer;
import java.net.*;
import java.beans.*;
import se.sics.prologb eans.*;
import java.nio.channe ls.SocketChanne l;

public class EvaluateGUI implements ActionListener {

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

public ServerSocket srv;
public Socket soc;

public EvaluateGUI() {
JFrame frame = new JFrame("Prolog Evaluator");
Container panel = frame.getConten tPane();
panel.add(new JScrollPane(tex t), BorderLayout.CE NTER);
JPanel inputPanel = new JPanel(new BorderLayout()) ;
inputPanel.add( input, BorderLayout.CE NTER);
inputPanel.add( evaluate, BorderLayout.EA ST);
panel.add(input Panel, BorderLayout. SOUTH);
text.setEditabl e(false);
evaluate.addAct ionListener(thi s);
input.addAction Listener(this);
session.setPort (1363);
session.setHost ("192.168.0.84" );
session.setAuto Connect(true);
/*try{

srv = new ServerSocket(13 63,10);
soc = srv.accept();
}
catch(Exception e)
{
System.out.prin tln("Socket exception");
}
*/
try{
session.connect ();
System.out.prin tln("connected" );
System.out.prin tln(session.get Timeout() + " time out");
}
catch(Exception e)
{
System.out.prin tln("Exception thrown");
}
//frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
frame.pack();
frame.setVisibl e(true);
}

public void actionPerformed (ActionEvent event) {
try {
//session.setAuto Connect(true);i nput.getText()

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

if (session.isConn ected())
System.out.prin tln("IT IS CONNECTED");
else
System.out.prin tln("IT IS NOT CONNECTED");

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

QueryAnswer answer = session.execute Query(myquery,
bindings);
System.out.prin tln("after answer " + bindings);

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

session.disconn ect();
}
}

public static void main(String[] args) {
new EvaluateGUI();
}
}
Jul 17 '05 #1
0 2701

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

Similar topics

2
2880
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 goal of getting an MCAD, or going the JAVA route getting a Sun certification. What do you think? Is the Sun training the best for JAVA? What about C# or VB training? A little background. I've been a programmer for about 20 years with...
73
8062
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 as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
3
14870
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++. My questions are 1. How good a language is C++ for network programming? 2. what are good onine resources to learn C++ network programming? 3. How similar it is to Java socket programming? Thanks in advance.
133
8573
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
31
2600
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 insensitive string comparison, and a substring search. Then I found that there are no networking classes to speak of (except sockets). After I thought about it, C++ really pales by comparison. Java has a rich set of networking classes, regular...
5
1601
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 backend, and this program, unfortunately, is a 24/7 live process. My teammate is going to write a php program to handle the front end request, so my question is that, is it possible for such a design to work? If it is, how? Any help is greatly...
13
1577
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 any good... I just unpacked and tried out InstantRails, after turning off the local Plone stack. Looking over the IR stack, making the required hacks to the examples, looking at all it's pieces(including some of the more powerful PHP...
4
8786
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 you suggest me a solution for this. Its very urgent.
3
2243
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 multiple paths available and it all gets a bit confusing (to me). If, for example, I have something like (this is not a web app): - a front end which controls a number (variable at runtime) of terminals/workstations. - a database later - a main...
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10028
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9868
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.