【问题标题】:Is this a proper way of defining a rule in NRules?这是在 NRules 中定义规则的正确方法吗?
【发布时间】:2016-03-02 20:01:56
【问题描述】:
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 算法的全部功能,因为我正在为自己预先评估事物。 谁能指导我如何解决这个问题?

【问题讨论】:

    标签: c# asp.net rule-engine rete nrules


    【解决方案1】:

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

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

    .Query 代替.Exists 实现complex logic,将封装的逻辑转换为linq 或lambda 表达式。 然后应用DSL Extension,让你的代码更具可读性。

    【讨论】:

      猜你喜欢
      • 2020-08-31
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多