iassignHelpers.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. import { parserCodeVisual } from "./codeParser";
  9. function parseActivityData (data) {
  10. let algorithm_in_ilm = null;
  11. if (data.split("\n::algorithm::")[1]) {
  12. algorithm_in_ilm = data.split("\n::algorithm::")[1].split("\n::logs::")[0];
  13. const logs = data.split("\n::algorithm::")[1].split("\n::logs::")[1];
  14. if (logs != null) {
  15. parseLogs(logs);
  16. }
  17. }
  18. let content;
  19. try {
  20. content = JSON.parse(data.split("\n::algorithm::")[0]);
  21. content["algorithm_in_ilm"] = algorithm_in_ilm;
  22. } catch (e) {
  23. console.error(e);
  24. return Maybe.none();
  25. }
  26. return Maybe.some(content);
  27. }
  28. function configAuxiliar (obj_config) {
  29. return new Map(
  30. Object
  31. .keys(obj_config)
  32. .map(
  33. key => [key, obj_config[key]]
  34. )
  35. )
  36. }
  37. function setActivityConfig (config_json) {
  38. ivprogCore.Config.activity_commands = configAuxiliar(config_json.commands);
  39. ivprogCore.Config.activity_functions = configAuxiliar(config_json.functions);
  40. ivprogCore.Config.activity_datatypes = configAuxiliar(config_json.datatypes);
  41. ivprogCore.Config.activity_filter = configAuxiliar(config_json.filter);
  42. ivprogCore.Config.activity_programming_type = configAuxiliar(config_json.programming);
  43. }
  44. function setPreviousAlgorithm (code) {
  45. var code_obj = null;
  46. try {
  47. code_obj = ivprogCore.parseCode(code)
  48. parserCodeVisual(code_obj)
  49. }
  50. catch(e) {
  51. showInvalidData()
  52. console.log(e)
  53. return
  54. }
  55. }
  56. export function prepareActivityToStudentHelperJSON (ilm_cont) {
  57. setTestCases(ilm_cont.test_cases)
  58. setActivityConfig(ilm_cont.settings)
  59. if (ilm_cont.code)
  60. setPreviousAlgorithm(ilm_cont.code)
  61. else
  62. if (ilm_cont.algorithm)
  63. setPreviousAlgorithm(ilm_cont.algorithm)
  64. }
  65. export function prepareActivityToStudentHelper (ilm_cont) {
  66. const maybe_content = parseActivityData(ilm_cont);
  67. return maybe_content.map((content) => {
  68. const testCases = content.testcases;
  69. setTestCases(testCases);
  70. let prog_type = null;
  71. if (content.settings_programming_type) {
  72. prog_type = content.settings_programming_type[0].value;
  73. } else {
  74. prog_type = "visual";
  75. }
  76. return {
  77. settingsProgrammingType: prog_type,
  78. settingsDataTypes: content.settings_data_types,
  79. settingsCommands: content.settings_commands,
  80. settingsFunctions: content.settings_functions,
  81. algorithmInIlm: content.algorithm_in_ilm,
  82. settingsFilter: content.settings_filter,
  83. };
  84. });
  85. }
  86. function compareArray (a, b) {
  87. for (let i = 0; i < a.length; ++i) {
  88. const elementA = a[i];
  89. const elementB = b[i];
  90. if (elementA != elementB) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. function compareTestcases (original, student) {
  97. if (original.length != student.length) {
  98. return false;
  99. }
  100. for (let i = 0; i < original.length; ++i) {
  101. const elementO = original[i];
  102. const elementS = student[i];
  103. if (!compareArray(elementO.input, elementS.input)) {
  104. return false;
  105. }
  106. if (!compareArray(elementO.output, elementS.output)) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. export function autoEval (originalData, callback) {
  113. const code = generate();
  114. //console.debug(code);
  115. const original = parseActivityData(originalData).getOrElse(undefined);
  116. if (original == null) {
  117. alert("iAssign did not provide the original activity data!");
  118. return callback(null);
  119. }
  120. if (code == null) {
  121. return callback(-1); // @FeedbackConvention Casos de teste vazios
  122. } else {
  123. if (!compareTestcases(original.testcases, getTestCases())) {
  124. return callback(-2); // @FeedbackConvention Casos de teste alterados pelo aluno
  125. }
  126. try {
  127. const ast_code = SemanticAnalyser.analyseFromSource(code);
  128. const con = new TestConsole([]);
  129. const autoAssessment = new IVProgAssessment(
  130. ast_code,
  131. original.testcases,
  132. con
  133. );
  134. autoAssessment
  135. .runTest()
  136. .then((grade) => callback(grade))
  137. .catch((err) => {
  138. console.log(err);
  139. callback(-5); // @FeedbackConvention Falha na execução
  140. });
  141. } catch (e) {
  142. console.error(e);
  143. callback(-5);
  144. }
  145. }
  146. }