1
0

output_matching.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})$`;
  20. return new RegExp(str);
  21. }
  22. static get BOOLEAN_IN_STRING_REGEX () {
  23. const str = `(${LocalizedStrings.getUI("logic_value_true")}|${LocalizedStrings.getUI("logic_value_false")})`;
  24. return new RegExp(str, 'g');
  25. }
  26. constructor (program, input_list, expected_output, test_name) {
  27. this.program = program;
  28. this.name = test_name;
  29. this.input_list = input_list;
  30. this.expected_output = expected_output;
  31. }
  32. eval () {
  33. const refThis = this;
  34. const input = new InputAssessment(this.input_list);
  35. const gen_output = new OutputTest();
  36. this.program.registerInput(input);
  37. this.program.registerOutput(gen_output);
  38. const start_time = Date.now();
  39. return this.program.interpretAST().then( sto => {
  40. const final_time = Date.now() - start_time;
  41. if(input.isInputAvailable()) {
  42. return new OutputAssessmentResult(this.name, 1, input.input_list,
  43. null, sto, final_time, refThis.getErrorMessage('test_case_few_reads', this.name+1))
  44. }
  45. const result = gen_output.list.map((g_out, i) => {
  46. if(i >= this.expected_output.length) {
  47. return new OutputResult.OutputMatchResult(null, g_out, 0, this.getPotentialOutputType(g_out));
  48. }
  49. return this.outputMatch(g_out, this.expected_output[i]);
  50. }, this);
  51. if(this.expected_output.length > gen_output.list.length) {
  52. console.log("Saída insuficientes!",this.expected_output.length,gen_output.list.length);
  53. for(let i = gen_output.list.length; i < this.expected_output.length; ++i) {
  54. const e_out = this.expected_output[i];
  55. result.push(new OutputResult.OutputMatchResult(e_out, null, 0, this.getPotentialOutputType(e_out)));
  56. }
  57. } else if (this.expected_output.length == 0 && this.expected_output.length == gen_output.list.length) {
  58. // no output expected....
  59. result.push(new OutputResult.OutputMatchResult("", "", 1, "string"));
  60. }
  61. return new OutputAssessmentResult(this.name, 0, input.input_list, result, sto, final_time);
  62. }).catch(error => {
  63. return new OutputAssessmentResult(this.name, 1, input.input_list, null, null,
  64. null, refThis.getErrorMessage('test_case_exception', this.name + 1, error.message))
  65. });
  66. }
  67. getPotentialOutputType (output) {
  68. if(OutputMatching.NUM_REGEX.test(output)) {
  69. return "number";
  70. } else if (OutputMatching.BOOLEAN_REGEX.test(output)) {
  71. return "bool";
  72. } else {
  73. return "string";
  74. }
  75. }
  76. outputMatch (g_output, e_output) {
  77. if(OutputMatching.NUM_REGEX.test(e_output)) {
  78. if(!OutputMatching.NUM_REGEX.test(g_output)) {
  79. return this.checkStrings(g_output, e_output);
  80. }
  81. const g_num = new Decimal(g_output);
  82. const e_num = new Decimal(e_output);
  83. return this.checkNumbers(g_num, e_num);
  84. } else if (OutputMatching.BOOLEAN_REGEX.test(e_output)) {
  85. if (!OutputMatching.BOOLEAN_REGEX.test(g_output)) {
  86. return this.checkStrings(g_output, e_output)
  87. }
  88. const g_bool = TypeParser.toBool(g_output);
  89. const e_bool = TypeParser.toBool(e_output);
  90. return this.checkBoolean(g_bool, e_bool);
  91. } else {
  92. return this.checkStrings(g_output, e_output);
  93. }
  94. }
  95. checkNumbers (g_num, e_num) {
  96. const decimalPlaces = Math.min(e_num.dp(), Config.decimalPlaces);
  97. g_num = new Decimal(g_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  98. e_num = new Decimal(e_num.toFixed(decimalPlaces, Decimal.ROUND_FLOOR));
  99. const result = g_num.eq(e_num);
  100. const grade = result ? 1 : 0;
  101. return OutputResult.createNumberResult(e_num.toNumber(), g_num.toNumber(), grade);
  102. }
  103. checkBoolean (g_bool, e_bool) {
  104. const grade = g_bool == e_bool ? 1 : 0;
  105. const g_bool_text = TypeParser.convertBoolToString(g_bool);
  106. const e_bool_text = TypeParser.convertBoolToString(e_bool);
  107. return OutputResult.createBoolResult(e_bool_text, g_bool_text, grade);
  108. }
  109. checkStrings (g_output, e_ouput) {
  110. const assessmentList = []
  111. let e_output_clean = e_ouput.trim();
  112. let g_output_clean = g_output.trim();
  113. if (OutputMatching.NUM_IN_STRING_REGEX.test(e_ouput)) {
  114. const expected_numbers = e_ouput.match(OutputMatching.NUM_IN_STRING_REGEX);
  115. const generated_numbers = g_output.match(OutputMatching.NUM_IN_STRING_REGEX) || [];
  116. const result = generated_numbers.map((val, i) => {
  117. if(i >= expected_numbers.length) {
  118. return OutputResult.createNumberResult(null, val, 0);
  119. }
  120. const g_val = new Decimal(val)
  121. const e_val = new Decimal(expected_numbers[i]);
  122. return this.checkNumbers(g_val, e_val);
  123. }, this);
  124. if(expected_numbers.length > generated_numbers.length) {
  125. for(let i = generated_numbers.length; i < expected_numbers.length; ++i) {
  126. result.push(OutputResult.createNumberResult(expected_numbers[i], null, 0));
  127. }
  128. }
  129. e_output_clean = e_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '');
  130. g_output_clean = g_output_clean.replace(OutputMatching.NUM_IN_STRING_REGEX, '');
  131. const numberGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  132. assessmentList.push(numberGrade);
  133. }
  134. if(OutputMatching.BOOLEAN_IN_STRING_REGEX.test(e_ouput)) {
  135. const expected_bools = e_ouput.match(OutputMatching.BOOLEAN_IN_STRING_REGEX);
  136. const generated_bools = g_output.match(OutputMatching.BOOLEAN_IN_STRING_REGEX) || [];
  137. const result = generated_bools.map((val, i) => {
  138. if(i >= expected_bools.length) {
  139. return OutputResult.createBoolResult(null, val, 0);
  140. }
  141. const g_bool = TypeParser.toBool(val);
  142. const e_bool = TypeParser.toBool(expected_bools[i]);
  143. return this.checkBoolean(g_bool, e_bool );
  144. }, this);
  145. if(expected_bools.length > generated_bools.length) {
  146. for(let i = generated_bools.length; i < expected_bools.length; ++i) {
  147. result.push(OutputResult.createBoolResult(expected_bools[i], null, 0));
  148. }
  149. }
  150. e_output_clean = e_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '');
  151. g_output_clean = g_output_clean.replace(OutputMatching.BOOLEAN_IN_STRING_REGEX, '');
  152. const boolGrade = result.reduce((prev, r) => prev + r.grade, 0) / result.length;
  153. assessmentList.push(boolGrade);
  154. }
  155. const dist = levenshteinDistance(g_output_clean, e_output_clean);
  156. let gradeDiff = Math.max(0, e_output_clean.length - dist)/e_output_clean.length;
  157. gradeDiff = Number.isNaN(gradeDiff) ? 0 : gradeDiff;
  158. const assessment_size = assessmentList.length + 1;
  159. const gradeAcc = assessmentList.reduce((prev, val) => prev + val/assessment_size, 0);
  160. const finalGrade = 1 * (gradeDiff/assessment_size + gradeAcc);
  161. return OutputResult.createStringResult(e_ouput, g_output, finalGrade);
  162. }
  163. getErrorMessage (errorID, ...args) {
  164. return LocalizedStrings.getError(errorID, args);
  165. }
  166. }