Backend Development 16 min read

Using URule Rule Engine in Spring Boot: Installation, Configuration, and Practical Examples

This article introduces the URule rule engine, detailing its background, features, installation steps, core concepts such as library files and rule sets, provides code examples for Spring Boot integration, and discusses practical usage scenarios and decision table configuration.

Architect
Architect
Architect
Using URule Rule Engine in Spring Boot: Installation, Configuration, and Practical Examples

Background

The author needed a flexible way to handle complex conditional logic during a project refactor and explored various rule engines, ultimately selecting URule for its browser‑based visual configuration and cross‑platform support.

Introduction

URule is a component that can be embedded in applications to separate business rules from code, allowing the program to focus on core logic while the engine evaluates inputs against predefined rules.

Features Comparison

A table compares the PRO and open‑source versions of URule, highlighting features such as wizard‑style decision sets, script‑style decision sets, decision trees, decision flows, decision tables, and advanced capabilities like knowledge‑package compression and server‑push.

Installation & Usage

Four deployment modes are described (embedded, local, distributed, standalone). The article focuses on the open‑source version integrated with Spring Boot.

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 configuring the database, start the edas-rule-server service; it will create the necessary tables on first launch.

Core Concepts

URule consists of two main parts: the designer (library files) and the execution engine. Library files include variable libraries, constant libraries, parameter libraries, and action libraries.

Variable Library

Maps Java POJOs (e.g., Stu ) to rule variables using @Label annotations.

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;
}

Constant Library

Defines enums or constant values that can be referenced in rules.

Parameter Library

Provides temporary variables (similar to a Map) for rule evaluation.

Action Library

Exposes Spring beans methods to the rule engine using @ExposeAction annotations.

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;
        } else if(username.equals("张三")){
            return true;
        }
        return false;
    }
    @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);
        }
    }
}

Rule Sets and Decision Tables

Rules can be created via a wizard‑style UI (if‑then‑else) or script‑style. Decision tables offer a spreadsheet‑like view for easier comprehension.

Example Controller

package com.cicada;

import cn.hutool.core.bean.BeanUtil;
import com.bstek.urule.Utils;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rule")
public class RuleDataController {
    @PostMapping("/stu")
    public Result rule(@RequestBody StuReq stuReq) throws IOException {
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("xxx/xxx");
        KnowledgeSession knowledgeSession = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
        Stu stu = BeanUtil.copyProperties(stuReq, Stu.class);
        knowledgeSession.insert(stu);
        knowledgeSession.fireRules();
        return Result.success(stu.getTeacher());
    }
}

The controller loads the rule package, inserts a Stu object, fires the rules, and returns the result configured in the "then" branch.

Application Scenarios

A practical scenario is presented where user membership levels (ordinary, member, elite) are upgraded based on registration counts, order amounts, and order continuation rates, modeled with a decision table.

Conclusion

URule can simplify complex business logic by externalizing rules, but it requires careful analysis and abstraction of requirements; it is an optional enhancement rather than a mandatory component.

Reference Source Code

Links to the author's Git repositories are provided for further exploration.

BackendJavarule engineSpring BootURuleDecision Table
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

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.