Browse Source

Restructure and clean main program.

Timur Sağlam 5 years ago
parent
commit
416bfe7099
1 changed files with 206 additions and 248 deletions
  1. 206 248
      jplag/src/main/java/jplag/JPlag.java

+ 206 - 248
jplag/src/main/java/jplag/JPlag.java

@@ -29,110 +29,133 @@ import jplagUtils.PropertiesLoader;
  */
 public class JPlag implements ProgramI {
 
+    // CONSTANTS:
     private static final Properties versionProps = PropertiesLoader.loadProps("jplag/version.properties");
-
     public static final String name = "JPlag" + versionProps.getProperty("version", "devel");
-
     public static final String name_long = "JPlag (Version " + versionProps.getProperty("version", "devel") + ")";
 
-    /**
-     * This stores the name of the submission that is currently parsed.
-     * <p>
-     * TODO PB: This should be moved to parseSubmissions(...)
-     */
-    private String currentSubmissionName = "<Unknown submission>";
-
-    /**
-     * Vector of errors that occurred during the execution of the program.
-     */
-    private Vector<String> errorVector = new Vector<>();
-
-    private int errors = 0;
-
-    private Language language;
-
-    /**
-     * The base code directory is considered a separate submission.
-     */
+    // INPUT:
     private Submission baseCodeSubmission = null;
+    private HashSet<String> excludedFileNames = null; // Set of file names to be excluded in comparison.
+    private Language language;
 
-    /**
-     * Comparison strategy to use.
-     */
+    // CORE COMPONENTS:
     private ComparisonStrategy comparisonStrategy;
+    private GreedyStringTiling gSTiling = new GreedyStringTiling(this); // Contains the comparison logic.
+    private final JPlagOptions options;
+    private FileWriter writer = null;
 
-    /**
-     * Set of file names to be excluded in comparison.
-     */
-    private HashSet<String> excludedFileNames = null;
+    // ERROR REPORTING:
+    private String currentSubmissionName = "<Unknown submission>"; // TODO PB: This should be moved to parseSubmissions(...)
+    private int errors = 0;
+    private Vector<String> errorVector = new Vector<>(); // Vector of errors that occurred during the execution of the program.
 
     /**
-     * Contains the comparison logic.
+     * Creates and initializes a JPlag instance, parameterized by a set of options.
+     * @param options determines the parameterization.
+     * @throws ExitException if the initialization fails.
      */
-    private GreedyStringTiling gSTiling = new GreedyStringTiling(this);
+    public JPlag(JPlagOptions options) throws ExitException {
+        this.options = options;
+        initializeLanguage();
+        initializeComparisonStrategy();
+        checkBaseCodeOption();
+    }
 
     /**
-     * JPlag configuration options.
+     * Main procedure, executes the comparison of source code submissions.
+     * @return the results of the comparison, specifically the submissions whose similarity exceeds a set threshold.
+     * @throws ExitException if the JPlag exits preemptively.
      */
-    private final JPlagOptions options;
+    public JPlagResult run() throws ExitException {
+        // 1. Preparation:
+        File rootDir = new File(options.getRootDirName());
+        if (!rootDir.exists()) {
+            throw new ExitException("Root directory " + options.getRootDirName() + " does not exist!");
+        }
+        if (!rootDir.isDirectory()) {
+            throw new ExitException(options.getRootDirName() + " is not a directory!");
+        }
+        readExclusionFile(); // This file contains all files names which are excluded
+    
+        // 2. Parse and validate submissions:
+        Vector<Submission> submissions = findSubmissions(rootDir);
+        parseAllSubmissions(submissions, baseCodeSubmission);
+        submissions = filterValidSubmissions(submissions);
+        if (submissions.size() < 2) {
+            printErrors();
+            throw new ExitException("Not enough valid submissions! (found " + submissions.size() + " valid submissions)",
+                    ExitException.NOT_ENOUGH_SUBMISSIONS_ERROR);
+        }
+    
+        // 3. Compare valid submissions:
+        errorVector = null; // errorVector is not needed anymore
+        System.gc();
+        JPlagResult result = comparisonStrategy.compareSubmissions(submissions, baseCodeSubmission);
+        closeWriter();
+        return result;
+    }
 
     /**
-     * File writer.
+     * Add an error to the errorVector.
      */
-    private FileWriter writer = null;
+    @Override
+    public void addError(String errorMessage) {
+        errorVector.add("[" + currentSubmissionName + "]\n" + errorMessage);
+        print(errorMessage, null);
+    }
 
     public Language getLanguage() {
         return language;
     }
 
-    /**
-     * Creates and initializes a JPlag instance, parameterized by a set of options.
-     * @param options determines the parameterization.
-     * @throws ExitException if the initialization fails.
-     */
-    public JPlag(JPlagOptions options) throws ExitException {
-        this.options = options;
-        this.initialize();
+    public JPlagOptions getOptions() {
+        return this.options;
     }
 
-    public void initialize() throws ExitException {
-        this.initializeLanguage();
-        this.initializeComparisonStrategy();
-        this.checkBaseCodeOption();
+    public boolean hasValidSuffix(File file) {
+        String[] validSuffixes = options.getFileSuffixes();
+        if (validSuffixes == null || validSuffixes.length == 0) {
+            return true; // TODO TS: Why does this return true if the valid suffixes are not set?
+        }
+        return Arrays.stream(validSuffixes).anyMatch(suffix -> file.getName().endsWith(suffix));
     }
 
-    private void initializeComparisonStrategy() throws ExitException {
-        ComparisonMode mode = options.getComparisonMode();
-        switch (mode) { // TODO TS: Currently only one comparison strategy supported
-        case NORMAL:
-            this.comparisonStrategy = new NormalComparisonStrategy(options, gSTiling);
-            return;
-        default:
-            throw new ExitException("Illegal comparison mode: " + options.getComparisonMode());
+    /**
+     * Check if a file is excluded or not.
+     */
+    public boolean isFileExcluded(File file) {
+        if (excludedFileNames == null) {
+            return false;
         }
+        return excludedFileNames.stream().anyMatch(excludedName -> file.getName().endsWith(excludedName));
     }
 
-    private void initializeLanguage() throws ExitException {
-        LanguageOption languageOption = this.options.getLanguageOption();
-
+    @Override
+    public void print(String message, String longMessage) {
+        if (options.getVerbosity() == PARSER) {
+            if (longMessage != null) {
+                myWrite(longMessage);
+            } else if (message != null) {
+                myWrite(message);
+            }
+        }
+        if (options.getVerbosity() == QUIET) {
+            return;
+        }
         try {
-            Constructor<?> constructor = Class.forName(languageOption.getClassPath()).getConstructor(ProgramI.class);
-            Object[] constructorParams = {this};
-
-            Language language = (Language) constructor.newInstance(constructorParams);
-
-            this.language = language;
-            this.options.setLanguage(language);
-        } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException
-                | IllegalArgumentException | InvocationTargetException e) {
-            e.printStackTrace();
+            if (message != null) {
+                System.out.print(message);
+            }
 
-            throw new ExitException("Language instantiation failed", ExitException.BAD_LANGUAGE_ERROR);
+            if (longMessage != null) {
+                if (options.getVerbosity() == LONG) {
+                    System.out.print(longMessage);
+                }
+            }
+        } catch (Throwable e) {
+            System.out.println(e.getMessage());
         }
-
-        this.options.setLanguageDefaults(this.getLanguage());
-
-        System.out.println("Initialized language " + this.getLanguage().name());
     }
 
     /**
@@ -168,82 +191,21 @@ public class JPlag implements ProgramI {
         System.out.println("Basecode directory \"" + baseCodePath + "\" will be used");
     }
 
-    public JPlagOptions getOptions() {
-        return this.options;
-    }
-
-    /**
-     * Main procedure, executes the comparison of source code submissions.
-     * @return the results of the comparison, specifically the submissions whose similarity exceeds a set threshold.
-     * @throws ExitException if the JPlag exits preemptively.
-     */
-    public JPlagResult run() throws ExitException {
-        File rootDir = new File(options.getRootDirName());
-
-        if (!rootDir.exists()) {
-            throw new ExitException("Root directory " + options.getRootDirName() + " does not exist!");
-        }
-
-        if (!rootDir.isDirectory()) {
-            throw new ExitException(options.getRootDirName() + " is not a directory!");
-        }
-
-        // This file contains all files names which are excluded
-        readExclusionFile();
-
-        Vector<Submission> submissions = findSubmissions(rootDir);
-        parseAllSubmissions(submissions, baseCodeSubmission);
-        submissions = filterValidSubmissions(submissions);
-
-        if (submissions.size() < 2) {
-            printErrors();
-            throw new ExitException("Not enough valid submissions! (found " + submissions.size() + " valid submissions)",
-                    ExitException.NOT_ENOUGH_SUBMISSIONS_ERROR);
+    private void closeWriter() {
+        try {
+            if (writer != null) {
+                writer.close();
+            }
+        } catch (IOException ex) {
+            ex.printStackTrace();
         }
-
-        // errorVector is not needed anymore
-        errorVector = null;
-
-        System.gc();
-
-        JPlagResult result = comparisonStrategy.compareSubmissions(submissions, baseCodeSubmission);
-
-        closeWriter();
-
-        return result;
+        writer = null;
     }
 
     private Vector<Submission> filterValidSubmissions(Vector<Submission> submissions) {
         return submissions.stream().filter(submission -> !submission.hasErrors).collect(Collectors.toCollection(Vector::new));
     }
 
-    /**
-     * TODO PB: Find a better way to separate parseSubmissions(...) and parseBaseCodeSubmission(...)
-     */
-    private void parseAllSubmissions(Vector<Submission> submissions, Submission baseCodeSubmission) throws ExitException {
-        try {
-            parseSubmissions(submissions);
-            System.gc();
-            parseBaseCodeSubmission(baseCodeSubmission);
-        } catch (OutOfMemoryError e) {
-            System.gc();
-
-            throw new ExitException("Out of memory during parsing of submission \"" + currentSubmissionName + "\"");
-        } catch (Throwable e) {
-            e.printStackTrace();
-
-            throw new ExitException("Unknown exception during parsing of " + "submission \"" + currentSubmissionName + "\"");
-        }
-    }
-
-    public boolean hasValidSuffix(File file) {
-        String[] validSuffixes = options.getFileSuffixes();
-        if (validSuffixes == null || validSuffixes.length == 0) {
-            return true; // TODO TS: Why does this return true if the valid suffixes are not set?
-        }
-        return Arrays.stream(validSuffixes).anyMatch(suffix -> file.getName().endsWith(suffix));
-    }
-
     /**
      * Find all submissions in the given root directory.
      */
@@ -270,6 +232,40 @@ public class JPlag implements ProgramI {
         return fileNamesInRootDir;
     }
 
+    private void initializeComparisonStrategy() throws ExitException {
+        ComparisonMode mode = options.getComparisonMode();
+        switch (mode) { // TODO TS: Currently only one comparison strategy supported
+        case NORMAL:
+            this.comparisonStrategy = new NormalComparisonStrategy(options, gSTiling);
+            return;
+        default:
+            throw new ExitException("Illegal comparison mode: " + options.getComparisonMode());
+        }
+    }
+
+    private void initializeLanguage() throws ExitException {
+        LanguageOption languageOption = this.options.getLanguageOption();
+
+        try {
+            Constructor<?> constructor = Class.forName(languageOption.getClassPath()).getConstructor(ProgramI.class);
+            Object[] constructorParams = {this};
+
+            Language language = (Language) constructor.newInstance(constructorParams);
+
+            this.language = language;
+            this.options.setLanguage(language);
+        } catch (NoSuchMethodException | SecurityException | ClassNotFoundException | InstantiationException | IllegalAccessException
+                | IllegalArgumentException | InvocationTargetException e) {
+            e.printStackTrace();
+
+            throw new ExitException("Language instantiation failed", ExitException.BAD_LANGUAGE_ERROR);
+        }
+
+        this.options.setLanguageDefaults(this.getLanguage());
+
+        System.out.println("Initialized language " + this.getLanguage().name());
+    }
+
     private Vector<Submission> mapFileNamesInRootDirToSubmissions(String[] fileNames, File rootDir) throws ExitException {
         Vector<Submission> submissions = new Vector<>();
 
@@ -312,14 +308,71 @@ public class JPlag implements ProgramI {
         return submissions;
     }
 
+    private void myWrite(String str) {
+        if (writer != null) {
+            try {
+                writer.write(str);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        } else {
+            System.out.print(str);
+        }
+    }
+
     /**
-     * Check if a file is excluded or not.
+     * TODO PB: Find a better way to separate parseSubmissions(...) and parseBaseCodeSubmission(...)
      */
-    public boolean isFileExcluded(File file) {
-        if (excludedFileNames == null) {
-            return false;
+    private void parseAllSubmissions(Vector<Submission> submissions, Submission baseCodeSubmission) throws ExitException {
+        try {
+            parseSubmissions(submissions);
+            System.gc();
+            parseBaseCodeSubmission(baseCodeSubmission);
+        } catch (OutOfMemoryError e) {
+            System.gc();
+
+            throw new ExitException("Out of memory during parsing of submission \"" + currentSubmissionName + "\"");
+        } catch (Throwable e) {
+            e.printStackTrace();
+
+            throw new ExitException("Unknown exception during parsing of " + "submission \"" + currentSubmissionName + "\"");
         }
-        return excludedFileNames.stream().anyMatch(excludedName -> file.getName().endsWith(excludedName));
+    }
+
+    /**
+     * Parse the given base code submission.
+     */
+    private void parseBaseCodeSubmission(Submission subm) throws ExitException {
+        if (subm == null) {
+            // TODO:
+            // options.useBasecode = false;
+            return;
+        }
+
+        long msec = System.currentTimeMillis();
+        print("----- Parsing basecode submission: " + subm.name + "\n", null);
+
+        // lets go:
+
+        if (!subm.parse()) {
+            printErrors();
+            throw new ExitException("Bad basecode submission");
+        }
+
+        if (subm.tokenList != null && subm.getNumberOfTokens() < options.getMinTokenMatch()) {
+            throw new ExitException("Basecode submission contains fewer tokens " + "than minimum match length allows!\n");
+        }
+
+        if (options.hasBaseCode()) {
+            gSTiling.createHashes(subm.tokenList, options.getMinTokenMatch(), true);
+        }
+
+        print("\nBasecode submission parsed!\n", null);
+
+        long time = System.currentTimeMillis() - msec;
+
+        print("\n", "\nTime for parsing Basecode: " + ((time / 3600000 > 0) ? (time / 3600000) + " h " : "")
+                + ((time / 60000 > 0) ? ((time / 60000) % 60000) + " min " : "") + (time / 1000 % 60) + " sec\n");
     }
 
     /**
@@ -383,39 +436,17 @@ public class JPlag implements ProgramI {
     }
 
     /**
-     * Parse the given base code submission.
+     * Print all errors from the errorVector.
      */
-    private void parseBaseCodeSubmission(Submission subm) throws ExitException {
-        if (subm == null) {
-            // TODO:
-            // options.useBasecode = false;
-            return;
-        }
-
-        long msec = System.currentTimeMillis();
-        print("----- Parsing basecode submission: " + subm.name + "\n", null);
-
-        // lets go:
-
-        if (!subm.parse()) {
-            printErrors();
-            throw new ExitException("Bad basecode submission");
-        }
-
-        if (subm.tokenList != null && subm.getNumberOfTokens() < options.getMinTokenMatch()) {
-            throw new ExitException("Basecode submission contains fewer tokens " + "than minimum match length allows!\n");
-        }
+    private void printErrors() {
+        StringBuilder errorStr = new StringBuilder();
 
-        if (options.hasBaseCode()) {
-            gSTiling.createHashes(subm.tokenList, options.getMinTokenMatch(), true);
+        for (String str : errorVector) {
+            errorStr.append(str);
+            errorStr.append('\n');
         }
 
-        print("\nBasecode submission parsed!\n", null);
-
-        long time = System.currentTimeMillis() - msec;
-
-        print("\n", "\nTime for parsing Basecode: " + ((time / 3600000 > 0) ? (time / 3600000) + " h " : "")
-                + ((time / 60000 > 0) ? ((time / 60000) % 60000) + " min " : "") + (time / 1000 % 60) + " sec\n");
+        System.out.println(errorStr.toString());
     }
 
     /*
@@ -449,77 +480,4 @@ public class JPlag implements ProgramI {
             }
         }
     }
-
-    private void myWrite(String str) {
-        if (writer != null) {
-            try {
-                writer.write(str);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        } else {
-            System.out.print(str);
-        }
-    }
-
-    private void closeWriter() {
-        try {
-            if (writer != null) {
-                writer.close();
-            }
-        } catch (IOException ex) {
-            ex.printStackTrace();
-        }
-        writer = null;
-    }
-
-    /**
-     * Add an error to the errorVector.
-     */
-    @Override
-    public void addError(String errorMessage) {
-        errorVector.add("[" + currentSubmissionName + "]\n" + errorMessage);
-        print(errorMessage, null);
-    }
-
-    /**
-     * Print all errors from the errorVector.
-     */
-    private void printErrors() {
-        StringBuilder errorStr = new StringBuilder();
-
-        for (String str : errorVector) {
-            errorStr.append(str);
-            errorStr.append('\n');
-        }
-
-        System.out.println(errorStr.toString());
-    }
-
-    @Override
-    public void print(String message, String longMessage) {
-        if (options.getVerbosity() == PARSER) {
-            if (longMessage != null) {
-                myWrite(longMessage);
-            } else if (message != null) {
-                myWrite(message);
-            }
-        }
-        if (options.getVerbosity() == QUIET) {
-            return;
-        }
-        try {
-            if (message != null) {
-                System.out.print(message);
-            }
-
-            if (longMessage != null) {
-                if (options.getVerbosity() == LONG) {
-                    System.out.print(longMessage);
-                }
-            }
-        } catch (Throwable e) {
-            System.out.println(e.getMessage());
-        }
-    }
 }