diff --git a/src/RulesEngine/RulesEngine.cs b/src/RulesEngine/RulesEngine.cs index 627b7b43..e9f80670 100644 --- a/src/RulesEngine/RulesEngine.cs +++ b/src/RulesEngine/RulesEngine.cs @@ -110,6 +110,22 @@ public async ValueTask> ExecuteAllRulesAsync(string workflo return ruleResultList; } + /// + /// This will execute a specific rule of the specified workflow + /// + /// The name of the workflow with rules to execute against the inputs + /// The name of the rule to execute + /// A variable number of rule parameters + /// A rule result + public async ValueTask> ExecuteRuleAsync(string workflowName, string ruleName, params RuleParameter[] ruleParams) + { + var sortedRuleParams = ruleParams.ToList(); + sortedRuleParams.Sort((RuleParameter a, RuleParameter b) => string.Compare(a.Name, b.Name)); + var ruleResultTree = ValidateWorkflowAndExecuteRule(workflowName, ruleName, sortedRuleParams.ToArray()); + await ExecuteActionAsync([ruleResultTree]); + return ruleResultTree; + } + private async ValueTask ExecuteActionAsync(IEnumerable ruleResultList) { foreach (var ruleResult in ruleResultList) @@ -268,6 +284,29 @@ private List ValidateWorkflowAndExecuteRule(string workflowName, return result; } + /// + /// This will validate workflow rules then call execute method + /// + /// type of entity + /// input + /// workflow name + /// list of rule result set + private RuleResultTree ValidateWorkflowAndExecuteRule(string workflowName, string ruleName, RuleParameter[] ruleParams) + { + if (RegisterRule(workflowName, ruleParams)) + { + var compiledRulesCacheKey = GetCompiledRulesKey(workflowName, ruleParameters); + IDictionary> compiledRules = _rulesCache.GetCompiledRules(compiledRulesCacheKey); + if (compiledRules.TryGetValue(ruleName, out var compiledRule)) + { + return compiledRule(ruleParams); + } + } + + // if rules are not registered with Rules Engine + throw new ArgumentException($"Rule config file is not present for the {workflowName} workflow"); + } + /// /// This will compile the rules and store them to dictionary ///