microsoft / microsoft/RulesEngine
Unable to bind values in rule chaining.
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.4k
- Forks
- 616
- Avg merge
- 6m
- Merged PRs (30d)
- 2
Description
My use case requires me to run three evaluations for each rule : RuleExpression, ValidationExpression, ActionExpression. I want to minimize C# code changes because these rules are dynamic and require frequent changes. I want to avoid C# code for executing the action because that would limit me to go through code deployment every time a rule is updated. And the order in which this is supposed to work is : If RuleExpression succeeds, we move to evaluate ValidationExpression and if ValidationExpression succeeds, we move to evaluate ActionExpression. With this context, here is a simplified example of what I did. Would appreciate some comments if this is not the right way
rules.json
[ { "WorkflowName": "Tuning", "Rules": [ { "RuleName": "EvaluationExpression", "SuccessEvent": "EvaluationExpressionPassed", "ErrorMessage": "EvaluationExpression not met.", "Expression": "metrics.current_value > 1000", "RuleExpressionType": "LambdaExpression" }, { "RuleName": "VerificationExpression", "SuccessEvent": "VerificationExpressionPassed", "ErrorMessage": "VerificationExpression failed.", "Expression": "@EvaluationExpression && metrics.cost_limit >= -1", "RuleExpressionType": "LambdaExpression" }, { "RuleName": "ActionExpression", "SuccessEvent": "ActionExpressionApplied", "ErrorMessage": "ActionExpression skipped.", "Expression": "VerificationExpressionPassed", "RuleExpressionType": "LambdaExpression", "Actions": { "OnSuccess": { "Name": "InlineAssignment", "Context": { "expression": "@VerificationExpression && metrics.cost_limit = metrics.cost_limit != -1 ? metrics.cost_limit + 100 : metrics.other_cost_limit + 100" } } }, "Enabled": true } ] } ]
The EvaluationExpression is getting evaluated as expected. However, this result is not getting passed to VerificationExpression. As a result, even though the metrics.cost_limit condition is met in VerificationExpression , I see VerificationExpression is getting evaluated to False. I tried removing @EvaluationExpression from the expression and then the VerificationExpression was evaluated as expected (True). I read other examples online and tried passing the SuccessEvent (EvaluationExpressionPassed) from the EvaluationExpression rule as well, but that did not work either.
My C# app code is below :
var workflows = JsonConvert.DeserializeObject<List>(ruleJson);
var metrics = MetricsProvider.GetSampleMetrics();
var inputs = new RuleParameter("metrics", metrics);
var engine = new RulesEngine.RulesEngine(workflows.ToArray(), new ReSettings { EnableScopedParams = true });
var results = await engine.ExecuteAllRulesAsync("Tuning", inputs);
foreach (var result in results)
{
Console.WriteLine($"- {result.Rule.RuleName}: {(result.IsSuccess ? "Passed" : "Failed")}");
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.