Why Rule Engines Matter: From Rete Algorithm to Node.js Nools
This article explains the origins and concepts of rule engines, demonstrates practical JavaScript implementations, dives into the Rete pattern‑matching algorithm, and explores design considerations for visual rule editors and lightweight Node.js components.
Concept
Rule engines are not new; they originated from rule‑based expert systems in the early days of artificial intelligence, using logical rules applied to a knowledge base to infer new information.
Example: awarding points based on product price and category.
const addPointsByOrderPrice = (order) => {
if (order.price > 500) {
add(order.account_id, 200);
} else if (order.price > 100) {
add(order.account_id, 80);
} else {
add(order.account_id, 20);
}
}When business requirements become more complex, hard‑coded if‑else logic is unsustainable, prompting the move to configuration‑driven rules.
let rules = await db.select("select * from rules");
const addPointsByOrderPrice = (order) => {
for (let rule of rules) {
if (order.price > rule.price) {
add(order.account_id, rule.point);
break;
}
}
}Further refinements may involve user tags, product tags, or multipliers, illustrating the tight coupling between business rules and code.
Rete Algorithm
The core algorithm used by Drools is the Rete algorithm, a forward‑chaining pattern‑matching technique whose performance is independent of the number of rules. It addresses two problems:
Efficient matching of many repeated conditions.
Rule conflict resolution.
Key concepts:
Fact : an object or attribute relationship.
Rule : an if … then statement; the left‑hand side (LHS) is the condition, the right‑hand side (RHS) is the action.
Module : the smallest indivisible condition (pattern).
DSL : domain‑specific language allowing business experts to define rules without technical details.
Matching process (simplified):
Select a rule r from the set of N rules.
Combine P patterns from the rule with M facts to form candidate combinations.
Test each combination; if LHS(r) is true, add the resulting RHS to the agenda.
Repeat until all rules and fact combinations are processed.
The algorithm uses Alpha nodes (single‑condition tests) and Beta nodes (join tests) with memory to avoid redundant evaluations.
Node.js Open‑Source Component – Nools
Nools implements the Rete algorithm in JavaScript. Although no longer actively maintained, it is useful for learning rule‑engine concepts.
var nools = require("nools");
var flow = nools.compile("./Demo2.nools"),
Param = flow.getDefined("Param");
var session = flow.getSession(new Param(11))
.on('assert', function (result) { console.log(result); })
.on('modify', function (result) { console.log(result, result.param1); })
.match().then(function () { console.log('end'); });Nools supports events such as assert, retract, modify, and fire, enabling fine‑grained control of rule execution.
define Param { param1: 0, note: 'test for nothing', constructor: function(p1){ this.param1 = p1; } }
rule condition1 {
when { p:Param p.param1 <= 10; }
then { assert("result111"); }
}
rule condition2 {
when { p:Param p.param1 > 10 && p.param1 <= 20; }
then {
assert('result2');
modify(p, function(){ p.param1 -= 10; });
}
}Scenario Design
A visual rule editor can let non‑developers define rules in natural language, which are then parsed into DSL for the engine. Example rule:
If
(old_user is true and active_days > 20) or new_user
Then
lottery_chance + 1Parsing can be implemented with PEG.js to convert the sentence into DSL.
The rule engine can be any Rete‑based implementation such as Nools or a lightweight library like json‑rules‑engine.
Discussion
Rule engines are widely used in large‑scale platforms (e.g., telecom billing, e‑commerce promotions, financial risk control). While Drools is powerful, its complexity and learning curve lead many teams to prefer lighter alternatives like Groovy, Aviator, EasyRule, or custom decision‑tree solutions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
