I am trying to compare 2 dates (let's say Date1 and Date 2) from 2 different entities(let's say Entity1 and Entity2) using Guided Rules Editor.
Date1 is a variable from Entity1 and Date2 is a variable from Entity2
The rule I want to execute is -
Package com.rules.xyz;
import com.rules.xyz.Entity1;
import com.rules.xyz.Entity2;
When
{
Entity1.Date1 <= Entity2. Date2 + 7 Days
}
then <perform some action>
I want to apply the same rule for months and years, i.e., When Date1 <= Date2 + 6 Months, then perform some action.
I am using Drools Workbench 7.2.0 Final
Could you please suggest a solution for the same.
In my case I have created a function to be able to reuse it, so it is defined in a separate DRL file:
function Date workWithDates(Date date, String operator, int entity, int number) {
if (date != null) {
java.util.Calendar newCal = java.util.Calendar.getInstance();
newCal.setTime(date);
if (operator.equals("-")) { //normally we would add, except if we want to subtract
number = -number;
}
if (entity == 1) { //years
newCal.add(java.util.Calendar.YEAR, number);
} else if (entity== 2) { //months
newCal.add(java.util.Calendar.MONTH, number);
} else if (entity== 3) { //days
newCal.add(java.util.Calendar.DATE, number);
} else if (entity== 4) { //hours
newCal.add(java.util.Calendar.HOUR, number);
} else if (entity== 5) { //minutes
newCal.add(java.util.Calendar.MINUTE, number);
} else if (entity== 6) { //seconds
newCal.add(java.util.Calendar.SECOND, number);
}
return newCal.getTime();
} else {
return date;
}
}
And in your DRL file where you would invoke this function you would use it like this:
when
Entity2 ( $date2 : Date2 != null )
Entity1 ( Date1 != null, Date1 <= (workWithDates($date2, "+", 3, 7)) )
If you opt to separate the function in a different file too, make sure it is in the same package. For example, I have defined all reusable functions in a 000 FUNCTION.drl