A simple class to make statefull decision trees
npm install @nfq/decision-treesh
npm install @nfq/decision-tree
`
if you are in yarn
`sh
yarn add @nfq/decision-tree
`
or on pnpm
`sh
pnpm install @nfq/decision-tree
`
---
---
Usage:
`javascript
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:
$3
- 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 |
$3
- 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`---
---