规则引挚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!

具体我就不解释程序了,应该比较清晰了

转载于:https://www.cnblogs.com/linbc/archive/2008/06/19/1226058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值