output_matching.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. this.tag,
  60. error,
  61. error.id
  62. );
  63. }
  64. const result = gen_output.list.map((g_out, i) => {
  65. if (i >= this.expected_output.length) {
  66. return new OutputResult.OutputMatchResult(
  67. null,
  68. g_out,
  69. 0,
  70. this.getPotentialOutputType(g_out)
  71. );
  72. }
  73. return this.outputMatch(g_out, this.expected_output[i]);
  74. }, this);
  75. if (this.expected_output.length > gen_output.list.length) {
  76. console.log(
  77. "Saída insuficientes!",
  78. this.expected_output.length,
  79. gen_output.list.length
  80. );
  81. for (
  82. let i = gen_output.list.length;
  83. i < this.expected_output.length;
  84. ++i
  85. ) {
  86. const e_out = this.expected_output[i];
  87. result.push(
  88. new OutputResult.OutputMatchResult(
  89. e_out,
  90. null,
  91. 0,
  92. this.getPotentialOutputType(e_out)
  93. )
  94. );
  95. }
  96. } else if (
  97. this.expected_output.length == 0 &&
  98. this.expected_output.length == gen_output.list.length
  99. ) {
  100. // no output expected....
  101. result.push(new OutputResult.OutputMatchResult("", "", 1, "string"));
  102. }
  103. return new OutputAssessmentResult(
  104. this.name,
  105. 0,
  106. input.input_list,
  107. result,
  108. sto,
  109. final_time,
  110. this.tag
  111. );
  112. })
  113. .catch((error) => {
  114. return new OutputAssessmentResult(
  115. this.name,
  116. 1,
  117. input.input_list,
  118. null,
  119. null,
  120. null,
  121. this.tags,
  122. this.getErrorMessage(
  123. "test_case_exception",
  124. this.name + 1,
  125. error.message
  126. ),
  127. error.id
  128. );
  129. });
  130. }
  131. getPotentialOutputType (output) {
  132. if (OutputMatching.NUM_REGEX.test(output)) {
  133. return "number";
  134. } else if (OutputMatching.BOOLEAN_REGEX.test(output)) {
  135. return "bool";
  136. } else {
  137. return "string";
  138. }
  139. }
  140. outputMatch (g_output, e_output) {
  141. if (OutputMatching.NUM_REGEX.test(e_output)) {
  142. if (!OutputMatching.NUM_REGEX.test(g_output)) {
  143. return this.checkStrings(g_output, e_output);
  144. }
  145. const g_num = new Decimal(g_output);
  146. const e_num = new Decimal(e_output);
  147. return this.checkNumbers(g_num, e_num);
  148. } else if (OutputMatching.BOOLEAN_REGEX.test(e_output)) {
  149. if (!OutputMatching.BOOLEAN_REGEX.test(g_output)) {
  150. return this.checkStrings(g_output, e_output);
  151. }
  152. const g_bool = TypeParser.toBool(g_output);
  153. const e_bool = TypeParser.toBool(e_output);
  154. return this.checkBoolean(g_bool, e_bool);
  155. } else {
  156. return this.checkStrings(g_output, e_output);
  157. }
  158. }
  159. checkNumbers (g_num, e_num) {
  160. const decimalPlaces = Math.min(e_num.dp(), Config.decimalPlaces);
  161. g_num = new Decimal(g_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  162. e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  163. const result = g_num.eq(e_num);
  164. const grade = result ? 1 : 0;
  165. return OutputResult.createNumberResult(
  166. e_num.toNumber(),
  167. g_num.toNumber(),
  168. grade
  169. );
  170. }
  171. checkBoolean (g_bool, e_bool) {
  172. const grade = g_bool == e_bool ? 1 : 0;
  173. const g_bool_text = TypeParser.convertBoolToString(g_bool);
  174. const e_bool_text = TypeParser.convertBoolToString(e_bool);
  175. return OutputResult.createBoolResult(e_bool_text, g_bool_text, grade);
  176. }
  177. checkStrings (g_output, e_ouput) {
  178. const assessmentList = [];
  179. let e_output_clean = e_ouput.trim();
  180. let g_output_clean = g_output.trim();
  181. if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
  182. const expected_numbers = e_ouput.match(
  183. OutputMatching.NUM_IN_STRING_REGEX
  184. );
  185. const generated_numbers =
  186. g_output.match(OutputMatching.NUM_IN_STRING_REGEX) || [];
  187. const result = generated_numbers.map((val, i) => {
  188. if (i >= expected_numbers.length) {
  189. return OutputResult.createNumberResult(null, val, 0);
  190. }
  191. const g_val = new Decimal(val);
  192. const e_val = new Decimal(expected_numbers[i]);
  193. return this.checkNumbers(g_val, e_val);
  194. }, this);
  195. if (expected_numbers.length > generated_numbers.length) {
  196. for (
  197. let i = generated_numbers.length;
  198. i < expected_numbers.length;
  199. ++i
  200. ) {
  201. result.push(
  202. OutputResult.createNumberResult(expected_numbers[i], null, 0)
  203. );
  204. }
  205. }
  206. e_output_clean = e_output_clean.replace(
  207. OutputMatching.NUM_IN_STRING_REGEX,
  208. ""
  209. );
  210. g_output_clean = g_output_clean.replace(
  211. OutputMatching.NUM_IN_STRING_REGEX,
  212. ""
  213. );
  214. const numberGrade =
  215. result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  216. assessmentList.push(numberGrade);
  217. }
  218. if (OutputMatching.BOOLEAN_IN_STRING_REGEX.test(e_ouput)) {
  219. const expected_bools = e_ouput.match(
  220. OutputMatching.BOOLEAN_IN_STRING_REGEX
  221. );
  222. const generated_bools =
  223. g_output.match(OutputMatching.BOOLEAN_IN_STRING_REGEX) || [];
  224. const result = generated_bools.map((val, i) => {
  225. if (i >= expected_bools.length) {
  226. return OutputResult.createBoolResult(null, val, 0);
  227. }
  228. const g_bool = TypeParser.toBool(val);
  229. const e_bool = TypeParser.toBool(expected_bools[i]);
  230. return this.checkBoolean(g_bool, e_bool);
  231. }, this);
  232. if (expected_bools.length > generated_bools.length) {
  233. for (let i = generated_bools.length; i < expected_bools.length; ++i) {
  234. result.push(
  235. OutputResult.createBoolResult(expected_bools[i], null, 0)
  236. );
  237. }
  238. }
  239. e_output_clean = e_output_clean.replace(
  240. OutputMatching.BOOLEAN_IN_STRING_REGEX,
  241. ""
  242. );
  243. g_output_clean = g_output_clean.replace(
  244. OutputMatching.BOOLEAN_IN_STRING_REGEX,
  245. ""
  246. );
  247. const boolGrade =
  248. result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  249. assessmentList.push(boolGrade);
  250. }
  251. const dist = levenshteinDistance(g_output_clean, e_output_clean);
  252. let gradeDiff =
  253. Math.max(0, e_output_clean.length - dist) / e_output_clean.length;
  254. gradeDiff = Number.isNaN(gradeDiff) ? 0 : gradeDiff;
  255. const assessment_size = assessmentList.length + 1;
  256. const gradeAcc = assessmentList.reduce(
  257. (prev, val) => prev + val / assessment_size,
  258. 0
  259. );
  260. const finalGrade = 1 * (gradeDiff / assessment_size + gradeAcc);
  261. return OutputResult.createStringResult(e_ouput, g_output, finalGrade);
  262. }
  263. getErrorMessage (errorID, ...args) {
  264. return LocalizedStrings.getError(errorID, args);
  265. }
  266. }