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
2 changes: 1 addition & 1 deletion src/main/java/election_structure/Ballot.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected WakerBehaviour timeoutBehaviour( String motivation, long timeout) {

@Override
protected void onWake() {
if ( motivation.equals("collectVotes") && !votesCollected) {
if ( motivation.equals("collectVotes") && Boolean.FALSE.equals(votesCollected)) {
votesCollected = true;
logger.log(Level.WARNING,
String.format("%s Agent voting time window ended! %s", ANSI_YELLOW, ANSI_RESET));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/election_structure/BaseAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class BaseAgent extends Agent {
public static final String UNEXPECTED_MSG = "RECEIVED AN UNEXPECTED MESSAGE FROM";
public static final String CREATE = "CREATE";
public static final String CREATOR = "Creator";
public static final String VOTER = "voter";
public static final String QUORUM = "QUORUM";
public static final String CANDIDATURE = "CANDIDATURE";
public static final String PROPOSAL = "PROPOSAL";
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/election_structure/Mediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Mediator extends BaseAgent {
private Boolean ballotCreated = false;
private Boolean ballotRequested = false;

private Hashtable<AID, Candidature> candidatures;
private transient Hashtable<AID, Candidature> candidatures;
private ArrayList<AID> preCandidates;
private Stack<Integer> candidateCodes;

Expand Down Expand Up @@ -88,7 +88,7 @@ public void action () {
preCandidates.add(msg.getSender());
}

if ( (registeredQuorum == totalQuorum) && (preCandidates.isEmpty()) && !ballotRequested) {
if ( (registeredQuorum == totalQuorum) && (preCandidates.isEmpty()) && Boolean.FALSE.equals(ballotRequested)) {
createBallot();
}

Expand All @@ -113,7 +113,7 @@ public void action () {
if ( votCode == votingCode ) {
startElection();
} else {
throw new Exception("Voting code does not match!");
throw new IllegalArgumentException("Voting code does not match!");
}
} catch ( Exception e ) {
logger.log(Level.SEVERE, String.format("%s ERROR WHILE PERFORMING BALLOT SETUP %s", ANSI_RED, ANSI_RESET));
Expand All @@ -127,15 +127,17 @@ public void action () {
String winnersCnt = splittedMsg[2];
String winnersVote = splittedMsg[4];

String winnersCode = "";


StringBuilder bld = new StringBuilder();

for(int i = 6; i < splittedMsg.length; i++){
winnersCode += splittedMsg[i] + " ";
bld.append(splittedMsg[i] + " ");
}

String results = String.format(" ELECTION RESULTS FOR VOTING %d: \n", votingCode);
results = results.concat(String.format(" \t\tWinner count: %s\n",winnersCnt));
results = results.concat(String.format(" \t\tWinner received votes %s\n", winnersVote));
String winnersCode = bld.toString();

String results = String.format(" ELECTION RESULTS FOR VOTING %d: %n", votingCode);
results = results.concat(String.format(" \t\tWinner count: %s%n",winnersCnt));
results = results.concat(String.format(" \t\tWinner received votes %s%n", winnersVote));
results = results.concat(String.format(" \t\tWinner Codes: %s ", winnersCode));

logger.log(Level.INFO, String.format("%s%s%s", ANSI_PURPLE, results, ANSI_RESET));
Expand Down Expand Up @@ -183,7 +185,7 @@ public void action() {

preCandidates.remove(msg.getSender());

if ( (registeredQuorum == totalQuorum) && (preCandidates.isEmpty()) && !ballotRequested) {
if ( (registeredQuorum == totalQuorum) && (preCandidates.isEmpty()) && Boolean.FALSE.equals(ballotRequested)) {
createBallot();
}

Expand All @@ -203,11 +205,11 @@ protected WakerBehaviour timeoutBehaviour( String motivation, long timeout) {

@Override
protected void onWake() {
if ( votingCode != -1 && motivation.equals("registration") && !ballotCreated ) {
if ( votingCode != -1 && motivation.equals("registration") && Boolean.FALSE.equals(ballotCreated) ) {
logger.log(Level.WARNING,
String.format("%s Agent registration timed out! %s", ANSI_YELLOW, ANSI_RESET));
createBallot();
} else if ( votingCode != -1 && motivation.equals("Create-Ballot") && !ballotCreated ){
} else if ( votingCode != -1 && motivation.equals("Create-Ballot") && Boolean.FALSE.equals(ballotCreated) ){
logger.log(Level.WARNING,
String.format("%s Ballot creation timed out! %s", ANSI_YELLOW, ANSI_RESET));
createBallot();
Expand All @@ -220,7 +222,7 @@ private void informWinner(String content){
ACLMessage msg2 = new ACLMessage(ACLMessage.INFORM);

ArrayList<DFAgentDescription> foundVotingParticipants;
String [] types = { Integer.toString(votingCode), "voter" };
String [] types = { Integer.toString(votingCode), VOTER };

foundVotingParticipants = new ArrayList<>(
Arrays.asList(searchAgentByType(types)));
Expand All @@ -247,7 +249,7 @@ protected void resetElection(Agent myAgent){
candidateCodes = new Stack<>();

deleteAgentServices(myAgent, Integer.toString(votingCode), Integer.toString(votingCode));
deleteAgentServices(myAgent, "voter", "Candidate");
deleteAgentServices(myAgent, VOTER, "Candidate");

votingCode = -1;

Expand Down Expand Up @@ -290,7 +292,7 @@ private void setupVotingWeights () {
private void startElection() {
try {
ArrayList<DFAgentDescription> foundVotingParticipants;
String [] types = { Integer.toString(votingCode), "voter" };
String [] types = { Integer.toString(votingCode), VOTER };

foundVotingParticipants = new ArrayList<>(
Arrays.asList(searchAgentByType(types)));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/election_structure/Voter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Voter extends BaseAgent {
private int candidatesCount;
private int candidatesExpected;
private int selectionMethod;
private int myCandidatureCode = -1;;
private int myCandidatureCode = -1;

@Override
protected void setup() {
Expand Down