Databases Reference
In-Depth Information
To write these rules, we will need to calculate the duration of the leave period, as
well as calculate how long it is before the start date. Out of the box, the rule engine
provides the
Duration
extension methods, which allow us to calculate the number of
days between two dates, but doesn't allow us to exclude weekends.
So we will need to write our own logic to calculate these values. Rather than
embedding this logic directly in each rule, best practice dictates that we place this
logic into a separate function. This not only ensures that we have a single version of
the logic to implement but minimizes the size of our rules, thus making them simpler
and easier to maintain. For our purposes, we will create the following functions:
•
•
startsIn
: Which returns the number of days before the specified start date
leaveDuration
: Which returns the number of days from the start date to the
end date, excluding weekends
Creating a function
To create our first function, within the rule editor, click on the
Functions
tab. This
will list all the functions currently defined to our ruleset. To create a new function,
click on the green plus icon, as shown in the following screenshot:
This will add a new function with a default name (for example,
Function_1
) to
our list. Click on the function name to select it and update it to
startsIn
. From the
drop-down list, select the
Return Type
of the function, which is
int
in our case.
Next, we need to specify the arguments we wish to pass to our function. Click
on the green plus sign, as shown in the following screenshot, and this will add
an argument to our list. Here we can specify the argument name (for example,
startDate
), and from the drop-down list, the argument
Type
, which should be
XMLGregorianCalendar
(when creating XML facts, the JAXB processor maps
the type
xsd:date
to
javax.xml.datatype.XMLGregorianCalendar
).