jsrules
jsrules
is a JavaScript rule engine that models formal propositional logic. It allows you to separate conditional logic from source code and database triggers in a reusable package, where explicit rules can be independently defined and managed.
jsrules
Overview of Rules
?
What are Rules are explicit constraints that govern actions.
Rules are defined and stored as JSON. They consist of three types of RuleElements
:
- Propositions: statements that are either,
true
,false
, ornull
(unknown) - Variables: symbols that represent the value of something
- Operators: Boolean and quantifier operators
RuleContexts
(aka "facts") and Rules
RuleContexts
are facts, stored in text files, databases, etc., that provide the informational context for the execution of Rules
. Rules
evaluate RuleContexts
, 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.
Example 1: Is this customer eligible for a discount?
Executing a Rule is simple. 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.
// Create the rulevar rule = 'eligibleForDiscount'; // Add a Proposition, i.e., a statement that has a value of true or falserule; // Create a RuleContext, i.e., a "Fact"var ruleContext = 'eligibleForDiscountContext'; // Provide the truth statement as to whether the actual customer// has a Gold CardruleContext; // Evaluatevar result = rule; // Log the resulting Propositionconsole; // Outputs// Proposition statement = customerIsGoldCardHolder, value = true
Example 2: Group discount for six or more people
Say you provide a discount to a group of six or more people:
// Create the rulevar rule = 'eligibleForGroupDiscount'; // Declare a "placeholder" variable for the actual number of people// (This value will be retrieved from the RuleContext)rule; // Declare the minimun number of people required for discountrule; // Compare the two, i.e.,// actualNumPeople >= minNumPeoplerule; // Create a RuleContext, i.e., a "Fact"var ruleContext = 'eligibleForGroupDiscountFact'; // 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; // Evaluatevar result = rule; // Log the resulting Propositionconsole; // OUTPUT:// Proposition statement =// (actualNumPeople >= minNumPeople), value = false
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.
// Create the rulevar rule = 'eligibleForUpgrade'; // Populate the rule using method chainingrule ; // Create the RuleContextvar fact = 'eligibleForUpgradeFact'; // Load it with the facts about the passengerfact ; // Log the resulting Propositionconsole; // Outputs (as a single string; newlines added here for readability):// Proposition statement = (// (passengerIsEconomy AND// (passengerIsGoldCardHolder OR passengerIsSilverCardHolder)// ) AND (// passengerCarryOnBaggageWeight <= passengerCarryOnBaggageAllowance// )// ), value = true
Installation
npm
$ npm install jsrules
bower
$ bower install jsrules
Specs/tests
Execute specs (and code coverage) with either
$ grunt test
or
$ npm test
jsrules
: the Rule Archetype Pattern
The origin of For a detailed description of jsrules
, please read chapter 12, “Rule archetype pattern,” in Enterprise Patterns and MDA: Building Better Software with Archetype Patterns and UML. I cannot recommend this book enough, and my thanks go to its authors — Jim Arlow and Ila Neustadt — for their permission to avail the “Rule Archetype Pattern” to JavaScript developers.
Development roadmap
Quality assuranceCode coverage withistanbul
.travis-ci
integration.Complexity reports.- Persistence: create examples where
Rules
are created, read, updated, and deleted (e.g., Redis or MongoDB)RuleContexts
(facts) are retrieved from multiple data stores- Universally-unique identifiers: create a
uuid
property forRules
,RuleContexts
, andRuleElements
(Propositions
,Variables
, andOperators
)