Skip to content

Commit 76d293d

Browse files
valentinbujdosomagwas
authored andcommitted
The beat matrix contains the beats #251 (#299)
* The beat matrix contains the beats #251
1 parent 0d4b152 commit 76d293d

File tree

5 files changed

+206
-28
lines changed

5 files changed

+206
-28
lines changed

src/main/java/org/rulez/demokracia/pdengine/BeatTable.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
import org.rulez.demokracia.pdengine.dataobjects.Pair;
77
import org.rulez.demokracia.types.MapMatrix;
88

9-
public class BeatTable extends MapMatrix<Choice, Pair> implements ContainingBeats{
9+
public class BeatTable extends MapMatrix<String, Pair> implements ContainingBeats{
1010
private static final long serialVersionUID = 1L;
1111

1212
public enum Direction {
1313
DIRECTION_FORWARD, DIRECTION_BACKWARD
1414
}
1515

1616
public BeatTable() {
17-
this(new ArrayList<Choice>());
17+
this(new ArrayList<String>());
1818
}
1919

20-
public BeatTable(final Collection<Choice> keyCollection) {
20+
public BeatTable(final Collection<String> keyCollection) {
2121
super(keyCollection);
2222
}
2323
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package org.rulez.demokracia.pdengine;
22

3+
import java.util.List;
4+
35
import org.rulez.demokracia.pdengine.BeatTable.Direction;
46
import org.rulez.demokracia.pdengine.dataobjects.Pair;
57
import org.rulez.demokracia.pdengine.exception.ReportedException;
68
import org.rulez.demokracia.types.Matrix;
79

8-
public interface ContainingBeats extends Matrix<Choice, Pair>{
9-
10+
public interface ContainingBeats extends Matrix<String, Pair> {
11+
1012
long serialVersionUID = 1L;
13+
1114
default void checkPair(final Pair pair) {
1215
if (pair == null)
1316
throw new ReportedException("Invalid Pair key");
1417
}
15-
16-
default int beatInformation(final Choice choice1, final Choice choice2, final Direction direction) {
18+
19+
default int beatInformation(final String choice1, final String choice2, final Direction direction) {
1720
if (direction == null)
1821
throw new ReportedException("Invalid direction");
19-
22+
2023
int result = 0;
2124
Pair pair = getElement(choice1, choice2);
22-
2325

2426
if (direction.equals(Direction.DIRECTION_FORWARD))
2527
result = pair.winning;
@@ -28,23 +30,60 @@ default int beatInformation(final Choice choice1, final Choice choice2, final Di
2830

2931
return result;
3032
}
31-
33+
3234
default Pair compareBeats(final Pair beat1, final Pair beat2) {
3335
checkPair(beat1);
3436
checkPair(beat2);
35-
37+
3638
if (beat1.winning > beat2.winning)
3739
return beat1;
3840
else if (beat1.winning < beat2.winning)
3941
return beat2;
42+
else if (beat1.losing < beat2.losing)
43+
return beat1;
44+
else if (beat2.losing < beat1.losing)
45+
return beat2;
4046
else
41-
if (beat1.losing < beat2.losing)
42-
return beat1;
43-
else if (beat2.losing < beat1.losing)
44-
return beat2;
45-
else
46-
throw new UnsupportedOperationException();
47+
throw new UnsupportedOperationException();
48+
}
49+
50+
default void initialize(List<CastVote> castVotes) {
51+
if (castVotes == null)
52+
throw new ReportedException("Invalid castVotes");
53+
54+
55+
for (CastVote castVote : castVotes) {
56+
List<RankedChoice> preferences = castVote.getPreferences();
57+
58+
for (int columnIndex = 0; columnIndex < preferences.size(); columnIndex++) {
59+
RankedChoice column = preferences.get(columnIndex);
60+
for (int rowIndex = columnIndex + 1; rowIndex < preferences.size(); rowIndex++) {
61+
RankedChoice row = preferences.get(rowIndex);
62+
if (column.rank > row.rank) {
63+
increasePairValue(preferences, columnIndex, rowIndex);
64+
} else if(column.rank < row.rank){
65+
increasePairValue(preferences, rowIndex, columnIndex);
66+
}
67+
}
68+
}
69+
}
4770
}
4871

49-
72+
73+
default Pair getPair(String beats1, String beats2) {
74+
Pair result = getElement(beats1, beats2);
75+
if (result == null)
76+
return new Pair(0, 0);
77+
return result;
78+
}
79+
80+
default void increasePairValue(List<RankedChoice> preferences, int column, int row) {
81+
Pair value1 = getPair(preferences.get(column).choiceId, preferences.get(row).choiceId);
82+
setElement(preferences.get(column).choiceId, preferences.get(row).choiceId,
83+
new Pair(value1.winning + 1, value1.losing));
84+
85+
Pair value2 = getPair(preferences.get(row).choiceId, preferences.get(column).choiceId);
86+
setElement(preferences.get(row).choiceId, preferences.get(column).choiceId,
87+
new Pair(value2.winning, value2.losing + 1));
88+
}
5089
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.rulez.demokracia.pdengine;
2+
3+
import static org.junit.Assert.*;
4+
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour;
9+
import org.rulez.demokracia.pdengine.annotations.TestedFeature;
10+
import org.rulez.demokracia.pdengine.annotations.TestedOperation;
11+
import org.rulez.demokracia.pdengine.dataobjects.Pair;
12+
import org.rulez.demokracia.pdengine.exception.ReportedException;
13+
import org.rulez.demokracia.pdengine.testhelpers.CreatedBeatTableForInitialization;
14+
15+
16+
@TestedFeature("Schulze method")
17+
@TestedOperation("compute initial beat matrix")
18+
@TestedBehaviour("the beat matrix contains the beats")
19+
public class BeatTableInitializeTest extends CreatedBeatTableForInitialization {
20+
21+
@Before
22+
public void setUp() throws ReportedException {
23+
super.setUp();
24+
}
25+
26+
@Test
27+
public void initialize_throws_exception_when_param_is_not_defined() {
28+
assertThrows(() -> beatTable.initialize(null)
29+
).assertMessageIs("Invalid castVotes");
30+
}
31+
32+
@Test
33+
public void initialize_does_not_modify_the_values_when_there_are_two_same_rank() {
34+
createBeatTableWithSameRank();
35+
beatTable.initialize(castVotes);
36+
assertTrue(beatTable.getElement(THIRD, FOURTH) == null);
37+
}
38+
39+
@Test
40+
public void initialize_sets_the_new_losing_value_FORWARD_when_the_matrix_is_empty() {
41+
beatTable.initialize(castVotes);
42+
Pair result = beatTable.getElement(FIRST, SECOND);
43+
assertTrue(result.winning == 0 && result.losing == 2);
44+
}
45+
46+
@Test
47+
public void initialize_sets_the_new_losing_value_BACKWARD_when_the_matrix_is_empty() {
48+
beatTable.initialize(castVotes);
49+
Pair result = beatTable.getElement(SECOND, FIRST);
50+
assertTrue(result.winning == 2 && result.losing == 0);
51+
}
52+
53+
@Test
54+
public void initialize_sets_the_new_winning_value_FORWARD_when_the_matrix_is_empty() {
55+
beatTable.initialize(castVotes);
56+
Pair result = beatTable.getElement(SECOND, THIRD);
57+
assertTrue(result.winning == 2 && result.losing == 0);
58+
}
59+
60+
@Test
61+
public void initialize_sets_the_new_winning_value_BACKWARD_when_the_matrix_is_empty1() {
62+
beatTable.initialize(castVotes);
63+
Pair result = beatTable.getElement(THIRD, SECOND);
64+
assertTrue(result.winning == 0 && result.losing == 2);
65+
}
66+
}

src/test/java/org/rulez/demokracia/pdengine/testhelpers/CreatedBeatTable.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55

66
import org.junit.Before;
77
import org.rulez.demokracia.pdengine.BeatTable;
8-
import org.rulez.demokracia.pdengine.Choice;
98
import org.rulez.demokracia.pdengine.dataobjects.Pair;
109
import org.rulez.demokracia.testhelpers.ThrowableTester;
1110

1211
public class CreatedBeatTable extends ThrowableTester{
1312
protected BeatTable beatTable;
14-
protected List<Choice> list;
13+
protected List<String> list;
1514

1615
protected Pair pair;
1716
protected Pair result;
1817

19-
protected Choice choice1;
20-
protected Choice choice2;
21-
protected Choice choice3;
18+
protected String choice1;
19+
protected String choice2;
20+
protected String choice3;
2221

2322
protected Pair beats1;
2423
protected Pair beats2;
@@ -29,11 +28,11 @@ public class CreatedBeatTable extends ThrowableTester{
2928
@Before
3029
public void setUp() {
3130
beatTable = new BeatTable();
32-
list = new ArrayList<Choice>();
31+
list = new ArrayList<String>();
3332
pair = new Pair(4, 5);
34-
choice1 = new Choice("name1", "userName1");
35-
choice2 = new Choice("name2", "userName2");
36-
choice3 = new Choice("name3", "userName3");
33+
choice1 = "name1";
34+
choice2 = "name2";
35+
choice3 = "name3";
3736
list.add(choice1);
3837
list.add(choice2);
3938
list.add(choice3);
@@ -59,7 +58,7 @@ protected void createNewBeatTableWithComplexData() {
5958
beatTable.setElement(choice1, choice2, pair);
6059
}
6160

62-
protected void setGetElementAsResult(final Choice first, final Choice second) {
61+
protected void setGetElementAsResult(final String first, final String second) {
6362
result = beatTable.getElement(first, second);
6463
}
6564

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.rulez.demokracia.pdengine.testhelpers;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.Before;
7+
import org.rulez.demokracia.pdengine.BeatTable;
8+
import org.rulez.demokracia.pdengine.RankedChoice;
9+
import org.rulez.demokracia.pdengine.CastVote;
10+
11+
public class CreatedBeatTableForInitialization extends CreatedBeatTable{
12+
13+
public static final String FIRST = "first";
14+
public static final String SECOND = "second";
15+
public static final String THIRD = "third";
16+
public static final String FOURTH = "fourth";
17+
18+
protected List<RankedChoice> preferences;
19+
20+
protected RankedChoice rankedChoice1;
21+
protected RankedChoice rankedChoice2;
22+
protected RankedChoice rankedChoice3;
23+
protected RankedChoice rankedChoice4;
24+
25+
protected List<CastVote> castVotes;
26+
27+
protected CastVote castVote1;
28+
protected CastVote castVote2;
29+
protected CastVote castVote3;
30+
31+
protected List<String> list;
32+
33+
@Before
34+
public void setUp() {
35+
super.setUp();
36+
37+
preferences = new ArrayList<RankedChoice>();
38+
rankedChoice1 = new RankedChoice(FIRST, 2);
39+
rankedChoice2 = new RankedChoice(SECOND, 3);
40+
rankedChoice3 = new RankedChoice(THIRD, 1);
41+
42+
preferences.add(rankedChoice1);
43+
preferences.add(rankedChoice2);
44+
preferences.add(rankedChoice3);
45+
46+
castVotes = new ArrayList<CastVote>();
47+
castVote1 = new CastVote("TestUser1", preferences);
48+
castVote2 = new CastVote("TestUser2", preferences);
49+
castVote3 = new CastVote("TestUser3", new ArrayList<RankedChoice>());
50+
51+
castVotes.add(castVote3);
52+
castVotes.add(castVote1);
53+
castVotes.add(castVote2);
54+
55+
list = new ArrayList<String>();
56+
list.add(FIRST);
57+
list.add(SECOND);
58+
list.add(THIRD);
59+
60+
beatTable = new BeatTable(list);
61+
}
62+
63+
public void createBeatTableWithSameRank() {
64+
rankedChoice4 = new RankedChoice(FOURTH, 1);
65+
preferences.add(rankedChoice4);
66+
castVotes.add(new CastVote("TestUser4", preferences));
67+
list.add(FOURTH);
68+
69+
beatTable = new BeatTable(list);
70+
71+
72+
}
73+
74+
}

0 commit comments

Comments
 (0)