2016-11-29 01:58:58 +00:00
|
|
|
import OperationConfig from '../config/OperationConfig';
|
|
|
|
import Operation from './Operation';
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* The Recipe controls a list of Operations and the Dish they operate on.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @param {Object} recipe_config
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
const Recipe = function (recipe_config) {
|
|
|
|
this.op_list = [];
|
|
|
|
|
|
|
|
if (recipe_config) {
|
|
|
|
this._parse_config(recipe_config);
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads and parses the given config.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} recipe_config
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype._parse_config = function (recipe_config) {
|
|
|
|
for (let c = 0; c < recipe_config.length; c++) {
|
|
|
|
const operation_name = recipe_config[c].op;
|
|
|
|
const operation_config = OperationConfig[operation_name];
|
|
|
|
const operation = new Operation(operation_name, operation_config);
|
|
|
|
operation.set_ing_values(recipe_config[c].args);
|
|
|
|
operation.set_breakpoint(recipe_config[c].breakpoint);
|
|
|
|
operation.set_disabled(recipe_config[c].disabled);
|
|
|
|
this.add_operation(operation);
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the Recipe as it should be displayed in a recipe config.
|
|
|
|
*
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.get_config = function () {
|
|
|
|
const recipe_config = [];
|
|
|
|
|
|
|
|
for (let o = 0; o < this.op_list.length; o++) {
|
|
|
|
recipe_config.push(this.op_list[o].get_config());
|
|
|
|
}
|
|
|
|
|
|
|
|
return recipe_config;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new Operation to this Recipe.
|
|
|
|
*
|
|
|
|
* @param {Operation} operation
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.add_operation = function (operation) {
|
|
|
|
this.op_list.push(operation);
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a list of Operations to this Recipe.
|
|
|
|
*
|
|
|
|
* @param {Operation[]} operations
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.add_operations = function (operations) {
|
|
|
|
this.op_list = this.op_list.concat(operations);
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a breakpoint on a specified Operation.
|
|
|
|
*
|
|
|
|
* @param {number} position - The index of the Operation
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.set_breakpoint = function (position, value) {
|
|
|
|
try {
|
|
|
|
this.op_list[position].set_breakpoint(value);
|
|
|
|
} catch (err) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Ignore index error
|
2016-11-29 00:22:34 +00:00
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove breakpoints on all Operations in the Recipe up to the specified position. Used by Flow
|
|
|
|
* Control Fork operation.
|
|
|
|
*
|
|
|
|
* @param {number} pos
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.remove_breaks_up_to = function (pos) {
|
|
|
|
for (let i = 0; i < pos; i++) {
|
|
|
|
this.op_list[i].set_breakpoint(false);
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if there is an Flow Control Operation in this Recipe.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.contains_flow_control = function () {
|
|
|
|
for (let i = 0; i < this.op_list.length; i++) {
|
|
|
|
if (this.op_list[i].is_flow_control()) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the index of the last Operation index that will be executed, taking into account disabled
|
|
|
|
* Operations and breakpoints.
|
|
|
|
*
|
|
|
|
* @param {number} [start_index=0] - The index to start searching from
|
|
|
|
* @returns (number}
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.last_op_index = function (start_index) {
|
|
|
|
let i = start_index + 1 || 0,
|
|
|
|
op;
|
|
|
|
|
|
|
|
for (; i < this.op_list.length; i++) {
|
|
|
|
op = this.op_list[i];
|
|
|
|
if (op.is_disabled()) return i - 1;
|
|
|
|
if (op.is_breakpoint()) return i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i - 1;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes each operation in the recipe over the given Dish.
|
|
|
|
*
|
|
|
|
* @param {Dish} dish
|
|
|
|
* @param {number} [start_from=0] - The index of the Operation to start executing from
|
|
|
|
* @returns {number} - The final progress through the recipe
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.execute = function (dish, start_from) {
|
|
|
|
start_from = start_from || 0;
|
|
|
|
let op,
|
|
|
|
input,
|
|
|
|
output,
|
|
|
|
num_jumps = 0;
|
|
|
|
|
|
|
|
for (let i = start_from; i < this.op_list.length; i++) {
|
|
|
|
op = this.op_list[i];
|
|
|
|
if (op.is_disabled()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (op.is_breakpoint()) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
input = dish.get(op.input_type);
|
|
|
|
|
|
|
|
if (op.is_flow_control()) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Package up the current state
|
2016-11-29 00:22:34 +00:00
|
|
|
let state = {
|
|
|
|
progress: i,
|
|
|
|
dish,
|
|
|
|
op_list: this.op_list,
|
|
|
|
num_jumps,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = op.run(state);
|
|
|
|
i = state.progress;
|
|
|
|
num_jumps = state.num_jumps;
|
|
|
|
} else {
|
|
|
|
output = op.run(input, op.get_ing_values());
|
|
|
|
dish.set(output, op.output_type);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
const e = typeof err === 'string' ? { message: err } : err;
|
|
|
|
|
|
|
|
e.progress = i;
|
|
|
|
e.display_str = `${op.name} - `;
|
|
|
|
if (e.fileName) {
|
|
|
|
e.display_str += `${e.name} in ${e.fileName
|
|
|
|
} on line ${e.lineNumber
|
|
|
|
}.<br><br>Message: ${e.message}`;
|
|
|
|
} else {
|
|
|
|
e.display_str += e.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2016-11-29 00:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.op_list.length;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the recipe configuration in string format.
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.to_string = function () {
|
|
|
|
return JSON.stringify(this.get_config());
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Recipe from a given configuration string.
|
|
|
|
*
|
|
|
|
* @param {string} recipe_str
|
|
|
|
*/
|
2016-11-29 00:22:34 +00:00
|
|
|
Recipe.prototype.from_string = function (recipe_str) {
|
|
|
|
const recipe_config = JSON.parse(recipe_str);
|
|
|
|
this._parse_config(recipe_config);
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|