I am researching some rule engines and I came across Microsoft RulesEngine: https://microsoft.github.io/RulesEngine/. It looks verry promising, because I get a lot of flexibility in the rules and the inputs. However I encountered some problems when tried to do a Select/Projection to a extending expression custom class/type.
Here is the scenario. Given the input:
"reportId": "r1",
"reportVersionId": 2,
"defects": [
{
"id": "d1",
"isMerging": true,
"mergeDefectId": "d2",
"state": "open",
"defectStartPoint": 2,
"defectEndPoint": 10
},
{
"id": "d2",
"isMerging": true,
"state": "close",
"defectStartPoint": 8,
"defectEndPoint": 15
},
{
"id": "d3",
"isMerging": false,
"state": "open",
"defectStartPoint": 1,
"defectEndPoint": 5
}
]
I want to perform some more complex checks on this list (just an example: if one of the defects have "status":"open" and "isMerging": true, check if the MergingDefectId is a valid id (another defect with that id exists and that defect is valid)). So based on this: https://microsoft.github.io/RulesEngine/#extending-expression-via-custom-classtype-injection I created this class:
public static class ComplexRulesMethods
{
public static bool IsValid(object input)
{
//Perform complex logic
return true;
}
}
And for the rule file I have:
[
{
"WorkflowName": "workflowName",
"Rules": [
{
"RuleName": "ruleName",
"ErrorMessage": "errorMessage",
"ErrorType": "Error",
"RuleExpressionType": "LambdaExpression",
"Expression": "ComplexRulesMethods.IsValid(input1.currentVersion.defects.Select( new {id as id, isMerging as isMerging, mergeDefectId as mergeDefectId} ))"
}
]
}
]
The plan is to make IsValid(object input) generic, so it will be able to validate a list of objects with "id","isMerging" and "mergedDefectId" properties. Doing this if I get another input on which I can do this validation, I will just create another expression and map the new input fields to those 3 ("id","isMerging" and "mergedDefectId").
The problem is that whenever I want to perform the select with multiple properties ( "Expression": "ComplexRulesMethods.IsValid(input1.defects.Select(id))" works fine), I get
this error:
Exception while parsing expression ComplexRulesMethods.IsValid(input1.defects.Select( new {id as id, isMerging as isMerging, mergeDefectId as mergeDefectId} )) - No property or field 'id' exists in type 'Boolean'.
I tried multiple variants using () instead of {}, with and without 'it.', but I always get that error, and I don't know exactly why, and if what I want is actually possible.
I think you need to use
( )instead of{ }.So like thisp:
See also this link on the GitHub project page.