Closed ghigad closed 1 year ago
I had the same issue. Although it is not an ideal solution, I had success with doing some data massaging before executing the expression. Specifically, replacing all "null" values in the object with a string "null", then in my expression I had some logic like this:
Int(record) > (recordThreshold== "null" ? (null) : Int(recordThreshold))
This allowed me to do comparisons with data that has a possibility of having a null value.
I think this is more of a Dynamic LINQ issue rather than a rule engine issue.
@ghigad Thanks for bringing this up. Let me share some details regarding RulesEngine which might help and this is based on the type of input you pass: 1) Pass a dynamic Object(aka ExpandoObject) - Here RulesEngine tries to convert the input to an approx typed object and may fail at compile if certain values are changing types (or null) 2) Pass a strong typed object - This will handle all scenarios perfectly as RulesEngine does not need to guess 3) Pass a JObject from Newtonsoft.Json - In this case, RulesEngine will support dynamic references to even not present properties, you would need to handle null checks at runtime. But it will require typecasting if you want to use any C# methods like sum.
There is another option to use CustomTypes and add a Utility class to handle such scenarios and keep the rules clean.
Let me know if this helps.
Hi @Bugattisport1 ,
Thanks for your feedback.
Unfortunately, I'm not sure this will help since the expression seems to fail as a whole, no matter if it is reached by the ternary operator...
Unless I didn't quite understand you suggestion.
Maybe an example could help me understand?
Best regard!
Hi @abbasc52,
Thank you for your feedback.
I also tried to create a utility class and pass it to the CustomTypes, but with very little success...
public static TResult Test<TValue, TResult>(TValue value, Expression<Func<TValue, TResult> expresssion)
Utils.Test(Activity, x => x.Quote.Items)
value.Quote != null
null
or an empty collectionUtils.Test
method was not foundAny other ideas?
Best regards!
Hi @abbasc52,
A quick follow-up...
I went through my tests again and found out that my test program was using an ExpandoObject.
I updated my test to use a strongly typed object and my basic rules worked!
I'll rerun through my monster rules and make sure they use strongly typed objects and see if I still face the issue.
I'll keep you posted of my results.
Thank you again for your help!
Best regards!
Hi @abbasc52,
Sorry for the delay...
Using strongly typed objects solved my issue!
Thank you for your help!
Best regards!
Hi,
I use RulesEngine, version 4.0.0 with .NET Core 3.1.
I have a bunch of rules that I simplified using GlobalParams and LocalParams.
The problem I have is that my params refer to array properties of a child object, but, in some situations, this child object may be null. When this occurs, the whole workflow breaks down instead of returning valid values. I would like to know what would be the best practice in that situation.
Here is an example of the input data, the rules I use and the output I get... Please note that I have simplified everything to the smallest meaningful sample... Our real-life objects are much more complex and I have lots of rules, so reusing logic with GlobalParams and LocalParams is a life saver!
Input object:
Here is the workflow I use:
For completeness sake, here is how the workflow is run. The
rules
variable holds my workflow and theactivity
variable holds my input data...When I run this workflow with the sample object provided, The PiecesCount is 3 and the OutputExpression is 270d, as expected.
In some cases, my "activities" have no Quote, like so:
In that case, I have this error message when the Rule is run:
I would expect the rule to return 0d, since
Activity.Quote == null
(as a reminder, the GlobalParam's expression isActivity.Quote == null ? 0d : Activity.Quote.Items.Sum(Quantity)
), but it seems the RulesEngine has interpreted the whole expression and failed because the Quote is null.If my workflow has many rules and some rules never refer to my GlobalParam, all rules fail, even those who don't refer the param...
For instance, all rules of this workflow fail with the same error message:
Here is the complete response:
Any help / workaround will be much appreciated!
Thanks in advance!
Best regards!