FlowControl.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import Recipe from "./Recipe.js";
  2. import Dish from "./Dish.js";
  3. /**
  4. * Flow Control operations.
  5. *
  6. * @author n1474335 [n1474335@gmail.com]
  7. * @copyright Crown Copyright 2016
  8. * @license Apache-2.0
  9. *
  10. * @namespace
  11. */
  12. const FlowControl = {
  13. /**
  14. * Fork operation.
  15. *
  16. * @param {Object} state - The current state of the recipe.
  17. * @param {number} state.progress - The current position in the recipe.
  18. * @param {Dish} state.dish - The Dish being operated on.
  19. * @param {Operation[]} state.opList - The list of operations in the recipe.
  20. * @returns {Object} The updated state of the recipe.
  21. */
  22. runFork: async function(state) {
  23. let opList = state.opList,
  24. inputType = opList[state.progress].inputType,
  25. outputType = opList[state.progress].outputType,
  26. input = state.dish.get(inputType),
  27. ings = opList[state.progress].getIngValues(),
  28. splitDelim = ings[0],
  29. mergeDelim = ings[1],
  30. ignoreErrors = ings[2],
  31. subOpList = [],
  32. inputs = [],
  33. i;
  34. if (input)
  35. inputs = input.split(splitDelim);
  36. // Create subOpList for each tranche to operate on
  37. // (all remaining operations unless we encounter a Merge)
  38. for (i = state.progress + 1; i < opList.length; i++) {
  39. if (opList[i].name === "Merge" && !opList[i].isDisabled()) {
  40. break;
  41. } else {
  42. subOpList.push(opList[i]);
  43. }
  44. }
  45. let recipe = new Recipe(),
  46. output = "",
  47. progress = 0;
  48. recipe.addOperations(subOpList);
  49. // Run recipe over each tranche
  50. for (i = 0; i < inputs.length; i++) {
  51. const dish = new Dish(inputs[i], inputType);
  52. try {
  53. progress = await recipe.execute(dish, 0);
  54. } catch (err) {
  55. if (!ignoreErrors) {
  56. throw err;
  57. }
  58. progress = err.progress + 1;
  59. }
  60. output += dish.get(outputType) + mergeDelim;
  61. }
  62. state.dish.set(output, outputType);
  63. state.progress += progress;
  64. return state;
  65. },
  66. /**
  67. * Merge operation.
  68. *
  69. * @param {Object} state - The current state of the recipe.
  70. * @param {number} state.progress - The current position in the recipe.
  71. * @param {Dish} state.dish - The Dish being operated on.
  72. * @param {Operation[]} state.opList - The list of operations in the recipe.
  73. * @returns {Object} The updated state of the recipe.
  74. */
  75. runMerge: function(state) {
  76. // No need to actually do anything here. The fork operation will
  77. // merge when it sees this operation.
  78. return state;
  79. },
  80. /**
  81. * Jump operation.
  82. *
  83. * @param {Object} state - The current state of the recipe.
  84. * @param {number} state.progress - The current position in the recipe.
  85. * @param {Dish} state.dish - The Dish being operated on.
  86. * @param {Operation[]} state.opList - The list of operations in the recipe.
  87. * @param {number} state.numJumps - The number of jumps taken so far.
  88. * @returns {Object} The updated state of the recipe.
  89. */
  90. runJump: function(state) {
  91. let ings = state.opList[state.progress].getIngValues(),
  92. jumpNum = ings[0],
  93. maxJumps = ings[1];
  94. if (jumpNum < 0) {
  95. jumpNum--;
  96. }
  97. if (state.numJumps >= maxJumps) {
  98. return state;
  99. }
  100. state.progress += jumpNum;
  101. state.numJumps++;
  102. return state;
  103. },
  104. /**
  105. * Conditional Jump operation.
  106. *
  107. * @param {Object} state - The current state of the recipe.
  108. * @param {number} state.progress - The current position in the recipe.
  109. * @param {Dish} state.dish - The Dish being operated on.
  110. * @param {Operation[]} state.opList - The list of operations in the recipe.
  111. * @param {number} state.numJumps - The number of jumps taken so far.
  112. * @returns {Object} The updated state of the recipe.
  113. */
  114. runCondJump: function(state) {
  115. let ings = state.opList[state.progress].getIngValues(),
  116. dish = state.dish,
  117. regexStr = ings[0],
  118. jumpNum = ings[1],
  119. maxJumps = ings[2];
  120. if (jumpNum < 0) {
  121. jumpNum--;
  122. }
  123. if (state.numJumps >= maxJumps) {
  124. return state;
  125. }
  126. if (regexStr !== "" && dish.get(Dish.STRING).search(regexStr) > -1) {
  127. state.progress += jumpNum;
  128. state.numJumps++;
  129. }
  130. return state;
  131. },
  132. /**
  133. * Return operation.
  134. *
  135. * @param {Object} state - The current state of the recipe.
  136. * @param {number} state.progress - The current position in the recipe.
  137. * @param {Dish} state.dish - The Dish being operated on.
  138. * @param {Operation[]} state.opList - The list of operations in the recipe.
  139. * @returns {Object} The updated state of the recipe.
  140. */
  141. runReturn: function(state) {
  142. state.progress = state.opList.length;
  143. return state;
  144. },
  145. /**
  146. * Comment operation.
  147. *
  148. * @param {Object} state - The current state of the recipe.
  149. * @param {number} state.progress - The current position in the recipe.
  150. * @param {Dish} state.dish - The Dish being operated on.
  151. * @param {Operation[]} state.opList - The list of operations in the recipe.
  152. * @returns {Object} The updated state of the recipe.
  153. */
  154. runComment: function(state) {
  155. return state;
  156. },
  157. };
  158. export default FlowControl;