Class OpenRules
java.lang.Object
cloud.opencode.base.rules.OpenRules
OpenRules Facade - Unified Entry Point for Rules Engine
OpenRules门面 - 规则引擎统一入口
Provides factory methods and utilities for creating rules, rule engines, decision tables, and related components.
提供创建规则、规则引擎、决策表和相关组件的工厂方法和工具。
Features | 主要功能:
- Rule creation via fluent DSL - 通过流式DSL创建规则
- Rule engine configuration - 规则引擎配置
- Decision table building - 决策表构建
- Conflict resolution strategies - 冲突解决策略
- Rule execution listeners - 规则执行监听器
Usage Examples | 使用示例:
// Create a simple rule
Rule discountRule = OpenRules.rule("discount-rule")
.description("Apply discount for VIP customers")
.priority(100)
.when(ctx -> "VIP".equals(ctx.get("customerType")))
.then(ctx -> ctx.put("discount", 0.15))
.build();
// Create and configure rule engine
RuleEngine engine = OpenRules.engine()
.register(discountRule)
.setConflictResolver(OpenRules.priorityResolver())
.addListener(OpenRules.loggingListener())
.build();
// Create and evaluate decision table
DecisionTable table = OpenRules.decisionTable()
.name("pricing")
.hitPolicy(HitPolicy.FIRST)
.input("customerType", String.class)
.input("amount", Double.class)
.output("discount", Double.class)
.row(new Object[]{"VIP", ">= 1000"}, new Object[]{0.15})
.row(new Object[]{"VIP", "-"}, new Object[]{0.10})
.row(new Object[]{"-", "-"}, new Object[]{0.0})
.build();
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: No (arguments must not be null) - 空值安全: 否(参数不能为null)
- Since:
- JDK 25, opencode-base-rules V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Actionaction(Consumer<RuleContext> consumer) Creates an action from a consumer 从消费者创建动作static ConditionCreates an always-false condition 创建始终为假的条件static ConditionCreates an always-true condition 创建始终为真的条件static Conditioncondition(Predicate<RuleContext> predicate) Creates a condition from a predicate 从谓词创建条件static RuleContextcontext()Creates a new empty rule context 创建新的空规则上下文static RuleContextCreates a rule context with initial facts 使用初始事实创建规则上下文static DecisionTableBuilderCreates a new decision table builder 创建新的决策表构建器static DecisionTableBuilderdecisionTable(String name) Creates a new decision table builder with the given name 使用给定名称创建新的决策表构建器static RuleEngineCreates a new default rule engine 创建新的默认规则引擎static RuleEngineBuilderengine()Creates a new rule engine builder 创建新的规则引擎构建器static RuleEngineengineWith(RuleGroup group) Creates a rule engine with the given rule group 使用给定规则组创建规则引擎static RuleEngineengineWith(Rule... rules) Creates a rule engine with the given rules 使用给定规则创建规则引擎static RuleGroupBuilderCreates a new rule group builder with the given name 使用给定名称创建新的规则组构建器static Stringinfo()Gets the library information 获取库信息static <T> TypedKey<T> Creates a typed key for type-safe fact access 创建类型化键用于类型安全的事实访问static RuleListenerCreates a logging rule listener 创建日志规则监听器static RuleMetricsmetrics()Creates a new rule metrics collector 创建新的规则指标收集器static MetricsListenerCreates a metrics listener with its own internal metrics collector 创建带有内部指标收集器的指标监听器static MetricsListenermetricsListener(RuleMetrics metrics) Creates a metrics listener backed by the given metrics collector 创建由指定指标收集器支持的指标监听器static ActionnoOp()Creates a no-op action 创建无操作动作static ConflictResolverGets the order-based conflict resolver 获取基于顺序的冲突解决器static ConflictResolverGets the priority-based conflict resolver 获取基于优先级的冲突解决器static RuleBuilderrule()Creates a new rule builder 创建新的规则构建器static RuleBuilderCreates a new rule builder with the given name 使用给定名称创建新的规则构建器static TracingRuleListenerCreates a tracing listener for execution tracing 创建追踪监听器用于执行追踪static ValidationReportvalidate(Collection<Rule> rules) Validates a collection of rules 验证规则集合static Stringversion()Gets the library version 获取库版本
-
Method Details
-
rule
Creates a new rule builder with the given name 使用给定名称创建新的规则构建器- Parameters:
name- the rule name | 规则名称- Returns:
- the rule builder | 规则构建器
-
rule
Creates a new rule builder 创建新的规则构建器- Returns:
- the rule builder | 规则构建器
-
group
Creates a new rule group builder with the given name 使用给定名称创建新的规则组构建器- Parameters:
name- the group name | 组名称- Returns:
- the rule group builder | 规则组构建器
-
engine
Creates a new rule engine builder 创建新的规则引擎构建器- Returns:
- the rule engine builder | 规则引擎构建器
-
defaultEngine
Creates a new default rule engine 创建新的默认规则引擎- Returns:
- the rule engine | 规则引擎
-
engineWith
Creates a rule engine with the given rules 使用给定规则创建规则引擎- Parameters:
rules- the rules to register | 要注册的规则- Returns:
- the rule engine | 规则引擎
-
engineWith
Creates a rule engine with the given rule group 使用给定规则组创建规则引擎- Parameters:
group- the rule group to register | 要注册的规则组- Returns:
- the rule engine | 规则引擎
-
decisionTable
Creates a new decision table builder 创建新的决策表构建器- Returns:
- the decision table builder | 决策表构建器
-
decisionTable
Creates a new decision table builder with the given name 使用给定名称创建新的决策表构建器- Parameters:
name- the table name | 表名称- Returns:
- the decision table builder | 决策表构建器
-
context
Creates a new empty rule context 创建新的空规则上下文- Returns:
- the rule context | 规则上下文
-
contextOf
Creates a rule context with initial facts 使用初始事实创建规则上下文- Parameters:
keyValues- alternating key-value pairs | 交替的键值对- Returns:
- the rule context | 规则上下文
-
condition
Creates a condition from a predicate 从谓词创建条件- Parameters:
predicate- the predicate | 谓词- Returns:
- the condition | 条件
-
alwaysTrue
Creates an always-true condition 创建始终为真的条件- Returns:
- the condition | 条件
-
alwaysFalse
Creates an always-false condition 创建始终为假的条件- Returns:
- the condition | 条件
-
action
Creates an action from a consumer 从消费者创建动作- Parameters:
consumer- the consumer | 消费者- Returns:
- the action | 动作
-
noOp
-
priorityResolver
Gets the priority-based conflict resolver 获取基于优先级的冲突解决器Rules are ordered by priority (lower number = higher priority).
规则按优先级排序(数字越小 = 优先级越高)。
- Returns:
- the priority conflict resolver | 优先级冲突解决器
-
orderResolver
Gets the order-based conflict resolver 获取基于顺序的冲突解决器Rules are kept in registration order.
规则保持注册顺序。
- Returns:
- the order conflict resolver | 顺序冲突解决器
-
loggingListener
Creates a logging rule listener 创建日志规则监听器- Returns:
- the logging listener | 日志监听器
-
key
-
tracingListener
Creates a tracing listener for execution tracing 创建追踪监听器用于执行追踪- Returns:
- the tracing listener | 追踪监听器
-
metrics
Creates a new rule metrics collector 创建新的规则指标收集器- Returns:
- the rule metrics | 规则指标
-
metricsListener
Creates a metrics listener backed by the given metrics collector 创建由指定指标收集器支持的指标监听器- Parameters:
metrics- the metrics collector | 指标收集器- Returns:
- the metrics listener | 指标监听器
-
metricsListener
Creates a metrics listener with its own internal metrics collector 创建带有内部指标收集器的指标监听器- Returns:
- the metrics listener | 指标监听器
-
validate
Validates a collection of rules 验证规则集合- Parameters:
rules- the rules to validate | 要验证的规则- Returns:
- the validation report | 验证报告
-
version
-
info
-