Skip to content

Commit

Permalink
Implemented timurs suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Oct 25, 2023
1 parent f6a07ba commit 659e7c7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 60 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @param secondName The second name
*/
public record ComparisonIdentifier(String firstName, String secondName) {
private static final String INVALID_LINE_ERROR_MESSAGE = "Comparison identifier file (%s) has an invalid line: %s";

@Override
public boolean equals(Object o) {
if (!(o instanceof ComparisonIdentifier other)) {
Expand All @@ -32,11 +34,14 @@ public int hashCode() {
* @param file The file to load
* @return The comparisons in the file
*/
public static Set<ComparisonIdentifier> loadIdentifiersFromFile(File file) {
public static Set<ComparisonIdentifier> loadIdentifiersFromFile(File file, String delimiter) {
try (Scanner scanner = new Scanner(file)) {
Set<ComparisonIdentifier> identifiers = new HashSet<>();
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(";");
String[] parts = scanner.nextLine().split(delimiter);
if (parts.length != 2) {
throw new IllegalStateException(String.format(INVALID_LINE_ERROR_MESSAGE, file.getAbsolutePath(), String.join(delimiter, parts)));
}
identifiers.add(new ComparisonIdentifier(parts[0], parts[1]));
}
return identifiers;
Expand Down
35 changes: 20 additions & 15 deletions endtoend-testing/src/main/java/de/jplag/endtoend/model/DataSet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.jplag.endtoend.model;

import java.io.File;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -24,7 +26,11 @@
public record DataSet(@JsonProperty(required = true) String name,
@JsonDeserialize(using = LanguageDeserializer.class) @JsonProperty(required = true) Language language,
@JsonProperty(required = true) DataSetFormat format, @JsonProperty String sourceDirectory, @JsonProperty String resultFile,
@JsonProperty String goldStandardFile, @JsonProperty Options options) {
@JsonProperty String goldStandardFile, @JsonProperty String goldStandardDelimiter, @JsonProperty Options options) {

private static final String DEFAULT_GOLD_STANDARD_DELIMITER = ";";
private static final String DEFAULT_SOURCE_DIRECTORY = "data/%s";
private static final String DEFAULT_RESULT_FILE_NAME = "%s.json";

/**
* Gets the source directories
Expand All @@ -41,7 +47,7 @@ public Set<File> getSourceDirectories() {
*/
String actualSourceDirectory() {
if (sourceDirectory == null) {
return "data/" + name;
return String.format(DEFAULT_SOURCE_DIRECTORY, this.name);
}
return sourceDirectory;
}
Expand All @@ -52,32 +58,31 @@ String actualSourceDirectory() {
*/
public File getResultFile() {
if (resultFile == null) {
return new File(TestDirectoryConstants.BASE_PATH_TO_RESULT_JSON.toFile(), name + ".json");
return new File(TestDirectoryConstants.BASE_PATH_TO_RESULT_JSON.toFile(), String.format(DEFAULT_RESULT_FILE_NAME, this.name));
} else {
return new File(TestDirectoryConstants.BASE_PATH_TO_RESULT_JSON.toFile(), resultFile);
}
}

/**
* @return The gold standard file. Can be null.
* @return The gold standard file as an optional.
*/
public File getGoldStandardFile() {
if (goldStandardFile == null) {
return null;
}

return new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), goldStandardFile);
public Optional<File> getGoldStandardFile() {
return Optional.ofNullable(this.goldStandardFile).map(name -> new File(TestDirectoryConstants.BASE_PATH_TO_RESOURCES.toFile(), name));
}

/**
* Helper function replacing null by the default value.
* @return The options
*/
public Options getOptions() {
if (this.options != null) {
return this.options;
} else {
return new Options(null, null);
}
return Objects.requireNonNullElseGet(this.options, Options::new);
}

/**
* Returns the actual delimiter, replacing null by the default value
*/
public String getActualDelimiter() {
return Objects.requireNonNullElse(this.goldStandardDelimiter, DEFAULT_GOLD_STANDARD_DELIMITER);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package de.jplag.endtoend.model;

import java.util.Collection;
import java.util.DoubleSummaryStatistics;
import java.util.Set;

import de.jplag.JPlagComparison;
import de.jplag.endtoend.helper.AverageCalculator;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -15,19 +15,19 @@
*/
public record GoldStandard(@JsonProperty double matchAverage, @JsonProperty double nonMatchAverage) {
public static GoldStandard buildFromComparisons(Collection<JPlagComparison> comparisonList, Set<ComparisonIdentifier> comparisonIdentifiers) {
AverageCalculator match = new AverageCalculator();
AverageCalculator nonMatch = new AverageCalculator();
DoubleSummaryStatistics match = new DoubleSummaryStatistics();
DoubleSummaryStatistics nonMatch = new DoubleSummaryStatistics();

for (JPlagComparison comparison : comparisonList) {
ComparisonIdentifier comparisonIdentifier = new ComparisonIdentifier(comparison.firstSubmission().getName(),
comparison.secondSubmission().getName());
if (comparisonIdentifiers.contains(comparisonIdentifier)) {
match.add(comparison.similarity());
match.accept(comparison.similarity());
} else {
nonMatch.add(comparison.similarity());
nonMatch.accept(comparison.similarity());
}
}

return new GoldStandard(match.calculate(), nonMatch.calculate());
return new GoldStandard(match.getAverage(), nonMatch.getAverage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
* The object contains required options for the endToEnd tests which are important for the test suite.
*/
public record Options(@JsonProperty Integer[] minimumTokenMatches, @JsonProperty String baseCodeDirectory) {

private static final int[] defaultTokenMatches = new int[] {3, 9};

/**
* Initializes a new options object with minimumTokenMatch and baseCodeDirectory both being null
*/
public Options() {
this(null, null);
}

/**
* Builds the list of all token matches that should be checked. That means all values from minimumTokenMatches and the
* default values (3 and 9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ void generateResultJson() throws ExitException, IOException {
.collect(Collectors.toMap(TestSuiteHelper::getTestIdentifier, ExpectedResult::fromComparison));

GoldStandard goldStandard = null;
if (dataSet.getGoldStandardFile() != null) {
if (dataSet.getGoldStandardFile().isPresent()) {
goldStandard = GoldStandard.buildFromComparisons(comparisons,
ComparisonIdentifier.loadIdentifiersFromFile(dataSet.getGoldStandardFile()));
ComparisonIdentifier.loadIdentifiersFromFile(dataSet.getGoldStandardFile().get(), dataSet.getActualDelimiter()));
}

resultDescriptions.add(new ResultDescription(runConfiguration.identifier(), expectedResults, goldStandard));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ private DynamicTest generateTest(String name, ExpectedResult expectedResult, JPl
*/
private DynamicNode generateGoldStandardTest(DataSet dataSet, Map<String, JPlagComparison> comparisonMap, GoldStandard goldStandard) {
if (goldStandard != null) {
Set<ComparisonIdentifier> goldStandardIdentifiers = ComparisonIdentifier.loadIdentifiersFromFile(dataSet.getGoldStandardFile());
Set<ComparisonIdentifier> goldStandardIdentifiers = ComparisonIdentifier
.loadIdentifiersFromFile(dataSet.getGoldStandardFile().orElseThrow(), dataSet.getActualDelimiter());
GoldStandard found = GoldStandard.buildFromComparisons(comparisonMap.values(), goldStandardIdentifiers);

DynamicTest goldStandardMatch = DynamicTest.dynamicTest("gold standard included", () -> assertEquals(goldStandard.matchAverage(),
Expand Down

0 comments on commit 659e7c7

Please sign in to comment.