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);
}
}
}
As you can see I am calling another method with the rule to evaluate few conditions. I feel like I am not using the full functionality of Rete algorithm here as I am pre-evaluating things for my self. Can anyone guide me on how to approach this?
Your code looks good, you have a complex rule and you encapsulate it.
Following the documentation and samples, you could implement an elegant solution.
Implementing complex logic with
.Query
instead of.Exists
, translating your encapsulated logic to a linq or lambda expresion. Then applying DSL Extension, making your code more readable.