是否可以向 drools 规则添加自定义属性?

发布于 2024-10-31 08:11:40 字数 811 浏览 5 评论 0原文

我们将使用 Drools 为客户实现一个规则引擎,其中一个要求是能够说规则与特定的法律要求相关联。

例如,法律规定驾驶执照只能颁发给 18 岁或以上的人,因此我们有:

rule "Driving Licence: Age >= 18"
    when
        $applicant: Applicant(age < 18)
        $application: Application()
    then 
        $application.setValid(false);
end

或 用于计算税收总额

rule "Tax: Top bracket"
    when
        $return: Return(income > 44000)
    then 
        $application.setTopBracket(true);
end

或类似内容。

一种解决方案是在名称中包含法律要求:

rule "Driving Licence: Age >= 18: Section 103 RTA 1988"

问题:实现这一目标的最佳方法是什么?名字是唯一的方法吗?有没有办法向规则添加自定义属性?如果可以的话,可以强制执行吗?我也可以使用评论,但我宁愿避免这样做。

如果在规则跟踪中我们也可以看到这些属性的跟踪,那就太好了,但这并不重要。

请注意,我不想改变规则逻辑,我只是​​想能够说“这条规则来自这条法律”。规则的决定并不重要。我不想更改 setValid 设置器。

We are going to be implementing a rules engine using Drools for a customer, and one of the requirements is to be able to say that a rule is associated with a particular legal requirement.

For instance, the law says that a driving licence can only be issued to someone 18 or over, so we have:

rule "Driving Licence: Age >= 18"
    when
        $applicant: Applicant(age < 18)
        $application: Application()
    then 
        $application.setValid(false);
end

or for the calculation of a tax total

rule "Tax: Top bracket"
    when
        $return: Return(income > 44000)
    then 
        $application.setTopBracket(true);
end

or similar.

One solution would be to have the legal requirement in the name:

rule "Driving Licence: Age >= 18: Section 103 RTA 1988"

The question: what would be the best way of achieving this? Is the name the only way? Is there a way to add a custom attribute to a rule? If so, can it be mandatory? I could also use comments, but I would prefer to avoid that.

It would be nice if in the rule trace we could see the trace of these attributes as well, but that isn't vital.

Please note that I don't want to change the rule logic, I just want to be able to say 'This rule comes from this law'. The decision of the rule is not important. I do not wish to change the setValid setter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-11-07 08:11:40

您可以为此使用规则元数据。元数据不会影响您的规则,除非您愿意,您可以在左侧或右侧使用它。您还可以在调试时访问元数据,例如,您可以使用 AgendaFilter 打印元数据并查看正在触发哪些规则以及它们有哪些法律要求。

如果您想强制每个规则使用元数据,则需要自己编写检查代码。

@metadata_key( metadata_value1, metadata_value2, ...  )

rule "Driving Licence: Age >= 18"
@LegalRequirement("Section 103 RTA 1988")
when
    $applicant: Applicant(age < 18)
    $application: Application()
then 
    $application.setValid(false);
end

这是 AgendaFilter 的示例。如果返回 false,它将阻止规则触发。我只是用它来打印法律要求:

StatefulKnowledgeSession ksession= createKSession();   
ksession.fireAllRules(new AgendaFilter() {
  public boolean accept(Activation activation) {
    Map<String, Object> metaData = activation.getRule().getMetaData();
    if (metaData.containsKey("LegalRequirement")) {
      System.out.println(metaData.get("LegalRequirement"));
    }
    return true;
  }
});

或者如果您想在 RHS 中使用它:

rule.getMetaData().get("LegalRequirement");

如果您使用 Guvnor 来管理您的规则,您也可以使用类别设置。对于 Guvnor 中存储的每个资产来说,类别是必需的。

You could use rule metadata for this. Metadata doesn't affect your rule, unless you want to, you can use it in the LHS or the RHS. You can also access metadata while debugging, for example you can print the metadata with AgendaFilter and see what rules are firing and what legal requirements they have.

If you want to force the metadata for each rule, you need to write the check up codes for that yourself.

@metadata_key( metadata_value1, metadata_value2, ...  )

rule "Driving Licence: Age >= 18"
@LegalRequirement("Section 103 RTA 1988")
when
    $applicant: Applicant(age < 18)
    $application: Application()
then 
    $application.setValid(false);
end

Here is an example of the AgendaFilter. If you return false it will prevent the rule from firing. I'm just using it to print the legal requirement:

StatefulKnowledgeSession ksession= createKSession();   
ksession.fireAllRules(new AgendaFilter() {
  public boolean accept(Activation activation) {
    Map<String, Object> metaData = activation.getRule().getMetaData();
    if (metaData.containsKey("LegalRequirement")) {
      System.out.println(metaData.get("LegalRequirement"));
    }
    return true;
  }
});

Or if you want to use it in the RHS:

rule.getMetaData().get("LegalRequirement");

If you are using Guvnor to manage your rules you can also use the category setting. Category is a mandatory for each asset stored in Guvnor.

鸵鸟症 2024-11-07 08:11:40

您可以修改方法 Application.setValid(false) 以采用其​​他参数,这将是法律要求。这样您就可以将其呈现给您的客户。

You could modify the method Application.setValid(false) to take an other parameter, that would be the legal requirement. That way you could present it to your customer.

温柔少女心 2024-11-07 08:11:40

我同意 Guillaume Alvarez 的观点,但也请记住(可能在您的系统中)应用程序可能会因多种原因被拒绝,因此您可能有这样的方法:

application.addInvalidReason(SomeType)

此方法可能会被同一应用程序的多个规则调用...

I agree with Guillaume Alvarez, but also keep in mind that (probably in your system) an application could be refused for several reasons, therefore you may have a method like:

application.addInvalidReason(SomeType)

This method may be called by several rules for the same application...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文