0

I have 2 collections of the same type and each object in the collection is key by an id. My goal is to find the same object in both collections and then compare a field against each other. If they are not the same field then store the differences.

My issue is performance, for every rule I re-scan the collection for the same object. Is there a way if the object matches then run all field validations instead of finding the item in the collection multiple times?

Fact Code:

public class ReconcilerFact 
{
    private List<Security> securitySystem1;
    private List<Security> securitySystem2;

    public ReconcilerFact(List<Security> securities1, List<Security> securities2) 
    {
        this.securitySystem1 = securities1;
        this.securitySystem2 = securities2;
    }

    public List<Security> getSecuritySystem1() 
    {
        return securitySystem1;
    }

    public List<Security> getSecuritySystem2() 
    {
        return securitySystem2;
    }
}

Drools Code:

rule "ISIN Rule"        
    no-loop 
    when
        ## conditions           
        ##                          
        $recon : ReconcilerFact()
        $security1 : Security() from $recon.securitySystem1
        $security2 : Security(sSecId == $security1.sSecId, sISIN != $security1.sISIN) from $recon.securitySystem2       
    then 
        ## For the valid condition
        ##      
        result.add($security1, SecurityFields.ISIN, $security1.getsISIN(), $security2.getsISIN());          
end

rule "Cusip Rule"       
    no-loop 
    when
        ## conditions           
        ##                          
        $recon : ReconcilerFact()
        $security1 : Security() from $recon.securitySystem1
        $security2 : Security(sSecId == $security1.sSecId, sCusip != $security1.sCusip) from $recon.securitySystem2     
    then 
        ## For the valid condition
        ##      
        result.add($security1, SecurityFields.CUSIP, $security1.getsCusip(), $security2.getsCusip());           
end

rule "Sedol Rule"       
    no-loop 
    when
        ## conditions           
        ##                          
        $recon : ReconcilerFact()
        $security1 : Security() from $recon.securitySystem1
        $security2 : Security(sSecId == $security1.sSecId, sSedol != $security1.sSedol) from $recon.securitySystem2     
    then 
        ## For the valid condition
        ##      
        result.add($security1, SecurityFields.SEDOL, $security1.getsSedol(), $security2.getsSedol());           
end
Joe Intrakamhang
  • 489
  • 2
  • 7
  • 14

1 Answers1

0

Instead of using the from Conditional Element you can just insert all the security objects and tag them with a Group field. So you will end up having: $s1: Security(group == "Group1") $s2: Security(group == "Group2", sSecId == $security1.sSecId)

That will treat each security as a fact and if you modify one single instance, only that instance will be reevaluated.

Cheers

salaboy
  • 4,123
  • 1
  • 14
  • 15