iassignHelpers.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. console.log('conteúdo: ')
  46. console.log(code)
  47. var code_obj = null;
  48. try {
  49. code_obj = ivprogCore.parseCode(code)
  50. parserCodeVisual(code_obj)
  51. }
  52. catch(e) {
  53. showInvalidData()
  54. console.log(e)
  55. return
  56. }
  57. }
  58. export function prepareActivityToStudentHelperJSON (ilm_cont) {
  59. setTestCases(ilm_cont.test_cases)
  60. setActivityConfig(ilm_cont.settings)
  61. if (ilm_cont.code)
  62. setPreviousAlgorithm(ilm_cont.code)
  63. else
  64. if (ilm_cont.algorithm)
  65. setPreviousAlgorithm(ilm_cont.algorithm)
  66. }
  67. export function prepareActivityToStudentHelper (ilm_cont) {
  68. const maybe_content = parseActivityData(ilm_cont);
  69. return maybe_content.map((content) => {
  70. const testCases = content.testcases;
  71. setTestCases(testCases);
  72. let prog_type = null;
  73. if (content.settings_programming_type) {
  74. prog_type = content.settings_programming_type[0].value;
  75. } else {
  76. prog_type = "visual";
  77. }
  78. return {
  79. settingsProgrammingType: prog_type,
  80. settingsDataTypes: content.settings_data_types,
  81. settingsCommands: content.settings_commands,
  82. settingsFunctions: content.settings_functions,
  83. algorithmInIlm: content.algorithm_in_ilm,
  84. settingsFilter: content.settings_filter,
  85. };
  86. });
  87. }
  88. function compareArray (a, b) {
  89. for (let i = 0; i < a.length; ++i) {
  90. const elementA = a[i];
  91. const elementB = b[i];
  92. if (elementA != elementB) {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. function compareTestcases (original, student) {
  99. if (original.length != student.length) {
  100. return false;
  101. }
  102. for (let i = 0; i < original.length; ++i) {
  103. const elementO = original[i];
  104. const elementS = student[i];
  105. if (!compareArray(elementO.input, elementS.input)) {
  106. return false;
  107. }
  108. if (!compareArray(elementO.output, elementS.output)) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. export function autoEval (originalData, callback) {
  115. const code = generate();
  116. //console.debug(code);
  117. const original = parseActivityData(originalData).getOrElse(undefined);
  118. if (original == null) {
  119. alert("iAssign did not provide the original activity data!");
  120. return callback(null);
  121. }
  122. if (code == null) {
  123. return callback(-1); // @FeedbackConvention Casos de teste vazios
  124. } else {
  125. if (!compareTestcases(original.testcases, getTestCases())) {
  126. return callback(-2); // @FeedbackConvention Casos de teste alterados pelo aluno
  127. }
  128. try {
  129. const ast_code = SemanticAnalyser.analyseFromSource(code);
  130. const con = new TestConsole([]);
  131. const autoAssessment = new IVProgAssessment(
  132. ast_code,
  133. original.testcases,
  134. con
  135. );
  136. autoAssessment
  137. .runTest()
  138. .then((grade) => callback(grade))
  139. .catch((err) => {
  140. console.log(err);
  141. callback(-5); // @FeedbackConvention Falha na execução
  142. });
  143. } catch (e) {
  144. console.error(e);
  145. callback(-5);
  146. }
  147. }
  148. }