Skip to content

Commit 49133f3

Browse files
committed
compute set variable node in graph run
1 parent 664dcc3 commit 49133f3

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const EvaluateOperators = {
2-
Add: '+',
3-
Subtract: '-',
2+
Add: 'Add two numbers',
3+
Subtract: 'Subtract two numbers',
44
};
55

66
export default EvaluateOperators;

src/components/molecules/flow/graph/Graph.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import authNode from './compute/authnode';
77
import complexNode from './compute/complexnode';
88
import assertNode from './compute/assertnode';
99
import requestNode from './compute/requestNode';
10+
import setVarNode from './compute/setvarnode';
1011

1112
class Graph {
1213
constructor(nodes, edges, startTime, initialEnvVars, initialLogs) {
@@ -150,6 +151,21 @@ class Graph {
150151
}
151152
}
152153

154+
if (node.type === 'setVarNode') {
155+
const sNode = new setVarNode(node.data, prevNodeOutputData, this.envVariables);
156+
const newVariable = sNode.evaluate();
157+
if (newVariable != undefined) {
158+
this.logs.push(`Evaluate variable: ${newVariable}`);
159+
this.envVariables = {
160+
...this.envVariables,
161+
...newVariable,
162+
};
163+
}
164+
result = {
165+
status: 'Success',
166+
};
167+
}
168+
153169
if (this.#checkTimeout()) {
154170
throw `Timeout of ${this.timeout} ms exceeded, stopping graph run`;
155171
}
@@ -171,8 +187,9 @@ class Graph {
171187
if (connectingEdge != undefined) {
172188
const nextNode = this.nodes.find(
173189
(node) =>
174-
['requestNode', 'outputNode', 'assertNode', 'delayNode', 'authNode', 'complexNode'].includes(node.type) &&
175-
node.id === connectingEdge.target,
190+
['requestNode', 'outputNode', 'assertNode', 'delayNode', 'authNode', 'complexNode', 'setVarNode'].includes(
191+
node.type,
192+
) && node.id === connectingEdge.target,
176193
);
177194
this.graphRunNodeOutput[node.id] = result.data ? result.data : {};
178195
return this.#computeNode(nextNode);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { computeNodeVariable } from './utils';
2+
import Node from './node';
3+
import EvaluateOperators from '../../constants/evaluateOperators';
4+
5+
class setVarNode extends Node {
6+
constructor(nodeData, prevNodeOutputData, envVariables) {
7+
super('setVarNode');
8+
this.nodeData = nodeData;
9+
this.prevNodeOutputData = prevNodeOutputData;
10+
this.envVariables = envVariables;
11+
}
12+
13+
getVariableValue(variable) {
14+
if (variable.type.toLowerCase() === 'variable') {
15+
if (Object.prototype.hasOwnProperty.call(this.envVariables, variable.value)) {
16+
return this.envVariables[variable.value];
17+
} else {
18+
throw Error(`Cannot find value of variable ${variable.value}`);
19+
}
20+
} else {
21+
return computeNodeVariable(variable, this.prevNodeOutputData);
22+
}
23+
}
24+
25+
evaluate() {
26+
console.log('Evaluating set variable node');
27+
if (this.nodeData.variable) {
28+
if (this.nodeData.variable.name && this.nodeData.variable.name.trim() != '') {
29+
const vName = this.nodeData.variable.name;
30+
const vType = this.nodeData.variable.type.trim();
31+
if (['String', 'Number', 'Boolean', 'Now', 'Select'].includes(vType)) {
32+
const value = computeNodeVariable(this.nodeData.variable, this.prevNodeOutputData);
33+
return {
34+
[vName]: value,
35+
};
36+
} else if (vType === 'Expression') {
37+
const variables = this.nodeData.variable.value.variables;
38+
if (variables && variables.var1 && variables.var2) {
39+
const var1 = this.getVariableValue(this.nodeData.variable.value.variables.var1);
40+
const var2 = this.getVariableValue(this.nodeData.variable.value.variables.var2);
41+
42+
const operator = this.nodeData.variable.value.operator;
43+
if (operator == undefined) {
44+
throw 'Operator undefined';
45+
}
46+
if (operator == EvaluateOperators.Add) {
47+
if (typeof var1 !== 'number' || typeof var2 !== 'number') {
48+
throw Error(`Cannot perform ${typeof var1} + ${typeof var2}`);
49+
}
50+
return {
51+
[vName]: var1 + var2,
52+
};
53+
} else if (operator == EvaluateOperators.Subtract) {
54+
if (typeof var1 !== 'number' || typeof var2 !== 'number') {
55+
throw Error(`Cannot perform ${typeof var1} + ${typeof var2}`);
56+
}
57+
return {
58+
[vName]: var1 - var2,
59+
};
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
export default setVarNode;

src/components/molecules/flow/nodes/SetVarNode.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ const SetVarNode = ({ id, data }) => {
7070
<option value={CHOOSE_OPERATOR_DEFAULT_VALUE_OBJ.value}>
7171
{CHOOSE_OPERATOR_DEFAULT_VALUE_OBJ.displayValue}
7272
</option>
73-
<option value={EvaluateOperators.Add}>{EvaluateOperators.Add}</option>
74-
<option value={EvaluateOperators.Subtract}>{EvaluateOperators.Subtract}</option>
73+
{Object.entries(EvaluateOperators).map(([key, value], index) => (
74+
<option key={key} value={value}>
75+
{value}
76+
</option>
77+
))}
7578
</select>
7679
</div>
7780
);

0 commit comments

Comments
 (0)