Fork me on GitHub

QLExpress规则引擎实例

what is QLExpress

QLExpress是阿里开源的一个规则引擎,对java支持良好,具体描述及用法直接移步github:https://github.com/alibaba/QLExpress

just do it!

github中对基本用法已经描述很清楚,此处不再赘述,我们直接通过一个入门案例来帮忙理解,

fisrt

我们先来看一段java伪代码

1
2
3
4
5
6
7
8
if(新用户) {
赠送优惠券;
} else {
赠送积分;
}
if(有手机号){
发送短信;
}

我们代码中都会出现类似的逻辑,如果此时我想要修改为新用户增送积分,否则赠送优惠券呢,这时候就需要修改if-else,此时我们可以使用规则引擎来轻松实现逻辑,并通过修改规则引擎描述就可以直接修改功能,咋做嘞,往下瞅~

show you code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.qlexpress.demo;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;

public class Test {

public static void main(String[] args) throws Exception {
ExpressRunner runner = new ExpressRunner();

// 如果 新用户 赠送代金券 否则 赠送积分
// 如果 有手机号 发送短信

// 定义逻辑
runner.addOperatorWithAlias("如果", "if", null);
runner.addOperatorWithAlias("则", "then", null);
runner.addOperatorWithAlias("否则", "else", null);

// 定义执行方法
runner.addFunctionOfClassMethod("赠送优惠券", Test.class.getName(),
"sendCoupon", new Class[]{Integer.class}, null);
runner.addFunctionOfClassMethod("赠送积分", Test.class.getName(),
"sendIntegral", new Class[]{Integer.class}, null);
runner.addFunctionOfServiceMethod("发送短信", new Test(), "sendMsg",
new String[]{"String"}, null);

// 定义逻辑
runner.addFunctionOfServiceMethod("是否新用户", new Test(), "isNewAcct",
new Class[]{Integer.class}, null);
runner.addFunctionOfServiceMethod("是否有手机号", new Test(), "isMobile",
new Class[]{Integer.class}, null);

String exp = "如果 (是否新用户(1)) 则 { 赠送优惠券(1)} 否则 { 赠送积分(1)} 如果 (是否有手机号(1)) 则 {发送短信(\"欢迎您哦\")}";
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
Object execute = runner.execute(exp, context, null, false, false, null);
System.out.println(execute);

}

public static void sendCoupon(Integer num) {
System.out.println("赠送优惠券啦:" + num);
}

public static void sendIntegral(Integer num) {
System.out.println("赠送积分啦:" + num);
}

public String sendMsg(String msg) {
System.out.println("发送短信啦:" + msg);
return msg;
}

public boolean isNewAcct(Integer userType) {
if (userType == 1) {
return true;
} else {
return false;
}
}

public boolean isMobile(Integer mobileType) {
if (mobileType == 1) {
return true;
} else {
return false;
}
}
}

end

案例中规则为:

1
如果  (是否新用户(1)) 则 { 赠送优惠券(1)} 否则 { 赠送积分(1)} 如果 (是否有手机号(1)) 则 {发送短信(\"欢迎您哦\")}

如果此时我要修改,我可以直接修改语句为:

1
如果  (是否新用户(2)) 则 { 赠送优惠券(1)} 否则 { 赠送积分(1)} 如果 (是否有手机号(1)) 则 {发送短信(\"欢迎您哦\")}

如果把这个规则直接作为配置化,只要语句符合规则,直接修改就ok了。


-------------本文结束感谢您的阅读-------------
感谢你的支持!