[rules-users] Find the lowest and highest dates

LarryW lwakeman at tmghealth.com
Fri Feb 21 07:29:55 EST 2014


I'm really new to Drools so this is probably a basic question.  However, it
has consequences as to coding style. 

I have to go through a class and find the lowest and highest dates.  I have
a simple rule that works, below.  I know there's also a way to do it using
accumulate, but it's ugly and probably not easily maintainable.  Someone
told me I should not use java in the THEN segment, but I don't see anyway
around it.  Is there a better way to write this rule than what I have here? 

The problem is that the Then segment need only fire once, while the file may
contain any number of service lines.  You have to process all service lines
before you know if the dates are the lowest and highest. 

rule "FindLowestHighestDates" 
when 
        $claim : Claim(); 

then 
        LocalDate lowestBeginDate =
$claim.getServiceLineInfoList().get(0).getServiceRelatedDates().getServiceDates().getBeginDate(); 
        LocalDate highestEndDate =
$claim.getServiceLineInfoList().get(0).getServiceRelatedDates().getServiceDates().getEndDate(); 

        for (ServiceLineDetail srvc: $claim.getServiceLineInfoList()) 
        { 
                DateRange relDates =
srvc.getServiceRelatedDates().getServiceDates(); 

                if (relDates.getBeginDate() != null &&
relDates.getBeginDate().isBefore(lowestBeginDate)) 
                { 
                        lowestBeginDate = relDates.getBeginDate(); 
                } 

                if (relDates.getEndDate() != null &&
relDates.getEndDate().isAfter(highestEndDate)) 
                { 
                        highestEndDate = relDates.getEndDate(); 
                } 
        } 

        ClaimDates cd = $claim.getClaimDates(); 
        if (cd == null) cd = $claim.createClaimDates(); 

        DateRange sd = cd.getServiceDates(); 
        if (sd == null) sd = cd.createServiceDates(); 

        sd.setBeginDate(lowestBeginDate); 
        sd.setEndDate(highestEndDate); 
end



--
View this message in context: http://drools.46999.n3.nabble.com/Find-the-lowest-and-highest-dates-tp4028231.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list