JPlagComparison.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package de.jplag;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.stream.IntStream;
  8. /**
  9. * This method represents the whole result of a comparison between two submissions.
  10. */
  11. public class JPlagComparison implements Comparator<JPlagComparison> { // FIXME TS: contains a lot of code duplication
  12. private static final int ROUNDING_FACTOR = 10;
  13. private final Submission firstSubmission;
  14. private final Submission secondSubmission;
  15. private JPlagComparison firstBaseCodeMatches = null;
  16. private JPlagComparison secondBaseCodeMatches = null;
  17. private final List<Match> matches;
  18. public JPlagComparison(Submission firstSubmission, Submission secondSubmission) {
  19. this.firstSubmission = firstSubmission;
  20. this.secondSubmission = secondSubmission;
  21. matches = new ArrayList<>();
  22. }
  23. /**
  24. * Add a match to the comparison (token indices and number of tokens), if it does not overlap with the existing matches.
  25. * @see Match#Match(int, int, int)
  26. */
  27. public final void addMatch(int startOfFirst, int startOfSecond, int length) {
  28. for (Match match : matches) {
  29. if (match.overlap(startOfFirst, startOfSecond, length)) {
  30. return;
  31. }
  32. }
  33. matches.add(new Match(startOfFirst, startOfSecond, length));
  34. }
  35. /**
  36. * The bigger a match (length) is relatively to the biggest match the redder is the color returned by this method.
  37. */
  38. public String color(int length) {
  39. int longestMatch = matches.stream().mapToInt(it -> it.getLength()).max().orElse(0);
  40. int color = 255 * length / longestMatch;
  41. String help = (color < 16 ? "0" : "") + Integer.toHexString(color);
  42. return "#" + help + "0000";
  43. }
  44. @Override
  45. public int compare(JPlagComparison comparison1, JPlagComparison comparison2) {
  46. return Float.compare(comparison2.similarity(), comparison1.similarity()); // comparison2 first!
  47. }
  48. @Override
  49. public boolean equals(Object other) {
  50. if (!(other instanceof JPlagComparison)) {
  51. return false;
  52. }
  53. return (compare(this, (JPlagComparison) other) == 0);
  54. }
  55. /**
  56. * This method returns all the files which contributed to a match. Parameter: j == 0 1st submission, j != 0 2nd
  57. * submission.
  58. */
  59. public final String[] files(int j) {
  60. if (matches.size() == 0) {
  61. return new String[] {};
  62. }
  63. TokenList tokenList = (j == 0 ? firstSubmission : secondSubmission).getTokenList();
  64. int i, h, starti, starth, count = 1;
  65. o1: for (i = 1; i < matches.size(); i++) {
  66. starti = (j == 0 ? matches.get(i).getStartOfFirst() : matches.get(i).getStartOfSecond());
  67. for (h = 0; h < i; h++) {
  68. starth = (j == 0 ? matches.get(h).getStartOfFirst() : matches.get(h).getStartOfSecond());
  69. if (tokenList.getToken(starti).file.equals(tokenList.getToken(starth).file)) {
  70. continue o1;
  71. }
  72. }
  73. count++;
  74. }
  75. String[] res = new String[count];
  76. res[0] = tokenList.getToken((j == 0 ? matches.get(0).getStartOfFirst() : matches.get(0).getStartOfSecond())).file;
  77. count = 1;
  78. o2: for (i = 1; i < matches.size(); i++) {
  79. starti = (j == 0 ? matches.get(i).getStartOfFirst() : matches.get(i).getStartOfSecond());
  80. for (h = 0; h < i; h++) {
  81. starth = (j == 0 ? matches.get(h).getStartOfFirst() : matches.get(h).getStartOfSecond());
  82. if (tokenList.getToken(starti).file.equals(tokenList.getToken(starth).file)) {
  83. continue o2;
  84. }
  85. }
  86. res[count++] = tokenList.getToken(starti).file;
  87. }
  88. /*
  89. * sort by file name. (so that equally named files are displayed approximately side by side.)
  90. */
  91. Arrays.sort(res);
  92. return res;
  93. }
  94. /**
  95. * @return the base code matches of the first submission.
  96. */
  97. public JPlagComparison getFirstBaseCodeMatches() {
  98. return firstBaseCodeMatches;
  99. }
  100. /**
  101. * @return the first of the two submissions.
  102. */
  103. public Submission getFirstSubmission() {
  104. return firstSubmission;
  105. }
  106. /**
  107. * @return all matches between the two submissions.
  108. */
  109. public List<Match> getMatches() {
  110. return matches;
  111. }
  112. /**
  113. * Get the total number of matched tokens for this comparison.
  114. */
  115. public final int getNumberOfMatchedTokens() {
  116. int numberOfMatchedTokens = 0;
  117. for (Match match : matches) {
  118. numberOfMatchedTokens += match.getLength();
  119. }
  120. return numberOfMatchedTokens;
  121. }
  122. /**
  123. * @return the base code matches of the second submissions.
  124. */
  125. public JPlagComparison getSecondBaseCodeMatches() {
  126. return secondBaseCodeMatches;
  127. }
  128. /**
  129. * @return the second of the two submissions.
  130. */
  131. public Submission getSecondSubmission() {
  132. return secondSubmission;
  133. }
  134. /**
  135. * @return Maximum similarity in percent of both submissions.
  136. */
  137. public final float maximalSimilarity() {
  138. return Math.max(similarityOfFirst(), similarityOfSecond());
  139. }
  140. /**
  141. * @return Minimum similarity in percent of both submissions.
  142. */
  143. public final float minimalSimilarity() {
  144. return Math.min(similarityOfFirst(), similarityOfSecond());
  145. }
  146. /**
  147. * @return Similarity in percent (what percentage of tokens across both submissions are matched).
  148. */
  149. public final float similarity() {
  150. float sa, sb;
  151. if (secondBaseCodeMatches != null && firstBaseCodeMatches != null) {
  152. sa = firstSubmission.getNumberOfTokens() - firstSubmission.getFiles().size() - firstBaseCodeMatches.getNumberOfMatchedTokens();
  153. sb = secondSubmission.getNumberOfTokens() - secondSubmission.getFiles().size() - secondBaseCodeMatches.getNumberOfMatchedTokens();
  154. } else {
  155. sa = firstSubmission.getNumberOfTokens() - firstSubmission.getFiles().size();
  156. sb = secondSubmission.getNumberOfTokens() - secondSubmission.getFiles().size();
  157. }
  158. return (200 * getNumberOfMatchedTokens()) / (sa + sb);
  159. }
  160. /**
  161. * @return Similarity in percent for the first submission (what percent of the first submission is similar to the
  162. * second).
  163. */
  164. public final float similarityOfFirst() {
  165. int divisor;
  166. if (firstBaseCodeMatches != null) {
  167. divisor = firstSubmission.getNumberOfTokens() - firstSubmission.getFiles().size() - firstBaseCodeMatches.getNumberOfMatchedTokens();
  168. } else {
  169. divisor = firstSubmission.getNumberOfTokens() - firstSubmission.getFiles().size();
  170. }
  171. return (divisor == 0 ? 0f : (getNumberOfMatchedTokens() * 100 / (float) divisor));
  172. }
  173. /**
  174. * @return Similarity in percent for the second submission (what percent of the second submission is similar to the
  175. * first).
  176. */
  177. public final float similarityOfSecond() {
  178. int divisor;
  179. if (secondBaseCodeMatches != null) {
  180. divisor = secondSubmission.getNumberOfTokens() - secondSubmission.getFiles().size() - secondBaseCodeMatches.getNumberOfMatchedTokens();
  181. } else {
  182. divisor = secondSubmission.getNumberOfTokens() - secondSubmission.getFiles().size();
  183. }
  184. return (divisor == 0 ? 0f : (getNumberOfMatchedTokens() * 100 / (float) divisor));
  185. }
  186. /**
  187. * @return Similarity in percent rounded down to the nearest tenth.
  188. */
  189. public final float roundedSimilarity() {
  190. return ((int) (similarity() * ROUNDING_FACTOR)) / (float) ROUNDING_FACTOR;
  191. }
  192. /**
  193. * @param submission Either the first of second submission of this comparison.
  194. *
  195. * @return Number of non EOF tokens int the submission, excluding base code matches.
  196. */
  197. public final int getNumberOfCountingTokens(Submission submission) {
  198. int numberOfTokens = submission.getNumberOfTokens() - submission.getFiles().size();
  199. if (submission == firstSubmission && firstBaseCodeMatches != null) {
  200. numberOfTokens -= firstBaseCodeMatches.getNumberOfMatchedTokens();
  201. } else if (submission == secondSubmission && secondBaseCodeMatches != null) {
  202. numberOfTokens -= secondBaseCodeMatches.getNumberOfMatchedTokens();
  203. }
  204. return numberOfTokens;
  205. }
  206. /**
  207. * @return Similarity of the first submission to the basecode in percent rounded down to the nearest tenth.
  208. */
  209. public final float basecodeSimilarityOfFirst() {
  210. return ((int) (firstBasecodeSimilarity() * ROUNDING_FACTOR)) / (float) ROUNDING_FACTOR;
  211. }
  212. /**
  213. * @return Similarity of the second submission to the basecode in percent rounded down to the nearest tenth.
  214. */
  215. public final float basecodeSimilarityOfSecond() {
  216. return ((int) (secondBasecodeSimilarity() * ROUNDING_FACTOR)) / (float) ROUNDING_FACTOR;
  217. }
  218. /**
  219. * Sets the base code matches of the second submissions.
  220. */
  221. public void setFirstBaseCodeMatches(JPlagComparison firstBaseCodeMatches) {
  222. this.firstBaseCodeMatches = firstBaseCodeMatches;
  223. }
  224. /**
  225. * Sets the base code matches of the second submissions.
  226. */
  227. public void setSecondBaseCodeMatches(JPlagComparison secondBaseCodeMatches) {
  228. this.secondBaseCodeMatches = secondBaseCodeMatches;
  229. }
  230. /**
  231. * Creates a permutation for the matches based on the indices of the matched token groups.
  232. * @param useFirst determines whether the start of the first or second submission is compared.
  233. * @return the permutation indices.
  234. */
  235. public final List<Integer> sort_permutation(boolean useFirst) {
  236. List<Integer> indices = new ArrayList<>(matches.size());
  237. IntStream.range(0, matches.size()).forEach(index -> indices.add(index));
  238. Comparator<Integer> comparator = (Integer i, Integer j) -> {
  239. return Integer.compare(selectStartof(i, useFirst), selectStartof(j, useFirst));
  240. };
  241. Collections.sort(indices, comparator);
  242. return indices;
  243. }
  244. private int selectStartof(Integer index, boolean useFirst) {
  245. Match match = matches.get(index);
  246. return useFirst ? match.getStartOfFirst() : match.getStartOfSecond();
  247. }
  248. @Override
  249. public String toString() {
  250. return firstSubmission.getName() + " <-> " + secondSubmission.getName();
  251. }
  252. private final float firstBasecodeSimilarity() {
  253. float sa = firstSubmission.getNumberOfTokens() - firstSubmission.getFiles().size();
  254. return firstBaseCodeMatches.getNumberOfMatchedTokens() * 100 / sa;
  255. }
  256. private final float secondBasecodeSimilarity() {
  257. float sb = secondSubmission.getNumberOfTokens() - secondSubmission.getFiles().size();
  258. return secondBaseCodeMatches.getNumberOfMatchedTokens() * 100 / sb;
  259. }
  260. }