Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/java/simple_voting_structure/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
package simple_voting_structure;

import jade.core.AID;
import jade.wrapper.AgentController;
import jade.wrapper.AgentContainer;
import jade.lang.acl.ACLMessage;

import java.io.IOException;
import java.util.ArrayList;



/**
* Class that set the main agent and it's actions
*/
Expand All @@ -37,15 +38,19 @@ protected void setup() {
votersQuorum = Integer.parseInt(args[0].toString());
}

int votingStarter = (int) (Math.random() * votersQuorum);

System.out.println("Agent number " + votingStarter + " will request to the mediator!");

for ( int i = 0; i < votersQuorum; ++i ) votersName.add("voter_" + i);

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

votersName.forEach(voter -> {
this.launchAgent(voter, "simple_voting_structure.Voter", null);
this.launchAgent(voter, "simple_voting_structure.Voter", null);

System.out.println(getLocalName() + " CREATED AND STARTED NEW VOTER: " + voter + " ON CONTAINER " + container.getName());
});
} catch (Exception any) {
Expand All @@ -60,15 +65,15 @@ protected void setup() {

// send them a message demanding start;
System.out.println("Starting system!");
sendMessage(m1AgentName, ACLMessage.INFORM, App.START);
sendMessage(votersName.get(votingStarter), ACLMessage.INFORM, App.START);
}



private void pauseSystem() {
try {
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)");
System.out.println("Press enter in the console to start the agents");
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);
System.out.println(App.ANSI_YELLOW + "Press enter in the console to start the agents" + App.ANSI_RESET);
System.in.read();
} catch (IOException e) {
e.printStackTrace();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/simple_voting_structure/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public abstract class BaseAgent extends Agent {
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() {}

Expand Down Expand Up @@ -48,6 +58,23 @@ protected void sendMessage(String agentName, int performative, String content) {
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 {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/simple_voting_structure/Voter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package simple_voting_structure;

import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

Expand Down Expand Up @@ -30,6 +34,25 @@ public void action() {
reply.setContent(Voter.ANSWER + " " + (Min + (int) (Math.random() * ((Max - Min) + 1))));
myAgent.send(reply);
System.out.println(myAgent.getLocalName() + " SENT ANSWER MESSAGE");
} else if (Voter.START.equalsIgnoreCase(msg.getContent())) {
ACLMessage msg2 = new ACLMessage(ACLMessage.INFORM);
msg2.setContent(Voter.START);

DFAgentDescription [] foundAgents = searchAgentByType("mediator");

try {
AID foundMediator = null;
if ( foundAgents.length > 0 ) {
foundMediator = foundAgents[0].getName();

msg2.addReceiver(foundMediator);

send(msg2);
System.out.println(getLocalName() + " SENT START MESSAGE TO " + foundMediator);
}
} catch ( Exception any ) {
any.printStackTrace();
}
} else if (Voter.THANKS.equalsIgnoreCase(msg.getContent())) {
System.out
.println(myAgent.getLocalName() + " RECEIVED THANKS MESSAGE FROM " + msg.getSender().getLocalName());
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/simple_voting_structure/launchAgent.java

This file was deleted.