title: How to use drools traits for multiple objects
body: <p>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?</p>
  
  <p>My traitable fact:</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>
  
  <p>My rule set:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The testcase that is failing:</p>
  
  <pre><code>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")));
      }
  }
  </code></pre>