IK Expression 简单用
翟俊哲
2023-12-01
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.wltea.expression.ExpressionEvaluator;
import org.wltea.expression.datameta.Variable;
public class test {
public String test(String name) {
return "hi, " + name;
}
public static void test1(){
String expression = " (a + b ) / c ";
//给表达式中的变量 "用户名" 付上下文的值
List variables = new ArrayList ();
variables.add( Variable.createVariable("a", Integer.valueOf("2")));
variables.add( Variable.createVariable("b", Integer.valueOf("2")));
variables.add( Variable.createVariable("c", Integer.valueOf("2") ));
//
variables.add(Variable.createVariable("用户名", args[0]));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
System.out.println("Result = " + result);
}
public static void test2(){
String expression = " (RRC_SuccConnEstab+RRC_SuccConnReestab)/(RRC_AttConnEstab+RRC_AttConnReestab)";
//给表达式中的变量 "用户名" 付上下文的值
List variables = new ArrayList ();
variables.add( Variable.createVariable("RRC_SuccConnEstab", Double.valueOf("200")));
variables.add( Variable.createVariable("RRC_SuccConnReestab", Double.valueOf("21")));
variables.add( Variable.createVariable("RRC_AttConnEstab", Double.valueOf("22") ));
variables.add( Variable.createVariable("RRC_AttConnReestab", Double.valueOf("112") ));
variables.add( Variable.createVariable("RRC_AttConnReestabsbd", Double.valueOf("112") ));
//
variables.add(Variable.createVariable("用户名", args[0]));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
//
ExpressionEvaluator.evaluate(expression);
//
ExpressionEvaluator.compile(expression);
System.out.println("Result = " + result);
}
public static void test3(){
String expression = " (a > b ) && (c >d ) || ( d == e ) ";
List variables = new ArrayList ();
variables.add( Variable.createVariable("a", Double.valueOf("200")));
variables.add( Variable.createVariable("b", Double.valueOf("21")));
variables.add( Variable.createVariable("c", Double.valueOf("22") ));
variables.add( Variable.createVariable("d", Double.valueOf("112") ));
variables.add( Variable.createVariable("e", Double.valueOf("112") ));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
System.out.println("Result = " + result);
}
public static void test4(){
String expression = " (申请金额>10000)?\"总经理审批\":\"部门经理审批\"";
List variables = new ArrayList ();
variables.add( Variable.createVariable("申请金额", Double.valueOf("200")));
variables.add( Variable.createVariable("b", Double.valueOf("21")));
variables.add( Variable.createVariable("c", Double.valueOf("22") ));
variables.add( Variable.createVariable("d", Double.valueOf("112") ));
variables.add( Variable.createVariable("e", Double.valueOf("112") ));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
System.out.println("Result = " + result);
}
public static void test5(){
String expression = " (申请金额>10000)?\"总经理审批\":((b<20)?\"if\":\"else\")";
List variables = new ArrayList ();
variables.add( Variable.createVariable("申请金额", Double.valueOf("200")));
variables.add( Variable.createVariable("b", Double.valueOf("21")));
variables.add( Variable.createVariable("c", Double.valueOf("22") ));
variables.add( Variable.createVariable("d", Double.valueOf("112") ));
variables.add( Variable.createVariable("e", Double.valueOf("112") ));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
System.out.println("Result = " + result);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String expression = "\"Hello World \" + 用户名";
//给表达式中的变量 "用户名" 付上下文的值
List variables = new ArrayList ();
variables.add( Variable.createVariable("用户名", "vv"));
//
variables.add(Variable.createVariable("用户名", args[0]));
//执行表达式
Object result = ExpressionEvaluator.evaluate(expression, variables);
System.out.println("Result = " + result);
test5();
}
}