可以将Drools配置为将规则应用于插入的对象字段中包含的对象吗?

时间:2017-02-23 14:08:03

标签: java drools

我有一套复杂的规则要在Drools中实现,我试图避免重复的规则。例如,我有一个由另外两个类使用的ForeignPerson类。

public class ForeignPerson {
  private String name;
  private String country;
}

public class Owner {
  private Individual individual;
  private ForeignPerson foreignPerson;
}

public class Beneficiary {
  private Individual individual;
  private ForeignPerson foreignPerson;
}

在ForeignPerson的每个实例中,country field必须是“USA”。我希望能够kieSession.insert(owner);,如果ForeignPerson字段不为null,则检查ForeignPerson规则以及所有者规则。

ForeignPerson.drl规则文件如:

rule "R001: Country must == USA"
  when 
    ForeignPerson(country != "USA")
  then 
    System.out.println("Country must be USA");
end

Owner.drl规则文件如:

rule "R001: Foreign Person must exist"
  when 
    Owner(foreignPerson == null)
  then 
    System.out.println("foreignPerson must not be null");
end

我不想写如下所示的Owner.drl文件,因为它会导致很多重复的规则。

rule "R001: Foreign Person must exist"
  when 
    Owner(foreignPerson == null)
  then 
    System.out.println("foreignPerson must not be null");
end

rule "R002: Foreign Person country must be USA"
  when 
    Owner(foreignPerson != null, foreignPerson.getCountry() != "USA")
  then 
    System.out.println("foreignPerson must have country USA");
end

这是可能的还是单独注入对象的唯一方法?

2 个答案:

答案 0 :(得分:0)

此规则规定外国人来自美国。

%>% select(1,5:9) %>%

您可以使用它来检查所有者事实:

rule "from the US"
when 
  $foreignPerson: ForeignPerson( country == "USA" )
then
end

受益人也一样。

答案 1 :(得分:0)

简短的回答是否定的。必须单独插入要直接检查的每个对象。扩展关键字可用于验证对象是否具有正确的父对象(请参阅此问题is it possible to extend a rule using a specified set of parameters or pass parameters into a rule in Drools?)。