Skip to content

Commit

Permalink
suggested fix for issue #384 (#386)
Browse files Browse the repository at this point in the history
* fix for ruleparameter name changes against cached rules

* fixed indentation to match rest of code

* suggested change implemented
  • Loading branch information
peeveen authored Sep 5, 2022
1 parent 77750ae commit a74f73a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/RulesEngine/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public async ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflo
/// <returns>List of rule results</returns>
public async ValueTask<List<RuleResultTree>> ExecuteAllRulesAsync(string workflowName, params RuleParameter[] ruleParams)
{
var ruleResultList = ValidateWorkflowAndExecuteRule(workflowName, ruleParams);
var sortedRuleParams = ruleParams.ToList();
sortedRuleParams.Sort((RuleParameter a, RuleParameter b) => string.Compare(a.Name, b.Name));
var ruleResultList = ValidateWorkflowAndExecuteRule(workflowName, sortedRuleParams.ToArray());
await ExecuteActionAsync(ruleResultList);
return ruleResultList;
}
Expand Down Expand Up @@ -343,7 +345,8 @@ private List<RuleResultTree> ExecuteAllRuleByWorkflow(string workflowName, RuleP

private string GetCompiledRulesKey(string workflowName, RuleParameter[] ruleParams)
{
var key = $"{workflowName}-" + string.Join("-", ruleParams.Select(c => c.Type.Name));
var ruleParamsHash = string.Join("-", ruleParams.Select(c => $"{c.Name}_{c.Type.Name}")).GetHashCode();
var key = $"{workflowName}-" + ruleParamsHash;
return key;
}

Expand Down
45 changes: 45 additions & 0 deletions test/RulesEngine.UnitTest/ParameterNameChangeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using RulesEngine.Models;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace RulesEngine.UnitTest
{
[ExcludeFromCodeCoverage]
public class ParameterNameChangeTest
{
[Fact]
public async Task RunTwiceTest_ReturnsExpectedResults()
{
var workflow = new Workflow {
WorkflowName = "ParameterNameChangeWorkflow",
Rules = new Rule[] {
new Rule {
RuleName = "ParameterNameChangeRule",
RuleExpressionType = RuleExpressionType.LambdaExpression,
Expression = "test.blah == 1"
}
}
};
var engine = new RulesEngine();
engine.AddOrUpdateWorkflow(workflow);

dynamic dynamicBlah = new ExpandoObject();
dynamicBlah.blah = (Int64)1;
var input_pass = new RuleParameter("test", dynamicBlah);
var input_fail = new RuleParameter("SOME_OTHER_NAME", dynamicBlah);
// RuleParameter name matches expression, so should pass.
var pass_results = await engine.ExecuteAllRulesAsync("ParameterNameChangeWorkflow", input_pass);
// RuleParameter name DOES NOT MATCH expression, so should fail.
var fail_results = await engine.ExecuteAllRulesAsync("ParameterNameChangeWorkflow", input_fail);
Assert.True(pass_results.First().IsSuccess);
Assert.False(fail_results.First().IsSuccess);
}
}
}

0 comments on commit a74f73a

Please sign in to comment.