规则引擎


关于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设备运行的时候,为了保证安全,应将不相干的设备关闭


开启n设备的规则


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

    public class 设备
    
{
        
//真表示开启,候表示关闭
        private bool _状态;
        
public bool 状态
        
get return _状态; } }

        
private string _名称;
        
public string 名称
        
{
            
get return _名称; }
            
set { _名称 = value; }
        }


        
public 设备(string name)
        
{ _名称 = name; }


        
public void open()
        
{
            _状态 
= true;
            System.Console.WriteLine(
"打开设备{0},{1}", _名称, _状态);
        }


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

    }


        
private static void 操作()
        
{
            
while (!a.状态)
            
{ a.open(); }

            
while (x.状态)
            
{
                
if (y.状态)
                
{ y.close();}
                
else
                
{ x.close();}
            }


            
if (!x.状态)
            
{
                
while (!m.状态)
                
{ m.open();}
                
while (!n.状态)
                
{ n.open();}
                System.Console.WriteLine(
"完成");
            }

        }


使用状态机的方式实现



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


public class 设备
    
{
        
//真表示开启,候表示关闭
        private bool _状态;
        
public bool 状态
        
{
            
get return _状态; }
            
set 
            
{
                
if (value)
                
{ System.Console.WriteLine("打开设备{0},{1}",_名称, _状态);}
                
else
                
{System.Console.WriteLine("关闭设备{0},{1}", _名称, _状态);}
                _状态 
= value; 
            }

        }

        
private string _名称;
        
public string 名称
        
{
            
get return _名称; }
            
set { _名称 = value; }
        }

        
public 设备(string name)
        
{ _名称 = name;}
    }

工作流


policyActivity1的ruleset



 

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("完成")




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

posted @ 2007-08-18 14:33  WXWinter(冬)  阅读(8518)  评论(8编辑  收藏  举报