Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
gangzai
qlexpress
提交
e15a478d
Q
qlexpress
项目概览
gangzai
/
qlexpress
与 Fork 源项目一致
从无法访问的项目Fork
通知
10
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
qlexpress
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e15a478d
编写于
1月 04, 2018
作者:
T
tianqiao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新的pom依赖,新的使用说明
上级
ccb1e005
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
77 addition
and
55 deletion
+77
-55
README.md
README.md
+71
-47
pom.xml
pom.xml
+1
-1
src/main/java/com/ql/util/express/IExpressContext.java
src/main/java/com/ql/util/express/IExpressContext.java
+1
-3
src/test/java/com/ql/util/express/example/WorkflowTest.java
src/test/java/com/ql/util/express/example/WorkflowTest.java
+4
-4
未找到文件。
README.md
浏览文件 @
e15a478d
#
# qlExpress相关文档
#
QLExpress基本语法
### 最简单的调用案例
## 背景介绍
由阿里的电商业务规则、表达式(布尔组合)、特殊数学公式计算(高精度)、语法分析、脚本二次定制等强需求而设计的一门动态脚本引擎解析工具。
在阿里集团有很强的影响力,同时为了自身不断优化、发扬开源贡献精神,于2009年开源。
## 依赖和调用说明
```
xml
<dependency>
<groupId>
com.
taobao.util
</groupId>
<artifactId>
taobao-e
xpress
</artifactId>
<version>
3.
0.17
</version>
<groupId>
com.
alibaba
</groupId>
<artifactId>
QLE
xpress
</artifactId>
<version>
3.
2.0
</version>
</dependency>
```
```
java
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
);
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
);
```
### 更多的语法介绍
1、java的绝大多数语法
## 1、操作符和java对象操作
### 普通java语法
```
//支持 +,-,*,/,<,>,<=,>=,==,!=,<>【等同于!=】,%,mod【取模等同于%】,++,--,
//in【类似sql】,like【sql语法】,&&,||,!,等操作符
...
...
@@ -44,7 +47,7 @@ max = a>b?a:b;
```
//关于对象,类,属性,方法的调用
### java的对象操作
```
import com.ql.util.express.test.OrderQuery;
...
...
@@ -57,8 +60,10 @@ query.buyer = "张三";//调用属性,默认会转化为setBuyer("张三")
result = bizOrderDAO.query(query);//调用bean对象的方法
System.out.println(result.getId());//静态方法
```
//自定义方法与调用
## 2、脚本中定义function
```
function add(int a,int b){
return a+b;
};
...
...
@@ -70,12 +75,12 @@ function sub(int a,int b){
a=10;
return add(a,4) + sub(a,9);
```
2、自定义操作符号:addOperatorWithAlias+addOperator+addFunction
```
## 3、扩展操作符:Operator
### 替换if then else 等关键字
```
java
runner
.
addOperatorWithAlias
(
"如果"
,
"if"
,
null
);
runner
.
addOperatorWithAlias
(
"则"
,
"then"
,
null
);
runner
.
addOperatorWithAlias
(
"否则"
,
"else"
,
null
);
...
...
@@ -83,8 +88,10 @@ runner.addOperatorWithAlias("否则", "else",null);
exp
=
"如果 (如果 1==2 则 false 否则 true) 则 {2+2;} 否则 {20 + 20;}"
;
DefaultContext
<
String
,
Object
>
context
=
new
DefaultContext
<
String
,
Object
>();
runner
.
execute
(
exp
,
nil
,
null
,
false
,
false
,
null
);
```
### 如何自定义Operator
```
java
//定义一个继承自com.ql.util.express.Operator的操作符
public
class
JoinOperator
extends
Operator
{
public
Object
executeInner
(
Object
[]
list
)
throws
Exception
{
...
...
@@ -102,38 +109,38 @@ public class JoinOperator extends Operator{
}
}
```
### 如何使用Operator
```
//(1)addOperator
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.addOperator("join",new JoinOperator());
Object r = runner.execute("1 join 2 join 3", context, null, false, false);
System.out.println(r);
//返回结果 [1, 2, 3]
class GroupOperator extends Operator {
public GroupOperator(String aName) {
this.name= aName;
}
public Object executeInner(Object[] list)throws Exception {
Object result = Integer.valueOf(0);
for (int i = 0; i < list.length; i++) {
result = OperatorOfNumber.add(result, list[i],false);//根据list[i]类型(string,number等)做加法
}
return result;
}
}
//(2)replaceOperator
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.replaceOperator("+",new JoinOperator());
Object r = runner.execute("1 + 2 + 3", context, null, false, false);
System.out.println(r);
//返回结果 [1, 2, 3]
runner.addFunction("group", new GroupOperator("group"));
//(3)addFunction
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.add
Operator
("join",new JoinOperator());
Object r = runner.execute("
group
(1,2,3)", context, null, false, false);
runner.add
Function
("join",new JoinOperator());
Object r = runner.execute("
join
(1,2,3)", context, null, false, false);
System.out.println(r);
//返回结果
6
//返回结果
[1, 2, 3]
```
## 4、绑定java类或者对象的method
3、类的静态方法,对象的方法绑定:
addFunctionOfClassMethod+addFunctionOfServiceMethod
addFunctionOfClassMethod+addFunctionOfServiceMethod
```
...
...
@@ -168,7 +175,7 @@ runner.execute(exp, context, null, false, false);
```
4
、macro 宏定义
## 5
、macro 宏定义
```
runner.addMacro("计算平均成绩", "(语文+数学+英语)/3.0");
...
...
@@ -183,7 +190,8 @@ System.out.println(r);
```
5、编译脚本,查询外部需要定义的变量,注意以下脚本int和没有int的区别
## 6、编译脚本,查询外部需要定义的变量和函数。
**注意以下脚本int和没有int的区别**
```
String express = "int 平均分 = (语文+数学+英语+综合考试.科目2)/4.0;return 平均分";
...
...
@@ -201,7 +209,7 @@ var : 英语
var : 语文
```
6
、关于不定参数的使用
## 7
、关于不定参数的使用
```
@Test
...
...
@@ -228,7 +236,7 @@ var : 语文
}
```
7
、关于集合的快捷写法
## 8
、关于集合的快捷写法
```
@Test
...
...
@@ -246,4 +254,20 @@ var : 语文
System.out.println(r);
}
```
```
## 9、集合的遍历
其实类似java的语法,只是ql不支持for(obj:list){}的语法,只能通过下标访问。
```
java
//遍历map
map = new HashMap();
map.put("a", "a_value");
map.put("b", "b_value");
keySet = map.keySet();
objArr = keySet.toArray();
for (i=0;i<objArr.length;i++) {
key = objArr[i];
System.out.println(map.get(key));
}
```
\ No newline at end of file
pom.xml
浏览文件 @
e15a478d
...
...
@@ -6,7 +6,7 @@
<groupId>
com.alibaba
</groupId>
<artifactId>
QLExpress
</artifactId>
<packaging>
jar
</packaging>
<version>
3.
1.7
</version>
<version>
3.
2.0
</version>
<name>
QLExpress
</name>
<description>
QLExpress is a powerful, lightweight, dynamic language for the Java platform aimed at improving developers’ productivity in different business scenes.
</description>
<url>
https://github.com/alibaba/QLExpress
</url>
...
...
src/main/java/com/ql/util/express/IExpressContext.java
浏览文件 @
e15a478d
...
...
@@ -8,16 +8,14 @@ package com.ql.util.express;
public
interface
IExpressContext
<
K
,
V
>
{
/**
* 根据名称从属性列表中提取属性值。如果表达式中用到了Spring的对象,也是通过此方法获取
* @param
name
属性名称
* @param
key
属性名称
* @return
* @throws Exception
*/
public
V
get
(
Object
key
);
/**
* 表达式计算的结果可以设置回调用系统,例如 userId = 3 + 4
* @param name 属性名称
* @param object 属性值
* @throws Exception
*/
public
V
put
(
K
name
,
V
object
);
}
src/test/java/com/ql/util/express/example/WorkflowTest.java
浏览文件 @
e15a478d
...
...
@@ -62,7 +62,7 @@ public class WorkflowTest {
expressContext
.
put
(
"申请人"
,
"小强"
);
expressContext
.
put
(
"金额"
,
new
Integer
(
4000
));
//执行表达式
runner
.
execute
(
exp
,
expressContext
,
null
,
false
,
false
);
//
runner.execute(exp, expressContext, null,false, false);
}
/**
...
...
@@ -94,7 +94,7 @@ public class WorkflowTest {
expressContext
.
put
(
"申请人"
,
"小强"
);
expressContext
.
put
(
"金额"
,
new
Integer
(
5000
));
runner
.
executeByExpressName
(
"example/approve1"
,
expressContext
,
null
,
false
,
false
,
null
);
//
runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
}
/**
...
...
@@ -120,7 +120,7 @@ public class WorkflowTest {
expressContext
.
put
(
"申请人"
,
"小强"
);
expressContext
.
put
(
"金额"
,
new
Integer
(
6000
));
runner
.
executeByExpressName
(
"example/approve"
,
expressContext
,
null
,
false
,
false
,
null
);
//
runner.executeByExpressName("example/approve", expressContext, null, false,false,null);
}
/**
...
...
@@ -148,7 +148,7 @@ public class WorkflowTest {
expressContext
.
put
(
"申请人"
,
"小强"
);
expressContext
.
put
(
"金额"
,
new
Integer
(
7000
));
runner
.
executeByExpressName
(
"example/approve1"
,
expressContext
,
null
,
false
,
false
,
null
);
//
runner.executeByExpressName("example/approve1", expressContext, null, false,false,null);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录