2017-02-12 68 views
0

如果我將一個事實添加到drools引擎,如果我添加兩個事實,則規則僅適用於第一個事實,我有一套正確工作的Drools規則。我如何使規則集適用於多個事實?如何爲多個對象使用drools特徵

我traitable事實:

@Traitable 
public class Policy { 

    private String postalCode; 
    private String houseHold; 
    private BigDecimal grossPremium; 

    public Policy(String postalCode, String houseHold) { 
     this.postalCode = postalCode; 
     this.houseHold = houseHold; 
    } 
} 

我的規則集:

import java.math.BigDecimal; 

declare trait PolicyWithFactors 
    policy : Policy 
    postalCodeFactor : Double 
    houseHoldFactor : Double 
end 

rule rule1 
when 
    $f : PolicyWithFactors(policy.postalCode == "1000", postalCodeFactor == null) 
then 
    System.out.println("Running rule1 for " + $f.getPolicy().getHouseHold() + " and " + $f.getPolicy().getPostalCode()); 
    modify($f) { setPostalCodeFactor(20.0); } 
end 

rule rule2 
when 
    $p : Policy() 
    not PolicyWithFactors() 
then 
    System.out.println("Running rule2 for " + $p.getHouseHold() + " and " + $p.getPostalCode()); 
    PolicyWithFactors $pwf = don($p, PolicyWithFactors.class); 
    modify ($pwf) { setPolicy($p); } 
end 

rule rule3 
when 
    $p : Policy(houseHold == "single") 
    $f : PolicyWithFactors(houseHoldFactor == null) 
then 
    System.out.println("Running rule3 for " + $p.getHouseHold() + " and " + $p.getPostalCode()); 
    modify($f) { setHouseHoldFactor(10.0); } 
end 

rule rule4 
when 
    $p : Policy(grossPremium == null) 
    $f : PolicyWithFactors(policy == $p, $pcf : postalCodeFactor != null, $hhf : houseHoldFactor != null) 
then 
    System.out.println("Running rule4 for " + $pcf + " and " + $hhf); 
    BigDecimal $gp = new BigDecimal($pcf * $hhf); 
    modify($p) { setGrossPremium($gp); } 
end 

rule rule5 
when 
    $p : Policy(houseHold == "family") 
    $f : PolicyWithFactors(houseHoldFactor == null) 
then 
    System.out.println("Running rule5 for " + $p.getHouseHold() + " and " + $p.getPostalCode()); 
    modify($f) { setHouseHoldFactor(15.0); } 
end 

發生故障的測試用例:

public class RuleTest { 

    @Test 
    public void test() { 
     KieServices kieServices = KieServices.Factory.get(); 
     KieContainer kieContainer = kieServices.getKieClasspathContainer(); 
     KieSession kieSession = kieContainer.newKieSession(); 
     Policy policy1 = new Policy("1000", "single"); 
     kieSession.insert(policy1); 
     Policy policy2 = new Policy("1000", "family"); 
     kieSession.insert(policy2); 
     kieSession.fireAllRules(50); 
     assertThat(policy1.getGrossPremium(), is(new BigDecimal("200"))); 
     assertThat(policy2.getGrossPremium(), is(new BigDecimal("300"))); 
    } 
} 

回答

0

罪魁禍首是規則2。只要工作內存中有一個PolicyWithFactors,它就不會啓動。

rule rule2 
when 
    $p : Policy() 
    not PolicyWithFactors(policy == $p) 
then ... end 

我還沒有嘗試過了解所有的休息,但請檢查該約束是否應該出現在所有的後續規則,即將一個政策一個PolicyWithFactors匹配。

+0

它現在計算第一次溢價兩次。下面是輸出:針對單一單和1000 運行規則1和1000 家庭運行規則2和1000 家庭運行規則1和1000 單對單運行規則3和1000 運行規則3和1000 20.0運行rule4運行規則2和10.0 運行規則4爲20.0和10.0 –

+0

已解決。需要從$ f.policy而不是$ p讀取值 –

相關問題