`
wenshao
  • 浏览: 269252 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

阿里巴巴开源项目SimpleEL发布0.1版本

阅读更多
Simple EL是一个表达式解析引擎。它将表达式解析分成四个部分:预处理、编译、缓存和执行。这四个步骤任意一部分都可以替换,类似设计模式中的Template Method。 SimpleEL将表达式处理成Java Source,调用Tools.jar的Javac API动态编译,缓存编译后反射得到的实例,使得表达式的解析速度和Java静态编译的速度接近。

这是一个性能极致、扩展性良好的表达式解析引擎。图示http://code.alibabatech.com/wiki/download/attachments/1966084/simple_el.png?version=2&modificationDate=1302249958000
目前SimpleEL有三种实现,缺省实现,TinyEL,QL,其中TinyEL在下一个版本中将会替换为缺省实现,QL是一种类SQL的表达式语言,它试图创造一种对象查询语言。

Slider : http://code.alibabatech.com/svn/SimpleEL/trunk/doc/SimpleEL.pptx
SVN:http://code.alibabatech.com/svn/simpleel
JIRA:http://code.alibabatech.com/jira/browse/simpleel
WIKI:http://code.alibabatech.com/wiki/display/simpleel/Home

Download: http://code.alibabatech.com/wiki/display/SimpleEL/Download

Maven Repository:
<repository>
	<id>opensesame</id>
	<name>Alibaba OpenSource Repsoitory</name>
	<url>http://code.alibabatech.com/mvn/releases/</url>
	<snapshots>
		<enabled>false</enabled>
	</snapshots>
</repository>


<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>SimpleEL</artifactId>
	<version>0.1</version>
</dependency>



import com.alibaba.simpleEL.eval.DefaultExpressEvalService;
import com.alibaba.simpleEL.preprocess.DefaultVariantResolver.Type;

DefaultExpressEvalService service = new DefaultExpressEvalService();
service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(7, service.eval(ctx, "@a + @b"));


DefaultExpressEvalService service = new DefaultExpressEvalService();
service.regsiterVariant(int.class, "a", "b");
service.setAllowMultiStatement(true); //support multi-statement

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(1, service.eval(ctx, "if (@a > @b) { return @a - @b; } else {return @b - @a; }"));


TinyEL是SimpleEL中的一种实现,和SimpleEL的缺省实现不同的是,它的预处理不是简单的替换变量,而是经过语法分析之后生成Java Source。变量名称不需要使用@标识。 TinyEL在下一个版本中,可能会升级为SimpleEL的缺省实现。
使用示例:
调用方法
import com.alibaba.simpleEL.dialect.tiny.TinyELEvalService;

TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(String.class, "name");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("name", "abc");

Assert.assertEquals(5, service.eval(ctx, "('aa' + name).length()"));



* 调用静态方法

TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(int.class, "a", "b"); //注册a和b两个变量为int类型

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(4, service.eval(ctx, "java.lang.Math.max(a, b)"));



* 构造对象
TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(long.class, "millis");

long millis = System.currentTimeMillis();
Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("millis", millis);

Assert.assertEquals(new java.util.Date(millis), service.eval(ctx, "new java.util.Date(millis)"));


* 三元表达式
TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(4, service.eval(ctx, "a > b ? a : b"));
Assert.assertEquals(true, service.eval(ctx, "a instanceof Number"));


* 数组访问
TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(int.class, "a", "b");
service.regsiterVariant(int[].class, "c");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);
ctx.put("c", new int[] { 1, 2 });

Assert.assertEquals(4, service.eval(ctx, "Math.max(c[0], c[1]) + 2"));


* 注册函数
TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(int.class, "a", "b");
service.registerFunction("max", Math.class.getMethod("max", new Class<?>[] { int.class, int.class }));

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(7, service.eval(ctx, "a + b"));
Assert.assertEquals(true, service.eval(ctx, "a < b"));
Assert.assertEquals(4, service.eval(ctx, "max(a, b)"));


* 各种运算符
TinyELEvalService service = new TinyELEvalService();

service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(3, service.eval(ctx, "a++"));
Assert.assertEquals(4, ctx.get("a"));
Assert.assertEquals(5, service.eval(ctx, "++a"));
Assert.assertEquals(5, ctx.get("a"));
Assert.assertEquals(4, service.eval(ctx, "--a"));
Assert.assertEquals(4, ctx.get("a"));
Assert.assertEquals(true, service.eval(ctx, "a > b || b > a || a == b"));
Assert.assertEquals(false, service.eval(ctx, "a > b && b > a && a == b"));
Assert.assertEquals(false, service.eval(ctx, "a != b"));


* 使用if语句 
TinyELEvalService service = new TinyELEvalService();
service.setAllowMultiStatement(true);
service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(1, service.eval(ctx, "if (a > b) { return a - b; } else {return b - a; }"));


* 声明本地变量
TinyELEvalService service = new TinyELEvalService();
service.setAllowMultiStatement(true);
service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(70, service.eval(ctx, "int i = 10; return a * i + b * i;"));


* 使用While
TinyELEvalService service = new TinyELEvalService();
service.setAllowMultiStatement(true);
service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(48, service.eval(ctx, "int i = 0; while (i < 10) { a += i++; } return a;"));

Assert.assertEquals(48, ctx.get("a"));
Assert.assertEquals(4, ctx.get("b"));


* 使用For
TinyELEvalService service = new TinyELEvalService();
service.setAllowMultiStatement(true);
service.regsiterVariant(int.class, "a", "b");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", 3);
ctx.put("b", 4);

Assert.assertEquals(52, service.eval(ctx, "for (int i = 0; i < 10; ++i) { a += i; } return a + b;"));

Assert.assertEquals(48, ctx.get("a"));
Assert.assertEquals(4, ctx.get("b"));


* 使用ForEach
TinyELEvalService service = new TinyELEvalService();
service.setAllowMultiStatement(true);
service.regsiterVariant(int[].class, "a");

Map<String, Object> ctx = new HashMap<String, Object>();
ctx.put("a", new int[] {1, 2, 3, 4, 5});

Assert.assertEquals(15, service.eval(ctx, "int sum = 0; for (int i : a) { sum += i; } return sum;"));



分享到:
评论
22 楼 lwb314 2016-11-23  
String相加不支持啊
21 楼 lwb314 2016-11-22  
if后边连续接多个else if 好像不支持
20 楼 lwb314 2016-11-03  
之前的文档都已经不能下载了,请问一下。
我如果希望同时执行
如下代码
我发现如果设置service.setAllowMultiStatement(true);  第一行就会报错,不设置第二行就会报错,有什么解决办法吗?
service.eval(ctx, "a > b || b > a || a == b");
service.eval(ctx,"for (int i = 0; i < 10; ++i) { a += i;b++; } return 0;");


第二个问题,是不是所有表达式都需要有个return?比如if或者for,如果我只想赋值比如
if(a>b){a--;}而不需要有什么返回值,这个时候也必须写个return吗?
19 楼 deepthink 2013-08-19  
感觉simpleel貌似没有自己的类型系统,沿用的也都是java的类型系统,所以在实例代码中出现了:
service.regsiterVariant(int.class, "a", "b");  

用来注册类型

但是,我个人对el的理解是必须要有弱的类型系统才能真正的发挥表达式语言的威力,
举个简单例子说:
if(b){
    ....
}

这里的b不管是String类型的'true'或者是boolean类型的true,亦或是object类型的一个非null对象,该条件都应该成立(不清楚simpleel对这种情况是否支持)。
之前曾经在csdn上看过一篇关于simpleel的分析文章,其中提到了simpleel在类型上的硬伤,不过不好意思,文章的地址找不到了。

当然,一种语言的存在可能是可能是为了一个设计目标的实现,对于对性能要求高的系统来说,可以做一个取舍,以牺牲一部分的灵活性来达到高性能的目的,个人理解这是simpleel的真正的用武之地。

不过目测ali的开源项目貌似都是以性能为最终目标的,之前看的httl也是需要注册很多类型信息才能使用
18 楼 carfily 2011-08-04  
这是个好东西,以前都想过要是java有这种功能就好了,现在终于出现了。
17 楼 gazeldx 2011-04-27  
看到这一句
Assert.assertEquals(48, service.eval(ctx, "int i = 0; while (i < 10) { a += i++; } return a;")); 

解决了我一直以来的问题:java代码如何复用,如何像freemarker之类的语言般使用。
16 楼 wenshao 2011-04-25  
vvggsky 写道
wenshao 写道
freish 写道
能不能把那个@去掉呢?


TinyELEvalService的实现是不需要使用@的,看例子!


你们内部 有个大侠 公开 对 这个项目 的实现方式表示过异议!


能够提供更多的信息么?
15 楼 vvggsky 2011-04-25  
wenshao 写道
freish 写道
能不能把那个@去掉呢?


TinyELEvalService的实现是不需要使用@的,看例子!


你们内部 有个大侠 公开 对 这个项目 的实现方式表示过异议!
14 楼 iq527 2011-04-23  
http://code.alibabatech.com/svn/simpleel
13 楼 Allen 2011-04-23  
这些利用EL从各种“数据源”里面获得数据的感觉上很有.NET里面LINQ的思路啊……
12 楼 sky_dream 2011-04-23  
List<Person> srcCollection = new ArrayList<Person>(); 
    srcCollection.add(new Person(18, "吴能")); 
    srcCollection.add(new Person(27, "刘芒")); 
    srcCollection.add(new Person(40, "黄警")); 
    srcCollection.add(new Person(50, "夏留")); 
    srcCollection.add(new Person(60, "刘晶")); 
    srcCollection.add(new Person(33, "石榴姐")); 
    srcCollection.add(new Person(90, "松下太郎")); 

LZ你太强了!
11 楼 ibadboy 2011-04-23  
不知在实际的项目中能起到多大作用,主要的应用场景是什么,期待详细的解释。。。
10 楼 gogole_09 2011-04-23  
利用模版生成java源代码,交给jdk编译执行,nice! 
9 楼 javatar 2011-04-23  
温少,贴一下性能对比吧,EL解析的工具很多,估计大家对性能会比较感兴趣。
8 楼 wenshao 2011-04-22  
freish 写道
能不能把那个@去掉呢?


TinyELEvalService的实现是不需要使用@的,看例子!
7 楼 lwjlaser 2011-04-22  
很好很强大,下下代码来慢慢学
6 楼 freish 2011-04-22  
能不能把那个@去掉呢?
5 楼 wzju64676266 2011-04-22  
温绍锦  ?
4 楼 william_ai 2011-04-22  
有点意思,和以前玩过的一个开源beanshell很像,抽空做个测试玩玩。
3 楼 bill.830420 2011-04-22  
支持开源,功能越来越强大了,现在支不支持在脚本中定义class呢?

相关推荐

Global site tag (gtag.js) - Google Analytics