Преглед изворни кода

Implement support for the CLI argument -c to specify the comparison mode.

Timur Sağlam пре 5 година
родитељ
комит
5055d50838

+ 7 - 8
jplag/src/main/java/jplag/CLI.java

@@ -1,5 +1,6 @@
 package jplag;
 
+import static jplag.strategy.ComparisonMode.NORMAL;
 import static net.sourceforge.argparse4j.impl.Arguments.storeTrue;
 
 import java.io.File;
@@ -8,6 +9,7 @@ import jplag.options.JPlagOptions;
 import jplag.options.LanguageOption;
 import jplag.options.Verbosity;
 import jplag.reporting.Report;
+import jplag.strategy.ComparisonMode;
 import net.sourceforge.argparse4j.ArgumentParsers;
 import net.sourceforge.argparse4j.inf.ArgumentParser;
 import net.sourceforge.argparse4j.inf.ArgumentParserException;
@@ -15,7 +17,6 @@ import net.sourceforge.argparse4j.inf.Namespace;
 
 /**
  * Command line interface class, allows using via command line.
- *
  * @see CLI#main(String[])
  */
 public class CLI {
@@ -30,7 +31,6 @@ public class CLI {
 
     /**
      * Main class for using JPlag via the CLI.
-     *
      * @param args are the CLI arguments that will be passed to JPlag.
      */
     public static void main(String[] args) {
@@ -58,9 +58,7 @@ public class CLI {
 
         // TODO SH: Fix code duplication of CLI arguments
         parser.addArgument("rootDir").help("The root-directory that contains all submissions");
-        parser.addArgument("-l")
-                .choices(LanguageOption.getAllDisplayNames())
-                .setDefault(LanguageOption.getDefault().getDisplayName())
+        parser.addArgument("-l").choices(LanguageOption.getAllDisplayNames()).setDefault(LanguageOption.getDefault().getDisplayName())
                 .help("Select the language to parse the submissions");
         parser.addArgument("-bc").help("Name of the directory which contains the base code (common framework)");
         parser.addArgument("-v").choices(verbosityOptions).setDefault("quiet").help("Verbosity");
@@ -71,11 +69,11 @@ public class CLI {
         parser.addArgument("-t").help("Tune the sensitivity of the comparison. A smaller <n> increases the sensitivity");
         parser.addArgument("-m").setDefault(0f).help("Match similarity Threshold [0-100]: All matches above this threshold will be saved");
         parser.addArgument("-r").setDefault("result").help("Name of directory in which the comparison results will be stored");
+        parser.addArgument("-c").choices(ComparisonMode.allNames()).setDefault(NORMAL.getName()).help("Comparison mode used to compare the programs");
     }
 
     /**
      * Parses an array of argument strings.
-     *
      * @param args is the array to parse.
      * @return the parsed arguments in a {@link Namespace} format.
      */
@@ -91,13 +89,12 @@ public class CLI {
 
     /**
      * Builds a options instance from parsed arguments.
-     *
      * @param namespace encapsulates the parsed arguments in a {@link Namespace} format.
      * @return the newly built options.F
      */
     public JPlagOptions buildOptionsFromArguments(Namespace namespace) {
         String fileSuffixString = namespace.getString("p");
-        String[] fileSuffixes = new String[]{};
+        String[] fileSuffixes = new String[] {};
         if (fileSuffixString != null) {
             fileSuffixes = fileSuffixString.replaceAll("\\s+", "").split(",");
         }
@@ -111,6 +108,8 @@ public class CLI {
         options.setSubdirectoryName(namespace.getString("S"));
         options.setFileSuffixes(fileSuffixes);
         options.setExclusionFileName(namespace.getString("x"));
+        ComparisonMode.fromName(namespace.getString("c")).ifPresentOrElse(it -> options.setComparisonMode(it),
+                () -> System.out.println("Unknown comparison mode, using default"));
 
         String minTokenMatch = namespace.getString("t");
         if (minTokenMatch != null) {

+ 48 - 3
jplag/src/main/java/jplag/strategy/ComparisonMode.java

@@ -1,9 +1,54 @@
 package jplag.strategy;
 
+import static java.util.stream.Collectors.toList;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Optional;
+
 /**
- * The comparison mode determines which `ComparisonStrategy` is used to compare submissions.
+ * The comparison mode determines which {@link ComparisonStrategy} is used to compare submissions.
  */
 public enum ComparisonMode {
-    NORMAL,
-    PARALLEL
+    NORMAL("Normal comparison (sequential)"),
+    PARALLEL("Faster comparison (parallel)");
+
+    private final String name;
+    private final String description;
+
+    private ComparisonMode(String description) {
+        this.description = description;
+        name = toString().toLowerCase();
+    }
+
+    /**
+     * @return the specifier for the comparison mode.
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * The textual description of the comparison mode.
+     * @return
+     */
+    public String getDescription() {
+        return description;
+    }
+
+    /**
+     * @return a collections of all mode names.
+     */
+    public static Collection<String> allNames() {
+        return Arrays.stream(values()).map(it -> it.getName()).collect(toList());
+    }
+
+    /**
+     * Retrieves a comparison mode from a specific name.
+     * @param name is that name.
+     * @return the optional mode or nothing if no name matches.
+     */
+    public static Optional<ComparisonMode> fromName(String name) {
+        return Arrays.stream(values()).filter(it -> it.name.equals(name)).findFirst();
+    }
 }