2017-02-23 31 views
0

我有一套複雜的規則在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 

這是可能的或者是單獨注入對象的唯一方法嗎?

回答

0

該規則規定,外國人是來自美國的A.

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 

受益人同樣的事情。

相關問題