. NET Rule Engine Introduction-rengine

Source: Internet
Author: User

Rule Engine

The rule engine, developed by the inference engine, is a component embedded in an application that separates business decisions from application code and writes business decisions using predefined semantic modules. Accept data entry, interpret business rules, and make business decisions based on business rules.

Personal understanding

The point of change is too concentrated, in order to accommodate the changes, and to write each of the various scenarios with their own code logic for that point, although the main business logic is the same.

Examples of applicable points

Calculate discount, you need to decide the discount point according to the customer's points

Routing control of workflow-specific nodes

Others (I hope you add, the imagination is limited ...) )

Usage

Take the calculation of customer discount points as an example:

We first have to create a new rule file to define the discount calculation for each scenario, as follows:

#region Discount Rule        //This is the rule name, which is used in C # code to this name, here is the rule default//default rule for "discount rules"       , must exist if C # If the child rule name specified in the code is not found, the default rule is applied (there is a switch to set if the child rule is not found error)        return 1;    End Rule    Rule a company         //Sub-rule name, here is "Company A"        if (customerscore>=0&& CUSTOMERSCORE<100)            return 1;        if (customerscore>=100&&customerscore<300)            return 0.8;        return 0.5;    End Rule    Rule B Company         //Sub-rule name, here is "Company B"        if (customerscore>=0&&customerscore< )            return 0.9;        if (customerscore>=100&&customerscore<300)            return 0.7;        return 0.6;    End Rule#endregion

In C # code, there are several scenarios-I want to invoke the default rule:

var result = Rengine.invokeasfloat (" discount rule ", Rengine.createparameter (" Customerscore " ));

I want to invoke the discount rule according to the company name:

varRESULT1 = Rengine.invokeasfloat ("Discount Rules","Company A", Rengine.createparameter ("Customerscore", -));varRESULT2 = Rengine.invokeasfloat ("Discount Rules","Company B", Rengine.createparameter ("Customerscore", -));varCompanyid="Company A";//This means that the company name comes from the variablevarRESULT3 = Rengine.invokeasfloat ("Discount Rules", CompanyID, Rengine.createparameter ("Customerscore", -));

Very convenient, simply too convenient.

What if I find a sub-rule name that doesn't exist based on CompanyID? I want a specific sub-rule to determine the rule, rather than the default rule, what's the best way to do that? Don't worry, see:

 <  appsettings  >  <  add  key  = "Rengine.rulefilespath"   value  = "Rules"  />  <  add  key  = "rengine.throwexceptionifnotfoundrule "   = "1"  />  </ appsettings  >  

If the Rengine.throwexceptionifnotfoundrule parameter is set to 1 in the configuration file, it will be an error if the specific child rule name is not found and the default defaults rule will not be applied

Introduction to the next Rengine API

Performance

Project Address

Rengine Address: Https://github.com/daibinhua888/REngine

. NET Rule Engine Introduction-rengine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.