drools中的单一遗传

drools中的单一遗传,drools,Drools,我们正在使用drools 7.31版本。我们有一条大规则,必须在其他规则中重复使用。我们需要的基本功能是在超级规则和子规则条件都为真时触发超级规则。似乎旧版本有“extends”关键字,这是可能的。我在看这篇老文章,它能做到这一点- 当我尝试使用7.31版本时,超级规则和子规则都是独立启动的。以下是我努力实现的一个例子: rule "super" @description("Generic super rule") when $person : Person(

我们正在使用drools 7.31版本。我们有一条大规则,必须在其他规则中重复使用。我们需要的基本功能是在超级规则和子规则条件都为真时触发超级规则。似乎旧版本有“extends”关键字,这是可能的。我在看这篇老文章,它能做到这一点-

当我尝试使用7.31版本时,超级规则和子规则都是独立启动的。以下是我努力实现的一个例子:

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
我得到的输出首先是所有家庭中人名为1/2的所有人,然后第二条规则是只打印99999个家庭id

我期望的输出只是打印Household99999的personId 1/2


谢谢。

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

解决这个问题的传统方法是使用三条规则。“基本”规则应该没有任何后果(即,没有右侧/then子句),因此它只是列出了常见的条件。然后,“超级”规则的结果在一个扩展基本规则的规则中,没有任何附加的“when”条件,并且Then子句是正常的。“子”规则也扩展了“基础”,包括其附加条件,然后是其自身的后果。如果你不想让“超级”在“sub”开火时开火,你就让它们互相排斥

下面是一个故意简化的示例:

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”开火时开火,我们只需使其与“Sub”相互排斥:

基本上,当您扩展规则时,您同时扩展了when和then子句。父规则的
then
子句(结果)将在子规则的结果之前触发。此外,如果子项无效,但父项无效,则仍将激发父项。因此,如果您有(比如)3个子规则和一个父规则,那么父规则最多可能被激发4次:当它自己被评估时激发一次,然后每个子规则再激发3次。当您使用有状态会话和
update
/
insert
子句时,事情会变得特别奇怪


我展示的“无后果”基本规则模式就是从这个现实中演变而来的。按照这种模式,触发“基本规则”的次数没有区别,因为没有副作用(results/then子句)

你的超级规则将独立启动,它本身就是一个有效的规则。什么能阻止它?此外,7.31仍有扩展;如果super中的所有条件(包括super的then条款)也发生火灾,则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