commit ad03b9ad1eb9ebcd31e70db2e3b0e16f2f0aa9dd Author: Jakub Wiewióra Date: Sat Dec 3 15:24:13 2022 +0100 Initial files from lecturer diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..06e26b6 --- /dev/null +++ b/build.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jade/lib/commons-codec/commons-codec-1.3.jar b/jade/lib/commons-codec/commons-codec-1.3.jar new file mode 100644 index 0000000..957b675 Binary files /dev/null and b/jade/lib/commons-codec/commons-codec-1.3.jar differ diff --git a/jade/lib/jade.jar b/jade/lib/jade.jar new file mode 100644 index 0000000..0578475 Binary files /dev/null and b/jade/lib/jade.jar differ diff --git a/src/MyAgent.java b/src/MyAgent.java new file mode 100644 index 0000000..180d7cb --- /dev/null +++ b/src/MyAgent.java @@ -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); + } + } + } +} diff --git a/src/ServiceAgent.java b/src/ServiceAgent.java new file mode 100644 index 0000000..f06fe0c --- /dev/null +++ b/src/ServiceAgent.java @@ -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("
")+4, response.lastIndexOf("
")); + } +} + +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); + } + } +}