Skip to content

Commit cb5e0ab

Browse files
author
Cache Hamm
committed
Update README to display a nested logic example
- add example file
1 parent be1a0b5 commit cb5e0ab

2 files changed

Lines changed: 123 additions & 31 deletions

File tree

README.md

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,76 @@ It's best to start with the [overview](./docs/overview.md) to understand the ter
3030

3131
To dive right in, start with the [basic example](./examples/basic.js).
3232

33-
## Hello World
33+
## Example
34+
35+
In basketball, a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out.
36+
37+
This example demonstrates an engine for detecting whether an individual player has fouled out.
38+
3439
```js
3540
import { Engine } from 'json-rules-engine'
36-
import { Rule } from 'json-rules-engine'
3741

3842
/**
3943
* Setup a new engine
4044
*/
4145
let engine = new Engine()
4246

43-
/**
44-
* Create a rule
45-
*/
46-
let rule = new Rule()
47-
48-
// define the 'conditions' for when "hello world" should display
49-
rule.setConditions({
50-
all: [{
51-
fact: 'displayMessage',
52-
operator: 'equal',
53-
value: true
54-
}]
55-
})
56-
// define the 'event' that will fire when the condition evaluates truthy
57-
rule.setEvent({
58-
type: 'message',
59-
params: {
60-
data: 'hello-world!'
47+
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
48+
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
49+
engine.addRule({
50+
conditions: {
51+
any: [{
52+
all: [{
53+
fact: 'gameDuration',
54+
operator: 'equal',
55+
value: 40
56+
}, {
57+
fact: 'personalFoulCount',
58+
operator: 'greaterThanInclusive',
59+
value: 5
60+
}]
61+
}, {
62+
all: [{
63+
fact: 'gameDuration',
64+
operator: 'equal',
65+
value: 48
66+
}, {
67+
fact: 'personalFoulCount',
68+
operator: 'greaterThanInclusive',
69+
value: 6
70+
}]
71+
}]
72+
},
73+
event: { // define the event to fire when the conditions evaluate truthy
74+
type: 'fouledOut',
75+
params: {
76+
message: 'Player has fouled out!'
77+
}
6178
}
6279
})
63-
// add rule to engine
64-
engine.addRule(rule)
6580

6681
/**
67-
* Pass initial values into the engine.
68-
* Fact values do NOT need to be known at engine runtime; see the
69-
* examples for how to pull in data asynchronously throughout a run()
82+
* define the facts
83+
* note: facts may be loaded asynchronously at runtime; see the advanced example below
7084
*/
71-
let facts = { displayMessage: true }
85+
let facts = {
86+
personalFoulCount: 6,
87+
gameDuration: 40
88+
}
7289

7390
// run the engine
7491
engine
7592
.run(facts)
76-
.then(triggeredEvents => { // run() return events with truthy conditions
77-
triggeredEvents.map(event => console.log(event.params.data))
93+
.then(events => { // run() return events with truthy conditions
94+
events.map(event => console.log(event.params.message))
7895
})
79-
.catch(console.log)
8096

8197
/*
82-
* hello-world!
98+
* Player has fouled out!
8399
*/
84100
```
85101

86-
[Try it out!](https://tonicdev.com/cachecontrol/json-rules-engine.hello-world)
102+
Run this example [here](./examples/nested-boolean-logic.js)
87103

88104

89105
## Persisting Rules

examples/nested-boolean-logic.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict'
2+
3+
/*
4+
* This example demonstates nested boolean logic - e.g. (x OR y) AND (a OR b).
5+
*
6+
* Usage:
7+
* node ./examples/nested-boolean-logic.js
8+
*
9+
* For detailed output:
10+
* DEBUG=json-rules-engine node ./examples/nested-boolean-logic.js
11+
*/
12+
13+
require('colors')
14+
var Engine = require('../dist').Engine
15+
16+
/**
17+
* Setup a new engine
18+
*/
19+
let engine = new Engine()
20+
21+
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
22+
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
23+
engine.addRule({
24+
conditions: {
25+
any: [{
26+
all: [{
27+
fact: 'gameDuration',
28+
operator: 'equal',
29+
value: 40
30+
}, {
31+
fact: 'personalFoulCount',
32+
operator: 'greaterThanInclusive',
33+
value: 5
34+
}]
35+
}, {
36+
all: [{
37+
fact: 'gameDuration',
38+
operator: 'equal',
39+
value: 48
40+
}, {
41+
fact: 'personalFoulCount',
42+
operator: 'greaterThanInclusive',
43+
value: 6
44+
}]
45+
}]
46+
},
47+
event: { // define the event to fire when the conditions evaluate truthy
48+
type: 'fouledOut',
49+
params: {
50+
message: 'Player has fouled out!'
51+
}
52+
}
53+
})
54+
55+
/**
56+
* define the facts
57+
* note: facts may be loaded asynchronously at runtime; see the advanced example below
58+
*/
59+
let facts = {
60+
personalFoulCount: 6,
61+
gameDuration: 40
62+
}
63+
64+
// run the engine
65+
engine
66+
.run(facts)
67+
.then(events => { // run() return events with truthy conditions
68+
events.map(event => console.log(event.params.message.red))
69+
})
70+
.catch(console.log)
71+
72+
/*
73+
* OUTPUT:
74+
*
75+
* Player has fouled out!
76+
*/

0 commit comments

Comments
 (0)