You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -24,67 +24,167 @@ A rules engine expressed in JSON
24
24
$ npm install json-rules-engine
25
25
```
26
26
27
-
## Documentation
27
+
## Basic Example
28
28
29
-
It's best to start with the [overview](./docs/overview.md) to understand the terminology. Next, see the [walkthrough](./docs/overview.md) and try out some [examples](./examples).
29
+
This example demonstrates an engine for detecting whether a basketball player has fouled out (a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out).
30
30
31
-
To dive right in, start with the [basic example](./examples/basic.js).
31
+
```js
32
+
import { Engine } from'json-rules-engine'
33
+
34
+
/**
35
+
* Setup a new engine
36
+
*/
37
+
let engine =newEngine()
38
+
39
+
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
40
+
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
41
+
engine.addRule({
42
+
conditions: {
43
+
any: [{
44
+
all: [{
45
+
fact:'gameDuration',
46
+
operator:'equal',
47
+
value:40
48
+
}, {
49
+
fact:'personalFoulCount',
50
+
operator:'greaterThanInclusive',
51
+
value:5
52
+
}]
53
+
}, {
54
+
all: [{
55
+
fact:'gameDuration',
56
+
operator:'equal',
57
+
value:48
58
+
}, {
59
+
fact:'personalFoulCount',
60
+
operator:'greaterThanInclusive',
61
+
value:6
62
+
}]
63
+
}]
64
+
},
65
+
event: { // define the event to fire when the conditions evaluate truthy
66
+
type:'fouledOut',
67
+
params: {
68
+
message:'Player has fouled out!'
69
+
}
70
+
}
71
+
})
72
+
73
+
/**
74
+
* Define facts the engine will use to evaluate the conditions above.
75
+
* Facts may also be loaded asynchronously at runtime; see the advanced example below
76
+
*/
77
+
let facts = {
78
+
personalFoulCount:6,
79
+
gameDuration:40
80
+
}
81
+
82
+
// Run the engine to evaluate
83
+
engine
84
+
.run(facts)
85
+
.then(events=> { // run() returns events with truthy conditions
Copy file name to clipboardExpand all lines: docs/facts.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,8 @@
1
1
# Facts
2
2
3
+
Facts are methods or constants registered with the engine prior to runtime and referenced within rule conditions. Each fact method should be a pure function that may return a computed value or promise.
4
+
As rule conditions are evaluated during runtime, they retrieve fact values dynamically and use the condition _operator_ to compare the fact result with the condition _value_.
Copy file name to clipboardExpand all lines: docs/rules.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Rules
2
2
3
+
Rules contain a set of _conditions_ and a single _event_. When the engine is run, each rule condition is evaluated. If the results are truthy, the rule's _event_ is triggered.
0 commit comments