iassignHelpers.js 4.3 KB

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