提交 e15a478d 编写于 作者: T tianqiao

新的pom依赖,新的使用说明

上级 ccb1e005
## qlExpress相关文档 # QLExpress基本语法
### 最简单的调用案例 ## 背景介绍
由阿里的电商业务规则、表达式(布尔组合)、特殊数学公式计算(高精度)、语法分析、脚本二次定制等强需求而设计的一门动态脚本引擎解析工具。
在阿里集团有很强的影响力,同时为了自身不断优化、发扬开源贡献精神,于2009年开源。
## 依赖和调用说明
```xml ```xml
<dependency> <dependency>
<groupId>com.taobao.util</groupId> <groupId>com.alibaba</groupId>
<artifactId>taobao-express</artifactId> <artifactId>QLExpress</artifactId>
<version>3.0.17</version> <version>3.2.0</version>
</dependency> </dependency>
``` ```
```java ```java
ExpressRunner runner = new ExpressRunner();
ExpressRunner runner = new ExpressRunner(); DefaultContext<String, Object> context = new DefaultContext<String, Object>();
DefaultContext<String, Object> context = new DefaultContext<String, Object>(); context.put("a",1);
context.put("a",1); context.put("b",2);
context.put("b",2); context.put("c",3);
context.put("c",3); String express = "a+b*c";
String express = "a+b*c"; Object r = runner.execute(express, context, null, true, false);
Object r = runner.execute(express, context, null, true, false); System.out.println(r);
System.out.println(r);
``` ```
### 更多的语法介绍 ## 1、操作符和java对象操作
1、java的绝大多数语法 ### 普通java语法
``` ```
//支持 +,-,*,/,<,>,<=,>=,==,!=,<>【等同于!=】,%,mod【取模等同于%】,++,--, //支持 +,-,*,/,<,>,<=,>=,==,!=,<>【等同于!=】,%,mod【取模等同于%】,++,--,
//in【类似sql】,like【sql语法】,&&,||,!,等操作符 //in【类似sql】,like【sql语法】,&&,||,!,等操作符
...@@ -44,7 +47,7 @@ max = a>b?a:b; ...@@ -44,7 +47,7 @@ max = a>b?a:b;
``` ```
//关于对象,类,属性,方法的调用 ### java的对象操作
``` ```
import com.ql.util.express.test.OrderQuery; import com.ql.util.express.test.OrderQuery;
...@@ -57,8 +60,10 @@ query.buyer = "张三";//调用属性,默认会转化为setBuyer("张三") ...@@ -57,8 +60,10 @@ query.buyer = "张三";//调用属性,默认会转化为setBuyer("张三")
result = bizOrderDAO.query(query);//调用bean对象的方法 result = bizOrderDAO.query(query);//调用bean对象的方法
System.out.println(result.getId());//静态方法 System.out.println(result.getId());//静态方法
```
//自定义方法与调用 ## 2、脚本中定义function
```
function add(int a,int b){ function add(int a,int b){
return a+b; return a+b;
}; };
...@@ -70,12 +75,12 @@ function sub(int a,int b){ ...@@ -70,12 +75,12 @@ function sub(int a,int b){
a=10; a=10;
return add(a,4) + sub(a,9); return add(a,4) + sub(a,9);
```
2、自定义操作符号:addOperatorWithAlias+addOperator+addFunction
``` ```
## 3、扩展操作符:Operator
### 替换if then else 等关键字
```java
runner.addOperatorWithAlias("如果", "if",null); runner.addOperatorWithAlias("如果", "if",null);
runner.addOperatorWithAlias("则", "then",null); runner.addOperatorWithAlias("则", "then",null);
runner.addOperatorWithAlias("否则", "else",null); runner.addOperatorWithAlias("否则", "else",null);
...@@ -83,8 +88,10 @@ runner.addOperatorWithAlias("否则", "else",null); ...@@ -83,8 +88,10 @@ runner.addOperatorWithAlias("否则", "else",null);
exp = "如果 (如果 1==2 则 false 否则 true) 则 {2+2;} 否则 {20 + 20;}"; exp = "如果 (如果 1==2 则 false 否则 true) 则 {2+2;} 否则 {20 + 20;}";
DefaultContext<String, Object> context = new DefaultContext<String, Object>(); DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.execute(exp,nil,null,false,false,null); runner.execute(exp,nil,null,false,false,null);
```
### 如何自定义Operator
```java
//定义一个继承自com.ql.util.express.Operator的操作符 //定义一个继承自com.ql.util.express.Operator的操作符
public class JoinOperator extends Operator{ public class JoinOperator extends Operator{
public Object executeInner(Object[] list) throws Exception { public Object executeInner(Object[] list) throws Exception {
...@@ -102,38 +109,38 @@ public class JoinOperator extends Operator{ ...@@ -102,38 +109,38 @@ public class JoinOperator extends Operator{
} }
} }
```
### 如何使用Operator
```
//(1)addOperator
ExpressRunner runner = new ExpressRunner(); ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>(); DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.addOperator("join",new JoinOperator()); runner.addOperator("join",new JoinOperator());
Object r = runner.execute("1 join 2 join 3", context, null, false, false); Object r = runner.execute("1 join 2 join 3", context, null, false, false);
System.out.println(r); System.out.println(r);
//返回结果 [1, 2, 3] //返回结果 [1, 2, 3]
class GroupOperator extends Operator { //(2)replaceOperator
public GroupOperator(String aName) { ExpressRunner runner = new ExpressRunner();
this.name= aName; DefaultContext<String, Object> context = new DefaultContext<String, Object>();
} runner.replaceOperator("+",new JoinOperator());
public Object executeInner(Object[] list)throws Exception { Object r = runner.execute("1 + 2 + 3", context, null, false, false);
Object result = Integer.valueOf(0); System.out.println(r);
for (int i = 0; i < list.length; i++) { //返回结果 [1, 2, 3]
result = OperatorOfNumber.add(result, list[i],false);//根据list[i]类型(string,number等)做加法
}
return result;
}
}
runner.addFunction("group", new GroupOperator("group")); //(3)addFunction
ExpressRunner runner = new ExpressRunner(); ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<String, Object>(); DefaultContext<String, Object> context = new DefaultContext<String, Object>();
runner.addOperator("join",new JoinOperator()); runner.addFunction("join",new JoinOperator());
Object r = runner.execute("group(1,2,3)", context, null, false, false); Object r = runner.execute("join(1,2,3)", context, null, false, false);
System.out.println(r); 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); ...@@ -168,7 +175,7 @@ runner.execute(exp, context, null, false, false);
``` ```
4、macro 宏定义 ## 5、macro 宏定义
``` ```
runner.addMacro("计算平均成绩", "(语文+数学+英语)/3.0"); runner.addMacro("计算平均成绩", "(语文+数学+英语)/3.0");
...@@ -183,7 +190,8 @@ System.out.println(r); ...@@ -183,7 +190,8 @@ System.out.println(r);
``` ```
5、编译脚本,查询外部需要定义的变量,注意以下脚本int和没有int的区别 ## 6、编译脚本,查询外部需要定义的变量和函数。
**注意以下脚本int和没有int的区别**
``` ```
String express = "int 平均分 = (语文+数学+英语+综合考试.科目2)/4.0;return 平均分"; String express = "int 平均分 = (语文+数学+英语+综合考试.科目2)/4.0;return 平均分";
...@@ -201,7 +209,7 @@ var : 英语 ...@@ -201,7 +209,7 @@ var : 英语
var : 语文 var : 语文
``` ```
6、关于不定参数的使用 ## 7、关于不定参数的使用
``` ```
@Test @Test
...@@ -228,7 +236,7 @@ var : 语文 ...@@ -228,7 +236,7 @@ var : 语文
} }
``` ```
7、关于集合的快捷写法 ## 8、关于集合的快捷写法
``` ```
@Test @Test
...@@ -246,4 +254,20 @@ var : 语文 ...@@ -246,4 +254,20 @@ var : 语文
System.out.println(r); 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
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId> <artifactId>QLExpress</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>3.1.7</version> <version>3.2.0</version>
<name>QLExpress</name> <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> <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> <url>https://github.com/alibaba/QLExpress</url>
......
...@@ -8,16 +8,14 @@ package com.ql.util.express; ...@@ -8,16 +8,14 @@ package com.ql.util.express;
public interface IExpressContext<K,V> { public interface IExpressContext<K,V> {
/** /**
* 根据名称从属性列表中提取属性值。如果表达式中用到了Spring的对象,也是通过此方法获取 * 根据名称从属性列表中提取属性值。如果表达式中用到了Spring的对象,也是通过此方法获取
* @param name 属性名称 * @param key 属性名称
* @return * @return
* @throws Exception
*/ */
public V get(Object key); public V get(Object key);
/** /**
* 表达式计算的结果可以设置回调用系统,例如 userId = 3 + 4 * 表达式计算的结果可以设置回调用系统,例如 userId = 3 + 4
* @param name 属性名称 * @param name 属性名称
* @param object 属性值 * @param object 属性值
* @throws Exception
*/ */
public V put(K name, V object); public V put(K name, V object);
} }
...@@ -62,7 +62,7 @@ public class WorkflowTest { ...@@ -62,7 +62,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强"); expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(4000)); 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 { ...@@ -94,7 +94,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强"); expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(5000)); 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 { ...@@ -120,7 +120,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强"); expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(6000)); 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 { ...@@ -148,7 +148,7 @@ public class WorkflowTest {
expressContext.put("申请人", "小强"); expressContext.put("申请人", "小强");
expressContext.put("金额", new Integer(7000)); 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.
先完成此消息的编辑!
想要评论请 注册