Skip to main content

NodeJS Engine

ZEN Engine is built as embeddable BRE for your Rust, NodeJS, Go 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';

const main = async () => {
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 '@gorules/zen-engine';
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));

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

or

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

...

const main = 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.

Evaluate expressions

You may also evaluate singular ZEN Expressions.

import { evaluateExpression, evaluateUnaryExpression } from '@gorules/zen-engine';

const main = async () => {
await evaluateExpression('1 + 1'); // 2
await evaluateExpression('a + b', { a: 10, b: 20 }); // 30

await evaluateUnaryExpression('> 10', { $: 5 }); // false
await evaluateUnaryExpression('> 10', { $: 15 }); // true
};