/**
* @Author wzq
* @Date 2022/6/24
* 规则格式转换校验
*/
public class RuleEngine {
/**
* @param operator 运算符
* @param threshold 阀值
* @return
* @throws Exception
*/
public static Boolean validationRule(String operator, String threshold, Object value) throws Exception {
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<>();
runner.addOperatorWithAlias("大于", ">", null);
runner.addOperatorWithAlias("小于", "<", null);
runner.addOperatorWithAlias("等于", "==", null);
runner.addOperatorWithAlias("大于等于", ">=", null);
runner.addOperatorWithAlias("小于等于", "<=", null);
//组装表达式
String express = "if(value " + operator + " " + threshold + ") then {return true} else {return false}";
//put值
context.put("value", value);
//调用execute进行校验
Object result = runner.execute(express, context, null, true, false);
return (Boolean) result;
}
}
qlExpess ,阿里巴巴规则引擎。简单使用
最新推荐文章于 2024-08-10 18:38:32 发布