In the AI Era, Why We’re Rewriting the Rules Engine — The QLExpress4 Refactoring Journey

In the AI Era, Why We’re Rewriting the Rules Engine — The QLExpress4 Refactoring Journey

AliMei Guide — QLExpress4 in the AI Era

---

Introduction

In the AI era, demand for rule engines has grown stronger than ever.

QLExpress4 is a major refactor — delivering huge performance boosts, better observability, and making it AI-friendly.

---

Why Still Build Rule Engines in the AI Age?

One day, a friend asked:

> "It's already the AI era — why are you still working on rule engines?"

QLExpress is a Java-based embedded scripting engine with Java-like syntax. It’s lightweight, easy to integrate, and highly flexible — making it popular for business rule scenarios.

Reason 1: Open-source commitment. QLExpress has been part of the community for years — we can’t abandon it casually.

Reason 2: Developer demand is still growing. Since 2022, project stars have more than doubled.

QLExpress4 is the largest refactor since open-sourcing, with nearly every line rewritten. The refactor was led by:

  • Me (former DingTalk, now iOrange)
  • Xiao A (former Supply Chain Platform, now Taotian)
  • Xiao B (former Alimama)

---

image

---

The Need for a Rewrite

The old codebase was patchworked for nearly a decade, accumulating 300+ hard-to-fix issues while parsing and VM tech evolved.

We decided to stop patching and rewrite it completely.

---

What’s New in QLExpress4

  • Performance
  • Compilation: ~10× faster
  • Execution: ~2× faster
  • Developer & AI Experience
  • Token-level error hints
  • Native JSON syntax support
  • Easy string operations
  • Expression tracing for deep debugging & observability
image

---

Feature Scenarios

Below are real-world use cases of QLExpress4 (latest 4.0.3) in large-scale deployments.

---

Rule Attribution Clustering — Taotian Group

Challenge:

Business staff write rules without visibility into real-time execution outcomes. For example, in

`isVip && not logged in for 10+ days`:

  • How many are blocked by VIP condition?
  • How many by login time condition?

Solution:

QLExpress4’s expression tracing records values for all intermediate steps, enabling attribution analysis for diagnosis and optimization.

image

See Docs: [Expression Calculation Tracing][1]

---

Dynamic Model Mapping — DingTalk

QLExpress4’s native JSON syntax allows quick creation of complex Java objects directly in scripts.

Example: JSON Lists & Maps

list = [
  { "name": "Li", "age": 10 },
  { "name": "Wang", "age": 15 }
]
assert(list[0].age == [10, 15])

Example: Complex Nested Java Object

myHome = {
  '@class': 'com.alibaba.qlexpress4.inport.MyHome',
  'sofa': 'a-sofa',
  'chair': 'b-chair',
  'myDesk': {
    'book1': 'The Moon and Sixpence',
    '@class': 'com.alibaba.qlexpress4.inport.MyDesk'
  }
}
assert(myHome.sofa == 'a-sofa')

📄 Docs: [Convenient Syntax Elements][2]

---

AI-Friendly Design

Initially titled “Experience Improvements,” this section was renamed AI-Friendly — many rules are now AI-generated rather than manually written.

Expression Tracing Tree Example:

        || true
       /     \
   ! false   myTest true
   /         /    \
true       a  10   11

📄 Docs: [Expression Calculation Tracing][1]

Benefit:

AI models can debug & diagnose rules more effectively using intermediate results.

We also provide qlexpress-mcp [3] example integrations.

---

Performance Optimizations

Benchmark Summary

  • Compile cache off: ~10× faster than v3
  • Compile cache on: ~2× faster than v3

Scenarios:

| Scenario | Focus Area | Chart |

|-------------------------|---------------|----------------------------------------|

| Long Script | Compile Perf |

image

|

| Simple Calc (Cache On) | Execution Perf|

image

|

| Fibonacci Sequence | Recursion Perf|

image

|

---

Online RT Tests (Taotian Timeliness):

| Scenario | Throughput | Success% | RT | Screenshot |

|-----------------|--------------|----------|------|--------------------------------------|

| Product Details | 4M/s | 99.9999% | <40µs|

image

|

| Order Rendering | 364k/s | 99.999% | <100µs|

image

|

| Transaction Create | 163k/s | — | — | — |

---

Pipeline Improvements

QLExpress scripts execute in 2 phases:

CompilationExecution

image

Compilation Pipeline

  • Replaced custom parser with Antlr4
  • Antlr4 uses ALL algorithm with DFA caching
  • Pre-initializes common paths to reduce first-run penalty
image

Execution Pipeline

  • Optimized timeout checks
  • Reduced instruction count: complex instructions replace multiple small ones → better speed & lower memory use
image

---

Test Cases & Documentation Engineering

Unit tests are essential — structured by feature in `src/test/resources/testsuite`.

Example:

  • Feature: `array`
  • Test file: `array_index_out_of_bound.ql`
  • 📊 Coverage: 77% lines, 100% scenarios
image
image

---

Using AsciiDoc for Docs

Instead of Markdown, we use AsciiDoc (`adoc`) for its ability to include snippets directly from source code.

Example: Tag in Java Test

// tag::firstQl[]
Express4Runner express4Runner = new Express4Runner(InitOptions.DEFAULT_OPTIONS);
Map context = new HashMap<>();
context.put("a", 1);
context.put("b", 2);
context.put("c", 3);
Object result = express4Runner.execute("a + b * c", context, QLOptions.DEFAULT_OPTIONS);
assertEquals(7, result);
// end::firstQl[]

Include in AsciiDoc

include::./src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=firstQl]

---

Problem: GitHub does not render `include` syntax.

Solution: Use GitHub Actions to preprocess.

Example Workflow: [reduce-adoc.yml][7]

name: Reduce Adoc
on:
  push:
    paths:
      - README-source.adoc
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: sudo gem install asciidoctor-reducer
      - run: asciidoctor-reducer -o README.adoc README-source.adoc
      - uses: EndBug/add-and-commit@v9
        with:
          add: README.adoc
image

---

Appendix

[1]: https://github.com/alibaba/QLExpress?tab=readme-ov-file#表达式计算追踪-1

[2]: https://github.com/alibaba/QLExpress?tab=readme-ov-file#方便语法元素

[3]: https://github.com/DQinYuan/qlexpress-mcp

[7]: https://github.com/alibaba/QLExpress/blob/main/.github/workflows/reduce-adoc.yml

---

This rewrite groups related ideas into sections, improves clarity with headings and bullet points, and uses bold emphasis for key concepts so developers can quickly scan QLExpress4’s capabilities.

Read more