iassignHelpers.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { setTestCases, setExerciseType, 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. const exerciseType = content.exercisetype;
  33. setExerciseType(exerciseType);
  34. let prog_type = null;
  35. if (content.settings_programming_type) {
  36. prog_type = content.settings_programming_type[0].value;
  37. } else {
  38. prog_type = "visual";
  39. }
  40. return {
  41. settingsProgrammingType: prog_type,
  42. settingsDataTypes: content.settings_data_types,
  43. settingsCommands: content.settings_commands,
  44. settingsFunctions: content.settings_functions,
  45. algorithmInIlm: content.algorithm_in_ilm,
  46. settingsFilter: content.settings_filter,
  47. };
  48. });
  49. }
  50. function compareArray (a, b) {
  51. for (let i = 0; i < a.length; ++i) {
  52. const elementA = a[i];
  53. const elementB = b[i];
  54. if (elementA != elementB) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. function compareTestcases (original, student) {
  61. if (original.length != student.length) {
  62. return false;
  63. }
  64. for (let i = 0; i < original.length; ++i) {
  65. const elementO = original[i];
  66. const elementS = student[i];
  67. if (!compareArray(elementO.input, elementS.input)) {
  68. return false;
  69. }
  70. if (!compareArray(elementO.output, elementS.output)) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. export function autoEval (originalData, callback) {
  77. const code = generate();
  78. //console.debug(code);
  79. const original = parseActivityData(originalData).getOrElse(undefined);
  80. if (original == null) {
  81. alert("iAssign did not provide the original activity data!");
  82. return callback(null);
  83. }
  84. if (code == null) {
  85. return callback(-1); // @FeedbackConvention Casos de teste vazios
  86. } else {
  87. if (!compareTestcases(original.testcases, getTestCases())) {
  88. return callback(-2); // @FeedbackConvention Casos de teste alterados pelo aluno
  89. }
  90. try {
  91. const ast_code = SemanticAnalyser.analyseFromSource(code);
  92. const con = new TestConsole([]);
  93. const autoAssessment = new IVProgAssessment(
  94. ast_code,
  95. original.exerciseType,
  96. original.testcases,
  97. con
  98. );
  99. autoAssessment
  100. .runTest()
  101. .then((grade) => callback(grade))
  102. .catch((err) => {
  103. console.log(err);
  104. callback(-5); // @FeedbackConvention Falha na execução
  105. });
  106. } catch (e) {
  107. console.error(e);
  108. callback(-5);
  109. }
  110. }
  111. }