admin管理员组文章数量:1415684
I'm working on a promotion system in ASP.NET Core using the RulesEngine library (RulesEngine.Models). My goal is to apply multiple promotion rules, but I have an issue where if the first rule fails, the remaining rules are not evaluated.
What I Want to Achieve: Fetch active promotion rules from the database. Evaluate each rule independently (so that even if one fails, others still run). If a rule condition is not met, skip it but continue evaluating the next rules. Ensure discounts apply only once per item (avoid duplicate calculations).
Problem Description:
Currently, I retrieve rules from the database and execute them one by one. However, if the first rule's condition is false, the next rules do not execute. Example of Rules:
"Receipt.Rows.Sum(r => r.Qta) > 10"
"Receipt.Rows.Any(r => r.Cd_AR == \"1\")"
If the first condition (Receipt.Rows.Sum(r => r.Qta) > 10) is false, → The second condition (Receipt.Rows.Any(r => r.Cd_AR == "1")) is never checked. I need all rules to be evaluated, even if some fail.
My Current Code: ApplyPromozioni (Controller)
`[HttpPost("apply")]
public async Task<IActionResult> ApplyPromozioni(Receipt receipt)
{
RuleEvaluator.SetReceipt(receipt, clusterARRepository);
var regole = await GetRegoleAttive();
if (!regole.Any()) return Ok(receipt);
var orderedRules = regole.OrderBy(r => r.Priorita).ToList();
Console.WriteLine($"[DEBUG] Total Rules to Evaluate: {orderedRules.Count}");
var reSettings = new ReSettings
{
CustomActions = new Dictionary<string, Func<ActionBase>>(),
IgnoreException = true,
};
foreach (var rule in orderedRules)
{
if (!reSettings.CustomActions.ContainsKey(rule.Azione))
{
reSettings.CustomActions[rule.Azione] = () => CreateActionInstance(rule.Azione, receipt);
}
}
var engine = new RulesEngine.RulesEngine(reSettings);
foreach (var rule in orderedRules)
{
try
{
Console.WriteLine($"[DEBUG] Evaluating Rule: {rule.Condizione}");
bool conditionMet = RuleEvaluator.EvaluateGeneralCondition(rule.Condizione);
Console.WriteLine($"[DEBUG] Rule Result: {rule.Condizione} => {conditionMet}");
if (!conditionMet)
{
Console.WriteLine($"⚠️ [DEBUG] Rule {rule.Condizione} FAILED, skipping...");
continue;
}
var workflow = new Workflow
{
WorkflowName = $"Workflow_{rule.Nome}",
Rules = new List<Rule>
{
new Rule
{
RuleName = rule.Nome,
Expression = "true", //
本文标签:
cASPNET Core Rules Engine If First Rule FailsOther Rules Are Not ExecutedStack Overflow
版权声明:本文标题:c# - ASP.NET Core Rules Engine: If First Rule Fails, Other Rules Are Not Executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745213621a2648033.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论