简体   繁体   English

如果drools决策表中没有条件适用,如何设置默认值?

[英]How to set the default value if no conditions apply in drools decision table?

I am working with drools 7.69.0,我正在使用流口水 7.69.0,

My purpose is to convert the below condition into a decision table at firstly,我的目的是首先将以下条件转换为决策表,

Stream.of(usecase1, usecase2, usecase3, usecase4).collect(Collectors.toSet()).contains(statusRequest.getUseCase())

|| (usecase6 == statusRequest.getUseCase() && aspect == statusRequest.getAspect())

|| (usecase5 != statusRequest.getUseCase() && statusRequest.isCorrection())

在此处输入图像描述

So how to apply default action if none of the conditions are applied?那么如果没有应用任何条件,如何应用默认操作呢?

There are two ways to do a "default" value:有两种方法可以设置“默认”值:

  1. Set the default in the highest priority rule, then rely on subsequent rules to overwrite it.在最高优先级规则中设置默认值,然后依靠后续规则覆盖它。 If no subsequent rules are valid, then the default remains.如果没有后续规则有效,则保留默认值。
  2. Set the default in the lowest priority rule, and have higher priority rules exit fast or set some condition such that the lowest priority rule is no longer valid.在最低优先级规则中设置默认值,让更高优先级的规则快速退出或设置一些条件,使最低优先级的规则不再有效。

In a decision table, #1 is easiest if you have Sequential set to TRUE in the Rule Set area.在决策表中,如果您在 Rule Set 区域中将 Sequential 设置为 TRUE,则 #1 是最简单的。 Default your "default case" rule in the first row, so it fires first.在第一行默认您的“默认情况”规则,因此它首先触发。 Give it no conditions, so it always triggers.不给它任何条件,所以它总是触发。

It would look something like this:它看起来像这样:

具有默认最高优先级规则的决策表的屏幕截图

The second is also possible, but for it to work you need to be able key off of something that all of the other "real" rules will cause to no longer be valid.第二个也是可能的,但要让它工作,你需要能够关闭所有其他“真实”规则将导致不再有效的东西。

In your case, all of your "real" rules are setting a value on the 'status' field of the model.在您的情况下,您所有的“真实”规则都在模型的“状态”字段上设置一个值。 Therefore, it stands to reason that if there is no value set on that field, we can set our default value.因此,按理说,如果该字段上没有设置值,我们可以设置我们的默认值。

This will require two things:这将需要两件事:

  • changing your ACTION to use a modify so that the results are visible to the conditional checks更改您的 ACTION 以使用modify ,以便条件检查可以看到结果
  • changing your data type for the 'status' field to allow a null value;更改“状态”字段的数据类型以允许空值; by seeing that the value is originally 'null', we'll know that no other rules have fired and that we can thus set the default value.通过查看该值最初为“null”,我们将知道没有触发其他规则,因此我们可以设置默认值。

The cool thing about this version is that it doesn't rely on rule order, so you don't need to set the Sequential property, and it also simply doesn't matter when the rule fires.这个版本很酷的一点是它不依赖于规则顺序,所以你不需要设置 Sequential 属性,而且规则何时触发也无关紧要 If the rule fires first, cool: the "real" rules will simply overwrite the value.如果规则首先触发,很酷:“真正的”规则将简单地覆盖该值。 If it fires last, also cool;如果它最后着火,也很酷; that means that none of the other rules fired.这意味着没有触发其他规则。 Somewhere in the middle?中间的某个地方? Still fine!还是不错的! Since none of the other rules care about the value of status, it doesn't matter when the rule fires as long as it fires only when the status is null.由于其他规则都不关心状态的值,因此规则何时触发并不重要,只要它仅在状态为空时触发即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM