microsoft / microsoft/RulesEngine

Unexpected True Result from Expression Evaluation with Default ReSettings

Open
#583 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
4.4k
Forks
616
Avg merge
6m
Merged PRs (30d)
2

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at RuleExpressionParser and trace evaluation with ReSettings, comparing the default UseFastExpressionCompiler path with the disabled setting shown in the reproduction. Re-run the provided null-value expression and verify that the default configuration returns False rather than True, with coverage for the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.