Backend Development 17 min read

Using URule Rule Engine in Java Projects: Installation, Configuration, and Practical Examples

This article introduces the URule rule engine, compares it with other engines, explains its features, demonstrates how to install and configure it in a Spring Boot project, and provides detailed guidance on creating variable, constant, parameter, and action libraries, building rule sets, decision tables, and applying them to real‑world scenarios.

Top Architect
Top Architect
Top Architect
Using URule Rule Engine in Java Projects: Installation, Configuration, and Practical Examples

When refactoring a project, the author encountered many conditional statements and decided to explore rule engines, ultimately choosing URule for its browser‑based visual configuration and cross‑platform support.

URule is a component that separates complex business logic from code, allowing rules to be defined visually or via scripts. It offers both open‑source and Pro versions, with the Pro version providing additional features such as cross‑decision tables and advanced scoring.

The feature comparison table highlights differences between the open‑source and Pro editions, showing which capabilities are available in each.

Installation and Usage

Create an empty database and configure the datasource in edas-rule-server :

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

Start the service and access the visual editor at http://localhost:8090/urule/frame to verify the startup.

Library Files

URule uses four types of libraries:

Variable library – maps Java POJOs (e.g., Stu ) to rule variables.

Constant library – stores enums or constant values.

Parameter library – temporary variables stored as a map.

Action library – exposes Spring beans and methods for rule actions.

Example of a POJO with @Label annotations:

package com.cicada;

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

/**
 * @author 往事如风
 * @version 1.0
 * @date 2023/3/3 15:38
 */
@Data
public class Stu {
    @Label("姓名")
    private String name;

    @Label("年龄")
    private int age;

    @Label("班级")
    private String classes;
}

Action class example exposing methods to the rule engine:

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="测试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 defining libraries, they can be imported into rule sets. Rule sets can be created in two styles:

Wizard‑style : visual point‑and‑click configuration.

Script‑style : write rule scripts directly.

Example of a simple wizard‑style rule with IF‑THEN‑ELSE logic is shown, followed by Java code that loads the knowledge package and executes the rules:

package com.cicada;

import cn.hutool.core.bean.BeanUtil;
import com.Result;
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 com.cicada.req.StuReq;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;

@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());
    }
}

Decision Table

A decision table provides a tabular view of rules, making them easier to understand for non‑technical stakeholders. The article includes a sample decision table for user promotion scenarios.

Use Cases

The author describes a real‑world scenario where users are promoted from regular to member to elite member based on registration counts, order amounts, and order continuity rates, and shows how to model these rules with URule.

Conclusion

URule can help decouple complex business logic from code, offering visual rule configuration and powerful execution capabilities. However, careful design and stable library definitions are required to avoid frequent changes once rules are in production.

References and source code links are provided for further exploration.

Javarule enginebackend developmentSpring BootURuleDecision Table
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.