public DeviationComparison(string outputPath) { this.outputPath = outputPath; votingSystemList = getVotingSystemList(); politicalSpectrum = new Spectrum(); voterList = Voter.createVoterList(Tweakables.VOTER_COUNT, politicalSpectrum); System.IO.File.WriteAllText(@outputPath, Tweakables.TRIAL_COUNT + " trials run with " + Tweakables.CANDIDATE_COUNT + " candidates and " + Tweakables.VOTER_COUNT + " voters and " + Tweakables.SPECTRUM_DIMENSIONS + " political spectrum dimensions." + Environment.NewLine); System.IO.File.AppendAllText(@outputPath, ElectionSummary.getHeader(votingSystemList)); System.IO.File.AppendAllText(@outputPath, Environment.NewLine); }
public void runSimulation(int trials) { Roster roster; List <Ballot> ballotList; Dictionary <BallotInstructions, List <Ballot> > ballotDictionary = new Dictionary <BallotInstructions, List <Ballot> >(); VotingSystemResult votingSystemResult; List <VoteSummary> voteSummaryList = new List <VoteSummary>(); for (int i = 0; i < trials; i++) { roster = new Roster(voterList); foreach (Voter voter in voterList) { voter.setRoster(roster); } ballotDictionary.Clear(); foreach (VotingSystem votingSystem in votingSystemList) { // Get or create the ballots if (ballotDictionary.ContainsKey(votingSystem.ballotInstructions)) { ballotList = ballotDictionary[votingSystem.ballotInstructions]; } else { ballotList = new List <Ballot>(); foreach (Voter voter in voterList) { ballotList.Add(voter.createBallot(votingSystem.ballotInstructions)); } ballotDictionary.Add(votingSystem.ballotInstructions, ballotList); } // Get the vote summary votingSystemResult = votingSystem.getResult(roster, ballotList); voteSummaryList.Add(getVoteSummary(votingSystemResult)); } ElectionSummary summary = new ElectionSummary( roster.lowestDeviationCandidate.voter.position.deviation, roster.highestDeviationCandidate.voter.position.deviation, voteSummaryList ); System.IO.File.AppendAllText(@outputPath, summary.ToString() + Environment.NewLine); System.Console.WriteLine("Simulation {0} complete", i + 1); Console.ReadKey(); System.Console.WriteLine(""); } }
private static void SortTallies(this ElectionSummary summary) { if (summary?.BallotEntries == null) { return; } foreach (var entry in summary.BallotEntries) { entry.Tallies = entry.Tallies.OrderByDescending(t => t.VoteCount); } }
private static ElectionSummary ToOrderedSummary(this ElectionSummary summary) { if (summary is null) { return(null); } // This is a quick way to make a deep copy. We may want to look at automapper // and converting the extension methods to a service so that Automapper can be injected // into the service and used. For now, just a quick serialization and deserialization // does the trick without having to worry about specific columns var serialized = JsonConvert.SerializeObject(summary); var copy = JsonConvert.DeserializeObject <ElectionSummary>(serialized); copy.SortTallies(); return(copy); }
public async Task ImportElectionResultsData(ElectionSummary electionSummary) { try { if (electionSummary != null) { var table = await GetElectionSummaryTable(); var operation = TableOperation.InsertOrReplace(electionSummary.ToElectionSummaryEntity()); await table.ExecuteAsync(operation); } } catch (Exception ex) { _logger.LogError(ex, "Failed to import tracking data."); throw; } }
public static ElectionSummaryEntity ToElectionSummaryEntity(this ElectionSummary summary) { if (summary is null) { return(null); } var json = JsonConvert.SerializeObject(summary.BallotEntries); var orderedJson = JsonConvert.SerializeObject(summary.ToOrderedSummary().BallotEntries); return(new ElectionSummaryEntity { IsComplete = true, PartitionKey = AzTableStorageElectionResultsRepository.ElectionSummaryPartitionKey, RowKey = AzTableStorageElectionResultsRepository.ElectionSummaryRowKey, BallotEntries = json, OrderedBallotEntries = orderedJson }); }
public static ElectionSummary ToElectionSummary(this ElectionSummaryEntity entity) { if (entity is null) { return(null); } var summary = new ElectionSummary { IsComplete = entity.IsComplete, BallotEntries = JsonConvert.DeserializeObject <List <BallotEntry> >(entity.OrderedBallotEntries ?? entity.BallotEntries) }; if (string.IsNullOrWhiteSpace(entity.OrderedBallotEntries)) { summary.SortTallies(); } return(summary); }
public static Mock <IElectionResultsService> CreateElectionResultsService( List <BallotStatus> trackers = null, ElectionSummary summary = null) { var mock = new Mock <IElectionResultsService>(); mock.Setup(m => m.GetBallotsMatchingPattern(It.IsAny <string>())) .ReturnsAsync(() => { return(trackers ?? new List <BallotStatus>()); }); mock.Setup(m => m.GetElectionSummary()) .ReturnsAsync(() => { return(summary ?? new ElectionSummary { IsComplete = false }); }); return(mock); }
private ElectionSummary GetTestElectionSummary() { var summary = new ElectionSummary { IsComplete = true, BallotEntries = new List <BallotEntry> { new BallotEntry { RaceId = "president", Description = "President", Name = "President", Tallies = new List <Tally> { new Tally { SelectionId = "candidate1", Name = "Candidate 1", Description = "Candidate 1", Party = "Party 1", VoteCount = 3 }, new Tally { SelectionId = "candidate2", Name = "Candidate 2", Description = "Candidate 2", Party = "Party 2", VoteCount = 54 } } } } }; return(summary); }
public static Mock <IDataParser <ElectionSummary> > CreateElectionResultsDataParser(ElectionSummary summary = null) { var mock = new Mock <IDataParser <ElectionSummary> >(); mock.Setup(m => m.ParseCsvData(It.IsAny <Stream>())) .Returns(() => { var s = summary ?? new ElectionSummary(); return(new List <ElectionSummary> { s }); }); return(mock); }
public void ToElectionSummaryEntity_ReturnsNull() { ElectionSummary summary = null; Assert.IsNull(summary.ToElectionSummaryEntity()); }