-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
206 lines (175 loc) · 4.75 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
const ProcessGraphError = require('./error');
const Utils = require('./utils');
/**
* A Process graph node.
*
* @class
*/
class ProcessGraphNode {
constructor(node, id, parent = null) {
if (typeof id !== 'string' || id.length === 0) {
throw new ProcessGraphError('NodeIdInvalid');
}
if (!Utils.isObject(node)) {
throw new ProcessGraphError('NodeInvalid', {node_id: id});
}
if (typeof node.process_id !== 'string') {
throw new ProcessGraphError('ProcessIdMissing', {node_id: id});
}
this.id = id;
this.processGraph = parent;
this.source = node;
this.process_id = node.process_id;
this.namespace = node.namespace || null;
this.arguments = Utils.isObject(node.arguments) ? Utils.deepClone(node.arguments) : {};
this.description = node.description || null;
this.isResultNode = node.result || false;
this.expectsFrom = []; // From which node do we expect results from
this.receivedFrom = []; // From which node have received results from so far
this.passesTo = [];
this.computedResult = undefined;
}
toJSON() {
let args = Utils.mapObjectValues(this.arguments, arg => Utils.isObject(arg) && typeof arg.toJSON === 'function' ? arg.toJSON() : arg);
return Object.assign({}, this.source, {
process_id: this.process_id,
namespace: this.namespace,
description: this.description,
arguments: args,
result: this.isResultNode
});
}
getProcessGraph() {
return this.processGraph;
}
getParent() {
if (this.processGraph !== null) {
return this.processGraph.getParentNode();
}
return null;
}
getArgumentNames() {
return Object.keys(this.arguments);
}
hasArgument(name) {
return typeof this.arguments[name] !== 'undefined';
}
getArgumentType(name) {
return Utils.getType(this.getRawArgument(name));
}
getRawArgument(name) {
return Utils.isObject(this.source.arguments) ? this.source.arguments[name] : undefined;
}
getParsedArgument(name) {
return this.arguments[name];
}
getArgument(name, defaultValue = undefined) {
if (typeof this.arguments[name] === 'undefined') {
return defaultValue;
}
return this.evaluateArgument(this.arguments[name]);
}
getArgumentRefs(name) {
return Utils.getRefs(this.getRawArgument(name), false);
}
getRefs() {
return Utils.getRefs(this.source.arguments, false);
}
getProcessGraphParameterValue(name) {
// 1. Check local parameter, then check parents
// 2. Check parents
// 3. Try to get default value
// 4. Fail if no value is available
let defaultValue;
let pg = this.processGraph;
do {
if (pg.hasArgument(name)) {
return pg.getArgument(name);
}
if (pg.hasParameterDefault(name)) {
defaultValue = pg.getParameterDefault(name);
}
pg = pg.getParent();
} while (pg !== null);
if (typeof defaultValue !== 'undefined') {
return defaultValue;
}
if (!this.processGraph.allowUndefinedParameterRefs) {
throw new ProcessGraphError('ProcessGraphParameterMissing', {
argument: name,
node_id: this.id,
process_id: this.process_id,
namespace: this.namespace || 'n/a'
});
}
}
evaluateArgument(arg) {
var type = Utils.getType(arg);
switch(type) {
case 'result':
return this.processGraph.getNode(arg.from_node).getResult();
case 'callback':
return arg;
case 'parameter':
return this.getProcessGraphParameterValue(arg.from_parameter);
case 'array':
case 'object':
let copy = type === 'array' ? [] : {};
for(var i in arg) {
copy[i] = this.evaluateArgument(arg[i]);
}
return copy;
default:
return arg;
}
}
isStartNode() {
return (this.expectsFrom.length === 0);
}
addPreviousNode(node) {
if (!this.expectsFrom.find(other => other.id === node.id)) {
this.expectsFrom.push(node);
}
}
getPreviousNodes() {
// Sort nodes to ensure a consistent execution order
return this.expectsFrom.sort((a,b) => a.id.localeCompare(b.id));
}
addNextNode(node) {
if (!this.passesTo.find(other => other.id === node.id)) {
this.passesTo.push(node);
}
}
getNextNodes() {
// Sort nodes to ensure a consistent execution order
return this.passesTo.sort((a,b) => a.id.localeCompare(b.id));
}
reset() {
this.computedResult = undefined;
this.receivedFrom = [];
}
getDescription() {
return this.description;
}
setDescription(description) {
if (typeof description === 'string') {
this.description = description;
}
else {
this.description = null;
}
}
setResult(result) {
this.computedResult = result;
}
getResult() {
return this.computedResult;
}
solveDependency(dependencyNode) {
if (dependencyNode !== null && this.expectsFrom.includes(dependencyNode)) {
this.receivedFrom.push(dependencyNode);
}
return (this.expectsFrom.length === this.receivedFrom.length); // all dependencies solved?
}
}
module.exports = ProcessGraphNode;