Skip to content

Commit

Permalink
Improved JavaDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
uuqjz committed Aug 4, 2023
1 parent 00256b6 commit 2b341e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
35 changes: 32 additions & 3 deletions core/src/main/java/de/jplag/merging/MatchMerging.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MatchMerging(JPlagResult result, JPlagOptions options) {

/**
* Runs the internal match merging pipeline. It computes neighboring matches, merges them based on
* {@link MergingParameters} and removes the merge buffer afterwards.
* {@link MergingParameters} and removes remaining too short matches afterwards.
* @return JPlagResult containing the merged matches
*/
public JPlagResult run() {
Expand All @@ -50,14 +50,20 @@ public JPlagResult run() {
secondSubmission = comparisons.get(i).secondSubmission().copy();
List<Match> globalMatches = new ArrayList<>(comparisons.get(i).matches());
globalMatches.addAll(comparisons.get(i).ignoredMatches());
globalMatches = removeBuffer(mergeNeighbors(globalMatches));
globalMatches = removeTooShortMatches(mergeNeighbors(globalMatches));
comparisons.set(i, new JPlagComparison(firstSubmission, secondSubmission, globalMatches, new ArrayList<>()));

}
long durationInMillis = System.currentTimeMillis() - timeBeforeStartInMillis;
return new JPlagResult(comparisons, result.getSubmissions(), result.getDuration() + durationInMillis, options);
}

/**
* Computes neighbors by sorting based on order of matches in the first and the second submission and then checking
* which are next to each other in both.
* @param globalMatches
* @return neighbors containing a list of pairs of neighboring matches
*/
private List<List<Match>> computeNeighbors(List<Match> globalMatches) {
List<List<Match>> neighbors = new ArrayList<>();
List<Match> sortedByFirst = new ArrayList<>(globalMatches);
Expand All @@ -72,6 +78,12 @@ private List<List<Match>> computeNeighbors(List<Match> globalMatches) {
return neighbors;
}

/**
* This function iterates through the neighboring matches and checks which fit the merging criteria. Those who do are
* merged and the original matches are removed. This is done, until there are either no neighbors left, or none fit the
* criteria
* @return globalMatches containing merged matches.
*/
private List<Match> mergeNeighbors(List<Match> globalMatches) {
int i = 0;
List<List<Match>> neighbors = computeNeighbors(globalMatches);
Expand All @@ -97,6 +109,19 @@ private List<Match> mergeNeighbors(List<Match> globalMatches) {
return globalMatches;
}

/**
* This function removes token from both submissions after a merge has been performed. Additionally it moves the
* starting positions from matches, that occur after the merged neighboring matches, by the amount of removed token.
* @param globalMatches
* @param startFirst begin of the upper neighbor in the first submission
* @param startSecond begin of the upper neighbor in the second submission
* @param lengthUpper length of the upper neighbor
* @param seperatingFirst amount of token that separate the neighboring matches in the first submission and need to be
* removed
* @param seperatingSecond amount token that separate the neighboring matches in the send submission and need to be
* removed
* @return globalMatches with the mentioned changes.
*/
private List<Match> removeToken(List<Match> globalMatches, int startFirst, int startSecond, int lengthUpper, int seperatingFirst,
int seperatingSecond) {
List<Token> tokenFirst = new ArrayList<>(firstSubmission.getTokenList());
Expand All @@ -121,7 +146,11 @@ private List<Match> removeToken(List<Match> globalMatches, int startFirst, int s
return globalMatches;
}

private List<Match> removeBuffer(List<Match> globalMatches) {
/**
* This method marks the end of the merging pipeline and removes the remaining too short matches from
* @param globalMatches
*/
private List<Match> removeTooShortMatches(List<Match> globalMatches) {
List<Match> toRemove = new ArrayList<>();
for (Match match : globalMatches) {
if (match.length() < options.minimumTokenMatch()) {
Expand Down
23 changes: 15 additions & 8 deletions core/src/test/java/de/jplag/merging/MergingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@
import de.jplag.strategy.ComparisonStrategy;
import de.jplag.strategy.ParallelComparisonStrategy;

/**
* This class extends on {@link TestBase} and performs several test on Match Merging, in order to check its
* functionality. Therefore it uses two java programs and feds them into the JPlag pipeline. Results are stored before-
* and after Match Merging and used for all tests.
*/
class MergingTest extends TestBase {
private JPlagOptions options;
private JPlagResult result;
private List<Match> matches;
private List<JPlagComparison> comparisonsBefore;
private List<JPlagComparison> comparisonsAfter;
private final int MERGE_BUFFER = 8;
private final int SEPERATING_THRESHOLD = 2;

MergingTest() throws ExitException {
options = getDefaultOptions("merging").withMergingParameters(new MergingParameters(8, 2));
options = getDefaultOptions("merging").withMergingParameters(new MergingParameters(MERGE_BUFFER, SEPERATING_THRESHOLD));

GreedyStringTiling coreAlgorithm = new GreedyStringTiling(options);
ComparisonStrategy comparisonStrategy = new ParallelComparisonStrategy(options, coreAlgorithm);
Expand All @@ -46,19 +53,19 @@ class MergingTest extends TestBase {
}

@Test
@DisplayName("Test Lenght of Matches after Match Merging")
@DisplayName("Test length of matches after Match Merging")
void testBufferRemoval() {
checkMatchLength(JPlagComparison::matches, options.minimumTokenMatch(), comparisonsAfter);
}

@Test
@DisplayName("Test Lenght of Matches after Greedy String Tiling")
@DisplayName("Test length of matches after Greedy String Tiling")
void testGSTMatches() {
checkMatchLength(JPlagComparison::matches, options.minimumTokenMatch(), comparisonsBefore);
}

@Test
@DisplayName("Test Lenght of Ignored Matches after Greedy String Tiling")
@DisplayName("Test length of ignored matches after Greedy String Tiling")
void testGSTIgnoredMatches() {
int matchLengthThreshold = options.minimumTokenMatch() - options.mergingParameters().mergeBuffer();
checkMatchLength(JPlagComparison::ignoredMatches, matchLengthThreshold, comparisonsBefore);
Expand All @@ -74,15 +81,15 @@ private void checkMatchLength(Function<JPlagComparison, List<Match>> matchFuncti
}

@Test
@DisplayName("Test if Similarity Increased after Match Merging")
@DisplayName("Test if similarity increased after Match Merging")
void testSimilarityIncreased() {
for (int i = 0; i < comparisonsAfter.size(); i++) {
assertTrue(comparisonsAfter.get(i).similarity() >= comparisonsBefore.get(i).similarity());
}
}

@Test
@DisplayName("Test if Amount of Matches reduced after Match Merging")
@DisplayName("Test if amount of matches reduced after Match Merging")
void testFewerMatches() {
for (int i = 0; i < comparisonsAfter.size(); i++) {
assertTrue(comparisonsAfter.get(i).matches().size() + comparisonsAfter.get(i).ignoredMatches().size() <= comparisonsBefore.get(i)
Expand All @@ -91,7 +98,7 @@ void testFewerMatches() {
}

@Test
@DisplayName("Test if Amount of Token reduced after Match Merging")
@DisplayName("Test if amount of token reduced after Match Merging")
void testFewerToken() {
for (int i = 0; i < comparisonsAfter.size(); i++) {
assertTrue(comparisonsAfter.get(i).firstSubmission().getTokenList().size() <= comparisonsBefore.get(i).firstSubmission().getTokenList()
Expand All @@ -102,7 +109,7 @@ void testFewerToken() {
}

@Test
@DisplayName("Test if Merged Matches have counterparts in the original Matches")
@DisplayName("Test if merged matches have counterparts in the original matches")
void testCorrectMerges() {
boolean correctMerges = true;
for (int i = 0; i < comparisonsAfter.size(); i++) {
Expand Down

0 comments on commit 2b341e0

Please sign in to comment.