iassignHelpers.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function parseActivityData (data) {
  7. let algorithm_in_ilm = null;
  8. if (data.split('\n::algorithm::')[1]) {
  9. algorithm_in_ilm = data.split('\n::algorithm::')[1].split('\n::logs::')[0];
  10. const logs = data.split('\n::algorithm::')[1].split('\n::logs::')[1];
  11. if (logs != null) {
  12. parseLogs(logs);
  13. }
  14. }
  15. let content = JSON.parse(data.split('\n::algorithm::')[0]);
  16. content['algorithm_in_ilm'] = algorithm_in_ilm;
  17. return content;
  18. }
  19. export function prepareActivityToStudentHelper (ilm_cont) {
  20. const content = parseActivityData(ilm_cont);
  21. const testCases = content.testcases;
  22. setTestCases(testCases);
  23. let prog_type = null;
  24. if (content.settings_programming_type) {
  25. prog_type = content.settings_programming_type[0].value;
  26. } else {
  27. prog_type = "visual";
  28. }
  29. return {
  30. settingsProgrammingType: prog_type,
  31. settingsDataTypes: content.settings_data_types,
  32. settingsCommands: content.settings_commands,
  33. settingsFunctions: content.settings_functions,
  34. algorithmInIlm: content.algorithm_in_ilm
  35. }
  36. }
  37. export function autoEval (originalData, callback) {
  38. const code = generate();
  39. const original = parseActivityData(originalData);
  40. if (code == null) {
  41. return callback(-1);
  42. } else {
  43. if (!compareTestcases(original.testcases, getTestCases())) {
  44. return callback(-2);
  45. }
  46. const autoAssessment = new IVProgAssessment(code, getTestCases(), new TestConsole([]));
  47. autoAssessment.runTest().then( grade => callback(grade)).catch(err => console.log(err))
  48. }
  49. }
  50. function compareTestcases (original, student) {
  51. if (original.length != student.length) {
  52. return false;
  53. }
  54. for (let i = 0; i < original.length; ++i) {
  55. const elementO = original[i];
  56. const elementS = student[i];
  57. if(!compareArray(elementO.input, elementS.input)) {
  58. return false;
  59. }
  60. if(!compareArray(elementO.output, elementS.output)) {
  61. return false;
  62. }
  63. }
  64. }
  65. function compareArray (a, b) {
  66. for (let i = 0; i < a.length; ++i) {
  67. const elementA = a[i];
  68. const elementB = b[i];
  69. if (elementA != elementB) {
  70. return false;
  71. }
  72. }
  73. }