Skip to content
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

Unexpected True Result from Expression Evaluation with Default ReSettings #583

Open
RenanCarlosPereira opened this issue Feb 15, 2024 · 0 comments

Comments

@RenanCarlosPereira
Copy link

I've encountered an unexpected behavior when evaluating an expression using the RulesEngine RuleExpressionParser with default ReSettings. Specifically, an expression that should logically return false is instead returning true. This issue arises under certain conditions when the UseFastExpressionCompiler option is enabled (which is the default setting).

Steps to Reproduce

  1. Use the following code snippet to set up a basic evaluation scenario with RulesEngine:
using RulesEngine.ExpressionBuilders;
using RulesEngine.Models;

// Setup a parameter with null values
var parameter = RuleParameter.Create("Item", new { Value1 = (decimal?)null, Value2 = (decimal?)null });

// Initialize the parser with default settings
var parser = new RuleExpressionParser(new ReSettings());

// Define an expression that should evaluate to false
const string expression = @"Item.Value1 / Item.Value2 < 0.2";

// Evaluate the expression
var result = parser.Evaluate<bool>(expression, new[] { parameter });

// Output the result
Console.WriteLine(result);
Console.ReadLine();
  1. Run the above code. The output is True, which is unexpected based on the expression logic.

Expected Behavior

Given the expression Item.Value1 / Item.Value2 < 0.2 and both Item.Value1 and Item.Value2 being null, the expected result should logically be False since the comparison cannot be meaningfully made.

Observed Behavior

When executing the provided code snippet, the evaluation result is True, which contradicts the expected logical outcome.

Workaround

The issue can be mitigated by disabling the UseFastExpressionCompiler option in the ReSettings:

var parser = new RuleExpressionParser(new ReSettings { UseFastExpressionCompiler = false });

Disabling UseFastExpressionCompiler yields the correct evaluation result.

Suggested Next Steps

It seems there might be a bug in how expressions are compiled or evaluated when UseFastExpressionCompiler is enabled. It would be beneficial for the maintainers to investigate this behavior further to understand the underlying cause and implement a fix.

I believe this issue could affect others who rely on the default behavior of the RulesEngine, and making the community aware of this potential pitfall (and the workaround) could prevent confusion and errors in expression evaluations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant