本文是对java整合mvel2.0的一点示例:

如果表达式中有变量,解析表达式时必须传一个map

MVEL.eval(expression, vars);
/**
* 基本解析表达式
*/
@Test
public void test(){
String expression ="foobar > 99";
Map vars = new HashMap();
vars.put("foobar",new Integer(100));
// We know this expressionshould return a boolean.
Boolean result = (Boolean) MVEL.eval(expression, vars);
if (result.booleanValue()) {
System.out.println("Itworks!");
}
}
/**
* 变量判空
*/
@Test
public void test4(){
String expression = "a == empty && b == empty"; Map<String, Object> paramMap = new HashMap<>();
paramMap.put("a", "");
paramMap.put("b", null);
Object object = MVEL.eval(expression, paramMap);
System.out.println(object); // true
}
   /**
*
*/
@Test
public void test(){
HashMap<Object, Object> srcMap = new HashMap<>();
srcMap.put("name","zs");
srcMap.put("age",10);
srcMap.put("sex","女");
//字段映射关系
HashMap<String, String> mapping = new HashMap<>();
mapping.put("name","name");
mapping.put("age","age");
//这里先把当前年份写死为2019
mapping.put("birthYear","2019-age");
//目标对象
HashMap<Object, Object> targetMap = new HashMap<>();
//k为目标表字段,v为转换规则
mapping.forEach((k,v)->{
Object reValue = MVEL.eval(v,srcMap);
System.out.println(v+":"+reValue);
targetMap.put(k,reValue);
});
System.out.println("源对象"+srcMap); //源对象{sex=女, name=zs, age=10}
System.out.println("目标对象"+targetMap); //目标对象{birthYear=2009, name=zs, age=10}
}
/**
* 获取对象属性
*/
@Test
public void test2(){
UserInfo user = new UserInfo();
user.setUserName("1234");
String expression = "user.userName = '123'";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("user",user);
Object object = MVEL.eval(expression, paramMap);
System.out.println(object); // true
}
 /**
* 多语句分号隔开 返回值无需 return
*/
@Test
public void test3(){
String expression = " a = 10; b = (a = a * 2) + 10;b;";   Map<String, Object> paramMap = new HashMap<>();
//即便map没有任何内容 ,也需要传map 不然会报错
        Object object = MVEL.eval(expression,paramMap );
        System.out.println(object); // true
}[
/**
* null 和nil 都表示空
*/
@Test
public void test4(){
String expression = "a == null && b == nil";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("a", null);
paramMap.put("b", null);
Object object = MVEL.eval(expression, paramMap);
System.out.println(object); // true
}
/**
* 两种解析表达式方式
*/
@Test
public void test5(){
UserInfo foo = new UserInfo();
foo.setUserName("test");
Map context = new HashMap();
String expression = "foo.userName == 'test'";
VariableResolverFactory functionFactory = new MapVariableResolverFactory(context);
context.put("foo",foo);
Boolean result = (Boolean) MVEL.eval(expression,functionFactory);
System.out.println(result); Serializable compileExpression = MVEL.compileExpression(expression);
result = (Boolean) MVEL.executeExpression(compileExpression, context, functionFactory);
System.out.print(result);
}
/**
*基本表达式计算,map工厂
*/
@Test
public void test3(){
UserInfo userInfo = new UserInfo();
String exp = "a + b > c";
Map varsMap = new HashMap();
varsMap.put("a", 1); varsMap.put("b", 2);varsMap.put("c", 2);
VariableResolverFactory factory = new MapVariableResolverFactory(varsMap);
Object eval = MVEL.eval(exp, factory);
System.out.println(eval);
}
/**
*空安全运算符user.?parent.name
*/
@Test
public void test4(){
UserInfo user = new UserInfo();
user.setUserName("xm");
user.setPassword("123");
user.setParent(new Parent("xmfather"));
//user 有一个parent对象属性 parent属性中有一个name属性
String exp = "user.?parent.name";
Map<String,Object> factory = new HashMap<>();
factory.put("user",user);
// VariableResolverFactory factory = new MapVariableResolverFactory();
Object eval = MVEL.eval(exp, factory);
System.out.println(eval);
}
/**
* 集合遍历
*/
@Test
public void test5(){
List<Integer> list = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
Map<String,Object> map2 = new HashMap<>();
list.add(0);
list.add(1);
list.add(2);
String exp = "list[0]";
String exp2 = "map2.list";
map2.put("list",list);
map.put("list",list);
map.put("map2",map2);
Object eval = MVEL.eval(exp2,map);
System.out.println(eval);
}
/**
* Strings as Arrays
*/
@Test
public void test6(){
String foo = "My String";
String exp = "foo[0]";
Map<String,Object> map = new HashMap<>();
map.put("foo",foo);
Object eval = MVEL.eval(exp,map);
System.out.println(eval);
} /**
* If-Then-Else
*/
@Test
public void test7(){
String exp = "\n" +
"if (a > 0) {\n" +
" System.out.println(\"Greater than zero!\");\n" +
"}\n" +
"else if (a == -1) { \n" +
" System.out.println(\"Minus one!\");\n" +
"}\n" +
"else { \n" +
" System.out.println(\"Something else!\");\n" +
"}\n";
Map<String,Object> map = new HashMap<>();
map.put("a",1);
Object eval = MVEL.eval(exp,map);
}  
/**
* 三元声明
*/
@Test
public void test9(){
String expression = "num > 0 ? \"Yes\" : \"No\";";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("num", 2);
Object object = MVEL.eval(expression, paramMap);
System.out.println(object);
}
/**
* Foreach 它接受由冒号分隔的两个参数,第一个是当前元素的局部变量,
* 第二个是要迭代的集合或数组。
*/
@Test
public void test10(){ List<String> people = new ArrayList<>();
people.add("name");
people.add("xm");
people.add("xl");
String expression = "count = 0;\n" +
"foreach (name : people) {\n" +
" count++;\n" +
" System.out.println(\"Person #\" + count + \":\" + name);\n" +
"}";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("people", people);
Object object = MVEL.eval(expression, paramMap);
System.out.println(object); String exp2 = "str = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n" +
" \n" +
"foreach (el : str) {\n" +
" System.out.print(\"[\"+ el + \"]\"); \n" +
"}";
MVEL.eval(exp2,paramMap); } /**
* MVEL 2.0,使用for关键字简单地简化foreach
*/
@Test
public void test10_2(){
String expression ="['Jim','Bob','Tom']";
List<String> l =(List<String>) MVEL.eval(expression);
for(String str:l){
System.out.println(str);
} String exp = "count = 0;\n" +
"for (name : l) {\n" +
" count++;\n" +
" System.out.println(\"l #\" + count + \":\" + name);\n" +
"}";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("l",l);
MVEL.eval(exp,paramMap);
}
  /**
*MVEL对列表、数组、map的操作
*/
@Test
public void test11(){
//list
String expression ="['Jim','Bob','Tom']";
List<String> l =(List<String>) MVEL.eval(expression);
for(String str:l){
System.out.println("list"+str);
} //数组
String exp2 = "{'Jim','Bob','Tom'}";
Object str = MVEL.eval(exp2);
if(str.getClass().isArray()){
System.out.println("array"+String.valueOf(Array.get(str, 0)));
} //map
String exp3 ="['Bob' : new com.demo.po.UserInfo('Bob'), 'Michael' : new com.demo.po.UserInfo('Michael')]";
Map o = (Map ) MVEL.eval(exp3);
UserInfo u = (UserInfo) o.get("Bob");
System.out.println("map"+u.getUserName()); } /**
* Do While
*/
@Test
public void test12(){
String exp3 = "do { \n" +
" System.out.println(\"当符合while中条件就执行:\"+x);\n" +
" --x } \n" +
"while (x >= 5 );";
Map<String,Object> map = new HashMap<>();
map.put("x",10);
MVEL.eval(exp3,map); }
/**
* Do Until
*/
@Test
public void test13(){
String exp3 = "do { \n" +
" System.out.println(\"当不符合while中条件就执行:\"+x);\n" +
" --x } \n" +
"until (x < 5 );";
Map<String,Object> map = new HashMap<>();
map.put("x",10);
MVEL.eval(exp3,map);} }
/**
* while
*/
@Test
public void test(){
String exp3 = "while(x >= 5){" +
"System.out.println(\"当符合while中条件就执行:\"+x);" +
"--x}";
Map<String,Object> map = new HashMap<>();
map.put("x",10);
MVEL.eval(exp3,map);
} /**
* until
*/
@Test
public void test2(){
String exp3 = "until(x < 5){" +
"System.out.println(\"当不符合while中条件就执行:\"+x);" +
"--x}";
Map<String,Object> map = new HashMap<>();
map.put("x",10);
MVEL.eval(exp3,map);
}
/**
* 投影1.0
*/
@Test
public void test3(){
List<UserInfo> users = new ArrayList<>();
users.add(new UserInfo("xm"));
users.add(new UserInfo("xh"));
users.add(new UserInfo("xl"));
String exp3 = "parentNames = (userName in users); parentNames;" ;
Map<String,Object> map = new HashMap<>();
map.put("users",users);
List<UserInfo> eval = (List<UserInfo>) MVEL.eval(exp3, map);
System.out.println(eval);
}
/**
*投影2.0
*/
@Test
public void test3_2(){
List<UserInfo> users = new ArrayList<>();
users.add(new UserInfo(new Parent("xm")));
users.add(new UserInfo(new Parent("xh")));
users.add(new UserInfo(new Parent("xl")));
// String exp3 = "foo=(name in (parent in users));foo" ;
String exp3 = "foo=(parent.name in users);foo" ;
Map<String,Object> map = new HashMap<>();
map.put("users",users);
List<UserInfo> eval = (List<UserInfo>) MVEL.eval(exp3, map);
System.out.println(eval);
} /**
*投影3.0过滤投影
*/
@Test
public void test3_3(){
List<UserInfo> users = new ArrayList<>();
List<UserInfo> fams = new ArrayList<>();
users.add(new UserInfo("xm",fams));
users.add(new UserInfo("xh",fams));
users.add(new UserInfo("xl",fams));
String exp3 = "($ in users if $.userName contains 'h');" ;
Map<String,Object> map = new HashMap<>();
map.put("users",users);
Object eval = MVEL.eval(exp3, map);
System.out.println(eval);
}
/**
* 过滤投影2.0
*/
@Test
public void test4(){
String exp3 = "(($ < 10) in [2,4,8,16,32]); " ;
String exp4 = "($ in [2,4,8,16,32] if $ < 10); "; // returns [2,4,8]
Object eval = MVEL.eval(exp3);
System.out.println(eval);
Object eval2 = MVEL.eval(exp4);
System.out.println(eval2);
}
/**
* 导入调用简单hello函数
*/
@Test
public void test()throws IOException {
File scriptFile = new File("src/main/resources/mvel/hello.el");
Map map = new HashMap();
map.put("name", "小明");
MVEL. evalFile(scriptFile, ParserContext.create(), map);
Object obj = MVEL. eval("hello(name);", map);
}
//hello.el的内容
def hello(String name){
System.out.println("hello" + name);
}
/**
* Lambda(匿名函数)
*/
@Test
public void test2(){
String exp = "threshold = def (x) { x >= 10 ? x : 0 };\n" +
"result = threshold(num);\n" +
"System.out.println(result);";
Map map = new HashMap();
map.put("num",15);
MVEL. eval(exp,map);
} 目前拦截器,闭包,交集,类型转换以及使用格式还有些问题没研究透
												

