|
| 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 | +---- |
0 commit comments