规则引挚NxBRE文档纪要--流引挚简介(二)

上一篇都写总体上的认识,现在开始具体点,先把规则文件贴出来
<?xml version="1.0" encoding="UTF-8"?>
<xBusinessRules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xBusinessRules.xsd">
    
<!-- global values -->
    
<Integer id="10i" value="10"/>
    
<Integer id="40i" value="40"/>  
    
<ObjectLookup id="QuantityOrdered" objectId="CurrentOrder" member="Quantity"/>
    
<Logic>
        
<If>
            
<And>
                
<GreaterThanEqualTo leftId="ClientRating" rightId="ClientRatingThreshold">
                    
<ObjectLookup id="ClientRating" objectId="CurrentOrder" member="ClientRating"/>
                    
<String id="ClientRatingThreshold" value="C"/>
                
</GreaterThanEqualTo>
            
</And>
            
<Do>
                
<!-- Discount rules for high rate customers -->
                
<Logic>
                    
<If>
                        
<And>
                            
<GreaterThan leftId="QuantityOrdered" rightId="40i"/>
                        
</And>
                        
<Do>
                            
<Evaluate id="AppliedDiscount">
                                
<Parameter name="Percent" value=".7"/>
                            
</Evaluate>
                        
</Do>
                    
</If>
                    
<ElseIf>
                        
<And>
                            
<GreaterThan leftId="QuantityOrdered" rightId="10i"/>
                        
</And>
                        
<Do>
                            
<Evaluate id="AppliedDiscount">
                                
<Parameter name="Percent" value=".8"/>
                            
</Evaluate>
                        
</Do>
                    
</ElseIf>
                    
<Else>
                        
<Evaluate id="AppliedDiscount">
                            
<Parameter name="Percent" value=".9"/>
                        
</Evaluate>
                    
</Else>
                
</Logic>
            
</Do>
        
</If>
        
<Else>
            
<!-- Discount rules for low rate customers -->
            
<Logic>
                
<If>
                    
<And>
                        
<GreaterThan leftId="QuantityOrdered" rightId="40i"/>
                    
</And>
                    
<Do>
                        
<Evaluate id="AppliedDiscount">
                            
<Parameter name="Percent" value=".9"/>
                        
</Evaluate>
                    
</Do>
                
</If>
                
<Else>
                    
<Evaluate id="AppliedDiscount">
                        
<Parameter name="Percent" value="1"/>
                    
</Evaluate>
                
</Else>
            
</Logic>
        
</Else>
    
</Logic>
</xBusinessRules>

以下是关于上面的规则文件写的伪码
Percent就是返回的拆扣
int 10i = 10;
int 40i = 40;
Order order;
QuantityOrdered 
= CurrentOrder.QuantityOrdered;//数量
string ClientRatingThreshold = "C";//等级
double Percent;/*折扣*/
if(order.ClientRating/*客户等级*/ >= ClientRatingThreshold)// Discount rules for high rate customers 
{
    
if(QuantityOrdered > 40i)
    
{
        Percent 
= 0.7;
    }

    
else if(QuantityOrdered > 10i)
    
{
        Percent 
= 0.8;
    }

    
else
    
{
        Percent 
= 0.9;
    }

}

else// Discount rules for low rate customers 
{
    
if(QuantityOrdered>40i)
    
{
        Percent 
= 0.9;
    }

    
else
    
{
        Percent 
= 1;
    }

}

好了,费话不说,直接贴程序的主要代码吧
using System;
using System.Collections;
using System.Text;

using NxBRE.FlowEngine;
using NxBRE.FlowEngine.IO;

using BREFactory = NxBRE.FlowEngine.Factories.BREFactory;
using Reflection = NxBRE.Util.Reflection;

namespace MyDiscount
{   
    
class Program
    
{
        
/// <summary>
        
/// 规则xml文件名称
        
/// </summary>

        public const string FLOW_RULES_FILENAME = "discount.xbre";
        
public const string ORDER = "CurrentOrder";
        
public const string APPLIED_DISCOUNT = "AppliedDiscount";
        
public const string PERCENT = "Percent";

        
/// <summary>
        
/// 定单
        
/// </summary>

        struct Order
        
{
            
public Int32 Quantity;
            
public Double TotalCost;
            
public string ClientRating;

            
public Order(int _q, double _t, string _c){
                
this.Quantity = _q;
                
this.TotalCost = _t;
                
this.ClientRating = _c;
            }

        }


        
/// <summary>
        
/// 计算结果
        
/// </summary>
        
/// <param name="aBRC">规则引挚上下文</param>
        
/// <param name="aMap"></param>
        
/// <param name="aStep"></param>
        
/// <returns>结果</returns>

        static object AppliedDiscount(IBRERuleContext aBRC, IDictionary aMap, object aStep)
        
{
            Order _order 
= (Order)aBRC.GetObject(ORDER);
            
double _d = Convert.ToDouble(Reflection.CastValue(aMap[PERCENT], typeof(double)));
            
return _order.TotalCost * _d;
        }


        
static void Main(string[] args)
        
{
            
//载入规则
            IRulesDriver rulesDriver = new XBusinessRulesFileDriver(FLOW_RULES_FILENAME);
            
//工厂
            BREFactory breFactory = new BREFactory();
            
//引挚实例
            IFlowEngine bre = breFactory.NewBRE(rulesDriver);

            
//委托实例
            ExecuteRuleDelegate executeRuleDelegate = new ExecuteRuleDelegate(AppliedDiscount);

            bre.RuleContext.SetFactory(APPLIED_DISCOUNT, 
new BRERuleFactory(executeRuleDelegate));

            
//设置规则引挚环境变量
            Order order = new Order(525"A");
            bre.RuleContext.SetObject(ORDER, order);
            
//执行
            bre.Process();
            
//得到执行结果
            double result = (double)bre.RuleContext.GetResult(APPLIED_DISCOUNT).Result;

            Console.Out.WriteLine(
"\nOrder: Calculated discounted total={0} (expected: {1})\n",
                                      result,
25);

            Console.ReadLine();
        }

    }

}


运行,输出
Order: Calculated discounted total=25 (expected: 25)
根据我们上面的规则走完得到了的拆扣的值

这个例子是我从原来的exapmles里面精简出来的,原先的例子把我真正要看的东西给搞得复杂化了了,所以写这个简单点,
这才是真正的HelloWorld!

具体我就不解释程序了,应该比较清晰了
posted @ 2008-06-19 17:13  冷侃  阅读(1930)  评论(2编辑  收藏  举报