@nfq/decision-tree
v2.0.0
Published
A simple class to make statefull decision trees
Downloads
170
Readme
@nfq/decision-tree
Table of Contents
Description:
An DecisionTree class to simplify the generation of decision trees. For example branching surveys or everything that needs to decide on an data object.
Installation:
To install the package run
npm install @nfq/decision-tree
if you are in yarn
yarn add @nfq/decision-tree
or on pnpm
pnpm install @nfq/decision-tree
Usage:
import DecisionTree, {hasSomeData} from '@nfq/decision-tree';
const tree = new DecisionTree(firstStepDefaultData)
.addBranch(
hasSomeData,
new DecisionTree(secondStepDefaultData)
.addBranch(hasSomeData, returnData)
)
.addBranch(
data => data.text === 'bla',
returnDataForBlaChoosed
);
const dataToDecideOn = [{text: 'bla'}];
tree.evaluate(dataToDecideOn); // returns returnDataForBlaChoosed
const dataToDecideOn2 = [{text: 'nope'}];
tree.evaluate(dataToDecideOn); // returns secondStepDefaultData
Methods:
DecisionTree
- Contructor(data): | Param | type | Description | | ----- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | data | object, function(data, depth) | Data to give back if no other branch is matching. If you provide an function you will get the actual depth and the corresponding data from the evaluation array. |
- addBranch(condition, action): | Param | type | Description | | ---------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | | contition | boolean, function(data) | An boolean or an function that evaluates to an boolean. Gets the actual data for thism step. | | action | object, function(data, depth), DecisionTree | The action taken if condition is true. Can be another DecisionTree (for branching) or an function that gets the actual step data and the depth |
hasSomeData
- Shortcut method for an condition that only checks if something is in the data object for this step.
Contributions:
.NFQ | Christoph Kruppe
License:
The licence used is: MIT
Click on licence badge for licence details:
Questions:
If you have any furter questions please contact the following email address: email: [email protected]