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 算法的全部功能,因为我正在为自己进行预评估。 谁能指导我如何处理这个问题?
最佳答案
您的代码看起来不错,您有一个复杂的规则并将其封装。
按照文档和示例,您可以实现一个优雅的解决方案。
实现 complex logic使用 .Query
而不是 .Exists
,将封装的逻辑转换为 linq 或 lambda 表达式。
然后申请DSL Extension ,使您的代码更具可读性。
关于c# - 这是在 NRules 中定义规则的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756983/