Flexible discount policies based on the NXBRE rule engine, flexible discounts for nxbre rules

Source: Internet
Author: User

Flexible discount policies based on the NXBRE rule engine, flexible discounts for nxbre rules

Developed by the inference engine, the rule engine is a component embedded in an application that isolates business decisions from application code, use predefined semantic modules to write business decisions. Accept data input, interpret business rules, and make business decisions based on business rules. Application background: enterprise-level managers have the following requirements for enterprise IT system development:

1. To improve efficiency, management processes must be automated, even if modern business rules are exceptionally complex.

2. The market requires frequent changes in business rules. It systems must be updated quickly and at a low cost based on changes in business rules.

3. for fast and low-cost updates, business personnel should be able to directly manage rules in IT systems without the involvement of program developers.

Next we will introduce an open-source engine (NXBRE Rule-engine) for dynamic discount price calculation:

The discount logic configuration uses XML (extension. xbre) as the file, and later modifies the XML discount policy, so that the program code does not need to be modified.Flexible Discount Policies.

Discount rule file:Discount. xbre

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <xBusinessRules xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: noNamespaceSchemaLocation = "xBusinessRules. xsd"> 3 <! -- Global variable --> 4 <Integer id = "10i" value = "10"/> 5 <Integer id = "40i" value = "40"/> 6 <ObjectLookup id = "QuantityOrdered" objectId = "CurrentOrder" member = "Quantity"/> 7 <Logic> 8 <If> 9 <And> 10 <GreaterThanEqualTo leftId = "ClientRating" rightId = "comment"> 11 <! -- CurrentOrder is the order object --> 12 <ObjectLookup id = "ClientRating" objectId = "CurrentOrder" member = "ClientRating"/> 13 <String id = "ClientRatingThreshold" value = "C" /> 14 </GreaterThanEqualTo> 15 </And> 16 <Do> 17 <! -- Discount policy Discount rules for high rate MERs --> 18 <Logic> 19 <If> 20 <And> 21 <GreaterThan leftId = "QuantityOrdered" rightId =" 40i "/> 22 </And> 23 <Do> 24 <! -- AppliedDiscount is the discount for the Application --> 25 <Evaluate id = "AppliedDiscount"> 26 <! -- Percent is the discount ratio --> 27 <Parameter name = "Percent" value = ". 7 "/> 28 </Evaluate> 29 </Do> 30 </If> 31 <ElseIf> 32 <And> 33 <GreaterThan leftId =" QuantityOrdered "rightId =" 10i" /> 34 </And> 35 <Do> 36 <Evaluate id = "AppliedDiscount"> 37 <Parameter name = "Percent" value = ". 8 "/> 38 </Evaluate> 39 </Do> 40 </ElseIf> 41 <Else> 42 <Evaluate id =" AppliedDiscount "> 43 <Parameter name =" Percent" value = ". 9 "/> 44 </Evaluate> 45 </Else> 46 </Logic> 47 </Do> 48 </If> 49 <Else> 50 <! -- Discount policy Discount rules for low rate MERs --> 51 <Logic> 52 <If> 53 <And> 54 <GreaterThan leftId = "QuantityOrdered" rightId = "40i"/> 55 </And> 56 <Do> 57 <Evaluate id = "AppliedDiscount"> 58 <Parameter name = "Percent" value = ". 9 "/> 59 </Evaluate> 60 </Do> 61 </If> 62 <Else> 63 <Evaluate id =" AppliedDiscount "> 64 <Parameter name =" Percent" value = "1"/> 65 </Evaluate> 66 </Else> 67 </Logic> 68 </Else> 69 </Logic> 70 </xBusinessRules>

All business logic isDiscount. xbreThe following defines a form to parse the discount logic and display the calculation result:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. threading. tasks; 9 using System. windows. forms; 10 using System. collections; 11 using NxBRE. flowEngine; 12 using NxBRE. flowEngine. IO; 13 using BREFactory = NxBRE. flowEngine. factories. BREFactory; 14 using Reflection = NxBRE. util. reflection; 15 namespace WinApp 16 {17 public partial class frmDiscountRBE: Form 18 {19 public frmDiscountRBE () 20 {21 InitializeComponent (); 22 23} 24 25 /// <summary> 26 // rule xml file name 27 /// </summary> 28 public const string FLOW_RULES_FILENAME = "discount. xbre "; 29 public const string ORDER =" CurrentOrder "; 30 public const string APPLIED_DISCOUNT =" AppliedDiscount "; 31 public const string PERCENT =" Percent "; 32/**/33 // <summary> 34 // Order 35 // </summary> 36 struct Order 37 {38 public Int32 Quantity; 39 public Double TotalCost; 40 public string ClientRating; 41 public Order (int _ q, double _ t, string _ c) 42 {43 this. quantity = _ q; 44 this. totalCost = _ t; 45 this. clientRating = _ c; 46} 47} 48/**/49 // <summary> 50 // Calculation Result 51 // </summary> 52 // <param name = "aBRC "> rule reference context </param> 53 // <param name =" aMap "> </param> 54 // <param name =" aStep "> </ param> 55 // <returns> result </returns> 56 static object AppliedDiscount (IBRERuleContext aBRC, IDictionary aMap, object aStep) 57 {58 Order _ order = (Order) aBRC. getObject (ORDER); 59 double _ d = Convert. toDouble (Reflection. castValue (aMap [PERCENT], typeof (double); 60 return _ order. totalCost * _ d; 61} 62 private void btnDiscount_Click (object sender, EventArgs e) 63 {64 try 65 {66 // loading rule 67 IRulesDriver rulesDriver = new scheme (FLOW_RULES_FILENAME ); 68 // factory 69 BREFactory breFactory = new BREFactory (); 70 // cited worker instance 71 IFlowEngine bre = breFactory. newBRE (rulesDriver); 72 // delegate instance 73 ExecuteRuleDelegate executeRuleDelegate = new ExecuteRuleDelegate (AppliedDiscount); 74 bre. ruleContext. setFactory (APPLIED_DISCOUNT, new BRERuleFactory (executeRuleDelegate); 75 // set the rule to introduce the environment variable 76 // Order order = new Order (5, 25, ""); 77 Order order = new Order(Int32.Parse(this.txt Quantity. text), Double.Parse(this.txt TotalCost. text), this.txt ClientRating. text); 78 bre. ruleContext. setObject (ORDER, order); 79 // execute 80 bre. process (); 81 // obtain the execution result 82 double result = (double) bre. ruleContext. getResult (APPLIED_DISCOUNT ). result; 83 84 this.txt PayCost. text = result. toString (); 85 this.txt PayCost. readOnly = true; 86 // Console. out. writeLine ("\ nOrder: Calculated discounted total = {0} (expected: {1}) \ n", 87 // result, 25); 88 // Console. readLine (); 89} 90 catch (Exception ex) 91 {92 MessageBox. show (ex. message); 93} 94} 95 96 private void frmDiscountRBE_Load (object sender, EventArgs e) 97 {98 99} 100 101} 102}

Run the program on the following page:

Quantity> 40, with a discount of 0.7 Quantity greater than 10 and less than 40, discount 0.8

Related Article

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.