FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json-rules-engine/examples/07-rule-chaining.js at master · feenst/json-rules-engine · GitHub
feenst
/
json-rules-engine
Public
forked from
CacheControl/json-rules-engine
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
json-rules-engine
/
examples
/
07-rule-chaining.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (91 loc) · 2.76 KB
Breadcrumbs
json-rules-engine
/
examples
/
07-rule-chaining.js
Copy path
File metadata and controls
99 lines (91 loc) · 2.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
/*
* This is an advanced example demonstrating rules that passed based off the
* results of other rules
*
* Usage:
* node ./examples/07-rule-chaining.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/07-rule-chaining.js
*/
require
(
'colors'
)
let
Engine
=
require
(
'../dist'
)
.
Engine
/**
* Setup a new engine
*/
let
engine
=
new
Engine
(
)
/**
* Rule for identifying people who may like screwdrivers
*/
let
drinkRule
=
{
conditions
:
{
all
:
[
{
fact
:
'drinksOrangeJuice'
,
operator
:
'equal'
,
value
:
true
}
,
{
fact
:
'enjoysVodka'
,
operator
:
'equal'
,
value
:
true
}
]
}
,
event
:
{
type
:
'drinks-screwdrivers'
}
,
priority
:
10
,
// IMPORTANT! Set a higher priority for the drinkRule, so it runs first
onSuccess
:
function
(
event
,
almanac
)
{
almanac
.
addRuntimeFact
(
'screwdriverAficionado'
,
true
)
}
,
onFailure
:
function
(
event
,
almanac
)
{
almanac
.
addRuntimeFact
(
'screwdriverAficionado'
,
false
)
}
}
engine
.
addRule
(
drinkRule
)
/**
* Rule for identifying people who should be invited to a screwdriver social
* - Only invite people who enjoy screw drivers
* - Only invite people who are sociable
*/
let
inviteRule
=
{
conditions
:
{
all
:
[
{
fact
:
'screwdriverAficionado'
,
// this fact value is set when the drinkRule is evaluated
operator
:
'equal'
,
value
:
true
}
,
{
fact
:
'isSociable'
,
operator
:
'equal'
,
value
:
true
}
]
}
,
event
:
{
type
:
'invite-to-screwdriver-social'
}
,
priority
:
5
// Set a lower priority for the drinkRule, so it runs later (default: 1)
}
engine
.
addRule
(
inviteRule
)
/**
* Register listeners with the engine for rule success and failure
*/
let
facts
engine
.
on
(
'success'
,
(
event
,
almanac
)
=>
{
console
.
log
(
facts
.
accountId
+
' DID '
.
green
+
'meet conditions for the '
+
event
.
type
.
underline
+
' rule.'
)
}
)
.
on
(
'failure'
,
event
=>
{
console
.
log
(
facts
.
accountId
+
' did '
+
'NOT'
.
red
+
' meet conditions for the '
+
event
.
type
.
underline
+
' rule.'
)
}
)
// define fact(s) known at runtime
facts
=
{
accountId
:
'washington'
,
drinksOrangeJuice
:
true
,
enjoysVodka
:
true
,
isSociable
:
true
}
engine
.
run
(
facts
)
// first run, using washington's facts
.
then
(
(
)
=>
{
facts
=
{
accountId
:
'jefferson'
,
drinksOrangeJuice
:
true
,
enjoysVodka
:
false
,
isSociable
:
true
}
return
engine
.
run
(
facts
)
// second run, using jefferson's facts; facts & evaluation are independent of the first run
}
)
.
catch
(
console
.
log
)
/*
* OUTPUT:
*
* washington DID meet conditions for the drinks-screwdrivers rule.
* washington DID meet conditions for the invite-to-screwdriver-social rule.
* jefferson did NOT meet conditions for the drinks-screwdrivers rule.
* jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.
*/
Back
|
FazBrowse Home
|
New Git URL