QLExpress rule engine instance

what is QLExpress

QLExpress Ali is a open source rules engine, support for java good, detailed description and usage directly venue github: https: //github.com/alibaba/QLExpress

just do it!

github has been described in the basic usage is very clear, not repeat them here, to help us understand through a direct entry case

fisrt

We look at a java pseudo code

if(新用户) {
  赠送优惠券;
} else {
  赠送积分;
}
if(有手机号){
  发送短信;
}

Our code will appear similar logic, if I want to change this time as a new user by sending points, coupons or gift it, this time on the need to modify the if-else, then we can use the rules engine to easily implement the logic, and can be modified by modifying the rules engine directly describe the function, do ye le, look down ~

show you code

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

In case rules are:

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

If this time I had to change, I can directly modify the statement as follows:

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

If this rule directly as a configuration of, as long as the statement is in line with the rules, directly modify ok.

Released three original articles · won praise 2 · Views 316

Guess you like

Origin blog.csdn.net/Anonymous_L/article/details/104016635
Recommended