规则引擎


关于RuleSet,在以前的文章中有详细说明,这里就不再重提了.

先看一个应用场景
1.  y设备启动要先启动x设备,x设备关闭要先关闭y设备
2.  n设备启动要先启动m设备,m设备关闭要先关闭n设备
3.  m设备,n设备要靠a设备供电,所以要启动m或n设备必需要先启动a设备
4.  要关闭a设备,m设备与n设备都要处于关闭状态
5.  由于n设备耗电量效大,n设备运行的时候,为了保证安全,应将不相干的设备关闭

10_2.JPG
开启n设备的规则
10_3.JPG

不用工作流,使用传统代码方式实现

None.gif      public   class  设备
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//真表示开启,候表示关闭
InBlock.gif
        private bool _状态;
InBlock.gif        
public bool 状态
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gifget dot.gifreturn _状态; } }
InBlock.gif
InBlock.gif        
private string _名称;
InBlock.gif        
public string 名称
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _名称; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _名称 = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public 设备(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ _名称 = name; }
InBlock.gif
InBlock.gif
InBlock.gif        
public void open()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _状态 
= true;
InBlock.gif            System.Console.WriteLine(
"打开设备{0},{1}", _名称, _状态);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _状态 
= false;
InBlock.gif            System.Console.WriteLine(
"关闭设备{0},{1}", _名称, _状态);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif        
private   static   void  操作()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
while (!a.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{ a.open(); }
InBlock.gif
InBlock.gif            
while (x.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (y.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ y.close();}
InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ x.close();}
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (!x.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
while (!m.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ m.open();}
InBlock.gif                
while (!n.状态)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ n.open();}
InBlock.gif                System.Console.WriteLine(
"完成");
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif


使用状态机的方式实现

10_4.JPG


使用Inplicit(隐式反应链)规则实现


None.gif public   class  设备
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//真表示开启,候表示关闭
InBlock.gif
        private bool _状态;
InBlock.gif        
public bool 状态
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _状态; }
InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{ System.Console.WriteLine("打开设备{0},{1}",_名称, _状态);}
InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{System.Console.WriteLine("关闭设备{0},{1}", _名称, _状态);}
InBlock.gif                _状态 
= value; 
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string _名称;
InBlock.gif        
public string 名称
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _名称; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _名称 = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public 设备(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{ _名称 = name;}
ExpandedBlockEnd.gif    }

None.gif

工作流
10_0.JPG

policyActivity1的ruleset

10_1.JPG

 

a: IF this.a.状态 THEN ELSE this.a.状态 = True
xtyt: IF this.x.状态 && this.y.状态 THEN this.y.状态 = False
xtyf: IF this.x.状态 && !this.y.状态 THEN this.x.状态 = False
xfmf: IF !this.x.状态 && !this.m.状态 THEN this.m.状态 = True
mtnf: IF this.m.状态 && !this.n.状态 THEN this.n.状态 = True
mtnt: IF this.m.状态 && this.n.状态 THEN System.Console.Write("完成")




三个例子的代码
http://files.cnblogs.com/foundation/p.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值