Backend Development 10 min read

Design and Implementation of a Dynamic Business Rule Engine Using QLExpress

This article presents a lightweight solution for dynamic business rule management by abstracting rules into variable‑condition‑result models, employing the QLExpress script engine for execution, and demonstrating how to bind Java methods, enabling flexible, maintainable backend logic without extensive if‑else code.

政采云技术
政采云技术
政采云技术
Design and Implementation of a Dynamic Business Rule Engine Using QLExpress

In everyday development, complex and frequently changing business logic often leads to cumbersome if‑else code, making maintenance tedious and error‑prone.

To address this, we propose separating dynamic business rules from the main workflow, allowing users to author rules that the system converts into executable code, thus freeing developers from manual conditional logic.

Use Cases

We illustrate the approach with two scenarios: financial risk control and transaction promotion, each requiring configurable rule logic.

Requirement Model

Rule Data Model

Business scenarios are abstracted into a data model where a rule consists of variables, conditions, and results, enabling bidirectional conversion between rule definitions and executable code.

System Model

The system is divided into two modules: a rule executor that selects and runs rules, and a rule manager that provides structured parsing and exchange of business rules and code.

Implementation

The implementation consists of several layers: a script language executor for dynamic code execution, structuring of business rule data, and allowing business users to define custom algorithms within the system.

Script Language Executor

Within the JVM, various script languages are possible; this solution adopts Alibaba's open‑source QLExpress for its lightweight, extensible expression language.

Comparison of Groovy and QLExpress shows both have high performance, but QLExpress offers stronger extensibility, which aligns with our requirements.

ExpressRunner runner = new ExpressRunner();
// Business context parameters
DefaultContext
context = new DefaultContext<>();
context.put("a", 1);
context.put("b", 2);
context.put("c", 3);
// Rule script
String express = "a + b * c";
// Execute rule
Object r = runner.execute(express, context, null, true, false);
System.out.println(r);

Integrating with business systems only requires providing context parameters and handling the rule result.

Data Structuring

Rules are decomposed into three dimensions: variables, conditions, and results. Simple conditions combine variables with logical operators, while complex conditions can be composed of multiple simple ones.

Example: for an order discount rule, variables are derived from user ID and product ID, conditions check location, purchase history, and time of day, and the result yields an 80% discount.

public void functionABC(Long a, Integer b, String c) {
    System.out.println("functionABC");
}

ExpressRunner runner = new ExpressRunner();
runner.addFunctionOfServiceMethod("abc", singleton, "functionABC", new Class[]{Long.class, Integer.class, String.class}, null);
String exp = "abc(a,b,c)";
IExpressContext
context = new DefaultContext<>();
context.put("a", 1L);
context.put("b", 2);
context.put("c", "3");
runner.execute(exp, context, null, false, false);

QLExpress can bind Java class or object methods, and even remote services (e.g., via Dubbo), giving business users control over which methods to invoke.

Summary

The presented solution structures rule data, translates it into script language, and executes it, achieving flexible rule management while keeping core business processes stable. It has been applied in government procurement cloud’s merchant transaction and expense control modules.

Future Outlook

Future work includes supporting multiple or nested rule paths, optimizing intermediate variable handling during execution, and further enhancing the system’s usability and efficiency.

Javarule enginebackend developmentQLExpressdynamic business rulesscript execution
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.