archetypes-rules
Move conditional logic out of source code and database triggers and into a reusable package, where explicit rules can be independently defined and managed.
archetypes-rules
models Boolean logic. Instead of writing conditionals like
if / else if / else, you can instead create Rules that describe and
evaluate Facts (aka, RuleContexts).
Table of Contents
1. Overview
Figure 1: Class diagram for the archetypes.rules
namespace.
Rules
1.1. Rules are explicit constraints that govern actions. Rules evaluate Facts, or RuleContexts.
Rules are defined with⏤and stored as⏤JSON.
RuleElements
1.2. Rules contain one or more RuleElements. There are three types of RuleElements
:
Propositions
, Operators
, and Variables
.
Propositions
1.2.1. Propositions are statements that are either, true
, false
, or null
(unknown).
Learn more...
Variables
1.2.2. Variables are symbols that represent the value of something. Learn more...
Operators
1.2.3. Operators: Boolean and quantifier operators.
RuleContexts
(aka "facts")
1.3. RuleContexts
are facts, stored as JSON in files, databases, etc.
Rules
evaluate RuleContexts
/Facts
1.4. Rules
evaluate RuleContexts
. During evaluation, we determine whether a
RuleContext
/Fact
complies with a Rule
.
returning a Proposition
that tells us whether a given set of facts conform to
the defined Rule
.
RuleElements
are evaluated using
Reverse Polish Notation (RPN).
See the examples below for details.
2. Installation
npm install archetypes-rules
3. Usage
3.1. Example 1: Is this customer eligible for a discount
Suppose we have a very simple rule that checks whether a customer is eligible for a discount. In order to be eligible, the customer simply needs to be a Gold Card holder.
const Rule RuleContext = // Create the ruleconst rule = 'eligibleForDiscount' // Add a Proposition, i.e., a statement that has a value of true or falserule // Create a RuleContext, i.e., a "Fact"const ruleContext = // Provide the truth statement as to whether the actual customer// has a Gold CardruleContext // Evaluateconst result = rule // Log the resulting Proposition // Outputs// Proposition statement = customerIsGoldCardHolder, value = true
3.2. Example 2: Group discount for six or more people
Say you provide a discount to a group of six or more people:
// Create the ruleconst rule = // Declare a "placeholder" variable for the actual number of people// (This value will be retrieved from the RuleContext)rule // Declare the minimum number of people required for discountrule // Compare the two, i.e.,// actual-num-people >= min-num-peoplerule // Create a RuleContext, i.e., a "Fact"const ruleContext = // How many people are there?ruleContext // Declare the "placeholder" minimun number of people required for discount// (This value will be retrieved from the Rule)ruleContext // Evaluateconst result = rule // Log the resulting Proposition // OUTPUT:// Proposition statement =// (actualNumPeople >= minNumPeople), value = false
3.3. Example 3: Is an airline passenger eligible for an upgrade?
In this example, we’re determining whether a given airline passenger is eligible to have their coach seat upgraded to a first-class seat. In order to be eligible, a passenger must:
- Be in economy class now and either
- Hold a Gold member card or
- Hold a Silver member card and
- Their carry-on luggage must be less than or equal to 15.0 pounds.
In order to determine this, we must compare a passenger’s facts with our rule.
const Rule RuleContext RuleElement = // Create the ruleconst rule = // Populate the rule using method chainingrule // Create the RuleContextconst fact = // Load it with the facts about the passengerfact // Log the resulting Proposition // =>// Proposition statement = (// (passengerIsEconomy AND// (passengerIsGoldCardHolder OR passengerIsSilverCardHolder)// ) AND (// passenger-carry-on-baggage-weight <= passenger-carry-on-baggage-allowance// )// ), value = true
4. Maintainers
The Maintainer Guide describes how we develop and release archetype-rules (and has useful information for Maintainers and Trusted Committers).
5. Contributions
We gratefully accept Pull Requests. Here's what you need to know to get started.
Before submitting a Pull Request, please read our:
- Code of Conduct
- Contributing Aggreement
- Developer Guide
- Maintainer/Trusted Committer Guide
- Architecture Decision Records
6. License
MIT © 2019 Greg Swindle