-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnode.js
268 lines (253 loc) · 6.86 KB
/
node.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
const Utils = require("../utils");
const Parameter = require("./parameter");
/**
* A class that represents a process node and also a result from a process.
*/
class BuilderNode {
/**
* Creates a new process node for the builder.
*
* @param {Builder} parent
* @param {string} processId
* @param {object.<string, *>} [processArgs={}]
* @param {?string} [processDescription=null]
* @param {?string} [processNamespace=null]
*/
constructor(parent, processId, processArgs = {}, processDescription = null, processNamespace = null) {
/**
* The parent builder.
* @type {Builder}
*/
this.parent = parent;
/**
* The specification of the process associated with this node.
* @type {Process}
* @readonly
*/
this.spec = this.parent.spec(processId, processNamespace);
if (!this.spec) {
throw new Error("Process doesn't exist: " + processId);
}
/**
* The unique identifier for the node (not the process ID!).
* @type {string}
*/
this.id = parent.generateId(processId);
/**
* The namespace of the process - EXPERIMENTAL!
* @type {string}
*/
this.namespace = processNamespace;
/**
* The arguments for the process.
* @type {object.<string, *>}
*/
this.arguments = Array.isArray(processArgs) ? this.namedArguments(processArgs) : processArgs;
/**
* @ignore
*/
this._description = processDescription;
/**
* Is this the result node?
* @type {boolean}
*/
this.result = false;
this.addParametersToProcess(this.arguments);
}
/**
* Converts a sorted array of arguments to an object with the respective parameter names.
*
* @param {Array.<object.<string, *>>} processArgs
* @returns {object.<string, *>}
* @throws {Error}
*/
namedArguments(processArgs) {
if (processArgs.length > (this.spec.parameters || []).length) {
throw new Error("More arguments specified than parameters available.");
}
let obj = {};
if (Array.isArray(this.spec.parameters)) {
for(let i = 0; i < this.spec.parameters.length; i++) {
obj[this.spec.parameters[i].name] = processArgs[i];
}
}
return obj;
}
/**
* Checks the arguments given for parameters and add them to the process.
*
* @param {object.<string, *>|Array} processArgs
*/
addParametersToProcess(processArgs) {
for(let key in processArgs) {
let arg = processArgs[key];
if (arg instanceof Parameter) {
if (Utils.isObject(arg.spec.schema)) {
this.parent.addParameter(arg.spec);
}
}
else if (arg instanceof BuilderNode) {
this.addParametersToProcess(arg.arguments);
}
else if (Array.isArray(arg) || Utils.isObject(arg)) {
this.addParametersToProcess(arg);
}
}
}
/**
* Gets/Sets a description for the node.
*
* Can be used in a variety of ways:
*
* By default, this is a function:
* `node.description()` - Returns the description.
* `node.description("foo")` - Sets the description to "foo". Returns the node itself for method chaining.
*
* You can also "replace" the function (not supported in TypeScript!),
* then it acts as normal property and the function is not available any longer:
* `node.description = "foo"` - Sets the description to "foo".
* Afterwards you can call `node.description` as normal object property.
*
* @param {string|undefined} description - Optional: If given, set the value.
* @returns {string|BuilderNode}
*/
description(description) {
if (typeof description === 'undefined') {
return this._description;
}
else {
this._description = description;
return this;
}
}
/**
* Converts the given argument into something serializable...
*
* @protected
* @param {*} arg - Argument
* @param {string} name - Parameter name
* @returns {*}
*/
exportArgument(arg, name) {
const Formula = require('./formula');
if (Utils.isObject(arg)) {
if (arg instanceof BuilderNode || arg instanceof Parameter) {
return arg.ref();
}
else if (arg instanceof Formula) {
let builder = this.createBuilder(this, name);
arg.setBuilder(builder);
arg.generate();
return builder.toJSON();
}
else if (arg instanceof Date) {
return arg.toISOString();
}
else if (typeof arg.toJSON === 'function') {
return arg.toJSON();
}
else {
let obj = {};
for(let key in arg) {
if (typeof arg[key] !== 'undefined') {
obj[key] = this.exportArgument(arg[key], name);
}
}
return obj;
}
}
else if (Array.isArray(arg)) {
return arg.map(element => this.exportArgument(element), name);
}
// export child process graph
else if (typeof arg === 'function') {
return this.exportCallback(arg, name);
}
else {
return arg;
}
}
/**
* Creates a new Builder, usually for a callback.
*
* @protected
* @param {?BuilderNode} [parentNode=null]
* @param {?string} [parentParameter=null]
* @returns {BuilderNode}
*/
createBuilder(parentNode = null, parentParameter = null) {
const Builder = require('./builder');
let builder = new Builder(this.parent.processes, this.parent);
if (parentNode !== null && parentParameter !== null) {
builder.setParent(parentNode, parentParameter);
}
return builder;
}
/**
* Returns the serializable process for the callback function given.
*
* @protected
* @param {Function} arg - callback function
* @param {string} name - Parameter name
* @returns {object.<string, *>}
* @throws {Error}
*/
exportCallback(arg, name) {
let builder = this.createBuilder(this, name);
let params = builder.getParentCallbackParameters();
// Bind builder to this, so that this.xxx can be used for processes
// Also pass builder as last parameter so that we can grab it in arrow functions
let node = arg.bind(builder)(...params, builder);
if (Array.isArray(node) && builder.supports('array_create')) {
node = builder.array_create(node);
}
else if (!Utils.isObject(node) && builder.supports('constant')) {
node = builder.constant(node);
}
if (node instanceof BuilderNode) {
node.result = true;
return builder.toJSON();
}
else {
throw new Error("Callback must return BuilderNode");
}
}
/**
* Returns a JSON serializable representation of the data that is API compliant.
*
* @returns {object.<string, *>}
*/
toJSON() {
let obj = {
process_id: this.spec.id,
arguments: {}
};
if (this.namespace) {
obj.namespace = this.namespace;
}
for(let name in this.arguments) {
if (typeof this.arguments[name] !== 'undefined') {
obj.arguments[name] = this.exportArgument(this.arguments[name], name);
}
}
if (typeof this.description !== 'function') {
obj.description = this.description;
}
else if (typeof this._description === 'string') {
obj.description = this._description;
}
if (this.result) {
obj.result = true;
}
return obj;
}
/**
* Returns the reference object for this node.
*
* @returns {FromNode}
*/
ref() {
return { from_node: this.id };
}
}
module.exports = BuilderNode;