Submission.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package de.jplag;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.util.Collection;
  7. import java.util.Objects;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. /**
  11. * Represents a single submission. A submission can contain multiple files.
  12. */
  13. public class Submission implements Comparable<Submission> {
  14. private static final Logger logger = LoggerFactory.getLogger(Submission.class);
  15. /**
  16. * Directory name for storing submission files with parse errors if so requested.
  17. */
  18. private static final String ERROR_FOLDER = "errors";
  19. /**
  20. * Identification of the submission (often a directory or file name).
  21. */
  22. private final String name;
  23. /**
  24. * Root of the submission files (including the subdir if used).
  25. */
  26. private final File submissionRootFile;
  27. /**
  28. * Whether the submission is new. That is, must be checked for plagiarism.
  29. */
  30. private final boolean isNew;
  31. /**
  32. * Files of the submission.
  33. */
  34. private final Collection<File> files;
  35. /**
  36. * Whether an error occurred during parsing the submission files.
  37. */
  38. private boolean hasErrors;
  39. /**
  40. * Parse result, tokens from all files.
  41. */
  42. private TokenList tokenList;
  43. /**
  44. * Base code comparison
  45. */
  46. private JPlagComparison baseCodeComparison;
  47. private final Language language;
  48. /**
  49. * Creates a submission.
  50. * @param name Identification of the submission (directory or filename).
  51. * @param submissionRootFile is the submission file, or the root of the submission itself.
  52. * @param isNew states whether the submission must be checked for plagiarism.
  53. * @param files are the files of the submissions, if the root is a single file it should just contain one file.
  54. * @param language is the language of the submission.
  55. */
  56. public Submission(String name, File submissionRootFile, boolean isNew, Collection<File> files, Language language) {
  57. this.name = name;
  58. this.submissionRootFile = submissionRootFile;
  59. this.isNew = isNew;
  60. this.files = files;
  61. this.language = language;
  62. }
  63. @Override
  64. public int compareTo(Submission other) {
  65. return name.compareTo(other.name);
  66. }
  67. @Override
  68. public boolean equals(Object obj) {
  69. if (obj == this) {
  70. return true;
  71. }
  72. if (!(obj instanceof Submission otherSubmission)) {
  73. return false;
  74. }
  75. return otherSubmission.getName().equals(name);
  76. }
  77. @Override
  78. public int hashCode() {
  79. return Objects.hash(name);
  80. }
  81. /**
  82. * @return base code comparison
  83. */
  84. public JPlagComparison getBaseCodeComparison() {
  85. return baseCodeComparison;
  86. }
  87. /**
  88. * @return a list of files this submission consists of.
  89. */
  90. public Collection<File> getFiles() {
  91. return files;
  92. }
  93. /**
  94. * @return name of the submission (directory or file name).
  95. */
  96. public String getName() {
  97. return name;
  98. }
  99. /**
  100. * @return Number of tokens in the parse result.
  101. */
  102. public int getNumberOfTokens() {
  103. if (tokenList == null) {
  104. return 0;
  105. }
  106. return tokenList.size();
  107. }
  108. /**
  109. * @return the unique file of the submission, which is either in a root folder or a subfolder of root folder when the
  110. * subdirectory option is used.
  111. */
  112. public File getRoot() {
  113. return submissionRootFile;
  114. }
  115. /**
  116. * @param subtractBaseCode If true subtract basecode matches if possible.
  117. * @return Similarity divisor for the submission.
  118. */
  119. public int getSimilarityDivisor(boolean subtractBaseCode) {
  120. int divisor = getNumberOfTokens() - getFiles().size();
  121. if (subtractBaseCode && baseCodeComparison != null) {
  122. divisor -= baseCodeComparison.getNumberOfMatchedTokens();
  123. }
  124. return divisor;
  125. }
  126. /**
  127. * @return Parse result of the submission.
  128. */
  129. public TokenList getTokenList() {
  130. return tokenList;
  131. }
  132. /**
  133. * @return Whether a comparison between the submission and the base code is available.
  134. */
  135. public boolean hasBaseCodeMatches() {
  136. return baseCodeComparison != null;
  137. }
  138. /**
  139. * @return true if at least one error occurred while parsing this submission; false otherwise.
  140. */
  141. public boolean hasErrors() {
  142. return hasErrors;
  143. }
  144. /**
  145. * @return whether the submission is new, That is, must be checked for plagiarism.
  146. */
  147. public boolean isNew() {
  148. return isNew;
  149. }
  150. /**
  151. * Resets the base code flag for all tokens of this submission.
  152. */
  153. public void resetBaseCode() {
  154. for (Token token : tokenList.allTokens()) {
  155. token.setBasecode(false);
  156. }
  157. }
  158. /**
  159. * Sets the base code comparison
  160. * @param baseCodeComparison is submissions matches with the base code
  161. */
  162. public void setBaseCodeComparison(JPlagComparison baseCodeComparison) {
  163. this.baseCodeComparison = baseCodeComparison;
  164. }
  165. /**
  166. * Sets the tokens that have been parsed from the files this submission consists of.
  167. * @param tokenList is the list of these tokens.
  168. */
  169. public void setTokenList(TokenList tokenList) {
  170. this.tokenList = tokenList;
  171. }
  172. /**
  173. * String representation of the code files contained in this submission, annotated with all tokens.
  174. * @return the annotated code as string.
  175. */
  176. public String getTokenAnnotatedSourceCode() {
  177. return TokenPrinter.printTokens(tokenList, files, submissionRootFile);
  178. }
  179. @Override
  180. public String toString() {
  181. return name;
  182. }
  183. /**
  184. * This method is used to copy files that can not be parsed to a special folder.
  185. */
  186. private void copySubmission() {
  187. File rootDirectory = submissionRootFile.getParentFile();
  188. assert rootDirectory != null;
  189. File submissionDirectory = createSubdirectory(rootDirectory, ERROR_FOLDER, language.getShortName(), name);
  190. for (File file : files) {
  191. try {
  192. Files.copy(file.toPath(), new File(submissionDirectory, file.getName()).toPath());
  193. } catch (IOException exception) {
  194. logger.error("Error copying file: " + exception.getMessage(), exception);
  195. }
  196. }
  197. }
  198. private static File createSubdirectory(File parent, String... subdirectoryNames) {
  199. File subdirectory = parent;
  200. for (String name : subdirectoryNames) {
  201. subdirectory = new File(subdirectory, name);
  202. }
  203. if (!subdirectory.exists()) {
  204. subdirectory.mkdirs();
  205. }
  206. return subdirectory;
  207. }
  208. /**
  209. * Map all files of this submission to their path relative to the submission directory.
  210. * <p>
  211. * This method is required to stay compatible with `language.parse(...)` as it requires the given file paths to be
  212. * relative to the submission directory.
  213. * <p>
  214. * In a future update, `language.parse(...)` should probably just take a list of files.
  215. * @param baseFile - File to base all relative file paths on.
  216. * @param files - List of files to map.
  217. * @return an array of file paths relative to the submission directory.
  218. */
  219. private String[] getRelativeFilePaths(File baseFile, Collection<File> files) {
  220. Path baseFilePath = baseFile.toPath();
  221. return files.stream().map(File::toPath).map(baseFilePath::relativize).map(Path::toString).toArray(String[]::new);
  222. }
  223. /* package-private */ void markAsErroneous() {
  224. hasErrors = true;
  225. }
  226. /**
  227. * Parse files of the submission.
  228. * @return Whether parsing was successful.
  229. */
  230. /* package-private */ boolean parse(boolean debugParser) {
  231. if (files == null || files.isEmpty()) {
  232. logger.error("ERROR: nothing to parse for submission \"{}\"", name);
  233. tokenList = null;
  234. hasErrors = true; // invalidate submission
  235. return false;
  236. }
  237. String[] relativeFilePaths = getRelativeFilePaths(submissionRootFile, files);
  238. tokenList = language.parse(submissionRootFile, relativeFilePaths);
  239. if (!language.hasErrors()) {
  240. if (tokenList.size() < 3) {
  241. logger.error("Submission \"{}\" is too short!", name);
  242. tokenList = null;
  243. hasErrors = true; // invalidate submission
  244. return false;
  245. }
  246. return true;
  247. }
  248. tokenList = null;
  249. hasErrors = true; // invalidate submission
  250. if (debugParser) {
  251. copySubmission();
  252. }
  253. return false;
  254. }
  255. }