Skip to content

Commit 87de44e

Browse files
authored
Merge pull request #3 from SMA-building-blocks/request-vote
✨ randomly selects a voter to request a new voting to the mediator
2 parents 8ca9ddf + 9ea0f7b commit 87de44e

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/main/java/simple_voting_structure/App.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*/
44
package simple_voting_structure;
55

6-
import jade.core.AID;
76
import jade.wrapper.AgentController;
87
import jade.wrapper.AgentContainer;
98
import jade.lang.acl.ACLMessage;
109

1110
import java.io.IOException;
1211
import java.util.ArrayList;
1312

13+
14+
1415
/**
1516
* Class that set the main agent and it's actions
1617
*/
@@ -37,15 +38,19 @@ protected void setup() {
3738
votersQuorum = Integer.parseInt(args[0].toString());
3839
}
3940

41+
int votingStarter = (int) (Math.random() * votersQuorum);
42+
43+
System.out.println("Agent number " + votingStarter + " will request to the mediator!");
44+
4045
for ( int i = 0; i < votersQuorum; ++i ) votersName.add("voter_" + i);
4146

4247
try {
4348
// create agents on the same container of the creator agent
4449
AgentContainer container = (AgentContainer) getContainerController(); // get a container controller for creating
4550

4651
votersName.forEach(voter -> {
47-
this.launchAgent(voter, "simple_voting_structure.Voter", null);
48-
52+
this.launchAgent(voter, "simple_voting_structure.Voter", null);
53+
4954
System.out.println(getLocalName() + " CREATED AND STARTED NEW VOTER: " + voter + " ON CONTAINER " + container.getName());
5055
});
5156
} catch (Exception any) {
@@ -60,15 +65,15 @@ protected void setup() {
6065

6166
// send them a message demanding start;
6267
System.out.println("Starting system!");
63-
sendMessage(m1AgentName, ACLMessage.INFORM, App.START);
68+
sendMessage(votersName.get(votingStarter), ACLMessage.INFORM, App.START);
6469
}
6570

6671

6772

6873
private void pauseSystem() {
6974
try {
70-
System.out.println("The system is paused -- this action is only here to let you activate the sniffer on the agents, if you want (see documentation)");
71-
System.out.println("Press enter in the console to start the agents");
75+
System.out.println(App.ANSI_YELLOW + "The system is paused -- this action is only here to let you activate the sniffer on the agents, if you want (see documentation)" + App.ANSI_RESET);
76+
System.out.println(App.ANSI_YELLOW + "Press enter in the console to start the agents" + App.ANSI_RESET);
7277
System.in.read();
7378
} catch (IOException e) {
7479
e.printStackTrace();

src/main/java/simple_voting_structure/BaseAgent.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public abstract class BaseAgent extends Agent {
2020
public static final String THANKS = "THANKS";
2121
public static final String START = "START";
2222

23+
public static final String ANSI_RESET = "\u001B[0m";
24+
public static final String ANSI_BLUE = "\u001B[34m";
25+
public static final String ANSI_BLACK = "\u001B[30m";
26+
public static final String ANSI_RED = "\u001B[31m";
27+
public static final String ANSI_GREEN = "\u001B[32m";
28+
public static final String ANSI_YELLOW = "\033[1;93m";
29+
public static final String ANSI_PURPLE = "\u001B[35m";
30+
public static final String ANSI_CYAN = "\u001B[36m";
31+
public static final String ANSI_WHITE = "\u001B[37m";
32+
2333
@Override
2434
protected void setup() {}
2535

@@ -48,6 +58,23 @@ protected void sendMessage(String agentName, int performative, String content) {
4858
send(msg);
4959
}
5060

61+
protected DFAgentDescription[] searchAgentByType (String type) {
62+
DFAgentDescription search = new DFAgentDescription();
63+
ServiceDescription sd = new ServiceDescription();
64+
DFAgentDescription [] foundAgents = null;
65+
66+
sd.setType(type);
67+
search.addServices(sd);
68+
69+
try {
70+
foundAgents = DFService.search(this, search);
71+
} catch ( Exception any ) {
72+
any.printStackTrace();
73+
}
74+
75+
return foundAgents;
76+
}
77+
5178
protected void takeDown() {
5279
// Deregister with the DF
5380
try {

src/main/java/simple_voting_structure/Voter.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package simple_voting_structure;
22

3+
import jade.core.AID;
34
import jade.core.behaviours.CyclicBehaviour;
5+
import jade.domain.DFService;
6+
import jade.domain.FIPAAgentManagement.DFAgentDescription;
7+
import jade.domain.FIPAAgentManagement.ServiceDescription;
48
import jade.lang.acl.ACLMessage;
59
import jade.lang.acl.MessageTemplate;
610

@@ -30,6 +34,25 @@ public void action() {
3034
reply.setContent(Voter.ANSWER + " " + (Min + (int) (Math.random() * ((Max - Min) + 1))));
3135
myAgent.send(reply);
3236
System.out.println(myAgent.getLocalName() + " SENT ANSWER MESSAGE");
37+
} else if (Voter.START.equalsIgnoreCase(msg.getContent())) {
38+
ACLMessage msg2 = new ACLMessage(ACLMessage.INFORM);
39+
msg2.setContent(Voter.START);
40+
41+
DFAgentDescription [] foundAgents = searchAgentByType("mediator");
42+
43+
try {
44+
AID foundMediator = null;
45+
if ( foundAgents.length > 0 ) {
46+
foundMediator = foundAgents[0].getName();
47+
48+
msg2.addReceiver(foundMediator);
49+
50+
send(msg2);
51+
System.out.println(getLocalName() + " SENT START MESSAGE TO " + foundMediator);
52+
}
53+
} catch ( Exception any ) {
54+
any.printStackTrace();
55+
}
3356
} else if (Voter.THANKS.equalsIgnoreCase(msg.getContent())) {
3457
System.out
3558
.println(myAgent.getLocalName() + " RECEIVED THANKS MESSAGE FROM " + msg.getSender().getLocalName());

src/main/java/simple_voting_structure/launchAgent.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)