From a39a3d65e99c89f9a415696b3af79815ef5ba12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Wiewi=C3=B3ra?= Date: Sat, 3 Dec 2022 15:33:36 +0100 Subject: [PATCH] Zadanie 2 --- build.xml | 2 +- src/TurboAgent.java | 128 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/TurboAgent.java diff --git a/build.xml b/build.xml index 06e26b6..c019c7d 100644 --- a/build.xml +++ b/build.xml @@ -20,7 +20,7 @@ - + diff --git a/src/TurboAgent.java b/src/TurboAgent.java new file mode 100644 index 0000000..9150c07 --- /dev/null +++ b/src/TurboAgent.java @@ -0,0 +1,128 @@ +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 TurboAgent 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("bouvier"); + + //add them all + dfad.addServices(sd1); + + try + { + DFService.register(this,dfad); + } + catch (FIPAException ex) + { + ex.printStackTrace(); + } + + addBehaviour(new BouvierCyclicBehaviour(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 BouvierCyclicBehaviour extends CyclicBehaviour +{ + TurboAgent agent; + + + public BouvierCyclicBehaviour(TurboAgent agent) + { + this.agent = agent; + } + + + public void action() + { + MessageTemplate template = MessageTemplate.MatchOntology("bouvier"); + 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("bouvier",content); + } + catch (NumberFormatException ex) + { + response = ex.getMessage(); + } + reply.setContent(response); + agent.send(reply); + } + } +}