我有一套複雜的規則在Drools中實現,我試圖避免重複的規則。例如,我有一個由另外兩個類使用的ForeignPerson類。可以將Drools配置爲將規則應用於插入的對象字段中包含的對象嗎?
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字段必須是「USA」。我希望能夠做到kieSession.insert(owner);
,並且如果ForeignPerson字段不爲空,請檢查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
這是可能的或者是單獨注入對象的唯一方法嗎?