{"id":15102207,"url":"https://github.com/microsoft/rulesengine","last_synced_at":"2025-05-06T01:30:07.408Z","repository":{"id":37270910,"uuid":"199628708","full_name":"microsoft/RulesEngine","owner":"microsoft","description":"A Json based Rules Engine with extensive Dynamic expression support","archived":false,"fork":false,"pushed_at":"2025-04-28T22:33:36.000Z","size":410,"stargazers_count":3892,"open_issues_count":59,"forks_count":586,"subscribers_count":100,"default_branch":"main","last_synced_at":"2025-04-30T12:49:21.061Z","etag":null,"topics":["dotnet","engine","expression-evaluator","nuget","rules","rules-engine","workflow"],"latest_commit_sha":null,"homepage":"https://microsoft.github.io/RulesEngine/","language":"C#","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/microsoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-30T10:20:41.000Z","updated_at":"2025-04-29T05:52:16.000Z","dependencies_parsed_at":"2022-07-09T18:46:42.670Z","dependency_job_id":"f4488ab5-f954-406e-8f60-9d346cf2a368","html_url":"https://github.com/microsoft/RulesEngine","commit_stats":{"total_commits":153,"total_committers":34,"mean_commits":4.5,"dds":0.5686274509803921,"last_synced_commit":"4ca3cc2b0a5d7af209404ab8767718d0df7ea960"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FRulesEngine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FRulesEngine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FRulesEngine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2FRulesEngine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microsoft","download_url":"https://codeload.github.com/microsoft/RulesEngine/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252604067,"owners_count":21775044,"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":["dotnet","engine","expression-evaluator","nuget","rules","rules-engine","workflow"],"created_at":"2024-09-25T18:49:13.580Z","updated_at":"2025-05-06T01:30:07.323Z","avatar_url":"https://github.com/microsoft.png","language":"C#","readme":"# Rules Engine\n![build](https://github.com/microsoft/RulesEngine/workflows/build/badge.svg?branch=main)\n[![Coverage Status](https://coveralls.io/repos/github/microsoft/RulesEngine/badge.svg?branch=main)](https://coveralls.io/github/microsoft/RulesEngine?branch=main)\n[![Nuget download][download-image]][download-url]\n\n[download-image]: https://img.shields.io/nuget/dt/RulesEngine\n[download-url]: https://www.nuget.org/packages/RulesEngine/\n\n## Overview\n\nRules Engine is a library/NuGet package for abstracting business logic/rules/policies out of a system. It provides a simple way of giving you the ability to put your rules in a store outside the core logic of the system, thus ensuring that any change in rules don't affect the core system.\n\n## Installation\n\nTo install this library, download the latest version of [NuGet Package](https://www.nuget.org/packages/RulesEngine/) from [nuget.org](https://www.nuget.org/) and refer it into your project.  \n\n## How to use it\n\nThere are several ways to populate workflows for the Rules Engine as listed below.\n\nYou need to store the rules based on the [schema definition](https://github.com/microsoft/RulesEngine/blob/main/schema/workflow-schema.json) given and they can be stored in any store as deemed appropriate like Azure Blob Storage, Cosmos DB, Azure App Configuration, [Entity Framework](https://github.com/microsoft/RulesEngine#entity-framework), SQL Servers, file systems etc. For RuleExpressionType `LambdaExpression`, the rule is written as a [lambda expressions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).\n\nAn example rule:\n\n```json\n[\n  {\n    \"WorkflowName\": \"Discount\",\n    \"Rules\": [\n      {\n        \"RuleName\": \"GiveDiscount10\",\n        \"SuccessEvent\": \"10\",\n        \"ErrorMessage\": \"One or more adjust rules failed.\",\n        \"ErrorType\": \"Error\",\n        \"RuleExpressionType\": \"LambdaExpression\",\n        \"Expression\": \"input1.country == \\\"india\\\" AND input1.loyaltyFactor \u003c= 2 AND input1.totalPurchasesToDate \u003e= 5000\"\n      },\n      {\n        \"RuleName\": \"GiveDiscount20\",\n        \"SuccessEvent\": \"20\",\n        \"ErrorMessage\": \"One or more adjust rules failed.\",\n        \"ErrorType\": \"Error\",\n        \"RuleExpressionType\": \"LambdaExpression\",\n        \"Expression\": \"input1.country == \\\"india\\\" AND input1.loyaltyFactor \u003e= 3 AND input1.totalPurchasesToDate \u003e= 10000\"\n      }\n    ]\n  }\n]\n```\n\nYou can inject the rules into the Rules Engine by initiating an instance by using the following code - \n\n```c#\nvar rulesEngine = new RulesEngine(workflow);\n```\nHere, *workflow* is a list of deserialized objects based on the schema explained above\nOnce initialised, the Rules Engine needs to execute the rules for a given input. This can be done by calling the method `ExecuteAllRulesAsync`: \n\n```c#\nList\u003cRuleResultTree\u003e response = await rulesEngine.ExecuteAllRulesAsync(workflowName, input);\n```\n\nHere, *workflowName* is the name of the workflow, which is *Discount* in the above mentioned example. And *input* is the object which needs to be checked against the rules,  which itself may consist of a list of class instances.\n\nThe *response* will contain a list of [*RuleResultTree*](https://github.com/microsoft/RulesEngine/wiki/Getting-Started#ruleresulttree) which gives information if a particular rule passed or failed. \n\n_Note: A detailed example showcasing how to use Rules Engine is explained in [Getting Started page](https://github.com/microsoft/RulesEngine/wiki/Getting-Started) of [Rules Engine Wiki](https://github.com/microsoft/RulesEngine/wiki)._\n\n_A demo app for the is available at [this location](https://github.com/microsoft/RulesEngine/tree/main/demo)._\n\n### Basic\n\nA simple example via code only is as follows:\n\n```c#\nList\u003cRule\u003e rules = new List\u003cRule\u003e();\n\nRule rule = new Rule();\nrule.RuleName = \"Test Rule\";\nrule.SuccessEvent = \"Count is within tolerance.\";\nrule.ErrorMessage = \"Over expected.\";\nrule.Expression = \"count \u003c 3\";\nrule.RuleExpressionType = RuleExpressionType.LambdaExpression;\nrules.Add(rule);\n\nvar workflows = new List\u003cWorkflow\u003e();\n\nWorkflow exampleWorkflow = new Workflow();\nexampleWorkflow.WorkflowName = \"Example Workflow\";\nexampleWorkflow.Rules = rules;\n\nworkflows.Add(exampleWorkflow);\n\nvar bre = new RulesEngine.RulesEngine(workflows.ToArray());\n```\n### Entity Framework\n\nConsuming Entity Framework and populating the Rules Engine is shown in the [EFDemo class](https://github.com/microsoft/RulesEngine/blob/main/demo/DemoApp/EFDemo.cs) with Workflow rules populating the array and passed to the Rules Engine, The Demo App includes an example [RulesEngineDemoContext](https://github.com/microsoft/RulesEngine/blob/main/demo/DemoApp.EFDataExample/RulesEngineDemoContext.cs) using SQLite and could be swapped out for another provider.\n\n```c#\nvar wfr = db.Workflows.Include(i =\u003e i.Rules).ThenInclude(i =\u003e i.Rules).ToArray();\nvar bre = new RulesEngine.RulesEngine(wfr, null);\n```\n\n*Note: For each level of nested rules expected, a ThenInclude query appended will be needed as shown above.*\n\n## How it works\n\n![](https://github.com/microsoft/RulesEngine/blob/main/assets/BlockDiagram.png)\n\nThe rules can be stored in any store and be fed to the system in a structure which adheres to the [schema](https://github.com/microsoft/RulesEngine/blob/main/schema/workflow-schema.json) of WorkFlow model.\n\nA wrapper needs to be created over the Rules Engine package, which will get the rules and input message(s) from any store that your system dictates and put it into the Engine. The wrapper then handles the output using appropriate means.\n\n_Note: To know in detail of the workings of Rules Engine, please visit [How it works section](https://github.com/microsoft/RulesEngine/wiki/Introduction#how-it-works) in [Rules Engine Wiki](https://github.com/microsoft/RulesEngine/wiki)._\n\n## 3rd Party Tools\n\n### RulesEngine Editor\nThere is an editor library with it's own [NuGet Package](https://www.nuget.org/packages/RulesEngineEditor/) written in Blazor, more information is in it's repo https://github.com/alexreich/RulesEngineEditor. \n\n#### Live Demo\nhttps://alexreich.github.io/RulesEngineEditor  \n\u003e This can also be installed as a standalone PWA and used offline.\n\n#### With Sample Data\nhttps://alexreich.github.io/RulesEngineEditor/demo\n\n## Contributing\n\nThis project welcomes contributions and suggestions.  Most contributions require you to agree to a\nContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us\nthe rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.\n\nThis project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).\nFor more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or\ncontact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.\n\n---\n\n_For more details please check out [Rules Engine Wiki](https://github.com/microsoft/RulesEngine/wiki)._\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2Frulesengine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicrosoft%2Frulesengine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2Frulesengine/lists"}