From 3e7957166c7063ff219540ecce8ce4b14849a24f Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 9 Apr 2024 10:00:56 +0200 Subject: [PATCH] Added progress bars for Match Merging and Token String Normalization. Also added a functional style syntax for progress bars. --- .../src/main/java/de/jplag/SubmissionSet.java | 2 +- .../de/jplag/logging/ProgressBarLogger.java | 24 +++++++++++++++++++ .../de/jplag/logging/ProgressBarType.java | 4 +++- .../java/de/jplag/merging/MatchMerging.java | 6 +++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/de/jplag/SubmissionSet.java b/core/src/main/java/de/jplag/SubmissionSet.java index 907687974..09b21b5db 100644 --- a/core/src/main/java/de/jplag/SubmissionSet.java +++ b/core/src/main/java/de/jplag/SubmissionSet.java @@ -95,7 +95,7 @@ public List getInvalidSubmissions() { } public void normalizeSubmissions() { - submissions.forEach(Submission::normalize); + ProgressBarLogger.iterate(ProgressBarType.TOKEN_STRING_NORMALIZATION, submissions, Submission::normalize); } private List filterValidSubmissions() { diff --git a/core/src/main/java/de/jplag/logging/ProgressBarLogger.java b/core/src/main/java/de/jplag/logging/ProgressBarLogger.java index 889d391bb..54870d681 100644 --- a/core/src/main/java/de/jplag/logging/ProgressBarLogger.java +++ b/core/src/main/java/de/jplag/logging/ProgressBarLogger.java @@ -1,5 +1,9 @@ package de.jplag.logging; +import java.util.Collection; +import java.util.Iterator; +import java.util.function.Consumer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,6 +36,26 @@ public static void setProgressBarProvider(ProgressBarProvider progressBarProvide ProgressBarLogger.progressBarProvider = progressBarProvider; } + /** + * Iterates over the given collection while showing and updating a progress bar of the given type. The progress bar is + * updated, everytime the given action is done. + * @param type The type of progress bar + * @param data The data to iterate over + * @param action The action to call for each item + * @param The type of data + */ + public static void iterate(ProgressBarType type, Collection data, Consumer action) { + Iterator iterator = data.iterator(); + ProgressBar progressBar = ProgressBarLogger.createProgressBar(type, data.size()); + + while (iterator.hasNext()) { + action.accept(iterator.next()); + progressBar.step(); + } + + progressBar.dispose(); + } + private static class DummyProvider implements ProgressBarProvider { @Override public ProgressBar initProgressBar(ProgressBarType type, int totalSteps) { diff --git a/core/src/main/java/de/jplag/logging/ProgressBarType.java b/core/src/main/java/de/jplag/logging/ProgressBarType.java index 88e520fcc..8ab721be3 100644 --- a/core/src/main/java/de/jplag/logging/ProgressBarType.java +++ b/core/src/main/java/de/jplag/logging/ProgressBarType.java @@ -6,7 +6,9 @@ public enum ProgressBarType { LOADING("Loading Submissions "), PARSING("Parsing Submissions "), - COMPARING("Comparing Submissions"); + COMPARING("Comparing Submissions"), + MATCH_MERGING("Merging matched subsequences "), + TOKEN_STRING_NORMALIZATION("Normalizing Token Sequence"); private final String defaultText; diff --git a/core/src/main/java/de/jplag/merging/MatchMerging.java b/core/src/main/java/de/jplag/merging/MatchMerging.java index 3067e32a6..bc31e269c 100644 --- a/core/src/main/java/de/jplag/merging/MatchMerging.java +++ b/core/src/main/java/de/jplag/merging/MatchMerging.java @@ -10,6 +10,8 @@ import de.jplag.SharedTokenType; import de.jplag.Submission; import de.jplag.Token; +import de.jplag.logging.ProgressBarLogger; +import de.jplag.logging.ProgressBarType; import de.jplag.options.JPlagOptions; /** @@ -44,7 +46,7 @@ public JPlagResult mergeMatchesOf(JPlagResult result) { List comparisons = new ArrayList<>(result.getAllComparisons()); List comparisonsMerged = new ArrayList<>(); - for (JPlagComparison comparison : comparisons) { + ProgressBarLogger.iterate(ProgressBarType.MATCH_MERGING, comparisons, comparison -> { Submission leftSubmission = comparison.firstSubmission().copy(); Submission rightSubmission = comparison.secondSubmission().copy(); List globalMatches = new ArrayList<>(comparison.matches()); @@ -52,7 +54,7 @@ public JPlagResult mergeMatchesOf(JPlagResult result) { globalMatches = mergeNeighbors(globalMatches, leftSubmission, rightSubmission); globalMatches = globalMatches.stream().filter(it -> it.length() >= options.minimumTokenMatch()).toList(); comparisonsMerged.add(new JPlagComparison(leftSubmission, rightSubmission, globalMatches, new ArrayList<>())); - } + }); long durationInMillis = System.currentTimeMillis() - timeBeforeStartInMillis; return new JPlagResult(comparisonsMerged, result.getSubmissions(), result.getDuration() + durationInMillis, options);