{"id":19036195,"url":"https://github.com/tx7do/go-rulengine-example","last_synced_at":"2026-05-05T21:30:16.624Z","repository":{"id":112433630,"uuid":"464099075","full_name":"tx7do/go-rulengine-example","owner":"tx7do","description":null,"archived":false,"fork":false,"pushed_at":"2022-03-08T17:00:33.000Z","size":1287,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-02T05:32:24.584Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tx7do.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-27T10:05:04.000Z","updated_at":"2024-12-20T04:07:33.000Z","dependencies_parsed_at":"2023-05-19T02:00:43.540Z","dependency_job_id":null,"html_url":"https://github.com/tx7do/go-rulengine-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fgo-rulengine-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fgo-rulengine-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fgo-rulengine-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fgo-rulengine-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tx7do","download_url":"https://codeload.github.com/tx7do/go-rulengine-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240093142,"owners_count":19746774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:53:42.183Z","updated_at":"2026-05-05T21:30:16.582Z","avatar_url":"https://github.com/tx7do.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-rulengine-example\n\n本项目是测试golang下面的规则引擎\n\n## 什么是规则引擎\n\n规则引擎是一个逻辑或条件，例如“当某些条件被评估为真时，然后执行某些任务。”规则引擎可以被视为一个复杂的 if/then 语句解释器。被解释的 if/then 语句称为规则。将规则引擎想象成一个将数据和规则作为输入的系统。它将这些规则应用于数据，并根据规则定义为我们提供输出。\n\n![什么是规则引擎](docs/rule_egine_1_.svg)\n\n## GO规则引擎列表\n\n| 名称\t                                                         | 规则描述语言\t    | 使用场景\t     | 使用复杂性 |\n|-------------------------------------------------------------|------------|-----------|-------|\n| [govaluate](https://github.com/Knetic/govaluate)            | 类Golang\t   | 表达式解析\t    | 低     |\n| [YQL](https://github.com/caibirdme/yql)                     | 类SQL\t      | 表达式解析\t    | 低     |\n| [Gval](https://github.com/PaesslerAG/gval)                  | 类Golang\t   | 表达式解析\t    | 低     |\n| [Grule](https://github.com/hyperjumptech/grule-rule-engine) | 自定义DSL     | 规则执行\t     | 中     |\n| [Gengine](https://github.com/bilibili/gengine)              | 自定义DSL     | 规则执行\t     | 中     |\n| [goja](https://github.com/dop251/goja)                      | JavaScript | 规则解析\t     | 中     |\n| [cel-go](https://github.com/google/cel-go)                  | 类C\t        | 通用表达式语言\t\t | 中     |\n| [GopherLua](https://github.com/yuin/gopher-lua)             | lua\t       | 规则解析\t     | 高     |\n\n## 规则执行模式\n\n通过对各种业务场景的分析提炼，一个规则引擎至少应该满足3种执行模式。但实际上，规则执行模式至少有5种，具体执行模式，如下图所示：\n\n![规则执行模式](docs/rule-model.png)\n\n### 1. **顺序模式(Sort Model)**\n\n![顺序模式](docs/sort_model.png)\n\n规则优先级高越高的越先执行，规则优先级低的越后执行。这也是drools支持的模式。此模式的缺点很明显：随着规则链越来越长，执行规则返回的速度也越来越慢。\n\n### 2. **并发执行模式(Concurrent Model)**\n\n![并发执行模式](docs/concurrent_model.png)\n\n在此执行模式下，多个规则执行时，不考虑规则之间的优先级，规则与规则之间并发执行。规则执行的返回的速度等于所有规则中的执行时间最长的那个规则的速度（**逆木桶原理**）。执行性能优异，但无法满足规则优先级。\n\n### 3. **混合执行模式（Mix Model）**\n\n![混合执行模式](docs/mix_model.png)\n\n规则引擎选择一个优先级最高规则的最先执行，剩下的规则并发执行。规则执行返回耗时 = 最高优先级的那个规则执行时间 + 并发执行中执行时间最长的那个规则耗时；此模式兼顾优先级和性能，适合于有豁免规则(或前置规则)的场景。\n\n### 4. **逆混合执行模式(Inverse Mix Model)**\n\n![逆混合执行模式](docs/inverse_mix_model.png)\n\n优先级最高的n-1个规则并发执行，执行完毕之后，再执行剩下的一个优先级最低的规则。这种模式适用于有很多前导判断规则的场景。其特性与混合模式类似，兼顾性能和优先级。\n\n### 5. **桶模式（Bucket Model）**\n\n![桶模式](docs/bucket_model.png)\n\n规则引擎基于规则优先级进行分桶，优先级相同的规则置于同一个桶中，桶内的规则并发执行，桶间的规则基于规则优先级顺序执行。\n\n## 参考资料\n\n- [RulesEngine - Martin Fowler](https://martinfowler.com/bliki/RulesEngine.html)\n- [Business rules engine](https://en.wikipedia.org/wiki/Business_rules_engine)\n- [What is Rule-Engine?](https://medium.com/@er.rameshkatiyar/what-is-rule-engine-86ea759ad97d)\n- [What is a rule engine ?](http://www.mastertheboss.com/bpm/drools/what-is-a-rule-engine/)\n- [B站新一代golang规则引擎的设计与实现](https://cloud.tencent.com/developer/news/667806)\n- [规则引擎在哔哩哔哩的应用](https://www.biaodianfu.com/bilibili-gengine.html)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Fgo-rulengine-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftx7do%2Fgo-rulengine-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Fgo-rulengine-example/lists"}