Skip to content

Commit

Permalink
Write ClusteringResult to Report
Browse files Browse the repository at this point in the history
  • Loading branch information
nestabentum committed Jun 10, 2022
1 parent 8503fbf commit fa32023
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.*;
import java.util.stream.Collectors;

import de.jplag.reporting.reportobject.mapper.ClusteringResultMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,9 +22,11 @@
public class ReportObjectFactory {

private static final Logger logger = LoggerFactory.getLogger(ReportObjectFactory.class);
private static final ClusteringResultMapper clusteringResultMapper = new ClusteringResultMapper();

/**
* Converts a JPlagResult to a JPlagReport.
*
* @return JPlagReport for the given JPlagResult.
*/
public static JPlagReport getReportObject(JPlagResult result) {
Expand Down Expand Up @@ -58,13 +61,14 @@ private static OverviewReport generateOverviewReport(JPlagResult result) {
overviewReport.setExecutionTime(result.getDuration());
overviewReport.setComparisonNames(getComparisonNames(comparisons));
overviewReport.setMetrics(getMetrics(result));
overviewReport.setClusters(getClusters(result));
overviewReport.setClusters(clusteringResultMapper.map(result));

return overviewReport;
}

/**
* Generates detailed ComparisonReport DTO for each comparison in a JPlagResult.
*
* @return A list with ComparisonReport DTOs.
*/
private static List<ComparisonReport> generateComparisonReports(JPlagResult result) {
Expand All @@ -87,6 +91,7 @@ private static List<Match> convertMatchesToReportMatches(JPlagResult result, JPl

/**
* Gets the names of all submissions.
*
* @return A list containing all submission names.
*/
private static List<String> extractSubmissionNames(List<JPlagComparison> comparisons) {
Expand All @@ -100,6 +105,7 @@ private static List<String> extractSubmissionNames(List<JPlagComparison> compari

/**
* Gets the names of all comparison.
*
* @return A list containing all comparisons.
*/
private static List<String> getComparisonNames(List<JPlagComparison> comparisons) {
Expand All @@ -113,6 +119,7 @@ private static List<String> getComparisonNames(List<JPlagComparison> comparisons

/**
* Gets the used metric in a JPlag comparison.
*
* @return A list contains Metric DTOs.
*/
private static List<Metric> getMetrics(JPlagResult result) {
Expand All @@ -125,6 +132,7 @@ private static List<Metric> getMetrics(JPlagResult result) {
/**
* Converts JPlagComparison to a DTO for displaying only comparisons. See
* {@link #generateComparisonReports(JPlagResult)} for a more detailed representation of a comparison.
*
* @return List containing TopComparison DTOs.
*/
private static List<TopComparison> getTopComparisons(List<JPlagComparison> comparisons) {
Expand All @@ -136,6 +144,7 @@ private static List<TopComparison> getTopComparisons(List<JPlagComparison> compa

/**
* Converts files of a submission to FilesOFSubmission DTO.
*
* @return A list containing FilesOfSubmission DTOs.
*/
private static List<FilesOfSubmission> getFilesForSubmission(Submission submission) {
Expand All @@ -144,9 +153,10 @@ private static List<FilesOfSubmission> getFilesForSubmission(Submission submissi

/**
* Converts a JPlag Match object to a Match DTO.
*
* @param comparison The comparison from which the match originates.
* @param match The match to be converted.
* @param usesIndex Indicates whether the language uses indexes.
* @param match The match to be converted.
* @param usesIndex Indicates whether the language uses indexes.
* @return A Match DTO.
*/
private static Match convertMatchToReportMatch(JPlagComparison comparison, de.jplag.Match match, Boolean usesIndex) {
Expand All @@ -166,12 +176,6 @@ private static Match convertMatchToReportMatch(JPlagComparison comparison, de.jp
return new Match(startTokenFirst.getFile(), startTokenSecond.getFile(), startFirst, endFirst, startSecond, endSecond, tokens);
}

// TODO implement after PR Read clustering #281
private static List<Cluster> getClusters(JPlagResult result) {
// List<ClusteringResult<Submission>> clusters = result.getClusteringResult();
// return clusters.map( c -> new Cluster(getAvgSimilarity, getStrength, c.getMembers().map(Submission::getName)))
return List.of();
}

private static List<String> readFileLines(File file) {
List<String> lines = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package de.jplag.reporting.reportobject.mapper;

import de.jplag.JPlagResult;
import de.jplag.Submission;
import de.jplag.clustering.ClusteringResult;
import de.jplag.reporting.reportobject.model.Cluster;

import java.util.Collection;
import java.util.List;

/**
* Extracts and maps the clusters from the JPlagResult to the corresponding JSON DTO
*/
public class ClusteringResultMapper {
public List<Cluster> map(JPlagResult result) {
var clusteringResult = result.getClusteringResult();
return clusteringResult.stream()
.map(ClusteringResult::getClusters)
.flatMap(Collection::stream)
.map(this::convertCluster)
.toList();
}

private Cluster convertCluster(de.jplag.clustering.Cluster<Submission> from) {
var strength = from.getCommunityStrength();
var avgSimilarity = from.getAverageSimilarity();
var member = from.getMembers().stream().map(Submission::getName).toList();
return new Cluster(avgSimilarity, strength, member);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.jplag.reporting.reportobject.mapper;

import de.jplag.JPlagResult;
import de.jplag.Submission;
import de.jplag.clustering.Cluster;
import de.jplag.clustering.ClusteringResult;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ClusteringResultMapperTest {
private final ClusteringResultMapper clusteringResultMapper = new ClusteringResultMapper();

@Test
public void test() {
// given
JPlagResult resultMock = mock(JPlagResult.class);
Cluster<Submission> cluster1 = createClusterWith(0.2f, 0.4f, "1", "2");
Cluster<Submission> cluster2 = createClusterWith(0.3f, 0.6f, "3", "4", "5");
when(resultMock.getClusteringResult()).thenReturn(List.of(new ClusteringResult<>(List.of(cluster1, cluster2), 0.3f)));

// when
var result = clusteringResultMapper.map(resultMock);

// then
assertEquals(List.of(
new de.jplag.reporting.reportobject.model.Cluster(0.4f, 0.2f, List.of("1", "2")),
new de.jplag.reporting.reportobject.model.Cluster(0.6f, 0.3f, List.of("3", "4", "5"))

), result);
}

private Cluster<Submission> createClusterWith(Float communityStrength, Float averageSimilarity, String... ids) {
var submissions = Arrays.stream(ids).map(this::submissionWithId).toList();
return new Cluster<>(submissions, communityStrength, averageSimilarity);
}

private Submission submissionWithId(String id) {
Submission submission = mock(Submission.class);
when(submission.getName()).thenReturn(id);
return submission;
}
}

0 comments on commit fa32023

Please sign in to comment.