具体使用语法规则可以查看下面地址中文档说明: 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;
public class Application {
public static void main(String[] args) throws Exception {
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
context.put("a", 1);
context.put("b", 2);
context.put("c", 3);
String express = "a + b * c";
Object r = runner.execute(express, context, null, true, false);
System.out.println(r);
runner.addOperatorWithAlias("如果", "if", null);
runner.addOperatorWithAlias("则", "then", null);
runner.addOperatorWithAlias("否则", "else", null);
express = "如果 (语文 + 数学 + 英语 > 270) 则 {return 1;} 否则 {return 0;}";
DefaultContext<String, Object> context1 = new DefaultContext<String, Object>();
Object execute = runner.execute(express, context1, null, false, false, null);
System.out.println(execute);
runner = new ExpressRunner();
runner.addOperatorWithAlias("如果", "if", null);
runner.addOperatorWithAlias("或", "||", null);
runner.addOperatorWithAlias("且", "&&", null);
runner.addOperatorWithAlias("等于", "==", null);
runner.addOperatorWithAlias("大于", ">", null);
runner.addOperatorWithAlias("小于", "<", null);
runner.addOperatorWithAlias("则", "then", null);
runner.addOperatorWithAlias("否则", "else", null);
runner.addOperatorWithAlias("返回", "return", null);
runner.addFunctionOfClassMethod("获取JSON中的值", Application.class.getName(), "getValue", new String[]{"String"}, null);
express = "如果 (获取JSON中的值(\"code\") 等于 2 ) 则 {返回 true} 否则 {返回 false}";
Object execute1 = runner.execute(express, context, null, true, false);
System.out.println(execute1);
}
public static Object getValue(String name) {
return JSONObject.parseObject(json).get(name);
}
private static final String json = "{\n" +
" \"code\": 1,\n" +
" \"message\": \"success\"\n" +
"}";
}
|