nested-rules-engine
🌲Decision Tree based Rules Engine
Synopsis
A simple Decision tree based Rule Engine described using json files. Rules are executed according to decision tree. Create a set of rules (make them nested as you like) and based on set of inputs run the rules.
Features
- Rules expressed in human readable JSON
- Create new set of inputs or change existing inputs as you traverse rules tree
- Do multiple executions of rules set
Installation
npm install nested-rules-engine --save
yarn add nested-rules-engine
Basic Example
JavaScript
const { executeEngine } = require('nested-rules-engine');
const rules = {
"you_are_a_human": {
"you_are_kind": "help_me_find_my_book",
"you_are_smart": "please_do_my_homework",
},
"default": "please_do_my_homework"
};
const inputs = {
"type": "human",
"kindnessLevel": 0,
"intelligence": 10
};
const functions = {
default: () => true,
you_are_a_human: ({type}) => type === 'human',
you_are_kind: ({kindnessLevel}) => kindnessLevel > 300,
you_are_smart: ({intelligence}) => intelligence > 5,
help_me_find_my_book: () => ({
payload: 'lets help someone',
effort: 'finding the book'
}),
please_do_my_homework: () => ({
payload: 'doing homework',
effort: 'im getting sick'
})
};
const res = executeEngine(inputs, functions, rules);
TypeScript
import { executeEngine } from 'nested-rules-engine';
interface Inputs {
type: string;
kindnessLevel: number;
intelligence: number;
}
interface Result {
payload: string;
effort: string;
}
const rules = {
"you_are_a_human": {
"you_are_kind": "help_me_find_my_book",
"you_are_smart": "please_do_my_homework",
},
"default": "please_do_my_homework"
} as const;
const inputs: Inputs = {
type: "human",
kindnessLevel: 0,
intelligence: 10
};
const functions = {
default: () => true,
you_are_a_human: ({type}: Inputs) => type === 'human',
you_are_kind: ({kindnessLevel}: Inputs) => kindnessLevel > 300,
you_are_smart: ({intelligence}: Inputs) => intelligence > 5,
help_me_find_my_book: (): Result => ({
payload: 'lets help someone',
effort: 'finding the book'
}),
please_do_my_homework: (): Result => ({
payload: 'doing homework',
effort: 'im getting sick'
})
};
const res = executeEngine(inputs, functions, rules);
Documentation
Engine Execution Signature:
executeEngine(variables: Record<string, any>, functions: Record<string, Function>, rules: Record<string, any>, options?: Options);
Inputs
-
variables
Collection of values on which rule engine will execute.
You can change these collection of variables (Add/Edit/Delete them) as you traverse the decision tree of rules.
-
functions
Collection of functions that decide which way the tree should be traversed.
- In case the function indicates a final decision in tree (leaf of decision tree): Output can be anything that you want to see as
result
- In case the function makes an intermediate decision (branch of decision tree):
- if output is
true
: this means this branch should be traversed
- else: the function will be executed
-
rules
Decision Tree that will be traversed by this Rule Engine
-
options
there are different options that you can provide to customize the execution nature
verbose
(boolean): Makes Sure you get enough logs while engine goes through all decision tree
multiple
(boolean): You can run multiple Decision Trees based on same inputs. Input sets are shared between each tree
Outputs
result
: Result of the engine execution. format of Result will be defined by you through functions
logs
: Detailed logs while engine got executed (by default its disabled)
Advanced Examples
- Example with verbose output, multiple executions Find Here
- Example with Creating new set of inputs while engine is executing Find Here
License
MIT