2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Flow Control operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2016-12-31 17:12:39 +00:00
|
|
|
var FlowControl = {
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
FORK_DELIM: "\\n",
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
MERGE_DELIM: "\\n",
|
2017-01-16 15:58:38 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
FORK_IGNORE_ERRORS: false,
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runFork: function(state) {
|
|
|
|
var 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 = [];
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
if (input)
|
2017-01-31 18:24:56 +00:00
|
|
|
inputs = input.split(splitDelim);
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
// Create subOpList for each tranche to operate on
|
2016-11-28 10:42:58 +00:00
|
|
|
// (all remaining operations unless we encounter a Merge)
|
2017-01-31 18:24:56 +00:00
|
|
|
for (var i = state.progress + 1; i < opList.length; i++) {
|
|
|
|
if (opList[i].name === "Merge" && !opList[i].isDisabled()) {
|
2016-11-28 10:42:58 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2017-01-31 18:24:56 +00:00
|
|
|
subOpList.push(opList[i]);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var recipe = new Recipe(),
|
|
|
|
output = "",
|
2017-01-16 15:58:38 +00:00
|
|
|
progress = 0;
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
recipe.addOperations(subOpList);
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
// Run recipe over each tranche
|
|
|
|
for (i = 0; i < inputs.length; i++) {
|
2017-01-31 18:24:56 +00:00
|
|
|
var dish = new Dish(inputs[i], inputType);
|
2017-01-16 15:58:38 +00:00
|
|
|
try {
|
|
|
|
progress = recipe.execute(dish, 0);
|
|
|
|
} catch(err) {
|
2017-01-31 18:24:56 +00:00
|
|
|
if (!ignoreErrors) {
|
2017-01-16 15:58:38 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
progress = err.progress + 1;
|
|
|
|
}
|
2017-01-31 18:24:56 +00:00
|
|
|
output += dish.get(outputType) + mergeDelim;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
state.dish.set(output, outputType);
|
2016-11-28 10:42:58 +00:00
|
|
|
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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runMerge: function(state) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// No need to actually do anything here. The fork operation will
|
|
|
|
// merge when it sees this operation.
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
JUMP_NUM: 0,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
MAX_JUMPS: 10,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
|
|
|
* @param {number} state.numJumps - The number of jumps taken so far.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runJump: function(state) {
|
|
|
|
var ings = state.opList[state.progress].getIngValues(),
|
|
|
|
jumpNum = ings[0],
|
|
|
|
maxJumps = ings[1];
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (state.numJumps >= maxJumps) {
|
2017-01-16 16:00:44 +00:00
|
|
|
state.progress++;
|
|
|
|
return state;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
state.progress += jumpNum;
|
|
|
|
state.numJumps++;
|
2016-11-28 10:42:58 +00:00
|
|
|
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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
|
|
|
* @param {number} state.numJumps - The number of jumps taken so far.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runCondJump: function(state) {
|
|
|
|
var ings = state.opList[state.progress].getIngValues(),
|
|
|
|
dish = state.dish,
|
|
|
|
regexStr = ings[0],
|
|
|
|
jumpNum = ings[1],
|
|
|
|
maxJumps = ings[2];
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (state.numJumps >= maxJumps) {
|
2017-01-16 16:00:44 +00:00
|
|
|
state.progress++;
|
|
|
|
return state;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (regexStr !== "" && dish.get(Dish.STRING).search(regexStr) > -1) {
|
|
|
|
state.progress += jumpNum;
|
|
|
|
state.numJumps++;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {Operation[]} state.opList - The list of operations in the recipe.
|
2016-11-28 10:42:58 +00:00
|
|
|
* @returns {Object} The updated state of the recipe.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runReturn: function(state) {
|
|
|
|
state.progress = state.opList.length;
|
2016-11-28 10:42:58 +00:00
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|