-
Notifications
You must be signed in to change notification settings - Fork 11
/
Step.js
311 lines (291 loc) · 8.29 KB
/
Step.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import _ from 'lodash';
import Action from './Action';
import Port from './Port';
/**
* Bind ports during step construction.
*
* @private
* @param {Object.<string, PortDesc>} portDescMap
* @param {Object.<string, Port>} portMap
*/
function bindPorts(portDescMap, portMap) {
_.forEach(portDescMap, (portDesc, portName) => {
const /** @type Port */ port = portMap[portName];
if (port) {
if (portDesc instanceof Port) {
port.bind(portDesc);
} else if (portDesc.bind) {
port.bind(portDesc.bind);
}
}
});
}
/**
* Synchronizes all ports of a step with their descriptions.
*
* @private
* @param {Object.<string, Port>} portMap
* @param {Object.<string, PortDesc>} portDescMap
* @param {Step} step
* @returns {*|Object}
*/
function syncPortsWithDesc(portMap, portDescMap, step) {
// disconnect removed ports
_.forEach(portMap, (port, name) => {
if (portDescMap[name] === undefined) {
port.disconnect();
}
});
const omitBy = _.omitBy || _.omit; // be prepared for legacy lodash 3.10.1
portMap = omitBy(portMap, (port, name) => portDescMap[name] === undefined);
_.forEach(portMap, (port, name) => {
const desc = portDescMap[name];
if (port.inputs.length > 1 && !desc.multi) {
port.disconnect();
}
port.desc = desc;
});
_.forEach(portDescMap, (desc, name) => {
if (portMap[name]) {
return;
}
portMap[name] = new Port(name, step, desc);
});
return portMap;
}
/**
* Class representing a specific step in a {@link Workflow}.
*
* @example
* // Create a step from an action and set input variable name
* const helloStep = new Step(helloAction.name, helloAction, {
* i: {
* name: {
* bind: 'World',
* },
* },
* });
* @example
* // Create a step AND an action in one statement
* const helloStep = new Step('hello', {
* i: {
* name: {
* type: 'String',
* bind: 'World',
* },
* },
* o: {
* response: {
* type: 'File',
* default: 'stdout()',
* },
* },
* data: {
* command: 'echo \'Hello ${name}!\'',
* },
* });
*/
export default class Step {
/**
* Create a workflow step. You should {@link Workflow#add add} it to a workflow or a compound step.
*
* @param {string} name - Step name. Must be unique in a parent step (e.g. {@link Workflow}).
* @param {Action|{}} action - Action to execute during the step. If no action is specified it will
* automatically be created based on the configuration. Multiple steps may share a single action.
* @param {object} config - Action configuration containing input bindings.
* It should include action description in case the action is missing.
*
*/
constructor(name, action = {}, config = {}) {
if (_.isUndefined(name)) {
throw new Error('Step must have a name');
}
if (!(action instanceof Action)) {
config = _.merge(action, config);
action = new Action(name, config);
}
action.on('changed', () => this._onActionChanged());
action.on('port-rename', (...args) => this._onPortRename(...args));
/**
* Step name.
* @type {string}
*/
this.name = name;
/**
* Action performed during the step.
* @type {Action}
*/
this.action = action;
/**
* Parent step. A step can only have a single parent.
* @type {?Step}
*/
this.parent = null;
/**
* Dictionary of child steps.
* @type {Object.<string, Step>}
*/
this.children = {};
/**
* Dictionary of input ports bindings.
* @type {Object.<string, Port>}
*/
this.i = _.mapValues(action.i, (portDesc, portName) => new Port(portName, this, portDesc));
bindPorts(config.i || {}, this.i);
/**
* Dictionary of output ports bindings.
* @type {Object.<string, Port>}
*/
this.o = _.mapValues(action.o, (portDesc, portName) => new Port(portName, this, portDesc));
bindPorts(config.o || {}, this.o);
}
/**
* Add a child step.
*
* @param {Step} child - Step to add.
* @returns {Step} Added step. May be used for easy step creation.
* @throws {Error} Generic exception if a child with the same name already exists.
* @example
* const child = parent.add(new Step('child', ...));
*/
add(child) {
const existing = this.children[child.name];
if (!existing) {
if (child.parent !== null) {
child.parent.remove(child.name);
}
child.parent = this;
this.children[child.name] = child;
// collect and add all child actions to the workflow
const root = this.workflow();
if (root) {
const actions = [];
this.walk(step => actions.push(step.action));
_.forEach(actions, action => root.addAction(action));
}
} else if (existing !== child) {
throw new Error(`Cannot add a child step with the same name ${child.name}`);
}
return child;
}
/**
* Remove a child.
*
* @param {string} name - Child to remove.
* @example
* parent.remove('child');
*/
remove(name) {
const child = this.children[name];
if (child) {
child.parent = null;
delete this.children[name];
}
}
/**
* Rename current step.
* @param newName
*/
rename(newName) {
const oldName = this.name;
const parent = this.parent;
if (parent) {
const existing = parent.children[newName];
if (existing === this) {
return;
}
if (existing) {
throw new Error('Cannot rename step - name is already taken');
}
delete parent.children[oldName];
parent.children[newName] = this;
}
this.name = newName;
}
/**
* Retrieve a workflow this step belongs to.
*
* @returns {?Workflow} Parent or grandparent workflow this step belongs to.
*/
workflow() {
let node = this;
while (!node.addAction && node.parent) {
node = node.parent;
}
return node.addAction ? node : null;
}
/**
* Walk down the step hierarchy applying a callback at each step (including the root).
* The callback accepts a {@link Step} as a parameter and may return `false` to prevent further processing.
*
* - If `before` callback returns `false`, children processing is disabled.
* - If `after` callback returns `false`, sibling processing is stopped.
*
* **Note.** Child nodes are visited in arbitrary order.
*
* @param {function|object} callback - Either a callback or a pair of callbacks to execute at each step.
* By default a `before` callback is specified.
* @param {function=} callback.before - Function to execute before visiting child nodes.
* @param {function=} callback.after - Function to execute after visiting child nodes.
* @returns {*} The return value of the `after` callback of the root node.
*
* @example
* // Create a JavaScript object which reflects the steps hierarchy starting at "root" node
* let tree = {};
* const trees = [];
* root.walk({
* before() {
* trees.push(tree);
* tree = {};
* },
* after(step) {
* const children = tree;
* tree = trees.pop();
* tree[step.name] = children;
* },
* });
* return tree;
*/
walk(callback) {
if (_.isFunction(callback)) {
callback = { before: callback };
}
const beforeResult = callback.before && callback.before(this);
if (beforeResult !== false) {
_.forEach(this.children, (child) => {
if (child === this || child.type !== 'workflow') {
child.walk(callback);
} else {
callback.before(child);
}
});
}
return callback.after && callback.after(this);
}
/**
* Checks whether or not a Port instance is an input port of this step.
* @param {Port} port
* @returns {boolean}
*/
hasInputPort(port) {
return this.i[port.name] === port;
}
/**
* Checks whether or not a Port instance is an output port of this step.
* @param {Port} port
* @returns {boolean}
*/
hasOutputPort(port) {
return this.o[port.name] === port;
}
_onActionChanged() {
this.i = syncPortsWithDesc(this.i, this.action.i, this);
this.o = syncPortsWithDesc(this.o, this.action.o, this);
}
_onPortRename(oldName, newName, isInput) {
const ports = isInput ? this.i : this.o;
ports[newName] = ports[oldName];
ports[newName].name = newName;
delete ports[oldName];
}
}