-
|
Hi, I'm trying to define custom nodes in nodejs. This is the code: const { ZenEngine } = require('@gorules/zen-engine');
const path = require('path');
const fs = require('fs/promises');
class AddNode {
handle(request) {
const { a, b } = request.input;
return { output: a + b, trace_data: { operation: 'addition' } };
}
}
class MulNode {
handle(request) {
const { a, b } = request.input;
return { output: a * b, trace_data: { operation: 'multiplication' } };
}
}
class SubNode {
handle(request) {
const { a, b } = request.input;
return { output: a - b, trace_data: { operation: 'subtraction' } };
}
}
class DivNode {
handle(request) {
const { a, b } = request.input;
if (b === 0) throw new Error('Division by zero');
return { output: a / b, trace_data: { operation: 'division' } };
}
}
// Custom node handlers map
const customNodes = {
mathAdd: new AddNode(),
mathMul: new MulNode(),
mathSub: new SubNode(),
mathDiv: new DivNode(),
};
function customNodeHandler(request) {
console.log("custom node handler")
const nodeHandler = customNodes[request.node.kind];
if (!nodeHandler) {
throw new Error(`Node handler not found for kind: ${request.node.kind}`);
}
return nodeHandler.handle(request);
}
// const zen = new ZenEngine({ customHandler: customNodeHandler });
const zen = new ZenEngine({ customHandler: async () => customNodeHandler })It’s not working—could there be something wrong? I tried replicating it using the example provided in Go, but I haven’t had any success. Thanks a lot. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi, It seems that now executes the custom node but throws an exception when returning the data. It seems that there is something wrong with the response. Any idea on how should it be? Code of the script: const { ZenEngine, ZenDecisionContent, ZenEngineResponse } = require('@gorules/zen-engine');
const fs = require('fs/promises');
const customNodes = {
add: {
handle(request) {
const a = request.input?.a;
const b = request.input?.b;
console.log("a", a)
console.log("b", b)
return {
output: a + b
};
}
}
};
function customNodeHandler(request) {
const nodeKind = request.node.kind;
const nodeHandler = customNodes[nodeKind];
if (!nodeHandler) {
throw new Error("Component not found");
}
return nodeHandler.handle(request);
}
const engine = new ZenEngine({ customHandler: customNodeHandler });
(async () => {
const content = await fs.readFile('./jdm_custom_node.json', 'utf8');
const decision = engine.createDecision(JSON.parse(content));
const result = await decision.evaluate({ a: 1, b: 2 });
console.log(result);
})();Error: |
Beta Was this translation helpful? Give feedback.
-
|
Hi, It seems that I have finally achieved. Here is the typescript code if anyone is interested: import { ZenEngine, ZenDecisionContent, ZenEngineResponse, ZenEngineHandlerRequest, ZenEngineHandlerResponse } from '@gorules/zen-engine';
import * as fs from 'fs/promises';
// Define the structure of custom node handlers
type CustomNodeRequest = {
input?: Record<string, any>;
node: {
kind: string;
};
};
type CustomNodeResponse = {
output: any;
traceData?: Record<string, any>;
};
type CustomNodeHandler = {
handle: (request: CustomNodeRequest) => CustomNodeResponse;
};
// Define custom nodes
const customNodes: Record<string, CustomNodeHandler> = {
add: {
handle(request: CustomNodeRequest): CustomNodeResponse {
const left = request.input?.left;
const right = request.input?.right;
const key = request.input?.key;
console.log("key:", key);
console.log("left:", left);
console.log("right:", right);
if (typeof left !== 'number' || typeof right !== 'number') {
throw new Error("Invalid input: 'left' and 'right' must be numbers");
}
const output = {};
output[key] = left + right
return {
output: output,
traceData: { operation: "addition", inputs: { left, right } }
};
}
}
};
// Custom node handler function
async function customNodeHandler(request: ZenEngineHandlerRequest): Promise<ZenEngineHandlerResponse> {
const nodeKind = request.node.kind;
const nodeHandler = customNodes[nodeKind];
if (!nodeHandler) {
throw new Error("Component not found");
}
const response = nodeHandler.handle(request);
return response;
}
// Initialize the ZenEngine
const engine = new ZenEngine({ customHandler: customNodeHandler });
(async () => {
try {
// Load the decision graph from a file
const content = await fs.readFile('./jdm_custom_node.json', 'utf8');
const decisionContent: ZenDecisionContent = JSON.parse(content);
// Create a decision instance
const decision = engine.createDecision(decisionContent);
// Evaluate the decision
const result = await decision.evaluate({ left: 1, right: 2, key: "calculated" }, { trace: true });
console.log(result.result);
} catch (error) {
console.error("Error:", error);
} finally {
console.log("Cleaning up resources...");
if (typeof engine.dispose === 'function') {
engine.dispose();
}
process.exit(0); // Force terminate the script
}
})();Regards. |
Beta Was this translation helpful? Give feedback.
Hi,
It seems that I have finally achieved. Here is the typescript code if anyone is interested: