c# – 这是在NRules中定义规则的正确方法吗?

c# – 这是在NRules中定义规则的正确方法吗?,第1张

概述public class AllowAtleastOneCountryRule : Rule{ public override void Define() { Profile profile = null; string str = @"At least one country has to be defined as 'permitted'";
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算法的全部功能,因为我正在为自己预先评估事物.
任何人都可以指导我如何处理这个问题?

解决方法 您的代码看起来很好,您有一个复杂的规则并且您将其封装起来.

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

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

总结

以上是内存溢出为你收集整理的c# – 这是在NRules中定义规则的正确方法吗?全部内容,希望文章能够帮你解决c# – 这是在NRules中定义规则的正确方法吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1226845.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存