This is an automated email from the git hooks/post-receive script. New commit to branch feature/361-BugJugementMajoritaire in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 09916f9006fb7651cbcd92524267cedf3c3f6918 Author: jcouteau <couteau@codelutin.com> Date: Fri Feb 5 11:52:08 2021 +0100 refs #361 :bug: Après un ajout de choix sur un jugement majoritaire, les résultats ne s'affichent plus --- .../votecounting/MajorityJudgmentChoiceResult.java | 20 ++++++ .../MajorityJudgmentVoteCountingStrategy.java | 12 +++- .../MajorityJudgmentVoteCountingStrategyTest.java | 74 ++++++++++++++++++++-- 3 files changed, 99 insertions(+), 7 deletions(-) diff --git a/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentChoiceResult.java b/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentChoiceResult.java index d050c99b..cb53364a 100644 --- a/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentChoiceResult.java +++ b/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentChoiceResult.java @@ -38,6 +38,26 @@ public class MajorityJudgmentChoiceResult implements Serializable { protected int median; + protected BigDecimal sumWeight; + + protected BigDecimal halfWeight; + + public BigDecimal getSumWeight() { + return sumWeight; + } + + public void setSumWeight(BigDecimal sumWeight) { + this.sumWeight = sumWeight; + } + + public BigDecimal getHalfWeight() { + return halfWeight; + } + + public void setHalfWeight(BigDecimal halfWeight) { + this.halfWeight = halfWeight; + } + public String getChoiceId() { return choiceId; } diff --git a/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategy.java b/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategy.java index cea2398f..49175df4 100644 --- a/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategy.java +++ b/pollen-votecounting-majority-judgment/src/main/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategy.java @@ -57,7 +57,7 @@ public class MajorityJudgmentVoteCountingStrategy extends AbstractVoteCountingSt detailResult.setHalfWeight(detailResult.getSumWeight().divide(BigDecimal.valueOf(2), MathContext.DECIMAL32)); - choiceResults.forEach(choiceResult -> computeMedian(choiceResult, detailResult.getHalfWeight())); + choiceResults.forEach(this::computeMedian); choiceResults.sort(CHOICE_RESULT_COMPARATOR.reversed()); @@ -109,15 +109,23 @@ public class MajorityJudgmentVoteCountingStrategy extends AbstractVoteCountingSt protected void addGrad(MajorityJudgmentChoiceResult choiceResult, Double voteValue, BigDecimal weight) { int grade = voteValue == null ? 0 : voteValue.intValue(); + BigDecimal sum = choiceResult.getSumWeight(); + if (sum == null) { + sum = new BigDecimal(0); + } + choiceResult.setSumWeight(sum.add(weight)); + choiceResult.setHalfWeight(choiceResult.getSumWeight().divide(BigDecimal.valueOf(2), MathContext.DECIMAL32)); List<BigDecimal> voteByGrad = choiceResult.getVoteByGrad(); BigDecimal vote = voteByGrad.get(grade).add(weight); voteByGrad.set(grade, vote); } - protected void computeMedian(MajorityJudgmentChoiceResult choiceResult, BigDecimal demiWeight) { + protected void computeMedian(MajorityJudgmentChoiceResult choiceResult) { BigDecimal temp = BigDecimal.ZERO; + BigDecimal demiWeight = choiceResult.getHalfWeight(); + Integer median = null; int grad = 0; diff --git a/pollen-votecounting-majority-judgment/src/test/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategyTest.java b/pollen-votecounting-majority-judgment/src/test/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategyTest.java index ddfa07e7..ec36d6ee 100644 --- a/pollen-votecounting-majority-judgment/src/test/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategyTest.java +++ b/pollen-votecounting-majority-judgment/src/test/java/org/chorem/pollen/votecounting/MajorityJudgmentVoteCountingStrategyTest.java @@ -222,6 +222,67 @@ public class MajorityJudgmentVoteCountingStrategyTest { Assert.assertEquals(1, compareDtoA); } + @Test + public void simpleVotecount3() throws Exception { + + // Ville A B C D + // voter 1 P F G + // voter 2 P F P + // voter 3 P G F + // voter 4 F P F + // voter 5 P P F P + //------------------------ + // Poor 7 2 3 1 + // Fair 1 5 3 0 + // Good 0 1 2 0 + //------------------------ + // median P F F P + // order 3 0 1 1 + + Set<Voter> voters = new SimpleVoterBuilder() + .newVoter("Voter 1", 1.) + .addVoteForChoice(CHOICE_A, POOR) + .addVoteForChoice(CHOICE_B, FAIR) + .addVoteForChoice(CHOICE_C, GOOD) + .newVoter("Voter 2", 1.) + .addVoteForChoice(CHOICE_A, POOR) + .addVoteForChoice(CHOICE_B, FAIR) + .addVoteForChoice(CHOICE_C, POOR) + .newVoter("Voter 3", 1.) + .addVoteForChoice(CHOICE_A, POOR) + .addVoteForChoice(CHOICE_B, GOOD) + .addVoteForChoice(CHOICE_C, FAIR) + .newVoter("Voter 4", 1.) + .addVoteForChoice(CHOICE_A, FAIR) + .addVoteForChoice(CHOICE_B, POOR) + .addVoteForChoice(CHOICE_C, FAIR) + .newVoter("Voter 5", 1.) + .addVoteForChoice(CHOICE_A, POOR) + .addVoteForChoice(CHOICE_B, POOR) + .addVoteForChoice(CHOICE_C, FAIR) + .addVoteForChoice(CHOICE_D, POOR) + .getVoters(); + + VoteCountingResult result = strategy.votecount(voters); + + assertThat(result).isNotNull(); + + MajorityJudgmentDetailResult detailResult = MajorityJudgmentDetailResult.class.cast(result.getDetailResult()); + + MajorityJudgmentChoiceResult resultA = detailResult.getChoiceResults().stream() + .filter(c -> CHOICE_A.equals(c.getChoiceId())) + .findFirst() + .orElseThrow(() -> new Exception("Not found")); + + MajorityJudgmentChoiceResult resultD = detailResult.getChoiceResults().stream() + .filter(c -> CHOICE_D.equals(c.getChoiceId())) + .findFirst() + .orElseThrow(() -> new Exception("Not found")); + + int compareDtoA = MajorityJudgmentVoteCountingStrategy.CHOICE_RESULT_COMPARATOR.compare(resultD, resultA); + Assert.assertEquals(-1, compareDtoA); + } + @Test public void testCompareEqual() { @@ -306,15 +367,12 @@ public class MajorityJudgmentVoteCountingStrategyTest { protected void testCompareChoice(String votesForA, String votesForB, int expected) { Preconditions.checkArgument(votesForA.length() == votesForB.length()); - - BigDecimal half = BigDecimal.valueOf(votesForA.length() / 2.); - MajorityJudgmentChoiceResult a = stringToMajorityJudgmentChoiceResult(CHOICE_A, votesForA); MajorityJudgmentChoiceResult b = stringToMajorityJudgmentChoiceResult(CHOICE_B, votesForB); - strategy.computeMedian(a, half); - strategy.computeMedian(b, half); + strategy.computeMedian(a); + strategy.computeMedian(b); int compareAtoB = MajorityJudgmentVoteCountingStrategy.CHOICE_RESULT_COMPARATOR.compare(a, b); @@ -327,6 +385,10 @@ public class MajorityJudgmentVoteCountingStrategyTest { } protected MajorityJudgmentChoiceResult stringToMajorityJudgmentChoiceResult(String choiceId, String votes) { + + BigDecimal half = BigDecimal.valueOf(votes.length() / 2.); + BigDecimal sum = BigDecimal.valueOf(votes.length()); + MajorityJudgmentChoiceResult choiceResult = new MajorityJudgmentChoiceResult(); choiceResult.setChoiceId(choiceId); List<BigDecimal> voteByGrad = GRADES_5.stream() @@ -334,6 +396,8 @@ public class MajorityJudgmentVoteCountingStrategyTest { .map(BigDecimal::valueOf) .collect(Collectors.toList()); choiceResult.setVoteByGrad(voteByGrad); + choiceResult.setHalfWeight(half); + choiceResult.setSumWeight(sum); return choiceResult; } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.