iassignHelpers.js 4.6 KB

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