Fundamentals 20 min read

Rule Engine Basics: Getting Started with the Open‑Source EasyRule

The article walks through the problems of hard‑coded if‑else logic, introduces rule engines as a flexible solution, explains EasyRule’s concepts, advantages, and classifications, and provides step‑by‑step Java examples using annotations, fluent API, MVEL, and YAML configurations.

Ubiquitous Tech
Ubiquitous Tech
Ubiquitous Tech
Rule Engine Basics: Getting Started with the Open‑Source EasyRule

Rule Engine Overview

A rule engine separates business rules from application code, allowing rules to be defined, modified, and executed without changing the underlying program. This improves flexibility, maintainability, efficiency, scalability, and decision‑making capability, especially for complex logic and large data volumes.

Typical Application Scenarios

Financial services – credit scoring, fraud detection

Telecom – billing, promotional strategies

E‑commerce – order processing, inventory management

Healthcare – claim validation, diagnosis assistance

Rule Engine Classifications

Expression‑based engines (e.g., QLExpress, Aviator) – focus on extracting mutable logic via expressions.

Rule‑based engines (e.g., Drools, urule, EasyRule) – emphasize matching and managing dispersed rules.

Decision‑based engines – provide rule sets, decision trees, scoring cards.

Process‑oriented engines – orchestrate complex workflow nodes (e.g., LiteFlow).

EasyRule Overview

EasyRule is a lightweight, open‑source Java rule engine that lets developers define rules as POJOs. Repository: https://github.com/j-easy/easy-rules

Core Concepts

Fact – input data used in rule evaluation.

Condition – boolean expression that determines whether a rule matches.

Action – code executed when the condition is true.

Rule – a combination of a condition and an action.

Rules – a collection of Rule objects.

RulesEngine – component that evaluates facts against registered rules.

Defining Rules – Four Approaches

1. Annotation‑based rule

@Rule(name = "WeatherRule", description = "If it rains, take an umbrella")
public class WeatherRule {
    @Condition
    public boolean itRains(@Fact("rain") boolean rain) {
        return rain;
    }
    @Action
    public void takeAnUmbrella() {
        System.out.println("It is raining, please take an umbrella");
    }
}

2. Fluent‑API rule

Rule weatherRule = new RuleBuilder()
    .name("WeatherRule")
    .description("If it rains, take an umbrella")
    .when(facts -> facts.get("rain").equals(true))
    .then(facts -> System.out.println("It is raining, take an umbrella!"))
    .build();

3. MVEL expression rule

Rule weatherRule = new MVELRule()
    .name("WeatherRule")
    .description("If it rains, take an umbrella")
    .when("rain == true")
    .then("System.out.println(\"It is raining, take an umbrella!\");");

4. YAML‑based rule

# weather-rule.yml
name: "WeatherRule"
description: "If it rains, take an umbrella"
condition: "rain == true"
actions:
  - "System.out.println(\"If it rains, take an umbrella!\");"

Loading and Firing Rules

Facts facts = new Facts();
facts.put("rain", true);
Rule weatherRule = /* any of the definitions above */;
Rules rules = new Rules();
rules.register(weatherRule);
DefaultRulesEngine engine = new DefaultRulesEngine();
engine.fire(rules, facts);

The engine evaluates each rule's condition against the provided facts; when a condition evaluates to true, the associated action is executed.

Important Annotations

@Rule – declares a rule class.

@Condition – marks the method that returns a boolean condition.

@Action – marks the method executed when the condition is true.

@Fact – binds a method parameter to a fact name.

@Priority – defines rule execution order (lower value = higher priority).

Integration Example (JD slow‑SQL component)

The JD sql-analysis component uses EasyRule to dynamically evaluate SQL analysis results. A sample YAML rule used in that component:

- name: "VIP满100减20"
  condition: "user.level == 'VIP' && totalAmount > 100"
  action: "discount = 20"

- name: "新用户首单50%折扣"
  condition: "user.isFirstOrder == true"
  action: "discount = totalAmount * 0.5"

These rules are loaded at runtime, allowing business users to modify discount logic without changing Java code.

References

EasyRule GitHub repository: https://github.com/j-easy/easy-rules

Expression‑based engines: QLExpress (https://juejin.cn/post/7236670763271847994), Aviator (https://juejin.cn/post/6959436412760358943)

Rule‑based engines: Drools, urule, JVS‑rules

Process‑oriented engine example: LiteFlow (https://github.com/dromara/liteflow)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javarule engineAnnotationMVELyamlbusiness rulesEasyRuleFluent API
Ubiquitous Tech
Written by

Ubiquitous Tech

A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.

0 followers
Reader feedback

How this landed with the community

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.