123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import Recipe from "./Recipe.js";
- import Dish from "./Dish.js";
- /**
- * Flow Control operations.
- *
- * @author n1474335 [n1474335@gmail.com]
- * @copyright Crown Copyright 2016
- * @license Apache-2.0
- *
- * @namespace
- */
- const FlowControl = {
- /**
- * Fork operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @returns {Object} The updated state of the recipe.
- */
- runFork: async function(state) {
- let opList = state.opList,
- inputType = opList[state.progress].inputType,
- outputType = opList[state.progress].outputType,
- input = state.dish.get(inputType),
- ings = opList[state.progress].getIngValues(),
- splitDelim = ings[0],
- mergeDelim = ings[1],
- ignoreErrors = ings[2],
- subOpList = [],
- inputs = [],
- i;
- if (input)
- inputs = input.split(splitDelim);
- // Create subOpList for each tranche to operate on
- // (all remaining operations unless we encounter a Merge)
- for (i = state.progress + 1; i < opList.length; i++) {
- if (opList[i].name === "Merge" && !opList[i].isDisabled()) {
- break;
- } else {
- subOpList.push(opList[i]);
- }
- }
- let recipe = new Recipe(),
- output = "",
- progress = 0;
- recipe.addOperations(subOpList);
- // Run recipe over each tranche
- for (i = 0; i < inputs.length; i++) {
- const dish = new Dish(inputs[i], inputType);
- try {
- progress = await recipe.execute(dish, 0);
- } catch (err) {
- if (!ignoreErrors) {
- throw err;
- }
- progress = err.progress + 1;
- }
- output += dish.get(outputType) + mergeDelim;
- }
- state.dish.set(output, outputType);
- state.progress += progress;
- return state;
- },
- /**
- * Merge operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @returns {Object} The updated state of the recipe.
- */
- runMerge: function(state) {
- // No need to actually do anything here. The fork operation will
- // merge when it sees this operation.
- return state;
- },
- /**
- * Jump operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @param {number} state.numJumps - The number of jumps taken so far.
- * @returns {Object} The updated state of the recipe.
- */
- runJump: function(state) {
- let ings = state.opList[state.progress].getIngValues(),
- jumpNum = ings[0],
- maxJumps = ings[1];
- if (jumpNum < 0) {
- jumpNum--;
- }
- if (state.numJumps >= maxJumps) {
- return state;
- }
- state.progress += jumpNum;
- state.numJumps++;
- return state;
- },
- /**
- * Conditional Jump operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @param {number} state.numJumps - The number of jumps taken so far.
- * @returns {Object} The updated state of the recipe.
- */
- runCondJump: function(state) {
- let ings = state.opList[state.progress].getIngValues(),
- dish = state.dish,
- regexStr = ings[0],
- jumpNum = ings[1],
- maxJumps = ings[2];
- if (jumpNum < 0) {
- jumpNum--;
- }
- if (state.numJumps >= maxJumps) {
- return state;
- }
- if (regexStr !== "" && dish.get(Dish.STRING).search(regexStr) > -1) {
- state.progress += jumpNum;
- state.numJumps++;
- }
- return state;
- },
- /**
- * Return operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @returns {Object} The updated state of the recipe.
- */
- runReturn: function(state) {
- state.progress = state.opList.length;
- return state;
- },
- /**
- * Comment operation.
- *
- * @param {Object} state - The current state of the recipe.
- * @param {number} state.progress - The current position in the recipe.
- * @param {Dish} state.dish - The Dish being operated on.
- * @param {Operation[]} state.opList - The list of operations in the recipe.
- * @returns {Object} The updated state of the recipe.
- */
- runComment: function(state) {
- return state;
- },
- };
- export default FlowControl;
|