Mastering URule: A Hands‑On Guide to Building Rule Engines in Java

This article walks through the background, features, installation, core concepts, and practical code examples of the URule rule engine, showing how to configure variable, constant, parameter, and action libraries, create decision tables and rule sets, and integrate the engine with a Spring Boot application.

Programmer DD
Programmer DD
Programmer DD
Mastering URule: A Hands‑On Guide to Building Rule Engines in Java

1. Background

During a recent project refactor, many conditional checks were needed. Instead of writing numerous if‑else statements, the author explored rule engines and chose URule for its browser‑based visual configuration and ease of integration.

2. Introduction

A rule engine is a component that separates complex business logic from code, taking input data, applying predefined rules, and producing results. URule runs on Windows, Linux, Unix and offers a pure‑browser editing mode without additional installations.

Feature Comparison (Pro vs. Open‑Source)

(The original table has been omitted for brevity.)

3. Installation & Usage

URule can be used in embedded, local, distributed, or standalone service modes. The author integrated the open‑source version with Spring Boot, creating a simple project structure.

Configure the database in edas-rule-server and start the service; the first launch creates the necessary tables.

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/urule-data?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=mysql

After starting, open http://localhost:8090/urule/frame in a browser to verify the UI.

4. Core Concepts

4.1 Overall Architecture

URule consists of two main parts: the designer (library files and rule files) and the rule execution engine.

4.2 Library Files

Four library types exist: variable, parameter, constant, and action libraries, analogous to Java entities, enums, constants, and methods.

4.2.1 Variable Library

Variable libraries map Java POJOs (e.g., Stu) to rule variables, allowing direct use in rules.

package com.cicada;

import com.bstek.urule.model.Label;
import lombok.Data;

@Data
public class Stu {
    @Label("姓名")
    private String name;
    @Label("年龄")
    private int age;
    @Label("班级")
    private String classes;
}

The @Label annotation links POJO fields to variable library titles.

4.2.2 Constant Library

Constant libraries store enums or fixed values, such as gender or organization codes.

4.2.3 Parameter Library

Parameter libraries act like a temporary Map for rule execution, holding dynamic variables.

4.2.4 Action Library

Action libraries expose Spring beans and methods to rules using @ExposeAction. Example:

package com.bstek.urule.cicada;

import com.bstek.urule.action.ActionId;
import com.bstek.urule.model.ExposeAction;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("action")
public class Action {
    @ActionId("Hello")
    public String hello() { return "hello"; }

    @ExposeAction(value="方法1")
    public boolean evalTest(String username) {
        if (username == null) return false;
        if (username.equals("张三")) return true;
        return false;
    }

    @ExposeAction(value="测试Int")
    public int testInt(int a, int b) { return a + b; }

    @ExposeAction(value="打印内容")
    public void printContent(String username, Date birthday) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (birthday != null) {
            System.out.println(username + "今年已经" + sd.format(birthday) + "岁了!");
        } else {
            System.out.println("Hello " + username);
        }
    }

    @ExposeAction(value="打印Stu")
    public void printUser(Stu m) {
        System.out.println("Hello " + m.getName() + ", is age:" + m.getAge());
    }
}

After adding a bean to the action library, its annotated methods become selectable in the rule UI.

5. Rule Sets and Decision Tables

Rule sets consist of if , then , and else parts. URule supports wizard‑style (visual) and script‑style rule sets.

5.1 Wizard‑Style Rule Set

Created via the “Decision Set” menu, import the necessary libraries, then configure conditions and actions through the visual interface.

5.2 Script‑Style Rule Set

Similar logic but written as scripts, requiring coding knowledge.

5.3 Decision Table

A tabular representation of rules, offering a clear view of conditions and outcomes, especially useful for non‑technical stakeholders.

6. Application Scenario

The author describes a membership promotion system where users can be upgraded from regular to member to elite member based on registration counts, order amounts, and order continuation rates. The rules are implemented using a decision table combined with variable and constant libraries.

7. Summary

Rule engines like URule can decouple complex business logic from application code, making the system easier to maintain and extend. However, successful adoption requires a solid understanding of the business requirements and careful abstraction of those rules.

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.

rule engineurulespring-boot
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.