Skip to content

Commit

Permalink
reverse distribution in report
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Oct 31, 2024
1 parent a5ff90e commit 7eec092
Showing 3 changed files with 19 additions and 18 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/de/jplag/JPlagResult.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.jplag;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.ToDoubleFunction;
@@ -122,6 +123,10 @@ public List<ClusteringResult<Submission>> getClusteringResult() {
return this.clusteringResult;
}

public List<Integer> calculateDistributionFor(ToDoubleFunction<JPlagComparison> similarityMetric) {
return Arrays.stream(calculateDistributionFor(this.comparisons, similarityMetric)).boxed().toList();
}

@Override
public String toString() {
return String.format("JPlagResult { comparisons: %d, duration: %d ms, language: %s, submissions: %d }", getAllComparisons().size(),
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.jplag.reporting.reportobject.mapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@@ -29,8 +27,11 @@ public MetricMapper(Function<Submission, String> submissionToIdFunction) {
* @return Map with key as name of metric and value as distribution
*/
public static Map<String, List<Integer>> getDistributions(JPlagResult result) {
return Map.of(SimilarityMetric.AVG.name(), convertDistribution(result.getSimilarityDistribution()), SimilarityMetric.MAX.name(),
convertDistribution(result.getMaxSimilarityDistribution()));
Map<String, List<Integer>> distributions = new HashMap<>();
for (SimilarityMetric metric : SimilarityMetric.values()) {
distributions.put(metric.name(), result.calculateDistributionFor(metric));
}
return distributions;
}

/**
@@ -48,10 +49,4 @@ public List<TopComparison> getTopComparisons(JPlagResult result) {
private Map<String, Double> getComparisonMetricMap(JPlagComparison comparison) {
return Map.of(SimilarityMetric.AVG.name(), comparison.similarity(), SimilarityMetric.MAX.name(), comparison.maximalSimilarity());
}

private static List<Integer> convertDistribution(int[] array) {
List<Integer> list = new ArrayList<>(Arrays.stream(array).boxed().toList());
Collections.reverse(list);
return list;
}
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
import static org.mockito.Mockito.mock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@@ -16,6 +16,7 @@
import de.jplag.JPlagResult;
import de.jplag.Submission;
import de.jplag.options.JPlagOptions;
import de.jplag.options.SimilarityMetric;
import de.jplag.reporting.reportobject.model.TopComparison;

public class MetricMapperTest {
@@ -39,7 +40,8 @@ public void test_getDistributions() {
Map<String, List<Integer>> result = MetricMapper.getDistributions(jPlagResult);

// then
Assertions.assertEquals(Map.of("AVG", EXPECTED_AVG_DISTRIBUTION, "MAX", EXPECTED_MAX_DISTRIBUTION), result);
Assertions.assertEquals(EXPECTED_AVG_DISTRIBUTION, result.get("AVG"));
Assertions.assertEquals(EXPECTED_MAX_DISTRIBUTION, result.get("MAX"));
}

@Test
@@ -58,9 +60,8 @@ public void test_getTopComparisons() {
}

private int[] distribution(List<Integer> expectedDistribution) {
var reversedDistribution = new ArrayList<>(expectedDistribution);
Collections.reverse(reversedDistribution);
return reversedDistribution.stream().mapToInt(Integer::intValue).toArray();
var distribution = new ArrayList<>(expectedDistribution);
return distribution.stream().mapToInt(Integer::intValue).toArray();
}

private CreateSubmission submission(String name) {
@@ -73,8 +74,8 @@ private Comparison comparison(CreateSubmission submission1, CreateSubmission sub

private JPlagResult createJPlagResult(int[] avgDistribution, int[] maxDistribution, Comparison... createComparisonsDto) {
JPlagResult jPlagResult = mock(JPlagResult.class);
doReturn(avgDistribution).when(jPlagResult).getSimilarityDistribution();
doReturn(maxDistribution).when(jPlagResult).getMaxSimilarityDistribution();
doReturn(Arrays.stream(avgDistribution).boxed().toList()).when(jPlagResult).calculateDistributionFor(SimilarityMetric.AVG);
doReturn(Arrays.stream(maxDistribution).boxed().toList()).when(jPlagResult).calculateDistributionFor(SimilarityMetric.MAX);

JPlagOptions options = mock(JPlagOptions.class);
doReturn(createComparisonsDto.length).when(options).maximumNumberOfComparisons();

0 comments on commit 7eec092

Please sign in to comment.