Class RuleBuilder
java.lang.Object
cloud.opencode.base.rules.dsl.RuleBuilder
Rule Builder - Fluent DSL for Rule Construction
规则构建器 - 规则构建的流式DSL
Provides a fluent API for constructing rules with conditions and actions.
提供用于构建带条件和动作的规则的流式API。
Features | 主要功能:
- Fluent API - 流式API
- Multiple conditions (AND) - 多个条件(AND)
- Multiple actions - 多个动作
- Priority and grouping - 优先级和分组
Usage Examples | 使用示例:
Rule rule = RuleBuilder.rule("vip-discount")
.description("VIP customer discount rule")
.priority(1)
.group("discounts")
.when(ctx -> ctx.get("customerType").equals("VIP"))
.and(ctx -> ctx.<Double>get("amount") > 1000)
.then(ctx -> ctx.setResult("discount", 0.15))
.build();
Security | 安全性:
- Thread-safe: No (builder pattern, single-threaded use) - 线程安全: 否(构建器模式,单线程使用)
- Null-safe: No (conditions and actions must not be null) - 空值安全: 否(条件和动作不能为null)
Performance | 性能特性:
- Time complexity: O(1) per condition/action addition - 每次条件/动作添加 O(1)
- Space complexity: O(c + a) where c = conditions, a = actions - O(c + a), c为条件数, a为动作数
- Since:
- JDK 25, opencode-base-rules V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a rule builder with a default name 使用默认名称创建规则构建器RuleBuilder(String name) Creates a rule builder with the given name 使用给定名称创建规则构建器 -
Method Summary
Modifier and TypeMethodDescriptionAdds an additional AND condition object 添加额外的AND条件对象and(Predicate<RuleContext> predicate) Adds an additional AND condition 添加额外的AND条件Adds an additional action object 添加额外的动作对象andThen(Consumer<RuleContext> action) Adds an additional action 添加额外的动作build()Builds the rule 构建规则description(String description) Sets the rule description 设置规则描述enabled(boolean enabled) Sets whether the rule is enabled 设置规则是否启用Sets the rule group 设置规则分组priority(int priority) Sets the rule priority (lower value = higher priority) 设置规则优先级(值越小优先级越高)static RuleBuilderCreates a rule builder with the given name 使用给定名称创建规则构建器terminal()Marks the rule as terminal (stops engine execution when fired) 将规则标记为终止规则(触发时停止引擎执行)terminal(boolean terminal) Sets whether the rule is terminal 设置规则是否为终止规则Adds an action object 添加动作对象then(Consumer<RuleContext> action) Adds an action using a consumer 使用消费者添加动作Adds a condition object 添加条件对象when(Predicate<RuleContext> predicate) Adds a condition using a predicate 使用谓词添加条件
-
Constructor Details
-
RuleBuilder
Creates a rule builder with the given name 使用给定名称创建规则构建器- Parameters:
name- the rule name | 规则名称
-
RuleBuilder
public RuleBuilder()Creates a rule builder with a default name 使用默认名称创建规则构建器
-
-
Method Details
-
rule
Creates a rule builder with the given name 使用给定名称创建规则构建器- Parameters:
name- the rule name | 规则名称- Returns:
- the builder | 构建器
-
description
Sets the rule description 设置规则描述- Parameters:
description- the description | 描述- Returns:
- this builder | 此构建器
-
priority
Sets the rule priority (lower value = higher priority) 设置规则优先级(值越小优先级越高)- Parameters:
priority- the priority | 优先级- Returns:
- this builder | 此构建器
-
group
Sets the rule group 设置规则分组- Parameters:
group- the group name | 组名- Returns:
- this builder | 此构建器
-
enabled
Sets whether the rule is enabled 设置规则是否启用- Parameters:
enabled- whether enabled | 是否启用- Returns:
- this builder | 此构建器
-
terminal
Marks the rule as terminal (stops engine execution when fired) 将规则标记为终止规则(触发时停止引擎执行)- Returns:
- this builder | 此构建器
-
terminal
Sets whether the rule is terminal 设置规则是否为终止规则- Parameters:
terminal- whether terminal | 是否为终止规则- Returns:
- this builder | 此构建器
-
when
Adds a condition using a predicate 使用谓词添加条件- Parameters:
predicate- the condition predicate | 条件谓词- Returns:
- this builder | 此构建器
-
when
Adds a condition object 添加条件对象- Parameters:
condition- the condition | 条件- Returns:
- this builder | 此构建器
-
and
Adds an additional AND condition 添加额外的AND条件- Parameters:
predicate- the condition predicate | 条件谓词- Returns:
- this builder | 此构建器
-
and
Adds an additional AND condition object 添加额外的AND条件对象- Parameters:
condition- the condition | 条件- Returns:
- this builder | 此构建器
-
then
Adds an action using a consumer 使用消费者添加动作- Parameters:
action- the action consumer | 动作消费者- Returns:
- this builder | 此构建器
-
then
Adds an action object 添加动作对象- Parameters:
action- the action | 动作- Returns:
- this builder | 此构建器
-
andThen
Adds an additional action 添加额外的动作- Parameters:
action- the action consumer | 动作消费者- Returns:
- this builder | 此构建器
-
andThen
Adds an additional action object 添加额外的动作对象- Parameters:
action- the action | 动作- Returns:
- this builder | 此构建器
-
build
Builds the rule 构建规则- Returns:
- the constructed rule | 构建的规则
- Throws:
IllegalStateException- if no condition or action is set | 如果未设置条件或动作则抛出
-