Java 可以将Drools配置为将规则应用于“插入的对象”字段中包含的对象吗?
我有一套复杂的规则要在Drools中实现,我正在努力避免重复的规则。例如,我有一个ForeignPerson类,它被另外两个类使用Java 可以将Drools配置为将规则应用于“插入的对象”字段中包含的对象吗?,java,drools,Java,Drools,我有一套复杂的规则要在Drools中实现,我正在努力避免重复的规则。例如,我有一个ForeignPerson类,它被另外两个类使用 public class ForeignPerson { private String name; private String country; } public class Owner { private Individual individual; private ForeignPerson foreignPerson; } public
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;
}
在每一个外国人的例子中,国家字段必须是“USA”。我希望能够执行kieSession.insert(所有者)
如果ForeignPerson字段不为空,请检查ForeignPerson规则以及所有者规则
ForeignPerson.drl规则文件如下:
rule "R001: Country must == USA"
when
ForeignPerson(country != "USA")
then
System.out.println("Country must be USA");
end
rule "R001: Foreign Person must exist"
when
Owner(foreignPerson == null)
then
System.out.println("foreignPerson must not be null");
end
Owner.drl规则文件,如:
rule "R001: Country must == USA"
when
ForeignPerson(country != "USA")
then
System.out.println("Country must be USA");
end
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
这是可能的还是唯一一种单独注入对象的方法?这条规则确定外国人来自美国
rule "from the US"
when
$foreignPerson: ForeignPerson( country == "USA" )
then
end
您可以使用此选项检查所有者事实:
rule "owner from the US"
extends "from the US"
when
Owner( foreignPerson == $foreignPerson )
then
// ... owner is from the US
end
受益人也是如此。简短的回答是“否”。每个要直接检查的对象必须单独插入。extends关键字可用于验证对象是否具有正确的父对象(请参见此问题)