Skip to main content

NodeJS Engine

ZEN Engine is built as embeddable BRE for your Rust, NodeJS or Python applications.

Installation

npm i @gorules/zen-engine

or

yarn add @gorules/zen-engine

Usage

To execute a simple decision you can use the code below.

import { ZenEngine } from '@gorules/zen-engine';
import fs from 'fs/promises';

(async () => {
// Example filesystem content, it is up to you how you obtain content
const content = await fs.readFile('./jdm_graph.json');
const engine = new ZenEngine();

const decision = engine.createDecision(content);
const result = await decision.evaluate({ input: 15 });
})();

Loaders

For more advanced use cases where you want to load multiple decisions and utilise graphs you can build loaders.

import { ZenEngine } from '../index';
import fs from 'fs/promises';
import path from 'path';

const dataRoot = path.join(__dirname, 'jdm_directory');

const loader = async (key: string) =>
fs.readFile(path.join(testDataRoot, key))(async () => {
const engine = new ZenEngine({
loader,
});

const result = await engine.evaluate('jdm_graph1.json', { input: 5 });
})();

or

...

(async () => {
const engine = new ZenEngine({
loader
});
const decision = await engine.getDecision('jdm_graph1.json');
const result = await decision.evaluate({input: 5});
})();

When engine.evaluate is invoked it will call loader and pass a key expecting a content of the JDM decision graph. In the case above we will assume file jdm_directory/jdm_graph1.json exists.

Similar to this example you can also utilise loader to load from different places, for example from REST API, from S3, Database, etc.