moonrules

    Explainable and budget-limited JSON business rules for MoonBit

    rules-engine
    json
    validation
    explainable
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    4 months ago
    Downloads
    48

    #MoonRules

    CI

    MoonBit 原生的可解释、受预算限制的 JSON 业务规则引擎。

    MoonRules 把动态业务条件解析成类型明确的规则树,在执行前给出诊断,在执行后返回 Pass、Fail 或 Indeterminate,并保留每一步变量取值、比较结果、错误与短路原因。

    #为什么不是 JSON Schema

    JSON Schema validates structure; MoonRules evaluates business decisions and explains them.

    JSON Schema 适合回答“输入的字段和类型是否合法”;MoonRules 回答“这份合法输入是否满足当前业务条件,以及为什么”。例如,订单结构可以完全合法,但用户仍可能因为年龄、身份或金额不满足优惠券规则。

    #30 秒示例

    规则:

    { "id": "adult-check", "description": "成年人准入", "condition": { ">=": [{ "var": "user.age" }, 18] } }

    数据:

    { "user": { "age": 20 } }

    结果不仅是 Pass,还包含独立的变量和字面量 Trace:

    PASS adult-check [>=] ├─ VALUE var(user.age) │ └─ result = 20 ├─ VALUE literal │ └─ result = 18 └─ result = true

    #安装

    先安装 MoonBit 工具链:

    curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash

    克隆项目后验证:

    moon check --deny-warn moon test moon build --target native

    #库 API

    公共入口只有四组:

    • parse(source):解析 JSON 字符串为 RuleDocument。
    • check(rule, budget?):返回 Array[Diagnostic];空数组表示通过。
    • evaluate(rule, data, budget, trace_mode?):执行已解析规则。
    • evaluate_json(rule_source, data_source, budget?, trace_mode?):完成解析、检查与执行。

    文本输出使用 render_report,结构化输出使用 report_to_json。完整契约见 docs/API.md。

    #CLI

    检查规则:

    moon run --target native cmd/main check examples/coupon.rule.json

    执行规则:

    moon run --target native cmd/main eval examples/coupon.rule.json --data examples/coupon.data.json

    退出码:

    • 0:规则通过。
    • 1:规则执行成功,但结果不通过。
    • 2:参数、I/O、解析、检查或执行出现错误。

    #可解释 Trace

    每个 var、字面量和操作符都有独立 Trace 节点。节点记录规则路径、状态、已解析输入、结果、消息和子节点。

    and 遇到确定性 false、or 遇到确定性 true 后,未执行的同级节点会标记为 SKIPPED,不会假装已经计算。预算超限时保留已有 Trace,并在截断点写入 ERROR 节点。

    #运行前检查

    check 在没有运行时数据的情况下检查:

    • 操作符参数数量。
    • V1 点号数据路径。
    • 明显错误的字面量类型。
    • 规则深度和节点数。
    • 调用方预算是否突破库上限。

    诊断包含稳定的错误码、规则路径、说明和修改建议。

    #安全预算

    默认确定性预算:

    预算默认上限
    规则深度64
    规则节点4096
    执行步骤10000
    Trace 节点4096
    文本预览字符256

    调用方可以调低限制,但不能突破默认上限。MoonRules 不使用墙钟超时改变规则结果。

    #V1 操作符

    • 数据:var、literal
    • 逻辑:and、or、not
    • 比较:==、!=、>、>=、<、<=
    • 集合与字符串:in、contains、starts_with、ends_with

    完整参数数量、类型和示例见 docs/DSL.md。

    #示例

    • 优惠券资格:examples/coupon.*
    • API 请求准入:examples/api-access.*
    • 会员资格:examples/membership.*

    每个场景都提供通过和不通过的数据。examples/coupon.data.json 是主演示中的不通过输入。

    #限制与非目标

    V1 不提供完整 JSONLogic 兼容、数组下标数据路径、循环、用户函数、自定义操作符、网络访问、数据库、Web 服务或 GUI。TraceMode::Off 和 Summary 只保留 API 形状,调用时会明确返回 UnsupportedTraceMode。

    #开发与测试

    moon fmt --check moon check --deny-warn moon test --target native moon test --target wasm-gc moon build --target native moon build --target wasm-gc

    核心库保持跨目标;CLI 因文件和终端 I/O 明确限定为 native。

    #许可证

    Apache License 2.0。详见 LICENSE。

    Budget

    pub(all) struct Budget {
    max_depth : Int
    max_rule_nodes : Int
    max_steps : Int
    max_trace_nodes : Int
    max_preview_chars : Int
    } derive(Eq,
    Debug
    )

    Budget::default

    fn Budget::default() -> Budget

    DataPathError

    pub(all) enum DataPathError {
    InvalidDataPath(String)
    MissingDataPath(String)
    NonObjectSegment(String)
    } derive(Eq,
    Debug
    )

    Decision

    pub(all) enum Decision {
    Pass
    Fail
    Indeterminate(EvalError)
    } derive(Eq,
    Debug
    )

    Decision::exit_code

    fn Decision::exit_code(self : Decision) -> Int

    Diagnostic

    pub(all) struct Diagnostic {
    code : String
    severity : DiagnosticSeverity
    rule_path : String
    message : String
    suggestion : String
    } derive(Eq,
    Debug
    )

    DiagnosticSeverity

    pub(all) enum DiagnosticSeverity {
    Error
    Warning
    } derive(Eq,
    Debug
    )

    EvalError

    pub(all) enum EvalError {
    MissingVariable(String)
    TypeMismatch(String)
    BudgetExceeded(String, Int, Int)
    UnsupportedTraceMode(String)
    } derive(Eq,
    Debug
    )

    EvaluationReport

    pub(all) struct EvaluationReport {
    decision : Decision
    trace : TraceNode
    stats : ExecutionStats
    } derive(Eq,
    Debug
    )

    ExecutionStats

    pub(all) struct ExecutionStats {
    steps_executed : Int
    nodes_evaluated : Int
    max_depth_reached : Int
    trace_nodes_emitted : Int
    } derive(Eq,
    Debug
    )

    Expr

    pub(all) enum Expr {
    Literal(Json)
    Variable(String)
    Operation(Operator, Array[Expr])
    } derive(Eq,
    Debug
    )

    Operator

    pub(all) enum Operator {
    And
    Or
    Not
    Equal
    NotEqual
    GreaterThan
    GreaterOrEqual
    LessThan
    LessOrEqual
    In
    Contains
    StartsWith
    EndsWith
    } derive(Eq,
    Debug
    )

    ResolvedInput

    pub(all) struct ResolvedInput {
    source : String
    value : Json
    } derive(Eq,
    Debug
    )

    RuleDocument

    pub(all) struct RuleDocument {
    id : String
    description : String
    condition : Expr
    } derive(Eq,
    Debug
    )

    RuleParseError

    pub(all) enum RuleParseError {
    ExpectedObject(String)
    MissingField(String)
    ExpectedString(String)
    MultipleOperatorKeys(String)
    UnknownOperator(String)
    InvalidVariable(String)
    InvalidLiteral(String)
    InvalidArguments(String)
    } derive(Eq,
    Debug
    )

    RuleParseError::message

    fn RuleParseError::message(self : RuleParseError) -> String

    RulePath

    pub(all) struct RulePath {
    segments : Array[RulePathSegment]
    } derive(Eq,
    Debug
    )

    RulePath::field

    fn RulePath::field(self : RulePath, name : String) -> RulePath

    RulePath::index

    fn RulePath::index(self : RulePath, index : Int) -> RulePath

    RulePath::root

    fn RulePath::root() -> RulePath

    RulePath::to_string

    fn RulePath::to_string(self : RulePath) -> String

    RulePathSegment

    pub(all) enum RulePathSegment {
    Field(String)
    Index(Int)
    } derive(Eq,
    Debug
    )

    TraceMode

    pub(all) enum TraceMode {
    Off
    Summary
    Full
    } derive(Eq,
    Debug
    )

    TraceNode

    pub(all) struct TraceNode {
    rule_path : String
    operator : String
    status : TraceStatus
    resolved_inputs : Array[ResolvedInput]
    result : Json?
    message : String
    children : Array[TraceNode]
    } derive(Eq,
    Debug
    )

    TraceStatus

    pub(all) enum TraceStatus {
    Value
    Pass
    Fail
    Error
    Skipped
    } derive(Eq,
    Debug
    )

    check

    fn check(rule : RuleDocument, budget? : Budget) -> Array[Diagnostic]

    check_expr

    fn check_expr(expr : Expr, budget : Budget) -> Array[Diagnostic]

    evaluate

    fn evaluate(rule : RuleDocument, data : Json, budget : Budget, trace_mode? : TraceMode) -> EvaluationReport

    evaluate_condition

    fn evaluate_condition(condition : Expr, data : Json, budget : Budget, trace_mode? : TraceMode) -> EvaluationReport

    evaluate_json

    fn evaluate_json(rule_source : String, data_source : String, budget? : Budget, trace_mode? : TraceMode) -> Result[EvaluationReport, String]

    json_deep_equal

    fn json_deep_equal(left : Json, right : Json) -> Bool

    parse

    fn parse(source : String) -> Result[RuleDocument, RuleParseError]

    parse_data_path

    fn parse_data_path(path : String) -> Result[Array[String], DataPathError]

    parse_rule_document

    fn parse_rule_document(json : Json) -> Result[RuleDocument, RuleParseError]

    render_report

    fn render_report(report : EvaluationReport, budget : Budget) -> String

    report_to_json

    fn report_to_json(report : EvaluationReport) -> Json

    resolve_data_path

    fn resolve_data_path(data : Json, path : String) -> Result[Json, DataPathError]

    validate_budget

    fn validate_budget(budget : Budget) -> Array[Diagnostic]