generated from SMA-building-blocks/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseAgent.java
More file actions
91 lines (71 loc) · 2.54 KB
/
BaseAgent.java
File metadata and controls
91 lines (71 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package simple_voting_structure;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
// import jade.wrapper.StaleProxyException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
public abstract class BaseAgent extends Agent {
private static final long serialVersionUID = 1L;
public static final String ODD = "ODD";
public static final String EVEN = "EVEN";
public static final String REQUEST = "REQUEST";
public static final String ANSWER = "ANSWER";
public static final String THANKS = "THANKS";
public static final String START = "START";
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\033[1;93m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
@Override
protected void setup() {}
protected void registerDF(Agent regAgent, String sdName, String sdType) {
try {
DFAgentDescription dfd = new DFAgentDescription();
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
sd.setType(sdType);
sd.setName(sdName);
dfd.addServices(sd);
DFService.register(regAgent, dfd);
System.out.println(getLocalName()+" REGISTERED WITH THE DF");
} catch (FIPAException e) {
e.printStackTrace();
}
}
protected void sendMessage(String agentName, int performative, String content) {
ACLMessage msg = new ACLMessage(performative);
msg.setContent(content);
msg.addReceiver(new AID(agentName, AID.ISLOCALNAME));
send(msg);
}
protected DFAgentDescription[] searchAgentByType (String type) {
DFAgentDescription search = new DFAgentDescription();
ServiceDescription sd = new ServiceDescription();
DFAgentDescription [] foundAgents = null;
sd.setType(type);
search.addServices(sd);
try {
foundAgents = DFService.search(this, search);
} catch ( Exception any ) {
any.printStackTrace();
}
return foundAgents;
}
protected void takeDown() {
// Deregister with the DF
try {
DFService.deregister(this);
System.out.println(getLocalName()+" DEREGISTERED WITH THE DF");
} catch (FIPAException e) {
e.printStackTrace();
}
}
}