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
实现复杂逻辑,将封装的逻辑转换为 linq 或 lambda 表达式。然后应用 DSL 扩展,使您的代码更具可读性。