diff --git a/src/main/java/election_structure/Ballot.java b/src/main/java/election_structure/Ballot.java index 5feec07..6401cf1 100644 --- a/src/main/java/election_structure/Ballot.java +++ b/src/main/java/election_structure/Ballot.java @@ -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)); diff --git a/src/main/java/election_structure/BaseAgent.java b/src/main/java/election_structure/BaseAgent.java index 0fee5df..f8b816e 100644 --- a/src/main/java/election_structure/BaseAgent.java +++ b/src/main/java/election_structure/BaseAgent.java @@ -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"; diff --git a/src/main/java/election_structure/Mediator.java b/src/main/java/election_structure/Mediator.java index b3d2492..016b0ab 100644 --- a/src/main/java/election_structure/Mediator.java +++ b/src/main/java/election_structure/Mediator.java @@ -29,7 +29,7 @@ public class Mediator extends BaseAgent { private Boolean ballotCreated = false; private Boolean ballotRequested = false; - private Hashtable candidatures; + private transient Hashtable candidatures; private ArrayList preCandidates; private Stack candidateCodes; @@ -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(); } @@ -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)); @@ -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)); @@ -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(); } @@ -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(); @@ -220,7 +222,7 @@ private void informWinner(String content){ ACLMessage msg2 = new ACLMessage(ACLMessage.INFORM); ArrayList foundVotingParticipants; - String [] types = { Integer.toString(votingCode), "voter" }; + String [] types = { Integer.toString(votingCode), VOTER }; foundVotingParticipants = new ArrayList<>( Arrays.asList(searchAgentByType(types))); @@ -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; @@ -290,7 +292,7 @@ private void setupVotingWeights () { private void startElection() { try { ArrayList foundVotingParticipants; - String [] types = { Integer.toString(votingCode), "voter" }; + String [] types = { Integer.toString(votingCode), VOTER }; foundVotingParticipants = new ArrayList<>( Arrays.asList(searchAgentByType(types))); diff --git a/src/main/java/election_structure/Voter.java b/src/main/java/election_structure/Voter.java index 45cfd49..9b68a15 100644 --- a/src/main/java/election_structure/Voter.java +++ b/src/main/java/election_structure/Voter.java @@ -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() {