Browse Source

Simplify error reporting and improve error messages.

Timur Sağlam 5 years ago
parent
commit
2a7c906ce1

+ 2 - 1
jplag.frontend-utils/src/main/java/de/jplag/ErrorConsumer.java

@@ -3,8 +3,9 @@ package de.jplag;
 public interface ErrorConsumer {
 
     /**
-     * Print and store an error.
+     * Adds an error to the error stack. The error is only printed directly when using the corresponding verbosity option.
      * @param errorMessage is the error message.
+     * @see JPlagOptions.getVerbosity()
      */
     public void addError(String errorMessage);
 

+ 22 - 19
jplag/src/main/java/de/jplag/ErrorCollector.java

@@ -1,7 +1,6 @@
 package de.jplag;
 
 import static de.jplag.options.Verbosity.LONG;
-import static de.jplag.options.Verbosity.QUIET;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -11,49 +10,53 @@ import de.jplag.options.Verbosity;
 
 public class ErrorCollector implements ErrorConsumer { // TODO TS should be eventually replaced with a true logger/logging manager
 
-    private final List<String> errorVector; // List of errors that occurred during the execution of the program.
+    private final List<String> collectedErrors; // List of errors that occurred during the execution of the program.
     private final JPlagOptions options;
     private String currentSubmissionName;
 
     public ErrorCollector(JPlagOptions options) {
         this.options = options;
-        errorVector = new ArrayList<>();
+        collectedErrors = new ArrayList<>();
         currentSubmissionName = "<Unknown submission>";
     }
 
     @Override
     public void addError(String errorMessage) {
-        errorVector.add("[" + currentSubmissionName + "]\n" + errorMessage);
-        print(errorMessage, null);
+        collectedErrors.add("[" + currentSubmissionName + "]\n" + errorMessage);
+        print(null, currentSubmissionName + ": " + errorMessage);
     }
 
     @Override
     public void print(String message, String longMessage) {
+        if (message == null && longMessage == null) {
+            throw new IllegalArgumentException("At least one message parameter needs to be non-null!");
+        }
         Verbosity verbosity = options.getVerbosity();
-        if (verbosity != QUIET) {
-            if (message != null) {
-                System.out.print(message);
-            }
-            if (longMessage != null && verbosity == LONG) {
-                System.out.print(longMessage);
-            }
+        if (message != null) {
+            System.out.print(message);
+        }
+        if (longMessage != null && verbosity == LONG) {
+            System.out.print(longMessage);
         }
     }
 
     /**
-     * Print all errors from the errorVector.
+     * Print all collected errors messages in a list-like fashion.
      */
     public void printErrors() {
-        StringBuilder errorStr = new StringBuilder();
-
-        for (String str : errorVector) {
-            errorStr.append(str);
-            errorStr.append('\n');
+        StringBuilder errorReport = new StringBuilder();
+        for (String message : collectedErrors) {
+            errorReport.append(message);
+            errorReport.append('\n');
         }
 
-        System.out.println(errorStr.toString());
+        System.out.println(errorReport.toString());
     }
 
+    /**
+     * Updates the name of the currently processed submission.
+     * @param currentSubmissionName is the name.
+     */
     public void setCurrentSubmissionName(String currentSubmissionName) {
         this.currentSubmissionName = currentSubmissionName;
     }

+ 17 - 21
jplag/src/main/java/de/jplag/SubmissionSet.java

@@ -3,7 +3,6 @@ package de.jplag;
 import static java.util.stream.Collectors.toList;
 
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
@@ -138,30 +137,26 @@ public class SubmissionSet {
         int count = 0;
 
         long startTime = System.currentTimeMillis();
-        Iterator<Submission> iter = submissions.iterator();
 
-        int invalid = 0;
-        while (iter.hasNext()) {
+        int tooSmallSubmissions = 0;
+        for (Submission submission : submissions) {
             boolean ok;
             boolean removed = false;
-            Submission subm = iter.next();
 
-            errorCollector.print(null, "------ Parsing submission: " + subm.getName() + "\n");
-            currentSubmissionName = subm.getName();
+            errorCollector.print(null, "------ Parsing submission: " + submission.getName() + "\n");
+            currentSubmissionName = submission.getName();
             errorCollector.setCurrentSubmissionName(currentSubmissionName);
 
-            if (!(ok = subm.parse(options.isDebugParser()))) {
+            if (!(ok = submission.parse(options.isDebugParser()))) {
                 errors++;
             }
 
-            count++;
-
-            if (subm.getTokenList() != null && subm.getNumberOfTokens() < options.getMinimumTokenMatch()) {
+            if (submission.getTokenList() != null && submission.getNumberOfTokens() < options.getMinimumTokenMatch()) {
                 errorCollector.addError("Submission contains fewer tokens than minimum match length allows!\n");
-                subm.setTokenList(null);
-                invalid++;
+                submission.setTokenList(null);
+                tooSmallSubmissions++;
                 removed = true;
-                subm.markAsErroneous();
+                submission.markAsErroneous();
             }
 
             if (ok && !removed) {
@@ -171,18 +166,19 @@ public class SubmissionSet {
             }
         }
 
-        errorCollector.print("\n" + (count - errors - invalid) + " allSubmissions parsed successfully!\n" + errors + " parser error"
-                + (errors != 1 ? "s!\n" : "!\n"), null);
+        int validSubmissions = submissions.size() - errors - tooSmallSubmissions;
+        errorCollector.print(validSubmissions + " allSubmissions parsed successfully!", null);
+        errorCollector.print(errors + " parser error" + (errors != 1 ? "s!\n" : "!\n"), null);
 
-        if (invalid != 0) {
-            errorCollector.print(null, invalid + ((invalid == 1)
-                    ? " submission is not valid because it contains" : " allSubmissions are not valid because they contain")
-                            + " fewer tokens than minimum match length allows.\n");
+        if (tooSmallSubmissions == 1) {
+            errorCollector.print(null, tooSmallSubmissions + " submission is not valid because it contains fewer tokens than minimum match length allows.\n");
+        } else if (tooSmallSubmissions > 1) {
+            errorCollector.print(null, tooSmallSubmissions + " submissions are not valid because they contain fewer tokens than minimum match length allows.\n");
         }
 
         long duration = System.currentTimeMillis() - startTime;
         errorCollector.print("\n\n", "\nTotal time for parsing: " + TimeUtil.formatDuration(duration) + "\n" + "Time per parsed submission: "
-                + (count > 0 ? (duration / count) : "n/a") + " msec\n\n");
+                + (count > 0 ? (duration / count) : "n/a") + " msec\n");
     }
 
 }