Skip to content

One rule execution #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/RulesEngine/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public async ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflo
return ruleResultList;
}

/// <summary>
/// This will execute a specific rule of the specified workflow
/// </summary>
/// <param name="workflowName">The name of the workflow with rules to execute against the inputs</param>
/// <param name="ruleName">The name of the rule to execute</param>
/// <param name="ruleParams">A variable number of rule parameters</param>
/// <returns>A rule result</returns>
public async ValueTask<RuleResultTree>> 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<RuleResultTree> ruleResultList)
{
foreach (var ruleResult in ruleResultList)
Expand Down Expand Up @@ -268,6 +284,29 @@ private List<RuleResultTree> ValidateWorkflowAndExecuteRule(string workflowName,
return result;
}

/// <summary>
/// This will validate workflow rules then call execute method
/// </summary>
/// <typeparam name="T">type of entity</typeparam>
/// <param name="input">input</param>
/// <param name="workflowName">workflow name</param>
/// <returns>list of rule result set</returns>
private RuleResultTree ValidateWorkflowAndExecuteRule(string workflowName, string ruleName, RuleParameter[] ruleParams)
{
if (RegisterRule(workflowName, ruleParams))
{
var compiledRulesCacheKey = GetCompiledRulesKey(workflowName, ruleParameters);
IDictionary<string, RuleFunc<RuleResultTree>> 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");
}

/// <summary>
/// This will compile the rules and store them to dictionary
/// </summary>
Expand Down