output_matching.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { Decimal } from "decimal.js";
  2. import { InputAssessment } from "../../util/input_assessment";
  3. import { OutputTest } from "../../util/outputTest";
  4. import { Config } from "../../util/config";
  5. import { levenshteinDistance } from "../../util/utils";
  6. import { OutputAssessmentResult } from "./assessment_result";
  7. import * as TypeParser from "./../../typeSystem/parsers";
  8. import * as LocalizedStringsService from "../../services/localizedStringsService";
  9. import * as OutputResult from "./output_result";
  10. const LocalizedStrings = LocalizedStringsService.getInstance();
  11. export class OutputMatching {
  12. static get NUM_REGEX () {
  13. return /^[+-]?([0-9]+([.][0-9]*)?(e[+-]?[0-9]+)?)$/;
  14. }
  15. static get NUM_IN_STRING_REGEX () {
  16. return /[+-]?([0-9]+([.][0-9]*)?(e[+-]?[0-9]+)?)/g;
  17. }
  18. static get BOOLEAN_REGEX () {
  19. const str = `^(${LocalizedStrings.getUI(
  20. "logic_value_true"
  21. )}|${LocalizedStrings.getUI("logic_value_false")})$`;
  22. return new RegExp(str);
  23. }
  24. static get BOOLEAN_IN_STRING_REGEX () {
  25. const str = `(${LocalizedStrings.getUI(
  26. "logic_value_true"
  27. )}|${LocalizedStrings.getUI("logic_value_false")})`;
  28. return new RegExp(str, "g");
  29. }
  30. constructor (program, input_list, expected_output, test_name, tag = null) {
  31. this.program = program;
  32. this.name = test_name;
  33. this.input_list = input_list;
  34. this.expected_output = expected_output;
  35. this.tag = tag;
  36. }
  37. eval () {
  38. const input = new InputAssessment(this.input_list);
  39. const gen_output = new OutputTest();
  40. this.program.registerInput(input);
  41. this.program.registerOutput(gen_output);
  42. const start_time = Date.now();
  43. return this.program
  44. .interpretAST()
  45. .then((sto) => {
  46. const final_time = Date.now() - start_time;
  47. if (input.isInputAvailable()) {
  48. const error = this.getErrorMessage(
  49. "test_case_few_reads",
  50. this.name + 1
  51. );
  52. return new OutputAssessmentResult(
  53. this.name,
  54. 1,
  55. input.input_list,
  56. null,
  57. sto,
  58. final_time,
  59. error,
  60. error.id
  61. );
  62. }
  63. const result = gen_output.list.map((g_out, i) => {
  64. if (i >= this.expected_output.length) {
  65. return new OutputResult.OutputMatchResult(
  66. null,
  67. g_out,
  68. 0,
  69. this.getPotentialOutputType(g_out)
  70. );
  71. }
  72. return this.outputMatch(g_out, this.expected_output[i]);
  73. }, this);
  74. if (this.expected_output.length > gen_output.list.length) {
  75. console.log(
  76. "Saída insuficientes!",
  77. this.expected_output.length,
  78. gen_output.list.length
  79. );
  80. for (
  81. let i = gen_output.list.length;
  82. i < this.expected_output.length;
  83. ++i
  84. ) {
  85. const e_out = this.expected_output[i];
  86. result.push(
  87. new OutputResult.OutputMatchResult(
  88. e_out,
  89. null,
  90. 0,
  91. this.getPotentialOutputType(e_out)
  92. )
  93. );
  94. }
  95. } else if (
  96. this.expected_output.length == 0 &&
  97. this.expected_output.length == gen_output.list.length
  98. ) {
  99. // no output expected....
  100. result.push(new OutputResult.OutputMatchResult("", "", 1, "string"));
  101. }
  102. return new OutputAssessmentResult(
  103. this.name,
  104. 0,
  105. input.input_list,
  106. result,
  107. sto,
  108. final_time
  109. );
  110. })
  111. .catch((error) => {
  112. return new OutputAssessmentResult(
  113. this.name,
  114. 1,
  115. input.input_list,
  116. null,
  117. null,
  118. null,
  119. this.getErrorMessage(
  120. "test_case_exception",
  121. this.name + 1,
  122. error.message
  123. ),
  124. error.id
  125. );
  126. });
  127. }
  128. getPotentialOutputType (output) {
  129. if (OutputMatching.NUM_REGEX.test(output)) {
  130. return "number";
  131. } else if (OutputMatching.BOOLEAN_REGEX.test(output)) {
  132. return "bool";
  133. } else {
  134. return "string";
  135. }
  136. }
  137. outputMatch (g_output, e_output) {
  138. if (OutputMatching.NUM_REGEX.test(e_output)) {
  139. if (!OutputMatching.NUM_REGEX.test(g_output)) {
  140. return this.checkStrings(g_output, e_output);
  141. }
  142. const g_num = new Decimal(g_output);
  143. const e_num = new Decimal(e_output);
  144. return this.checkNumbers(g_num, e_num);
  145. } else if (OutputMatching.BOOLEAN_REGEX.test(e_output)) {
  146. if (!OutputMatching.BOOLEAN_REGEX.test(g_output)) {
  147. return this.checkStrings(g_output, e_output);
  148. }
  149. const g_bool = TypeParser.toBool(g_output);
  150. const e_bool = TypeParser.toBool(e_output);
  151. return this.checkBoolean(g_bool, e_bool);
  152. } else {
  153. return this.checkStrings(g_output, e_output);
  154. }
  155. }
  156. checkNumbers (g_num, e_num) {
  157. const decimalPlaces = Math.min(e_num.dp(), Config.decimalPlaces);
  158. g_num = new Decimal(g_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  159. e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  160. const result = g_num.eq(e_num);
  161. const grade = result ? 1 : 0;
  162. return OutputResult.createNumberResult(
  163. e_num.toNumber(),
  164. g_num.toNumber(),
  165. grade
  166. );
  167. }
  168. checkBoolean (g_bool, e_bool) {
  169. const grade = g_bool == e_bool ? 1 : 0;
  170. const g_bool_text = TypeParser.convertBoolToString(g_bool);
  171. const e_bool_text = TypeParser.convertBoolToString(e_bool);
  172. return OutputResult.createBoolResult(e_bool_text, g_bool_text, grade);
  173. }
  174. checkStrings (g_output, e_ouput) {
  175. const assessmentList = [];
  176. let e_output_clean = e_ouput.trim();
  177. let g_output_clean = g_output.trim();
  178. if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
  179. const expected_numbers = e_ouput.match(
  180. OutputMatching.NUM_IN_STRING_REGEX
  181. );
  182. const generated_numbers =
  183. g_output.match(OutputMatching.NUM_IN_STRING_REGEX) || [];
  184. const result = generated_numbers.map((val, i) => {
  185. if (i >= expected_numbers.length) {
  186. return OutputResult.createNumberResult(null, val, 0);
  187. }
  188. const g_val = new Decimal(val);
  189. const e_val = new Decimal(expected_numbers[i]);
  190. return this.checkNumbers(g_val, e_val);
  191. }, this);
  192. if (expected_numbers.length > generated_numbers.length) {
  193. for (
  194. let i = generated_numbers.length;
  195. i < expected_numbers.length;
  196. ++i
  197. ) {
  198. result.push(
  199. OutputResult.createNumberResult(expected_numbers[i], null, 0)
  200. );
  201. }
  202. }
  203. e_output_clean = e_output_clean.replace(
  204. OutputMatching.NUM_IN_STRING_REGEX,
  205. ""
  206. );
  207. g_output_clean = g_output_clean.replace(
  208. OutputMatching.NUM_IN_STRING_REGEX,
  209. ""
  210. );
  211. const numberGrade =
  212. result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  213. assessmentList.push(numberGrade);
  214. }
  215. if (OutputMatching.BOOLEAN_IN_STRING_REGEX.test(e_ouput)) {
  216. const expected_bools = e_ouput.match(
  217. OutputMatching.BOOLEAN_IN_STRING_REGEX
  218. );
  219. const generated_bools =
  220. g_output.match(OutputMatching.BOOLEAN_IN_STRING_REGEX) || [];
  221. const result = generated_bools.map((val, i) => {
  222. if (i >= expected_bools.length) {
  223. return OutputResult.createBoolResult(null, val, 0);
  224. }
  225. const g_bool = TypeParser.toBool(val);
  226. const e_bool = TypeParser.toBool(expected_bools[i]);
  227. return this.checkBoolean(g_bool, e_bool);
  228. }, this);
  229. if (expected_bools.length > generated_bools.length) {
  230. for (let i = generated_bools.length; i < expected_bools.length; ++i) {
  231. result.push(
  232. OutputResult.createBoolResult(expected_bools[i], null, 0)
  233. );
  234. }
  235. }
  236. e_output_clean = e_output_clean.replace(
  237. OutputMatching.BOOLEAN_IN_STRING_REGEX,
  238. ""
  239. );
  240. g_output_clean = g_output_clean.replace(
  241. OutputMatching.BOOLEAN_IN_STRING_REGEX,
  242. ""
  243. );
  244. const boolGrade =
  245. result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  246. assessmentList.push(boolGrade);
  247. }
  248. const dist = levenshteinDistance(g_output_clean, e_output_clean);
  249. let gradeDiff =
  250. Math.max(0, e_output_clean.length - dist) / e_output_clean.length;
  251. gradeDiff = Number.isNaN(gradeDiff) ? 0 : gradeDiff;
  252. const assessment_size = assessmentList.length + 1;
  253. const gradeAcc = assessmentList.reduce(
  254. (prev, val) => prev + val / assessment_size,
  255. 0
  256. );
  257. const finalGrade = 1 * (gradeDiff / assessment_size + gradeAcc);
  258. return OutputResult.createStringResult(e_ouput, g_output, finalGrade);
  259. }
  260. getErrorMessage (errorID, ...args) {
  261. return LocalizedStrings.getError(errorID, args);
  262. }
  263. }