Zadanie 3
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
<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;TurboAgent1:jadelab1.TurboAgent" />
|
||||
<arg value="UserAgent:jadelab1.MyAgent;AllAgent1:jadelab1.AllAgent" />
|
||||
</java>
|
||||
</target>
|
||||
</project>
|
||||
|
||||
@@ -8,7 +8,7 @@ import jade.domain.FIPAAgentManagement.*;
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class TurboAgent extends Agent
|
||||
public class AllAgent extends Agent
|
||||
{
|
||||
protected void setup ()
|
||||
{
|
||||
@@ -19,7 +19,7 @@ public class TurboAgent extends Agent
|
||||
//service no 1
|
||||
ServiceDescription sd1 = new ServiceDescription();
|
||||
sd1.setType("answers");
|
||||
sd1.setName("bouvier");
|
||||
sd1.setName("all");
|
||||
|
||||
//add them all
|
||||
dfad.addServices(sd1);
|
||||
@@ -33,7 +33,7 @@ public class TurboAgent extends Agent
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
addBehaviour(new BouvierCyclicBehaviour(this));
|
||||
addBehaviour(new UniversalDICTCyclicBehaviour(this));
|
||||
//doDelete();
|
||||
}
|
||||
protected void takeDown()
|
||||
@@ -87,12 +87,12 @@ public class TurboAgent extends Agent
|
||||
}
|
||||
}
|
||||
|
||||
class BouvierCyclicBehaviour extends CyclicBehaviour
|
||||
class UniversalDICTCyclicBehaviour extends CyclicBehaviour
|
||||
{
|
||||
TurboAgent agent;
|
||||
AllAgent agent;
|
||||
|
||||
|
||||
public BouvierCyclicBehaviour(TurboAgent agent)
|
||||
public UniversalDICTCyclicBehaviour(AllAgent agent)
|
||||
{
|
||||
this.agent = agent;
|
||||
}
|
||||
@@ -100,7 +100,8 @@ class BouvierCyclicBehaviour extends CyclicBehaviour
|
||||
|
||||
public void action()
|
||||
{
|
||||
MessageTemplate template = MessageTemplate.MatchOntology("bouvier");
|
||||
MessageTemplate template = MessageTemplate.MatchAll();
|
||||
|
||||
ACLMessage message = agent.receive(template);
|
||||
if (message == null)
|
||||
{
|
||||
@@ -115,7 +116,7 @@ class BouvierCyclicBehaviour extends CyclicBehaviour
|
||||
String response = "";
|
||||
try
|
||||
{
|
||||
response = agent.makeRequest("bouvier",content);
|
||||
response = agent.makeRequest(message.getOntology(), content);
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
@@ -76,8 +76,30 @@ class MyCyclicBehaviour extends CyclicBehaviour
|
||||
dfad.addServices(sd);
|
||||
try
|
||||
{
|
||||
// Searching for ontology specific service
|
||||
DFAgentDescription[] result = DFService.search(myAgent, dfad);
|
||||
if (result.length == 0) myAgent.displayResponse("No service has been found ...");
|
||||
if (result.length == 0)
|
||||
{
|
||||
myAgent.displayResponse("No ontology specific service has been found ...");
|
||||
|
||||
// Search for universal service
|
||||
dfad.clearAllServices();
|
||||
sd.setName("all");
|
||||
dfad.addServices(sd);
|
||||
result = DFService.search(myAgent, dfad);
|
||||
|
||||
if (result.length == 0) myAgent.displayResponse("No universal service has been found ...");
|
||||
else
|
||||
{
|
||||
String foundAgent = result[0].getName().getLocalName();
|
||||
myAgent.displayResponse("Agent " + foundAgent + " is universal 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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String foundAgent = result[0].getName().getLocalName();
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
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");
|
||||
//service no 3
|
||||
ServiceDescription sd3 = new ServiceDescription();
|
||||
sd3.setType("answers");
|
||||
sd3.setName("gcide");
|
||||
|
||||
//add them all
|
||||
dfad.addServices(sd1);
|
||||
dfad.addServices(sd2);
|
||||
dfad.addServices(sd3);
|
||||
|
||||
try
|
||||
{
|
||||
DFService.register(this,dfad);
|
||||
}
|
||||
catch (FIPAException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
addBehaviour(new WordnetCyclicBehaviour(this));
|
||||
addBehaviour(new DictionaryCyclicBehaviour(this));
|
||||
addBehaviour(new GCIDECyclicBehaviour(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 GCIDECyclicBehaviour extends CyclicBehaviour
|
||||
{
|
||||
ServiceAgent agent;
|
||||
|
||||
|
||||
public GCIDECyclicBehaviour(ServiceAgent agent)
|
||||
{
|
||||
this.agent = agent;
|
||||
}
|
||||
|
||||
|
||||
public void action()
|
||||
{
|
||||
MessageTemplate template = MessageTemplate.MatchOntology("gcide");
|
||||
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("gcide",content);
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
response = ex.getMessage();
|
||||
}
|
||||
reply.setContent(response);
|
||||
agent.send(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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