C# 这是在规则中定义规则的正确方法吗? 公共类AllowAlleastoneContyrule:规则 { 公共覆盖无效定义() { Profile=null; string str=@“至少有一个国家必须定义为‘允许’”; bool enabled=AllRules.GetDict()[str];//检查规则是否已启用 当() .Match(()=>productProfile) .存在(p=>已启用,p=>规则冲突(p)); 然后() .Do(=>profile.DisplayError(str)); } 布尔规则违规(基金配置文件pp) { 尝试 { if(pp.DefaultMode.Equals(Helper.DefaultModes.Allow.ToString())) { if(pp.ListOfCountries.Count0)//好的 返回false; else//违反规则 返回true; } } 捕获(例外e) { 抛出新的InvalidRuleException(e.Message); } } }
正如您所看到的,我正在调用另一个具有规则的方法来评估一些条件。我觉得我在这里没有使用Rete算法的全部功能,因为我是在为自己预先评估事情。C# 这是在规则中定义规则的正确方法吗? 公共类AllowAlleastoneContyrule:规则 { 公共覆盖无效定义() { Profile=null; string str=@“至少有一个国家必须定义为‘允许’”; bool enabled=AllRules.GetDict()[str];//检查规则是否已启用 当() .Match(()=>productProfile) .存在(p=>已启用,p=>规则冲突(p)); 然后() .Do(=>profile.DisplayError(str)); } 布尔规则违规(基金配置文件pp) { 尝试 { if(pp.DefaultMode.Equals(Helper.DefaultModes.Allow.ToString())) { if(pp.ListOfCountries.Count0)//好的 返回false; else//违反规则 返回true; } } 捕获(例外e) { 抛出新的InvalidRuleException(e.Message); } } },c#,asp.net,rule-engine,rete,nrules,C#,Asp.net,Rule Engine,Rete,Nrules,正如您所看到的,我正在调用另一个具有规则的方法来评估一些条件。我觉得我在这里没有使用Rete算法的全部功能,因为我是在为自己预先评估事情。 有谁能指导我如何处理这个问题吗?你的代码看起来不错,你有一个复杂的规则,并且你封装了它 根据文档和示例,您可以实现一个优雅的解决方案 使用.Query而不是.Exists实现,将封装的逻辑转换为linq或lambda表达式。 然后应用,使代码更具可读性 public class AllowAtleastOneCountryRule : Rule {
有谁能指导我如何处理这个问题吗?你的代码看起来不错,你有一个复杂的规则,并且你封装了它 根据文档和示例,您可以实现一个优雅的解决方案 使用
.Query
而不是.Exists
实现,将封装的逻辑转换为linq或lambda表达式。
然后应用,使代码更具可读性
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);
}
}
}