Skip to content
All projects
Case studyCS/21
StatusPersonal project
TypeSystems
Year2024

NexRule

A rule engine that turns rule strings into stored ASTs

Rules written as plain strings (age > 18, status == 'active', joined with AND / OR) are tokenized into an abstract syntax tree, stored in MongoDB, combined with other rules, and evaluated against JSON data.

  • Tokenizer + AST
  • Stored ASTs
  • Rule combination
  • Dockerised

§1

The problem

Eligibility rules hard-coded in application logic need a deploy every time the business changes a threshold.

§2

My role

Wrote the tokenizer, AST builder and evaluator, and built the REST API and its tests.

§3

Architecture

Fig. 1 · NexRule architecture
ASTRULE STRINGage > 18 AND …TOKENIZERAST BUILDERstack-basedMONGODBstored ASTsEVALUATORtree walkJSON DATATRUE / FALSE
clientservicedataexternal

§4

Key decisions

  1. D/01

    Parse once, evaluate many

    Rules compile to an AST that is persisted, so evaluation is a cheap tree walk.

  2. D/02

    Combine at the tree level

    Several rules merge into one AST under AND nodes, so a combined rule is just another tree.

§5

Challenges

  • Keeping evaluation a pure function of (tree, data) so it's easy to test.
  • Next step: a precedence-aware parser for nested parentheses.

§6

Stack

  • Node.js
  • Express
  • MongoDB
  • Docker
  • Jest

Next case study

Veil

An AI data firewall: keep using any LLM without leaking customer data, secrets or code