1. Background
Some time ago, when I was doing project refactoring, I encountered a lot of conditions that needed to be judged in many places. Of course, a lot of if-else judgments can be used to solve it, but I didn't know what was going on at the time, so I wanted to play something else. Ever since, I went to investigate the rule engine.
Of course, there are many mature rule engines on the market with many functions and good performance. However, I just want to play something different (don’t do this when you do technology selection, this is a negative teaching material). In the end, a rule engine of URule attracted me, mainly because it can be directly configured by using a browser, without too much installation, and the visual rules are also well done. After a series of research, it was integrated into the project later, and the results of the research were recorded by the way.
2. Introduction
A rule engine is actually a component that can be embedded into a program. The complex judgment rules of the program are separated from the business code, so that the program only needs to care about its own business, and does not need to make complex logical judgments; the simple understanding is that the rules accept a set of input data and are configured through predetermined rules , and output a set of results.
Of course, there are many mature rule engines on the market, such as: Drools, Aviator, EasyRules, etc. But URule, which can run on various types of operating systems such as Windows, Linux, and Unix, adopts a pure browser editing mode, does not need to install tools, and directly edits and tests rules on the browser.
Of course, this rule engine has the difference between the open source version and the pro version. As for what the pro version is, everyone understands it. Let’s put a table below to understand the specific differences.
characteristic | PRO version | open source version |
---|---|---|
guided decision set | have | have |
scripted decision sets | have | have |
decision tree | have | have |
decision flow | have | have |
decision table | have | have |
cross decision table | have | none |
complex scorecard | have | none |
File name, project name refactoring | have | none |
Parameter name, variable constant name refactoring | have | none |
Excel decision table import | have | none |
Rule set template saving and loading | have | none |
Chinese project name and file name support | have | none |
The server pushes the knowledge package to the client function support | have | none |
Knowledge package optimization and compression support | have | none |
Push-pull support for large knowledge packages in client-server mode | have | none |
Support for execution groups in rule sets | have | none |
Support for wizard-style condition and action configuration for all nodes in the rule flow | have | none |
Cycle rule multiple cycle unit support | have | none |
Support for unconditional execution in loop rules | have | none |
Import project automatic renaming function | have | none |
Rule tree construction optimization | have | none |
Object lookup index support | have | none |
Support for short-circuit calculations in rule trees | have | none |
Rule condition redundant calculation cache support | have | none |
Scenario-based batch scenario testing function | have | none |
Knowledge package call monitoring | have | none |
More complete file read and write permission control | have | none |
Knowledge Pack Version Control | have | none |
Hot deployment of SpringBean and Java classes | have | none |
Technical Support | have | none |
3. Installation and use
In actual use, there are four ways to use URule Pro, namely embedded mode, local mode, distributed computing mode and independent service mode.
But we don't consider URule Pro here. Our own open source version is a secondary development based on the integration of springboot in the open source version. After searching around, there is actually a solution. The general project modules are as follows:
To create an empty database by yourself, you only need to modify the configuration of the database in the edas-rule-server service, and then start the service. After the first startup is complete, a table will be created in the database.
properties
复制代码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
As mentioned above, it is purely using a browser to edit and configure rules. You only need to open the browser and enter the address: http://localhost:8090/urule/frame. When you see this interface, it means that the startup is successful.
4. Basic concepts
3.1 Overall Introduction
Let me talk about the components of URule, mainly two parts: 1. The designer part 2. The rule execution engine. The designer part is mainly composed of library files and rule files. Let's look at the overall structure diagram
3.2 Library files
As shown in the figure above, there are 4 types of library files, including variable library, parameter library, constant library and action library. In fact, it is similar to the entity objects, enumerations, constants and methods in the system developed by Java.
As mentioned above, the rules are configured visually. In the process of configuring rules, it is necessary to introduce various defined library files, and then combine business requirements to configure business rules that meet business scenarios, so there are library files everywhere.
3.2.1 Variable library file
In business development, we will create many Getter and Setter Java classes, such as PO, VO, BO, DTO, POJO, etc. In fact, these new objects are mainly used as data carriers to transmit data.
In URule, the variable library is used to map these objects, which can then be used in rules, and finally complete the interaction between business and rules. The last picture is used to create a variable library
By the way, there are so many visual configurations in the above nonsense, this is the first time to show the configuration interface, ashamed.
The above picture is clear at a glance. Right click under the "Library" menu, then click Add variable library, and finally define the name of the variable library you like. Of course, the name only supports Chinese or English, and other characters are not available.
After creating the variable library, you can edit the variable library, which can be considered as adding attributes to POJO
I don't talk about any terminology, just understand it personally. On the left side of the figure is the creation class, where name is its alias, and the configuration rule uses it to replace this class. On the right side of the picture are the attributes of the class. I have written a few casually here, and I guess I can understand it after reading it.
Finally, create the corresponding class in the business system. Note that the fully qualified name is consistent with the class path of the configuration variable library.
package com.cicada;
import com.bstek.urule.model.Label;
import lombok.Data;
/**
* @author 往事如风
* @version 1.0
* @date 2023/3/3 15:38
* @description
*/
@Data
public class Stu {
@Label("姓名")
private String name;
@Label("年龄")
private int age;
@Label("班级")
private String classes;
}
Finally, let’s talk about this @Label
annotation. This annotation is provided by URule. It mainly describes the attributes of the field, and it should be consistent with the title column of the variable library. Listening to the official introduction, you can use this annotation to realize the mapping between POJO attributes and variable library attributes. That is, the POJO is written, and then the variable library corresponding to the rules does not need to be rewritten, and can be generated directly. Anyway, there is this function, and it is directly mentioned here.