【java】IKExpression表达式解析器
IK Expression是一个开源的(OpenSource),可扩展的(Extensible),基于java语言开发的一个超轻量级(Super lightweight)的公式化语言解析执行工具包。
源码地址:https://github.com/deonwu/ik-expression
参考文档:链接:https://pan.baidu.com/s/1lxY3qGUqAiwsFJpEHWQQ9Q 提取码:1234
//支持运算: //基础运算符: + - * / //逻辑运算符: ! && || //三元运算符:? //括号优先级等
使用方法演示:
演示1:
public static void main(String[] args) { String expression1 = "true || false"; String expression2 = "!true || false"; String expression3 = "true && false"; String expression4 = "true && (false || true)"; System.out.println(ExpressionEvaluator.evaluate(expression1));//true System.out.println(ExpressionEvaluator.evaluate(expression2));//false System.out.println(ExpressionEvaluator.evaluate(expression3));//false System.out.println(ExpressionEvaluator.evaluate(expression4));//true }
演示2:
public static void main(String[] args) { String expression1 = "(1>2) || false"; String expression2 = "(1-2) > 0"; String expression3 = "1*2 == 2"; String expression4 = "1*2 != 2"; System.out.println(ExpressionEvaluator.evaluate(expression1));//false System.out.println(ExpressionEvaluator.evaluate(expression2));//false System.out.println(ExpressionEvaluator.evaluate(expression3));//true System.out.println(ExpressionEvaluator.evaluate(expression4));//false }
演示3:
public static void main(String[] args) { Boolean a = true; Boolean b = true; Boolean c = false; String expression = "a || (b && c)"; //给表达式中的变量赋上下文的值 List<Variable> variables = new ArrayList<Variable>(); variables.add(Variable.createVariable("a", a)); variables.add(Variable.createVariable("b", b)); variables.add(Variable.createVariable("c", c)); //执行表达式 Object result = ExpressionEvaluator.evaluate(expression, variables);//true }
持续更新!!!