【问题标题】:easy-rules: is there an if-else rule?easy-rules:是否有 if-else 规则?
【发布时间】:2019-05-30 13:47:08
【问题描述】:

我正在尝试使用简单的规则。

我们可以使用简单规则执行if-else 规则吗?

我检查了ActivationRuleGroupConditionalRuleGroup,但似乎只有if 的所有内容。

【问题讨论】:

    标签: java rule-engine easy-rules


    【解决方案1】:

    所有规则都是if-then,没有别的;这是一种不同的想法。规则并不意味着取代您在普通源代码中看到的任何逻辑。它们最适用于类似于长开关案例逻辑或参数驱动逻辑的事情。所得税表格是参数驱动用例的一个示例。

    如果你需要基于一个事实的价值采取三个行动,例如:

    if age < 16
        action: discount = 15%
    else if age >= 16 and age <= 65
        action: discount = 0%
    else 
        action: discount = 20%
    

    那么你只需编写 3 条规则:

    when age < 16
        action: discount = 15%
    
    when age >= 16 and age <= 65
        action: discount = 0%
    
    when age > 65
        action: discount = 20%
    

    复合规则用于处理规则。

    ConditionalRuleGroup 有一个“主要”规则,当它评估为 true(已触发)时,该组中的其余规则将被触发,否则该组将被跳过。

    我使用UnitRuleGroup 进行类似表单的验证。表单中的所有数据都放入facts 并触发验证规则。如果任何一个规则为假,则整个组返回假,表格被视为无效。

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 2010-09-28
      • 2019-10-13
      • 2021-01-31
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多