@@ -107,30 +107,30 @@ npm install archetypes-rules
107
107
108
108
## 3. Usage
109
109
110
- ### 3.1. Example 1: Is this customer eligible for a discount?
110
+ ### 3.1. Example 1: Is this customer eligible for a discount
111
111
112
112
Suppose we have a very simple rule that checks whether a customer is eligible
113
113
for a discount. In order to be eligible, the customer simply needs to be a Gold
114
114
Card holder.
115
115
116
116
``` javascript
117
- const { Rule , RuleContext } = require (" archetypes-rules" );
117
+ const { Rule , RuleContext } = require (' archetypes-rules' )
118
118
119
119
// Create the rule
120
- const rule = new Rule (" eligibleForDiscount" );
120
+ const rule = new Rule (' eligibleForDiscount' )
121
121
122
122
// Add a Proposition, i.e., a statement that has a value of true or false
123
- rule .addProposition (" customerIsGoldCardHolder" , true );
123
+ rule .addProposition (' customerIsGoldCardHolder' , true )
124
124
125
125
// Create a RuleContext, i.e., a "Fact"
126
- const ruleContext = RuleContext (" eligibleForDiscountContext" );
126
+ const ruleContext = RuleContext (' eligibleForDiscountContext' )
127
127
128
128
// Provide the truth statement as to whether the actual customer
129
129
// has a Gold Card
130
- ruleContext .addProposition (" customerIsGoldCardHolder" , true );
130
+ ruleContext .addProposition (' customerIsGoldCardHolder' , true )
131
131
132
132
// Evaluate
133
- const result = rule .evaluate (ruleContext);
133
+ const result = rule .evaluate (ruleContext)
134
134
135
135
// Log the resulting Proposition
136
136
@@ -144,31 +144,31 @@ Say you provide a discount to a group of six or more people:
144
144
145
145
``` javascript
146
146
// Create the rule
147
- const rule = Rule (" eligible-for-group-discount" );
147
+ const rule = Rule (' eligible-for-group-discount' )
148
148
149
149
// Declare a "placeholder" variable for the actual number of people
150
150
// (This value will be retrieved from the RuleContext)
151
- rule .addVariable (" actual-num-people" , null );
151
+ rule .addVariable (' actual-num-people' , null )
152
152
153
153
// Declare the minimum number of people required for discount
154
- rule .addVariable (" min-num-people" , 6 );
154
+ rule .addVariable (' min-num-people' , 6 )
155
155
156
156
// Compare the two, i.e.,
157
157
// actual-num-people >= min-num-people
158
- rule .addOperator (Operator .GREATER_THAN_OR_EQUAL_TO );
158
+ rule .addOperator (Operator .GREATER_THAN_OR_EQUAL_TO )
159
159
160
160
// Create a RuleContext, i.e., a "Fact"
161
- const ruleContext = RuleContext (" eligible-for-group-discount-fact" );
161
+ const ruleContext = RuleContext (' eligible-for-group-discount-fact' )
162
162
163
163
// How many people are there?
164
- ruleContext .addVariable (" actual-num-people" , 5 );
164
+ ruleContext .addVariable (' actual-num-people' , 5 )
165
165
166
166
// Declare the "placeholder" minimun number of people required for discount
167
167
// (This value will be retrieved from the Rule)
168
- ruleContext .addVariable (" min-num-people" , " NULL_NUMBER_VARIABLE" );
168
+ ruleContext .addVariable (' min-num-people' , ' NULL_NUMBER_VARIABLE' )
169
169
170
170
// Evaluate
171
- const result = rule .evaluate (ruleContext);
171
+ const result = rule .evaluate (ruleContext)
172
172
173
173
// Log the resulting Proposition
174
174
@@ -191,33 +191,33 @@ eligible, a passenger must:
191
191
In order to determine this, we must compare a passenger’s facts with our rule.
192
192
193
193
``` javascript
194
- const { Rule , RuleContext , RuleElement } = require (" archetypes-rules" );
194
+ const { Rule , RuleContext , RuleElement } = require (' archetypes-rules' )
195
195
196
196
// Create the rule
197
- const rule = Rule (" eligible-for-upgrade" );
197
+ const rule = Rule (' eligible-for-upgrade' )
198
198
199
199
// Populate the rule using method chaining
200
200
rule
201
- .addProposition (" passenger-is-economy" , true )
202
- .addProposition (" passenger-is-gold-card-holder" , true )
203
- .addProposition (" passenger-is-silver-card-holder" , true )
204
- .addOperator (" OR " )
205
- .addOperator (" AND" )
206
- .addVariable (" passenger-carry-on-baggage-weight" , " NULL_NUMBER_VARIABLE" )
207
- .addVariable (" passenger-carry-on-baggage-allowance" , 15.0 )
208
- .addOperator (" LESS_THAN_OR_EQUAL_TO" )
209
- .addOperator (" AND" );
201
+ .addProposition (' passenger-is-economy' , true )
202
+ .addProposition (' passenger-is-gold-card-holder' , true )
203
+ .addProposition (' passenger-is-silver-card-holder' , true )
204
+ .addOperator (' OR ' )
205
+ .addOperator (' AND' )
206
+ .addVariable (' passenger-carry-on-baggage-weight' , ' NULL_NUMBER_VARIABLE' )
207
+ .addVariable (' passenger-carry-on-baggage-allowance' , 15.0 )
208
+ .addOperator (' LESS_THAN_OR_EQUAL_TO' )
209
+ .addOperator (' AND' )
210
210
211
211
// Create the RuleContext
212
- const fact = RuleContext (" eligibleForUpgradeFact" );
212
+ const fact = RuleContext (' eligibleForUpgradeFact' )
213
213
214
214
// Load it with the facts about the passenger
215
215
fact
216
- .addProposition (" passengerIsEconomy" , true )
217
- .addProposition (" passengerIsGoldCardHolder" , true )
218
- .addProposition (" passengerIsSilverCardHolder" , false )
219
- .addVariable (" passenger-carry-on-baggage-weight" , 10.0 )
220
- .addVariable (" passenger-carry-on-baggage-allowance" , " NULL_NUMBER_VARIABLE" );
216
+ .addProposition (' passengerIsEconomy' , true )
217
+ .addProposition (' passengerIsGoldCardHolder' , true )
218
+ .addProposition (' passengerIsSilverCardHolder' , false )
219
+ .addVariable (' passenger-carry-on-baggage-weight' , 10.0 )
220
+ .addVariable (' passenger-carry-on-baggage-allowance' , ' NULL_NUMBER_VARIABLE' )
221
221
222
222
// Log the resulting Proposition
223
223
0 commit comments