规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策。接受数据输入,解释业务规则,并根据业务规则做出业务决策。比较常见的业务规则引擎有Drools、VisualRules 和iLog。这里介绍另外一个C#开源工具RulesEngine。下面通过一个例子来他如何使用。
1 项目结构
在RulesEngine源代码中添加一个RulesEngineDemo的窗体应用程序,然后引用需要的类库,如下图所示:
2 订单等实体类定义
这里用订单的场景来用规则引擎处理折扣的业务逻辑:
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;//购买人
public string wfprocess;//审批人
public string wfprocessname;//审批人
public string memo;//备注 public override string ToString()
{
string res = string.Format("折扣={0},金额={1},审批人={2}({4}),折后金额={3}", discount, amount, wfprocess, afteramount,wfprocessname);
return res;
}
}
//用户
public class Emp
{
public string id;
public string leader;
public string name;
}
//明细
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 窗体定义
下面我们创建一个订单明细的窗体,用来修改金额等数据,用来测试业务规则是否正常运行:、
public partial class FrmOrder : Form
{
public FrmOrder()
{
InitializeComponent();
dgvOrder = this.dataGridView1;
} public DataGridView dgvOrder;
private void FrmOrder_Load(object sender, EventArgs e)
{
this.dataGridView1.Rows.Add(new object[] { "", "p1", "", "", "", "","","01.01" });
this.dataGridView1.Rows.Add(new object[] { "", "p2", "", "", "", "", "", "01.01" }); } private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
this.Hide();
}
}
然后我们定义一个主界面来定义规则和调用处理逻辑:
using RulesEngine;
using RulesEngine.BooEvaluator;
public partial class Form1 : Form
{ DataGridView dgvOrder = null;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.Rows.Add(new object[] {"return (订单.金额>200)",@"
订单.折扣=0.9;
订单.审批人=""admin"";
订单.折后金额=订单.金额*订单.折扣;
订单.备注 = ""规则引擎产生"";
return true;" });
this.dataGridView1.Rows.Add(new object[] {"return (订单.金额<=200 且 订单.金额>100)",@"
订单.折扣=0.8;
订单.折后金额=订单.金额*订单.折扣;
订单.备注 = ""规则引擎产生"";
return true;" }); this.dataGridView1.Rows.Add(new object[] {"return (订单.金额<=100 且 订单.金额>0)",@"
订单.折扣=1.0;
订单.折后金额=订单.金额*订单.折扣;
订单.备注 = ""规则引擎产生"";
return true;" }); }
//run
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{ DslRuleTests test = new DslRuleTests();
test.Setup(); //test.EvaluateRules(); var order = test.EvaluateRules(this.dataGridView1, dgvOrder);
this.txtResults.Text = order.ToString(); } private void toolStripButton1_Click(object sender, EventArgs e)
{
FrmOrder frm = new FrmOrder(); frm.ShowDialog(); dgvOrder = frm.dgvOrder;
}
} public class DslRuleTests
{
private BooLangEvaluator evaluator; private List<Emp> listEmp = new List<Emp>(); public void Setup()
{
var dslEngineStorage = new DslEngineStorage(); evaluator = new BooLangEvaluator(dslEngineStorage); listEmp.Add(new Emp { id = "", leader ="" ,name="jack"});
listEmp.Add(new Emp { id = "01.01", leader = "", name = "smith" });
listEmp.Add(new Emp { id = "01.01.01", leader = "01.01", name = "john" }); }
//审批处理
private void SP(Order o)
{ foreach (Emp emp in listEmp)
{
if (emp.id == o.maker)
{
o.wfprocess = emp.leader;
o.wfprocessname = getName(o.wfprocess);
}
} }
//获取职工姓名
private string getName(string id)
{
foreach (Emp emp in listEmp)
{
if (emp.id == id)
{ return emp.name;
}
}
return "";
}
//求和
private void SumAmount(Order o)
{
var sum = o.ItemLists.AsQueryable().Sum(x => x.amount);
o.amount = sum;
}
//解析规则脚本
public Order EvaluateRules(DataGridView dgv,DataGridView dgvOrder)
{
Order order =new Order { memo = "", maker = "01.01", discount = , datetime = DateTime.Now };
order.ItemLists = new List<items>();
foreach (DataGridViewRow dr in dgvOrder.Rows)
{
if (dr.Cells[].Value != null)
{
var item = new items
{
productid = dr.Cells[].Value.ToString(),
productname = dr.Cells[].Value.ToString(),
price = double.Parse(dr.Cells[].Value.ToString()),
num = double.Parse(dr.Cells[].Value.ToString()),
amount = double.Parse(dr.Cells[].Value.ToString()),
discount = double.Parse(dr.Cells[].Value.ToString()),
afteramount = double.Parse(dr.Cells[].Value.ToString()) }; order.ItemLists.Add(item);
}
} SumAmount(order);
SP(order);
string strDslStatement="";
string strDslActivity = "";
EvaluatorAccessPoint.DslConditionEvaluator = evaluator; foreach (DataGridViewRow dr in dgv.Rows)
{ strDslStatement = dr.Cells[].Value.ToString()
.Replace("订单", "this")
.Replace("金额", "amount")
.Replace("且", "and")
.Replace("或", "or");
strDslActivity = dr.Cells[].Value.ToString()
.Replace("折扣", "discount")
.Replace("折后金额", "afteramount")
.Replace("金额", "amount")
.Replace("订单", "this")
.Replace("备注", "memo")
.Replace("审批人", "wfprocessname")
.Replace("且", "and")
.Replace("或", "or"); var conditionRule = new DslCondition { DslStatement = strDslStatement }; var rule = new ActivityRule(conditionRule, new DslActivity
{
DslStatement = strDslActivity
}); var result = rule.Evaluate(order);
if (result)
{
//break;
return order; } }
return null; } }
4 结果展示
运行代码,界面如下: