Skip to content

Commit

Permalink
Compute vote results stores beatpath matrix #204 (#309)
Browse files Browse the repository at this point in the history
* Compute vote results stores beatpath matrix #204
  • Loading branch information
szirbucz authored and magwas committed Apr 11, 2019
1 parent a6a35a6 commit 1a5b5ed
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/BeatTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

import org.rulez.demokracia.pdengine.dataobjects.Pair;
import org.rulez.demokracia.types.MapMatrix;

public class BeatTable extends MapMatrix<String, Pair> implements TransitiveClosable, Normalizable {
private static final long serialVersionUID = 1L;

public BeatTable(final BeatTable beatTable) {
super(beatTable.getKeyCollection());
for (String row : getKeyCollection()) {
for (String col : getKeyCollection()) {
Pair sourcePair = beatTable.getElement(col, row);
if (Objects.nonNull(sourcePair)) {
this.setElement(col, row, createPair(sourcePair));
}
}
}
}

private Pair createPair(final Pair sourcePair) {
return new Pair(sourcePair.winning, sourcePair.losing);
}

public enum Direction {
DIRECTION_FORWARD, DIRECTION_BACKWARD
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/rulez/demokracia/pdengine/ComputedVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ComputedVote implements ComputedVoteInterface, Serializable {
private static final long serialVersionUID = 1L;
private BeatTable beatTable;
private final Vote vote;
private BeatTable beatPathTable;

public ComputedVote(final Vote vote) {
this.vote = vote;
Expand All @@ -25,9 +26,16 @@ public void computeVote() {

beatTable = new BeatTable(keySet);
beatTable.initialize(vote.getVotesCast());
beatPathTable = new BeatTable(beatTable);
beatPathTable.normalize();
beatPathTable.computeTransitiveClosure();
}

public BeatTable getBeatTable() {
return beatTable;
}

public BeatTable getBeatPathTable() {
return beatPathTable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;

import java.util.ArrayList;

Expand All @@ -18,23 +19,26 @@

@TestedFeature("Vote")
@TestedOperation("Compute vote results")
@TestedBehaviour("compares and stores initial beat matrix")
public class ComputedVoteTest extends CreatedDefaultCastVoteWithRankedChoices {

private static final Pair ZERO_PAIR = new Pair(0, 0);

@Before
@Override
public void setUp() {
super.setUp();
getTheVote().votesCast = castVote;
}

@TestedBehaviour("compares and stores initial beat matrix")
@Test
public void compute_vote_should_create_initial_matrix_with_full_key_set() {
BeatTable beatTable = initBeatTable();
assertEquals(Sets.newHashSet("A", "B", "C", "D"),
Sets.newHashSet(beatTable.getKeyCollection()));
}

@TestedBehaviour("compares and stores initial beat matrix")
@Test
public void compute_vote_should_create_empty_initial_matrix_when_voteCast_is_empty() {
getTheVote().votesCast = new ArrayList<>();
Expand All @@ -43,13 +47,60 @@ public void compute_vote_should_create_empty_initial_matrix_when_voteCast_is_emp
Sets.newHashSet(beatTable.getKeyCollection()));
}

@TestedBehaviour("compares and stores initial beat matrix")
@Test
public void after_compute_vote_beat_table_should_contain_beat_information() {
BeatTable beatTable = initBeatTable();
Pair abPair = beatTable.getElement("A", "B");
assertPairInitialized(abPair);
}

@TestedBehaviour("alculates and stores beatpath matrix")
@Test
public void beat_path_matrix_is_not_the_same_as_initial_matrix() {
ComputedVote computedVote = new ComputedVote(getTheVote());
computedVote.computeVote();
assertNotSame(computedVote.getBeatTable(), computedVote.getBeatPathTable());
}

@TestedBehaviour("calculates and stores beatpath matrix")
@Test
public void beat_path_matrix_is_normalized() {
ComputedVote computedVote = new ComputedVote(getTheVote());
computedVote.computeVote();
BeatTable beatPathTable = computedVote.getBeatPathTable();
for (String choice1 : beatPathTable.getKeyCollection()) {
for (String choice2 : beatPathTable.getKeyCollection()) {
Pair beat1 = beatPathTable.getElement(choice1, choice2);
Pair beat2 = beatPathTable.getElement(choice2, choice1);
assertEquals(ZERO_PAIR, beatPathTable.lessBeat(beat1, beat2));
}
}
}

@TestedBehaviour("calculates and stores beatpath matrix")
@Test
public void transitive_closure_done_on_beat_path_matrix() {
ComputedVote computedVote = new ComputedVote(getTheVote());

computedVote.computeVote();
BeatTable firstBeatTable = new BeatTable(computedVote.getBeatPathTable());

computedVote.getBeatPathTable().computeTransitiveClosure();
BeatTable secondBeatTable = computedVote.getBeatPathTable();

assertBeatTableEquals(firstBeatTable, secondBeatTable);
}

private void assertBeatTableEquals(final BeatTable firstBeatTable, final BeatTable secondBeatTable) {
for (String choice1 : firstBeatTable.getKeyCollection()) {
for (String choice2 : firstBeatTable.getKeyCollection()) {
assertEquals(secondBeatTable.getElement(choice1, choice2),
firstBeatTable.getElement(choice1, choice2));
}
}
}

private void assertPairInitialized(final Pair pair) {
assertNotNull(pair);
assertNotEquals(0, pair.winning + pair.losing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public void setUp() {

private List<CastVote> createCastVote() {
List<CastVote> result = new ArrayList<>();
result.add(createRankedChoices(Arrays.asList("A", "B", "C", "D")));
result.add(createRankedChoices(Arrays.asList("D", "B", "A", "C")));
result.add(createRankedChoices(Arrays.asList("A", "C", "B", "D")));
result.add(createRankedChoices(Arrays.asList("D", "A", "B", "C")));
result.add(createRankedChoices(Arrays.asList("B", "A", "C", "D")));
result.add(createRankedChoices(Arrays.asList("B", "A", "C", "D")));
for (int i = 0; i < 3; ++i) {
result.add(createRankedChoices(Arrays.asList("A", "C")));
result.add(createRankedChoices(Arrays.asList("C", "D")));
}

return result;
}
Expand Down

0 comments on commit 1a5b5ed

Please sign in to comment.