Search code examples
drools

How to use drools traits for multiple objects


I have a set of Drools rules that work correctly if I add one fact to the drools engine, if I add two facts, the rules are only applied on the first fact. How can I make the ruleset work for multiple facts?

My traitable fact:

@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;
    }
}

My rule set:

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

The testcase that is failing:

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")));
    }
}

Solution

  • The culprit is rule2. It will not fire as soon as there is one PolicyWithFactors in Working Memory.

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

    I haven't tried to understand all the rest, but please check whether this constraint should be present in all subsequent rules, about to match one Policy with one PolicyWithFactors.