Functions

functions are used to implement logic using Java constructions.  We sometimes use functions in our DSLs to extend the Drools logic capability, or to perform calculations that are difficult to do directly in the Drools language.

Here is an example of a function that we use to logically remove the time portion of a date-time value, which is useful when you are doing things like counting the number of distinct dates when a particular event happened:


Example Drools Function
function java.util.Date stripTimeComponent (java.util.Date dateTime)
{
/*
Returns a new java.util.Date which contains only the year month day components of the submitted java.util.Date,
and sets the hours, minutes, seconds and milliseconds components to 0, which makes it reference midnight of that date.  
Returns null if sent in null.
*/
    if (dateTime == null)
    {
        return null;
    }
 
    org.opencds.common.utilities.DateUtility dateUtility = org.opencds.common.utilities.DateUtility.getInstance();
    String dateAsString = dateUtility.getDateAsString(dateTime, "yyyy-MM-dd");
    return dateUtility.getDateFromString(dateAsString, "yyyy-MM-dd");
}


Most of the functions that we use for Drools were developed in the OpenCDS source code as java methods in the package opencds-guvnor-support/src/main/java/org/opencds/support/guvnor/GuvnorFunctionWorkup.java.