Drools - Find minimum Java 8 Local Date

Suresh Source

I am trying to find the minimum date from list of dates (Java 8) using accumulate function in Drools.

This is my rule:

rule "Print minimum Service Date from Bill Lines"
when
  accumulate ( 
    $lineItem : LineLevelData($dateOfService : dateOfService) , 
    $epochDay : min($dateOfService.toEpochDay())  
  )
  $minServiceDate : LocalDate() from LocalDate.ofEpochDay($epochDay)
then
  System.err.println("Min. Service Date used in rules calculation : " + $minServiceDate);
end

This is the exception I get:

Unable to Analyse Expression LocalDate.ofEpochDay($epochDay):
[Error: unable to resolve method using strict-mode: java.time.LocalDate.ofEpochDay(java.lang.Comparable)]
[Near : {... LocalDate.ofEpochDay($epochDay) ....}]
                       ^ : [Rule name='Print minimum Service Date from Bill Lines']

Obviously, I am missing some basics here. Can somebody help me to fix this one?

Drools version: 7.5.0

POJO:

public class LineLevelData{

LocalDate dateofService;
}

Update:

rule "Print minimum Service Date from Bill Lines"

    when
                    accumulate ( $lineItem : LineLevelData (  $dateOfService : dateOfService   ) , 
                                                              $epochDay : min($dateOfService.toEpochDay())  )
                    $epochLong : Number (longValue  > 0 ) from $epochDay
                    $minServiceDate : LocalDate(  ) from LocalDate.ofEpochDay($epochLong)
            then

            System.err.println("Min. Service Date used in rules calculation : " + $minServiceDate);

end

After adding the cast to Long, epoch is converted to Local date. Adding this in case if it helps someone looking for this.

java-8droolslocaldate

Answers

answered 1 month ago Esteban Aliverti #1

I guess the problem is that Drools is not preserving the type of the min function is returning and it is treating it as a Comparable instead of as a long.

There are 3 ways you can solve this problem:

Workaround:

rule "Print minimum Service Date from Bill Lines"
when
  $c: Comparable() from accumulate ( 
    LineLevelData($dateOfService : dateOfService) , 
    min($dateOfService)  
  )
  $minServiceDate: LocalDate() from $c
then
 System.err.println("Min. Service Date used in rules calculation : " + 
   $minServiceDate);
end

Hope it helps,

comments powered by Disqus