MVEL2.0的使用实例(一)的更多相关文章

  1. mvel2.0语法指南

    虽然mvel吸收了大量的java语法,但作为一个表达式语言,还是有着很多重要的不同之处,以达到更高的效率,比如:mvel像正则表达式一样,有直接支持集合.数组和字符串匹配的操作符. 除了表达式语言外, ...

  2. Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考

    写在前面: 1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补丁中附带的readme. 2.Oracle每季度都会更新一个最新的PSU,本文最新指的是当前最新 ...

  3. selenium2.0处理case实例(二)

    本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...

  4. redis5.0.3单实例简单安装记录

    redis5.0.3单实例简单安装记录 日常需要测试使用,索性记录下来,免得临时又麻烦的找资料. yum -y install make gcc-c++ cmake bison-devel ncurs ...

  5. Asianux 7.3安装Oracle 11.2.0.4单实例体验

    环境:Asianux 7.3 需求:安装Oracle 11.2.0.4 单实例 背景:系统使用默认的最小安装部署,Oracle安装额外需要的包统一使用yum安装. 查看当前系统相关信息: [root@ ...

  6. Windows Server 2008 安装 10.2.0.5 单实例

    需求:Windows Server 2008 安装 10.2.0.5 单实例 原以为非常简单的一次任务,实际却遇到了问题,故记录一下. 1.安装10.2.0.1 2.安装10.2.0.4 3.安装10 ...

  7. Linux平台Oracle 12.1.0.2 单实例安装部署

    主题:Linux平台Oracle 12.1.0.2 单实例安装部署 环境:RHEL 6.5 + Oracle 12.1.0.2 需求:安装部署OEM 13.2需要Oracle 12.1.0.2版本作为 ...

  8. MACD 下0轴后,强力=7上0轴的实例:

    MACD 下0轴后,强力=7上0轴的实例: 虽然再上0轴,但是由于是强力上,必须有缓解期.缓解期后MACD开始收口,虽然没有下0轴,但是开始趋向死叉. 由于并没有很强的做多市场环境,MACD只是略高于 ...

  9. Oracle 11.2.0.4单实例打补丁

    Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考 写在前面: ·         1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补 ...

随机推荐

  1. H3C DNS简介

  2. vue 项目使用局域网多端访问并实时自动更新(利用 browser-sync)

    在写vue的项目中时,虽然vue会自动更新页面,但是切换页面切来切去也很麻烦,有时候我们还要在公司另一台电脑或者手机上调试,这时候利用browser-sync插件,无需改动vue的代码即可实现: 1. ...

  3. ASP.NET Core 连接 GitLab 与 MatterMost 打造 devops 工具

    在现代化开发工具链里面就包含了自动化的通讯工具,而日志写代码我是推到 Gitlab 平台上,我今天听了郭锐大佬的分享之后,感觉我现在的团队的自动化做的远远不够.我在他的课程上学到的最重要一句话就是做工 ...

  4. git无密码push

    近来项目中调研,jupyterlab和git的整合内容,git server我使用的gitbucket和bitbucket.(项目要求使用bitbucket,看错一个字母下载了两个镜像) gitbuc ...

  5. 第四阶段:1.从零打造一款社区web产品

    ---恢复内容开始--- 熟人关系:微信 陌生人关系:微博 1.把各种竞品罗列起来形成一个分析池.分析其目标用户是哪些.这些产品满足了用户什么需求.可以从时间角度分析趋势.针对每一类竞品画一个商业模式 ...

  6. python入门之字符串的魔法

    1.test="alex" v=test.capitalize() print(v)               //输出结果首字母大写 2.test1="alex&qu ...

  7. 0013 CSS复合选择器:后代、子代、交集、并集、超链接伪类

    重点: 复合选择器 后代选择器 并集选择器 标签显示模式 CSS背景 背景位置 CSS三大特性 优先级 1. CSS复合选择器 目标 理解 理解css复合选择器分别的应用场景 应用 使用后代选择器给元 ...

  8. DEVOPS技术实践_12:创建持续集成的管道

    持续集成不仅包含了Jenkins或者相关其它的CI工具,也包含了包含代码如何控制,采用的什么分支策略等.不同的组织可能采用不同的类型的策略来完成CI,策略类型和项目的类型的有很大的关系. 一 分支策略 ...

  9. $Noip2015/Luogu2661$ 信息传递 并查集

    Luogu $Description$ 给定一个有向图,每个点只有一条出边.求图里的最小环. $Sol$ 使得这个题不难的地方就在于每个点只有一条出边叭. 一边连边一边更新答案.首先当然是初始$f[i ...

  10. Java集合使用之next方法与remove方法 | Java集合使用之remove方法使用易错

    Iterator接口的remove方法将会删除上次调用next方法时返回的元素. next方法和remove方法的调用具有相互依赖性,如果调用remove方法前没有调用next方法是不合法的. 错误使 ...