Skip to content

Commit 9cccd7b

Browse files
committed
add execute source adoc
1 parent e33b169 commit 9cccd7b

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

.github/workflows/reduce-adoc.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
paths:
55
- README-source.adoc
66
- README-EN-source.adoc
7+
- docs/execute-source.adoc
8+
- docs/execute-en-source.adoc
79
branches: ['**']
810
jobs:
911
build:
@@ -17,9 +19,12 @@ jobs:
1719
# to preserve preprocessor conditionals, add the --preserve-conditionals option
1820
run: asciidoctor-reducer --preserve-conditionals -o README.adoc README-source.adoc
1921
- name: Reduce README-EN
20-
# to preserve preprocessor conditionals, add the --preserve-conditionals option
2122
run: asciidoctor-reducer --preserve-conditionals -o README-EN.adoc README-EN-source.adoc
23+
- name: Reduce docs/execute
24+
run: asciidoctor-reducer --preserve-conditionals -o docs/execute.adoc docs/execute-source.adoc
25+
- name: Reduce docs/execute-en
26+
run: asciidoctor-reducer --preserve-conditionals -o docs/execute-en.adoc docs/execute-en-source.adoc
2227
- name: Commit and Push README files
2328
uses: EndBug/add-and-commit@v9
2429
with:
25-
add: README.adoc README-EN.adoc
30+
add: README.adoc README-EN.adoc docs/execute.adoc docs/execute-en.adoc

docs/execute-en-source.adoc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
:toc:
2+
3+
= Expression Execution
4+
5+
This section describes the different ways to execute expressions in QLExpress4.
6+
7+
== Use Map as the context
8+
9+
- `execute(String, Map<String,Object>, QLOptions)`
10+
- Description: According to the method comments, use a `Map` as the context; the keys in the `Map` are the variable names used in the script, and the values are the corresponding variable values.
11+
- Typical scenario: Build a `Map` from external business data and pass it in so the script can access variables directly by name.
12+
13+
[source,java,indent=0]
14+
----
15+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=firstQl]
16+
----
17+
18+
== Use object fields as the context
19+
20+
- `execute(String, Object, QLOptions)`
21+
- Description: Use a plain Java object as the context; variable names in the script correspond to the object's field names (or accessible getters).
22+
- Typical scenario: When you already have a DTO/POJO that carries the context data, pass the object directly so the script can read its fields.
23+
24+
// Note: This references the test code for execution with an object context.
25+
[source,java,indent=0]
26+
----
27+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=executeWithObject]
28+
----
29+
30+
== Use a set of aliased objects
31+
32+
- `executeWithAliasObjects(String, QLOptions, Object...)`
33+
- Description: Pass objects annotated with `@QLAlias`; the annotation value becomes the variable name in the context. Objects without the annotation are ignored.
34+
- Typical scenario: Expose object properties/methods with Chinese (or domain-specific) aliases so rule authors can write scripts more intuitively.
35+
36+
[source,java,indent=0]
37+
----
38+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=qlAlias]
39+
----
40+
41+
== Use a custom `ExpressContext`
42+
43+
- `execute(String, ExpressContext, QLOptions)`
44+
- Description: This overload allows passing a custom context that implements `ExpressContext` (for example, a dynamic variable context). The other `execute` overloads ultimately delegate to this method.
45+
- Typical scenario: When you need on-demand, by-name evaluation of variable values, read/write isolation, or integration with external storage, a custom context is the most flexible choice.
46+
47+
[source,java,indent=0]
48+
----
49+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=dynamicVar]
50+
----

docs/execute-source.adoc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
:toc:
2+
3+
= 表达式执行
4+
5+
本节说明在 QLExpress4 中执行表达式的多种方式。
6+
7+
== 使用 Map 作为上下文:
8+
9+
- `execute(String, Map<String,Object>, QLOptions)`
10+
- 说明:根据方法注释,使用 `Map` 作为上下文,`Map` 的 key 即为脚本中的变量名,value 为对应变量值。
11+
- 典型场景:从外部业务数据构造一个 `Map` 传入,直接在脚本里以变量名访问。
12+
13+
[source,java,indent=0]
14+
----
15+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=firstQl]
16+
----
17+
18+
== 使用对象字段作为上下文
19+
20+
- `execute(String, Object, QLOptions)`
21+
- 说明:根据方法注释,使用一个普通 Java 对象作为上下文,脚本中的变量名对应该对象的字段名(或可访问的 getter)。
22+
- 典型场景:已有一个承载上下文数据的 DTO/POJO,直接将对象传入以便脚本读取其字段。
23+
24+
// 说明:此处引用对象上下文执行的测试代码。
25+
[source,java,indent=0]
26+
----
27+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=executeWithObject]
28+
----
29+
30+
== 使用带别名的对象集合
31+
32+
- `executeWithAliasObjects(String, QLOptions, Object...)`
33+
- 说明:根据方法注释,传入标记了 `@QLAlias` 的对象,注解值将作为上下文中的变量名;未标记的对象会被忽略。
34+
- 典型场景:以中文(或领域语言)别名暴露对象属性/方法,供规则编写者直观地编写脚本。
35+
36+
[source,java,indent=0]
37+
----
38+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=qlAlias]
39+
----
40+
41+
== 使用自定义 `ExpressContext`
42+
43+
- `execute(String, ExpressContext, QLOptions)`
44+
- 说明:该重载允许传入实现了 `ExpressContext` 的自定义上下文(例如动态变量上下文)。其它 `execute` 重载最终也会委托到此方法。
45+
- 典型场景:需要按需按名计算变量值、隔离读写、或接入外部存储等高级能力时,自定义上下文最灵活。
46+
47+
[source,java,indent=0]
48+
----
49+
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=dynamicVar]
50+
----

src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,13 +1251,15 @@ public static class MyObj {
12511251

12521252
@Test
12531253
public void executeWithObjContextTest() {
1254+
// tag::executeWithObject[]
12541255
MyObj myObj = new MyObj();
12551256
myObj.a = 1;
12561257
myObj.b = "test";
12571258

12581259
Express4Runner express4Runner = new Express4Runner(InitOptions.DEFAULT_OPTIONS);
12591260
Object result = express4Runner.execute("a+b", myObj, QLOptions.DEFAULT_OPTIONS).getResult();
12601261
assertEquals("1test", result);
1262+
// end::executeWithObject[]
12611263
}
12621264

12631265
@Test

0 commit comments

Comments
 (0)