Skip to content

C# simple Rule Engine. High performance object rule matching. Support various complex grouped predicates.

License

Notifications You must be signed in to change notification settings

AmirSasson/NetRuleEngine

Repository files navigation

NetRuleEngine

C# simple Rule Engine. High performance object rule matching. Support various complex grouped predicates. available on nuget.

(Input) An Object + Rule(s) => (Output) Is Match

NuGet Badge Build Status

Use cases

  • Bussiness rules that are dynamicaly generated by a user interface, to dermine Yes/No conditions
  • Audience Matching
  • Dynamic Alerts detection

Common Usage Scenario

+---------------+   SET    +------------+                    +---------+
|  Rules Admin  |  +-----> |    Rule    |  +---------------> |  Rules  |
+---------------+          |   Editor   |      SAVE AS JSON  |   DB    |
  (design time)            |     UI     |                    |         |
                           +------------+                    +--+------+
                                                                ^
                                                                |
                                                                |
                                                                |
                           +-----------------+                  |
                           |    BUSSINESS    |                  |
 +---------------+         |      LOGIC      |                  |
 |               |         |                 |                  |
 |   Business    | +-----> |      with       +-------------------
 |     EVENT     |         |  NetRuleEngine  |
 +---------------+         |                 |
   (real time)             +-----------------+

  • Rules Admin - Actor Role - that sets the rules on design time to identify an event or desired state.
  • Rules DB - Database to store the rules
  • Business Event - an Event that occurs on the bussines flow, and changes a state on your backend, or a standalone event that needs to be tested for rule matching. can be anything as a site Visit, transaction, UI event, Login, Registration or whatever.
  • BUSSINESS LOGIC - this is the backend that referene the NetRuleEngine package, consumes the Rules from DB, and for each Event, run the Rule matchin and acts according to the result
this solution doesnt not provide any rules editor UX or DB.

Simple usage:

    IRulesService<TestModel> engine = RulesService<TestModel>.CreateDefault();
            
    var matching = engine.GetMatchingRules(
        new TestModel { NumericField = 5 },
        new[] {
            new RulesConfig {
                Id = Guid.NewGuid(),
                RulesOperator = Rule.InterRuleOperatorType.And,
                RulesGroups = new RulesGroup[] {
                    new RulesGroup {
                        RulesOperator = Rule.InterRuleOperatorType.And,
                        // every TestModel instance with NumericField Equal to 5 will match this rule
                        Rules = new[] {
                            new Rule { 
                            ComparisonOperator = Rule.ComparisonOperatorType.Equal,
                            ComparisonValue = 5.ToString(),
                            ComparisonPredicate = nameof(TestModel.NumericField) 
                            }
                        }
                    }
                }
            }
        });
  • depenent on LazyCache to store compiled rules for best performance.
  • compiles Expression Trees into dynamic cached code to support high performance usage.
  • dependency injection ready, inject Either IRulesService<> or its dependencies.
  • as the RulesService is statefull and is dependent on cache, it must be configured as singleton on your IOC (or at least, its cache dependency must be singleton)

Rule Editor UI Example (not included in this project):
editor
Rule Config JSON Format Example:

{
    "Id": "eff37f67-9279-4fff-b2ea-9ef6f92a5de7",
    "RulesOperator": "And",
    "RulesGroups": [
        {
            "RulesOperator": "Or",
            "Rules": [
                {
                    "ComparisonPredicate": "TextField",
                    "ComparisonOperator": "StringStartsWith",
                    "ComparisonValue": "NOT MATCHING PREFIX",
                    "PredicateType": null
                },
                {
                    "ComparisonPredicate": "NumericField",
                    "ComparisonOperator": "GreaterThan",
                    "ComparisonValue": "4",
                    "PredicateType": null
                }
            ]
        },
        {
            "RulesOperator": "Or",
            "Rules": [
                {
                    "ComparisonPredicate": "TextField",
                    "ComparisonOperator": "StringStartsWith",
                    "ComparisonValue": "SomePrefix",
                    "PredicateType": null
                },
                {
                    "ComparisonPredicate": "NumericField",
                    "ComparisonOperator": "GreaterThan",
                    "ComparisonValue": "55",
                    "PredicateType": null
                }
            ]
        }
    ]
}

this example represents a single rule consists on 2 groups with relation of AND (which means object must match both groups), on each group, at least 1 rule should match as both have OR operator and both have 2 criterias rules.


Features:

  • composite objects
  • enums
  • string
  • numbers
  • datetime
  • Dictionaries
  • collections

and many more. See units test for full usage scenarios.

About

C# simple Rule Engine. High performance object rule matching. Support various complex grouped predicates.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published