2016-03-02 56 views
2
public class AllowAtleastOneCountryRule : Rule 
{ 
    public override void Define() 
    { 
     Profile profile = null; 

     string str = @"At least one country has to be defined as 'permitted'"; 


     bool enabled = AllRules.GetDict()[str];//Checks if the rule is enabled 


     When() 
      .Match<FundProfile>(() => productProfile) 
      .Exists<FundProfile>(p => enabled, p => RuleViolation(p)); 


     Then() 
      .Do(_ => profile .DisplayError(str)); 


    } 


    bool RuleViolation(FundProfile pp) 
    { 
     try 
     { 


      if (pp.DefaultMode.Equals(Helper.DefaultModes.Allow.ToString())) 
      { 
       if (pp.ListOfCountries.Count < pp.TotalCountries)//Okay 
        return false; 
       else//Rule violation 
        return true; 
      } 
      else//Deny 
      { 
       if (pp.ListOfCountries.Count > 0)//Okay 
        return false; 
       else//Rule violation 
        return true; 
      } 

     } 
     catch(Exception e) 
     { 
      throw new InvalidRuleException(e.Message); 
     } 

    } 
} 

正如你所看到的,我正在调用另一种方法来评估一些条件。我觉得我没有在这里使用Rete算法的全部功能,因为我正在为自己预先评估一些事情。 任何人都可以指导我如何解决这个问题?这是在NRules中定义规则的正确方法吗?

回答

0

你的代码看起来不错,你有一个复杂的规则,你封装它。

按照文档和示例,您可以实现一个优雅的解决方案。

执行complex logic.Query而不是.Exists,将您的封装逻辑转换为linq或lambda表达式。 然后应用DSL Extension,使您的代码更具可读性。

相关问题