流口水的单继承 [英] Single inheritance in drools

查看:57
本文介绍了流口水的单继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Drools 7.31版本.我们有一个大规则,必须在其他规则中重复使用.我们需要的基本功能是在SUPER规则和SUB规则条件均成立时触发SUPER规则.似乎旧版本具有"extends"关键字,使用它可以实现这一点.我正在看这个较旧的帖子,它可以实现这一目标-

We are using drools 7.31 version. We have one big rule which must be reused in other rules. The basic functionality we need is to fire the SUPER rule when both SUPER rule and SUB rule conditions are true. It seems like older version had "extends" keyword using which this was possible. I was looking at this older post which is able to achieve this -

drools规则继承

当我尝试使用7.31版本结束时,SUPER和SUB规则均独立触发.这是我要实现的目标的一个示例:

When I tried on my end with 7.31 version, then both SUPER and SUB rules are firing independently. Here is an example of what I am trying to achieve:

rule "super"
    @description("Generic super rule")
    when 
        $person : Person()
        a : PersonInput() from $person.getInput()
        if (a.getPersonId() == 1) break[pvd1]
        else if (a.getPersonId() == 2) break[pvd2]
    then System.out.println("Default then = " + a.getPersonId());
    then[pvd1] System.out.println("Super RULE has person id = " + a.getPersonId());
    then[pvd2] System.out.println("Super RULE has person id = " + a.getPersonId());
end

rule "sub" extends "super"
    @description("Sub rule")
    when
        c : PersonInput(HouseholdId == "99999") from $person.getInput()
    then
        System.out.println("Sub RULE has HouseholdId == 99999 == " + c.getPersonId());
end

我得到的输出是首先所有家庭的id为1/2的人,然后第二条规则仅打印99999家庭ID.

The output I am getting is all persons with personId 1/2 of all households first, then then 2nd rule is printing only 99999 household id.

我期望的输出只是打印出HomeId 99999的personId 1/2.

The output that I expect is just to print personId 1/2 of householdId 99999.

谢谢.

推荐答案

Drools 7.31仍支持扩展"机制.这里的问题是,当您扩展基本规则时,基本规则会触发.(这很明显;如果基本规则无效,则子规则无效.如果基本 有效,则将触发.子规则触发,它也触发父规则.

Drools 7.31 still supports the 'extends' mechanism. The issue here is that when you extend a base rule, the base rule does fire. (This is kind of obvious; if the base rule isn't valid to fire, then the child rule isn't valid to fire. If the base is valid to fire, then it fires. And when the child rule fires, it also fires the parent rule.)

解决此问题的传统方法是使用三个规则.基本"规则不应有任何后果(即,没有右侧/then子句),因此它仅列出了常见条件.这样,超级"规则的后果就在于扩展了基本规则,没有任何其他何时"条件且正常情况下具有then子句的规则.而且子"规则还扩展了基础",包括其附加条件,然后包括其自身的后果.如果您不希望在"sub"执行时触发"super",则使它们互斥.

The traditional way to work around this is to use three rules. The "base" rule should have no consequences (that is, no right hand side/then clause) so it simply lists out the common conditions. Then the "super" rule's consequences are in a rule that extends the base rules, doesn't have any additional 'when' conditions, and has the then clause as normal. And the "sub" rule also extends "base", includes its additional conditions, and then its own consequences. If you don't want "super" to fire when "sub" does, you make them mutually exclusive.

这是一个故意简化的示例:

Here's an intentionally simplified example:

rule "Base Rule"
when
  $person: Person( $input: input != null )
  PersonInput( $id: personId in (1, 2) )
then
// intentionally no 'then' clauses
end

rule "Super" extends "Base Rule"
when
  // No additional conditions, just use whatever is in Base Rule
then
  System.out.println("Person has id: " + $id);
end

rule "Sub" extends "Base Rule"
when
  PersonInput( householdId == "99999") from $input
then
  System.out.println("Household ID is 99999; id: " + $id);
end

现在,如果我们不希望在"Sub"运行时触发"Super",则只需使其与"Sub"互斥:

Now if we don't want "Super" to fire when "Sub" does, we just make it mutually exclusive from "Sub":

rule "Super" extends "Base Rule"
when
  // This is the condition from Sub, inverted:
  PersonInput( householdId != "99999" ) from $input
then
  System.out.println("Person has id: " + $id);
end

基本上,当您扩展规则时,将同时扩展when then子句.父规则的 then 子句(后果)将在子规则产生后果之前触发.此外,如果孩子是无效的,但父母是有效的,则您仍将解雇父母.因此,如果您有(说)3条子规则和一条父规则,则该父规则可能会被解雇最多4次:单独评估父规则时会被解雇一次,然后每条子评估又会被解雇3次.当您使用状态会话和 update / insert 子句时,事情变得特别奇怪.

Basically, when you extend a rule, you're extending both the when and the then clauses. The then clause (consequences) of the parent rule will fire before the consequences of the child rule. Further, if the child is not valid, but the parent is, you will still fire the parent. So if you have (say) 3 child rules and a parent rule, the parent may be fired up to 4 times: once when it by itself is evaluated, and then 3 more times once per child evaluation. Things get particularly odd when you use stateful sessions and update/insert clauses.

我所展示的无后果"基本规则模式就是从这种现实演变而来的.按照此模式,因为没有副作用(后果/然后子句),因此基本规则"被触发多少次没有影响.

The "no consequence" base rule pattern I've shown evolved out of this reality. Following this pattern, it makes no difference how many times "Base Rule" gets fired because there are no side effects (consequences/then clause).

这篇关于流口水的单继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