iassignHelpers.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { setTestCases, getTestCases } from "../visualUI/functions";
  2. import { generate } from "../visualUI/code_generator";
  3. import { IVProgAssessment } from "../assessment/ivprogAssessment";
  4. import { TestConsole } from "./testConsole";
  5. import { parseLogs } from "./../services/userLog";
  6. import { SemanticAnalyser } from "../processor/semantic/semanticAnalyser";
  7. import { Maybe } from "./maybe";
  8. function parseActivityData (data) {
  9. let algorithm_in_ilm = null;
  10. if (data.split("\n::algorithm::")[1]) {
  11. algorithm_in_ilm = data.split("\n::algorithm::")[1].split("\n::logs::")[0];
  12. const logs = data.split("\n::algorithm::")[1].split("\n::logs::")[1];
  13. if (logs != null) {
  14. parseLogs(logs);
  15. }
  16. }
  17. let content;
  18. try {
  19. content = JSON.parse(data.split("\n::algorithm::")[0]);
  20. content["algorithm_in_ilm"] = algorithm_in_ilm;
  21. } catch (e) {
  22. console.error(e);
  23. return Maybe.none();
  24. }
  25. return Maybe.some(content);
  26. }
  27. export function prepareActivityToStudentHelper (ilm_cont) {
  28. const maybe_content = parseActivityData(ilm_cont);
  29. return maybe_content.map((content) => {
  30. const testCases = content.testcases;
  31. setTestCases(testCases);
  32. let prog_type = null;
  33. if (content.settings_programming_type) {
  34. prog_type = content.settings_programming_type[0].value;
  35. } else {
  36. prog_type = "visual";
  37. }
  38. return {
  39. settingsProgrammingType: prog_type,
  40. settingsDataTypes: content.settings_data_types,
  41. settingsCommands: content.settings_commands,
  42. settingsFunctions: content.settings_functions,
  43. algorithmInIlm: content.algorithm_in_ilm,
  44. settingsFilter: content.settings_filter,
  45. };
  46. });
  47. }
  48. function compareArray (a, b) {
  49. for (let i = 0; i < a.length; ++i) {
  50. const elementA = a[i];
  51. const elementB = b[i];
  52. if (elementA != elementB) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. function compareTestcases (original, student) {
  59. if (original.length != student.length) {
  60. return false;
  61. }
  62. for (let i = 0; i < original.length; ++i) {
  63. const elementO = original[i];
  64. const elementS = student[i];
  65. if (!compareArray(elementO.input, elementS.input)) {
  66. return false;
  67. }
  68. if (!compareArray(elementO.output, elementS.output)) {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. export function autoEval (originalData, callback) {
  75. const code = generate();
  76. //console.debug(code);
  77. const original = parseActivityData(originalData).getOrElse(undefined);
  78. if (original == null) {
  79. alert("iAssign did not provide the original activity data!");
  80. return callback(null);
  81. }
  82. if (code == null) {
  83. return callback(-1); // @FeedbackConvention Casos de teste vazios
  84. } else {
  85. if (!compareTestcases(original.testcases, getTestCases())) {
  86. return callback(-2); // @FeedbackConvention Casos de teste alterados pelo aluno
  87. }
  88. try {
  89. const ast_code = SemanticAnalyser.analyseFromSource(code);
  90. const con = new TestConsole([]);
  91. const autoAssessment = new IVProgAssessment(
  92. ast_code,
  93. original.testcases,
  94. con
  95. );
  96. autoAssessment
  97. .runTest()
  98. .then((grade) => callback(grade))
  99. .catch((err) => {
  100. console.log(err);
  101. callback(-5); // @FeedbackConvention Falha na execução
  102. });
  103. } catch (e) {
  104. console.error(e);
  105. callback(-5);
  106. }
  107. }
  108. }