Based on the business rule implementation of RulesEngine, rulesengine

Source: Internet
Author: User

Based on the business rule implementation of RulesEngine, rulesengine

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. Common Business Rule engines include Drools, VisualRules, and iLog. Here is another C # Open-Source Tool RulesEngine. Here is an example of how to use it.

1. Project Structure

Add a form application of RulesEngineDemo to the RulesEngine source code and reference the required class library, as shown in:

2. entity class definitions such as orders

Here we use the order scenario to use the rule engine to process the discount business logic:

Public class Order {public double amount; public double num; public double discount; public double afteramount; public DateTime datetime; public List <items> ItemLists; public string maker; // purchaser public string wfprocess; // approver public string wfprocessname; // approver public string memo; // remarks public override string ToString () {string res = string. format ("discount = {0}, amount = {1}, approver = {2} ({4}), discount amount = {3}", discount, amount, wfprocess, afteramount, wfprocessname); return res ;}// user public class Emp {public string id; public string leader; public string name ;} // details public class items {public string productid; public string productname; public double price; public double num; public double discount; public double amount; public double afteramount ;}

 3. Form Definition

Next, we create an Order Details Form to modify the amount and other data to test whether the business rules run properly :,

1 public partial class FrmOrder: Form 2 {3 public FrmOrder () 4 {5 InitializeComponent (); 6 dgvOrder = this. dataGridView1; 7} 8 9 public DataGridView dgvOrder; 10 private void FrmOrder_Load (object sender, EventArgs e) 11 {12 this. dataGridView1.Rows. add (new object [] {"1", "p1", "20", "10", "200", "1", "200 ", "01.01"}); 13 this. dataGridView1.Rows. add (new object [] {"2", "p2", "1", "8", "8", "1", "8 ", "01.01"}); 14 15} 16 17 private void save SToolStripButton_Click (object sender, EventArgs e) 18 {19 this. hide (); 20} 21}

Then we define a main interface to define the rules and call processing logic:

1 using RulesEngine; 2 using RulesEngine. booEvaluator; 3 public partial class Form1: Form 4 {5 6 DataGridView dgvOrder = null; 7 public Form1 () 8 {9 InitializeComponent (); 10} 11 12 private void Form1_Load (object sender, EventArgs e) 13 {14 this. dataGridView1.Rows. add (new object [] {"return (Order. amount> 200) ", @" 15 orders. discount = 0.9; 16 orders. approver = "" admin ""; 17 orders. discount Amount = order. amount * Order. discount; 18 orders. remarks = "" Rule Engine Generation ""; 19 retu Rn true; "}); 20 this. dataGridView1.Rows. add (new object [] {"return (Order. amount <= 200 and order. amount> 100) ", @" 21 Order. discount = 0.8; 22 orders. discount Amount = order. amount * Order. discount; 23 orders. remarks = "" Rule Engine Generation ""; 24 return true; "}); 25 26 this. dataGridView1.Rows. add (new object [] {"return (Order. amount <= 100 and order. amount> 0) ", @" 27 orders. discount = 1.0; 28 orders. discount Amount = order. amount * Order. discount; 29 orders. remarks = "" Rule Engine Generation ""; 30 return true; "}); 31 32} 33 // run 34 private void save SToolStripButton_Click (Object sender, EventArgs e) 35 {36 37 38 DslRuleTests test = new DslRuleTests (); 39 test. setup (); 40 41 // test. evaluateRules (); 42 43 var order = test. evaluateRules (this. dataGridView1, dgvOrder); 44 this.txt Results. text = order. toString (); 45 46 47 48} 49 50 private void toolstripbutton#click (object sender, EventArgs e) 51 {52 FrmOrder frm = new FrmOrder (); 53 54 frm. showDialog (); 55 56 dgvOr Der = frm. dgvOrder; 57} 58} 59 60 public class DslRuleTests 61 {62 private BooLangEvaluator evaluator; 63 64 private List <Emp> listEmp = new List <Emp> (); 65 66 public void Setup () 67 {68 var dslEngineStorage = new DslEngineStorage (); 69 70 evaluator = new BooLangEvaluator (dslEngineStorage); 71 72 listEmp. add (new Emp {id = "01", leader = "00", name = "jack"}); 73 listEmp. add (new Emp {id = "01.01 ", Leader =" 01 ", name =" smith "}); 74 listEmp. add (new Emp {id = "01.01.01", leader = "01.01", name = "john "}); 75 76 77} 78 // approval processing 79 private void SP (Order o) 80 {81 82 foreach (Emp emp in listEmp) 83 {84 if (emp. id = o. maker) 85 {86 o. wfprocess = emp. leader; 87 o. wfprocessname = getName (o. wfprocess); 88} 89} 90 91 92} 93 // get employee name 94 private string getName (string id) 95 {96 foreach (E Mp emp in listEmp) 97 {98 if (emp. id = id) 99 {100 101 return emp. name; 102} 103} 104 return ""; 105} 106 // sum 107 private void SumAmount (Order o) 108 {109 var sum = o. itemLists. asQueryable (). sum (x => x. amount); 110 o. amount = sum; 111} 112 // parsing rule script 113 public Order EvaluateRules (DataGridView dgv, DataGridView dgvOrder) 114 {115 Order order = new Order {memo = "", maker = "01.01", discount = 1, datetime = DateTime. now}; 116 order. itemLists = new List <items> (); 117 foreach (maid dr in dgvOrder. rows) 118 {119 if (dr. cells [0]. value! = Null) 120 {121 var item = new items122 {123 productid = dr. cells [0]. value. toString (), 124 productname = dr. cells [1]. value. toString (), 125 price = double. parse (dr. cells [2]. value. toString (), 126 num = double. parse (dr. cells [3]. value. toString (), 127 amount = double. parse (dr. cells [4]. value. toString (), 128 discount = double. parse (dr. cells [5]. value. toString (), 129 afteramount = double. parse (dr. cells [6]. value. toString () 130 131}; 132 133 order. itemLists. add (item); 134} 135} 136 137 SumAmount (order); 138 SP (order); 139 string strDslStatement = ""; 140 string strDslActivity = ""; 141 EvaluatorAccessPoint. dslConditionEvaluator = evaluator; 142 143 foreach (maid dr in dgv. rows) 144 {145 146 strDslStatement = dr. cells [0]. value. toString () 147. replace ("order", "this") 148. replace ("amount", "amount") 149. replace ("and", "and") 150. replace ("or", "or"); 151 strDslActivity = dr. cells [1]. value. toString () 152. replace ("discount", "discount") 153. replace ("discount amount", "afteramount") 154. replace ("amount", "amount") 155. replace ("order", "this") 156. replace ("Remarks", "memo") 157. replace ("approver", "wfprocessname") 158. replace ("and", "and") 159. replace ("or", "or"); 160 161 var conditionRule = new DslCondition {DslStatement = strDslStatement}; 162 163 var rule = new ActivityRule (conditionRule, new DslActivity164 {165 DslStatement = strDslActivity166}); 167 168 var result = rule. evaluate (order); 169 if (result) 170 {171 // break; 172 return order; 173 174} 175 176 177} 178 return null; 179 180 181} 182}View Code

4. Result Display

Run the code on the following page:

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.