1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | require('colors')
|
15 | let Engine = require('../dist').Engine
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | let engine = new Engine()
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | let drinkRule = {
|
26 | conditions: {
|
27 | all: [{
|
28 | fact: 'drinksOrangeJuice',
|
29 | operator: 'equal',
|
30 | value: true
|
31 | }, {
|
32 | fact: 'enjoysVodka',
|
33 | operator: 'equal',
|
34 | value: true
|
35 | }]
|
36 | },
|
37 | event: { type: 'drinks-screwdrivers' },
|
38 | priority: 10,
|
39 | onSuccess: function (event, almanac) {
|
40 | almanac.addRuntimeFact('screwdriverAficionado', true)
|
41 | },
|
42 | onFailure: function (event, almanac) {
|
43 | almanac.addRuntimeFact('screwdriverAficionado', false)
|
44 | }
|
45 | }
|
46 | engine.addRule(drinkRule)
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | let inviteRule = {
|
54 | conditions: {
|
55 | all: [{
|
56 | fact: 'screwdriverAficionado',
|
57 | operator: 'equal',
|
58 | value: true
|
59 | }, {
|
60 | fact: 'isSociable',
|
61 | operator: 'equal',
|
62 | value: true
|
63 | }]
|
64 | },
|
65 | event: { type: 'invite-to-screwdriver-social' },
|
66 | priority: 5
|
67 | }
|
68 | engine.addRule(inviteRule)
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | let facts
|
74 | engine
|
75 | .on('success', (event, almanac) => {
|
76 | console.log(facts.accountId + ' DID '.green + 'meet conditions for the ' + event.type.underline + ' rule.')
|
77 | })
|
78 | .on('failure', event => {
|
79 | console.log(facts.accountId + ' did ' + 'NOT'.red + ' meet conditions for the ' + event.type.underline + ' rule.')
|
80 | })
|
81 |
|
82 |
|
83 | facts = { accountId: 'washington', drinksOrangeJuice: true, enjoysVodka: true, isSociable: true }
|
84 | engine
|
85 | .run(facts)
|
86 | .then(() => {
|
87 | facts = { accountId: 'jefferson', drinksOrangeJuice: true, enjoysVodka: false, isSociable: true }
|
88 | return engine.run(facts)
|
89 | })
|
90 | .catch(console.log)
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|