From 7a054b057a0e5d3e9d08a09a5144910d24fb8df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Wiewi=C3=B3ra?= Date: Sat, 3 Dec 2022 15:28:03 +0100 Subject: [PATCH] Slighty reformatted. --- src/MyAgent.java | 38 ++++++++++++++++++++++++++++---------- src/ServiceAgent.java | 26 +++++++++++++++++++------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/MyAgent.java b/src/MyAgent.java index 180d7cb..c9e0684 100644 --- a/src/MyAgent.java +++ b/src/MyAgent.java @@ -9,19 +9,27 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; -public class MyAgent extends Agent { - protected void setup () { +public class MyAgent extends Agent +{ + + protected void setup() + { displayResponse("Hello, I am " + getAID().getLocalName()); addBehaviour(new MyCyclicBehaviour(this)); //doDelete(); } - protected void takeDown() { + + protected void takeDown() + { displayResponse("See you"); } - public void displayResponse(String message) { + public void displayResponse(String message) + { JOptionPane.showMessageDialog(null,message,"Message",JOptionPane.PLAIN_MESSAGE); } - public void displayHtmlResponse(String html) { + + public void displayHtmlResponse(String html) + { JTextPane tp = new JTextPane(); JScrollPane js = new JScrollPane(); js.getViewport().add(tp); @@ -36,16 +44,26 @@ public class MyAgent extends Agent { } } -class MyCyclicBehaviour extends CyclicBehaviour { +class MyCyclicBehaviour extends CyclicBehaviour +{ MyAgent myAgent; - public MyCyclicBehaviour(MyAgent myAgent) { + + + public MyCyclicBehaviour(MyAgent myAgent) + { this.myAgent = myAgent; } - public void action() { + + + public void action() + { ACLMessage message = myAgent.receive(); - if (message == null) { + if (message == null) + { block(); - } else { + } + else + { String ontology = message.getOntology(); String content = message.getContent(); int performative = message.getPerformative(); diff --git a/src/ServiceAgent.java b/src/ServiceAgent.java index f06fe0c..6562d3d 100644 --- a/src/ServiceAgent.java +++ b/src/ServiceAgent.java @@ -8,11 +8,14 @@ import jade.domain.FIPAAgentManagement.*; import java.net.*; import java.io.*; -public class ServiceAgent extends Agent { - protected void setup () { +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"); @@ -21,12 +24,17 @@ public class ServiceAgent extends Agent { ServiceDescription sd2 = new ServiceDescription(); sd2.setType("answers"); sd2.setName("dictionary"); + //add them all dfad.addServices(sd1); dfad.addServices(sd2); - try { + + try + { DFService.register(this,dfad); - } catch (FIPAException ex) { + } + catch (FIPAException ex) + { ex.printStackTrace(); } @@ -34,11 +42,15 @@ public class ServiceAgent extends Agent { addBehaviour(new DictionaryCyclicBehaviour(this)); //doDelete(); } - protected void takeDown() { + protected void takeDown() + { //services deregistration before termination - try { + try + { DFService.deregister(this); - } catch (FIPAException ex) { + } + catch (FIPAException ex) + { ex.printStackTrace(); } }