1
0

iassignHelpers.js 3.0 KB

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