java 自定义表达式 表达式解析 QLExpress使用
具体使用语法规则可以查看下面地址中文档说明:
QLExpress:https://github.com/alibaba/QLExpress
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.apache.commons.lang3.StringUtils;
public class Application {
public static void main(String[] args) throws Exception {
/** 简单运算表达式 */
ExpressRunner runner_1 = new ExpressRunner();
DefaultContext<String, Object> context_1 = new DefaultContext<String, Object>();
context_1.put("a", 1);
context_1.put("b", 2);
context_1.put("c", 3);
String express_1 = "a + b * c";
Object result_1 = runner_1.execute(express_1, context_1, null, true, false);
System.out.println(result_1);
/** 逻辑表达式 */
ExpressRunner runner_2 = new ExpressRunner();
runner_2.addOperatorWithAlias("如果", "if", null);
runner_2.addOperatorWithAlias("则", "then", null);
runner_2.addOperatorWithAlias("否则", "else", null);
DefaultContext<String, Object> context_2 = new DefaultContext<String, Object>();
context_2.put("语文", 100);
context_2.put("数学", 100);
context_2.put("英语", 60);
String express_2 = "如果 (语文 + 数学 + 英语 > 270) 则 {return 1;} 否则 {return 0;}";
Object result_2 = runner_2.execute(express_2, context_2, null, false, false, null);
System.out.println(result_2);
/** 复杂逻辑表达式 */
ExpressRunner runner_3 = new ExpressRunner();
runner_3 = new ExpressRunner();
runner_3.addOperatorWithAlias("如果", "if", null);
runner_3.addOperatorWithAlias("或", "||", null);
runner_3.addOperatorWithAlias("且", "&&", null);
runner_3.addOperatorWithAlias("等于", "==", null);
runner_3.addOperatorWithAlias("大于", ">", null);
runner_3.addOperatorWithAlias("小于", "<", null);
runner_3.addOperatorWithAlias("则", "then", null);
runner_3.addOperatorWithAlias("否则", "else", null);
runner_3.addOperatorWithAlias("返回", "return", null);
runner_3.addFunctionOfClassMethod("获取JSON中的值", Application.class.getName(), "getValue", new String[]{"String"}, null);
runner_3.addFunctionOfClassMethod("字符串等于", Application.class.getName(), "equals", new String[]{"String", "String"}, null);
DefaultContext<String, Object> context_3_1 = new DefaultContext<>();
String express_3_1 = "如果 (获取JSON中的值(\"code\") 等于 1 ) 则 {返回 true} 否则 {返回 false}";
Object result_3_1 = runner_3.execute(express_3_1, context_3_1, null, true, false);
System.out.println(result_3_1);
DefaultContext<String, Object> context_3_2 = new DefaultContext<>();
String express_3_2 = "如果 ( 字符串等于( 获取JSON中的值(\"message\"), \"success\") ) 则 {返回 true} 否则 {返回 false}";
Object result_3_2 = runner_3.execute(express_3_2, context_3_2, null, true, false);
System.out.println(result_3_2);
}
public static Object getValue(String name) {
String json = "{\"code\": 1,\"message\": \"success\"}";
return JSONObject.parseObject(json).get(name);
}
public static boolean equals(String param1, String param2) {
return StringUtils.equals(param1, param2);
}
}