Skip to content

Commit b94bbc6

Browse files
authored
Merge pull request CacheControl#45 from ValYouW/working-walkthrough
Fix walkthrough so anyone who follows it will be able to actually run it
2 parents 9601f78 + c868840 commit b94bbc6

File tree

2 files changed

+92
-38
lines changed

2 files changed

+92
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules
22
npm-debug.log
3-
.vscode
3+
.vscode
4+
.idea

docs/walkthrough.md

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Step 1: Create an Engine
44

55
```js
6-
let Engine = require('json-rules-engine')
7-
let engine = new Engine()
6+
let RuleEngine = require('json-rules-engine');
7+
let engine = new RuleEngine.Engine();
88
```
99

1010
More on engines can be found [here](./engine.md)
@@ -20,7 +20,7 @@ let event = {
2020
giftCard: 'amazon',
2121
value: 50
2222
}
23-
}
23+
};
2424
let conditions = {
2525
all: [
2626
{
@@ -32,27 +32,29 @@ let conditions = {
3232
operator: 'lessThanInclusive',
3333
value: 25
3434
},
35-
any: [
36-
{
37-
fact: 'state',
38-
params: {
39-
country: 'us'
40-
},
41-
operator: 'equal',
42-
value: 'colorado'
43-
}, {
44-
fact: 'state',
45-
params: {
46-
country: 'us'
47-
},
48-
operator: 'equal',
49-
value: 'utah'
50-
}
51-
]
35+
{
36+
any: [
37+
{
38+
fact: 'state',
39+
params: {
40+
country: 'us'
41+
},
42+
operator: 'equal',
43+
value: 'CO'
44+
}, {
45+
fact: 'state',
46+
params: {
47+
country: 'us'
48+
},
49+
operator: 'equal',
50+
value: 'UT'
51+
}
52+
]
53+
}
5254
]
53-
}
54-
let rule = new Rule({ conditions, event})
55-
engine.addRule(rule)
55+
};
56+
let rule = new RuleEngine.Rule({ conditions, event});
57+
engine.addRule(rule);
5658
```
5759

5860
The example above demonstrates a rule for finding individuals between _18 and 25_ who live in either _Utah or Colorado_.
@@ -66,17 +68,19 @@ Facts are constant values or pure functions. Using the current example, if the
6668
Let's define some facts:
6769

6870
```js
69-
7071
/*
7172
* Define the 'state' fact
7273
*/
7374
let stateFact = function(params, almanac) {
7475
// rule "params" value is passed to the fact
7576
// 'almanac' can be used to lookup other facts
7677
// via almanac.factValue()
77-
return stateLookupByZip(params.country, almanac.factValue('zip-code'))
78-
}
79-
engine.addFact('state', stateFact)
78+
return almanac.factValue('zip-code')
79+
.then(zip => {
80+
return stateLookupByZip(params.country, zip);
81+
});
82+
};
83+
engine.addFact('state', stateFact);
8084

8185
/*
8286
* Define the 'age' fact
@@ -85,12 +89,24 @@ let ageFact = function(params, almanac) {
8589
// facts may return a promise when performing asynchronous operations
8690
// such as database calls, http requests, etc to gather data
8791
return almanac.factValue('userId').then((userId) => {
88-
return db.getUser(userId)
92+
return getUser(userId);
8993
}).then((user) => {
90-
return user.age
94+
return user.age;
9195
})
92-
}
93-
engine.addFact('age', ageFact)
96+
};
97+
engine.addFact('age', ageFact);
98+
99+
/*
100+
* Define the 'zip-code' fact
101+
*/
102+
let zipCodeFact = function(params, almanac) {
103+
return almanac.factValue('userId').then((userId) => {
104+
return getUser(userId);
105+
}).then((user) => {
106+
return user.zipCode;
107+
})
108+
};
109+
engine.addFact('zip-code', zipCodeFact);
94110
```
95111

96112
Now when the engine is run, it will call the methods above whenever it encounters the ```fact: "age"``` or ```fact: "state"``` properties.
@@ -111,20 +127,21 @@ engine.on('young-adult-rocky-mnts', (params) => {
111127
// giftCard: 'amazon',
112128
// value: 50
113129
// }
114-
})
130+
});
115131

116132
// - OR -
117133

118134
// subscribe to any event emitted by the engine
119135
engine.on('success', function (event, engine) {
136+
console.log('Success event:\n', event);
120137
// event: {
121138
// type: "young-adult-rocky-mnts",
122139
// params: {
123140
// giftCard: 'amazon',
124141
// value: 50
125142
// }
126143
// }
127-
})
144+
});
128145
```
129146

130147
## Step 5: Run the engine
@@ -133,13 +150,49 @@ Running an engine executes the rules, and fires off event events for conditions
133150

134151
```js
135152
// evaluate the rules
136-
engine.run()
153+
//engine.run();
137154

138155
// Optionally, facts known at runtime may be passed to run()
139-
engine.run({ userId: 1 }) // any time a rule condition requires 'userId', '1' will be returned
156+
engine.run({ userId: 1 }); // any time a rule condition requires 'userId', '1' will be returned
140157

141158
// run() returns a promise
142-
engine.run().then((events) => {
159+
engine.run({ userId: 4 }).then((events) => {
143160
console.log('all rules executed; the following events were triggered: ', events)
144-
})
161+
});
162+
```
163+
Helper methods (for this example)
164+
```js
165+
function stateLookupByZip(country, zip) {
166+
var state;
167+
switch (zip.toString()) {
168+
case '80014':
169+
state = 'CO';
170+
break;
171+
case '84101':
172+
state = 'UT';
173+
break;
174+
case '90210':
175+
state = 'CA';
176+
break;
177+
default:
178+
state = 'NY';
179+
}
180+
181+
return state;
182+
}
183+
184+
var users = {
185+
1: {age: 22, zipCode: 80014},
186+
2: {age: 16, zipCode: 80014},
187+
3: {age: 35, zipCode: 84101},
188+
4: {age: 23, zipCode: 90210},
189+
};
190+
191+
function getUser(id) {
192+
return new Promise((resolve, reject) => {
193+
setTimeout(() => {
194+
resolve(users[id]);
195+
}, 500);
196+
});
197+
}
145198
```

0 commit comments

Comments
 (0)