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算法的全部功能,因为我是在为自己预先评估事情。有没有人可以指导我如何处理这个问题?
发布于 2017-08-11 00:11:06
你的代码看起来不错,你有一个复杂的规则,并且你封装了它。
遵循文档和示例,您可以实现一个优雅的解决方案。
使用.Query而不是.Exists实现complex logic,将封装的逻辑转换为linq或λ表达式。然后应用DSL Extension,使您的代码更具可读性。
https://stackoverflow.com/questions/35756983
复制相似问题