Question

Drools seems to support only days, hours, minutes, seconds and milliseconds for the units of time used with Temporal Operators. I am working on a rule that looks for people within a particular age group. For example: between 6 months and 5 years, younger than 18 years, older than 12 years etc..

My person Class has a dateOfBirth instance variable, but no person.age method to do a direct comparison like:

    $p : Person(age <= 18)

I don't have too much liberty to modify the Person Class and I am trying to avoid writing utils methods and using 'eval' for comparing, as 'evals' are not optimized. I am a drools newbie and have written the following rule.

rule "Under18Years"

when
    now : RuleTime()
    $p : Person(dateOfBirth after[-6570d, 0s] $now )
then
    System.out.println("Under18Years fired");
end

I know this isn't very accurate as I just used 6570 (365*18) days; ignoring the leap years. I might be better off using seconds in a standard SI year (31,556,926) times 18 to account for 18 years, but is there a better way? This doesn't work for conditions involving months either. Does anyone have any other ideas/solution to this problem?

Was it helpful?

Solution

You can create a package or a function that does the above

public int getAge() {
Years years = Years.yearsBetween(dateOfBirth, currentDate);
return years.getYears();

For aslong as i have been using drools there has allways been years available to call upon. i see this was posted a while ago and maybe you have found out since.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top