Initial files from lecturer
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
build
|
||||
26
build.xml
Normal file
26
build.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<project name="JadeLab1" default="run" basedir=".">
|
||||
<property name="src" location="src"/>
|
||||
<property name="build" location="build"/>
|
||||
|
||||
<target name="init">
|
||||
<mkdir dir="${build}"/>
|
||||
</target>
|
||||
|
||||
<target name="compile" depends="init" description="compile the source " >
|
||||
<javac srcdir="${src}" destdir="${build}">
|
||||
<classpath location="jade/lib/jade.jar" />
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<target name="clean" description="clean up" >
|
||||
<delete dir="${build}"/>
|
||||
<delete dir="${dist}"/>
|
||||
</target>
|
||||
|
||||
<target name="run" description="create agents" >
|
||||
<java fork="true" classpath="jade/lib/jade.jar;build" classname="jade.Boot">
|
||||
<arg value="-gui" />
|
||||
<arg value="UserAgent:jadelab1.MyAgent;ServiceAgent:jadelab1.ServiceAgent" />
|
||||
</java>
|
||||
</target>
|
||||
</project>
|
||||
BIN
jade/lib/commons-codec/commons-codec-1.3.jar
Normal file
BIN
jade/lib/commons-codec/commons-codec-1.3.jar
Normal file
Binary file not shown.
BIN
jade/lib/jade.jar
Normal file
BIN
jade/lib/jade.jar
Normal file
Binary file not shown.
86
src/MyAgent.java
Normal file
86
src/MyAgent.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package jadelab1;
|
||||
|
||||
import jade.core.*;
|
||||
import jade.core.behaviours.*;
|
||||
import jade.lang.acl.*;
|
||||
import jade.domain.*;
|
||||
import jade.domain.FIPAAgentManagement.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class MyAgent extends Agent {
|
||||
protected void setup () {
|
||||
displayResponse("Hello, I am " + getAID().getLocalName());
|
||||
addBehaviour(new MyCyclicBehaviour(this));
|
||||
//doDelete();
|
||||
}
|
||||
protected void takeDown() {
|
||||
displayResponse("See you");
|
||||
}
|
||||
public void displayResponse(String message) {
|
||||
JOptionPane.showMessageDialog(null,message,"Message",JOptionPane.PLAIN_MESSAGE);
|
||||
}
|
||||
public void displayHtmlResponse(String html) {
|
||||
JTextPane tp = new JTextPane();
|
||||
JScrollPane js = new JScrollPane();
|
||||
js.getViewport().add(tp);
|
||||
JFrame jf = new JFrame();
|
||||
jf.getContentPane().add(js);
|
||||
jf.pack();
|
||||
jf.setSize(400,500);
|
||||
jf.setVisible(true);
|
||||
tp.setContentType("text/html");
|
||||
tp.setEditable(false);
|
||||
tp.setText(html);
|
||||
}
|
||||
}
|
||||
|
||||
class MyCyclicBehaviour extends CyclicBehaviour {
|
||||
MyAgent myAgent;
|
||||
public MyCyclicBehaviour(MyAgent myAgent) {
|
||||
this.myAgent = myAgent;
|
||||
}
|
||||
public void action() {
|
||||
ACLMessage message = myAgent.receive();
|
||||
if (message == null) {
|
||||
block();
|
||||
} else {
|
||||
String ontology = message.getOntology();
|
||||
String content = message.getContent();
|
||||
int performative = message.getPerformative();
|
||||
if (performative == ACLMessage.REQUEST)
|
||||
{
|
||||
//I cannot answer but I will search for someone who can
|
||||
DFAgentDescription dfad = new DFAgentDescription();
|
||||
ServiceDescription sd = new ServiceDescription();
|
||||
sd.setName(ontology);
|
||||
dfad.addServices(sd);
|
||||
try
|
||||
{
|
||||
DFAgentDescription[] result = DFService.search(myAgent, dfad);
|
||||
if (result.length == 0) myAgent.displayResponse("No service has been found ...");
|
||||
else
|
||||
{
|
||||
String foundAgent = result[0].getName().getLocalName();
|
||||
myAgent.displayResponse("Agent " + foundAgent + " is a service provider. Sending message to " + foundAgent);
|
||||
ACLMessage forward = new ACLMessage(ACLMessage.REQUEST);
|
||||
forward.addReceiver(new AID(foundAgent, AID.ISLOCALNAME));
|
||||
forward.setContent(content);
|
||||
forward.setOntology(ontology);
|
||||
myAgent.send(forward);
|
||||
}
|
||||
}
|
||||
catch (FIPAException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
myAgent.displayResponse("Problem occured while searching for a service ...");
|
||||
}
|
||||
}
|
||||
else
|
||||
{ //when it is an answer
|
||||
myAgent.displayHtmlResponse(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
154
src/ServiceAgent.java
Normal file
154
src/ServiceAgent.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package jadelab1;
|
||||
|
||||
import jade.core.*;
|
||||
import jade.core.behaviours.*;
|
||||
import jade.lang.acl.*;
|
||||
import jade.domain.*;
|
||||
import jade.domain.FIPAAgentManagement.*;
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class ServiceAgent extends Agent {
|
||||
protected void setup () {
|
||||
//services registration at DF
|
||||
DFAgentDescription dfad = new DFAgentDescription();
|
||||
dfad.setName(getAID());
|
||||
//service no 1
|
||||
ServiceDescription sd1 = new ServiceDescription();
|
||||
sd1.setType("answers");
|
||||
sd1.setName("wordnet");
|
||||
//service no 2
|
||||
ServiceDescription sd2 = new ServiceDescription();
|
||||
sd2.setType("answers");
|
||||
sd2.setName("dictionary");
|
||||
//add them all
|
||||
dfad.addServices(sd1);
|
||||
dfad.addServices(sd2);
|
||||
try {
|
||||
DFService.register(this,dfad);
|
||||
} catch (FIPAException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
addBehaviour(new WordnetCyclicBehaviour(this));
|
||||
addBehaviour(new DictionaryCyclicBehaviour(this));
|
||||
//doDelete();
|
||||
}
|
||||
protected void takeDown() {
|
||||
//services deregistration before termination
|
||||
try {
|
||||
DFService.deregister(this);
|
||||
} catch (FIPAException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
public String makeRequest(String serviceName, String word)
|
||||
{
|
||||
StringBuffer response = new StringBuffer();
|
||||
try
|
||||
{
|
||||
URL url;
|
||||
URLConnection urlConn;
|
||||
DataOutputStream printout;
|
||||
DataInputStream input;
|
||||
url = new URL("http://dict.org/bin/Dict");
|
||||
urlConn = url.openConnection();
|
||||
urlConn.setDoInput(true);
|
||||
urlConn.setDoOutput(true);
|
||||
urlConn.setUseCaches(false);
|
||||
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
String content = "Form=Dict1&Strategy=*&Database=" + URLEncoder.encode(serviceName) + "&Query=" + URLEncoder.encode(word) + "&submit=Submit+query";
|
||||
//forth
|
||||
printout = new DataOutputStream(urlConn.getOutputStream());
|
||||
printout.writeBytes(content);
|
||||
printout.flush();
|
||||
printout.close();
|
||||
//back
|
||||
input = new DataInputStream(urlConn.getInputStream());
|
||||
String str;
|
||||
while (null != ((str = input.readLine())))
|
||||
{
|
||||
response.append(str);
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
//cut what is unnecessary
|
||||
return response.substring(response.indexOf("<hr>")+4, response.lastIndexOf("<hr>"));
|
||||
}
|
||||
}
|
||||
|
||||
class WordnetCyclicBehaviour extends CyclicBehaviour
|
||||
{
|
||||
ServiceAgent agent;
|
||||
public WordnetCyclicBehaviour(ServiceAgent agent)
|
||||
{
|
||||
this.agent = agent;
|
||||
}
|
||||
public void action()
|
||||
{
|
||||
MessageTemplate template = MessageTemplate.MatchOntology("wordnet");
|
||||
ACLMessage message = agent.receive(template);
|
||||
if (message == null)
|
||||
{
|
||||
block();
|
||||
}
|
||||
else
|
||||
{
|
||||
//process the incoming message
|
||||
String content = message.getContent();
|
||||
ACLMessage reply = message.createReply();
|
||||
reply.setPerformative(ACLMessage.INFORM);
|
||||
String response = "";
|
||||
try
|
||||
{
|
||||
response = agent.makeRequest("wn",content);
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
response = ex.getMessage();
|
||||
}
|
||||
reply.setContent(response);
|
||||
agent.send(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DictionaryCyclicBehaviour extends CyclicBehaviour
|
||||
{
|
||||
ServiceAgent agent;
|
||||
public DictionaryCyclicBehaviour(ServiceAgent agent)
|
||||
{
|
||||
this.agent = agent;
|
||||
}
|
||||
public void action()
|
||||
{
|
||||
MessageTemplate template = MessageTemplate.MatchOntology("dictionary");
|
||||
ACLMessage message = agent.receive(template);
|
||||
if (message == null)
|
||||
{
|
||||
block();
|
||||
}
|
||||
else
|
||||
{
|
||||
//process the incoming message
|
||||
String content = message.getContent();
|
||||
ACLMessage reply = message.createReply();
|
||||
reply.setPerformative(ACLMessage.INFORM);
|
||||
String response = "";
|
||||
try
|
||||
{
|
||||
response = agent.makeRequest("english", content);
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
response = ex.getMessage();
|
||||
}
|
||||
reply.setContent(response);
|
||||
agent.send(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user