@@ -30,60 +30,76 @@ It's best to start with the [overview](./docs/overview.md) to understand the ter
3030
3131To 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
3540import { Engine } from ' json-rules-engine'
36- import { Rule } from ' json-rules-engine'
3741
3842/**
3943 * Setup a new engine
4044 */
4145let 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
7491engine
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
0 commit comments