Sample function definition code

The following code examples illustrate the definition of user-defined functions.

Body Mass Index (BMI) function

The following code sample illustrates the definition of a function that returns the body-mass index (BMI) based on the values of height and weight. The function has a classification of Clinical. It takes two parameters, height and weight. Descriptive text for the function and its parameters appears in the Rule Wizard and the Invoke Function dialog box.

Note:

Oracle Health Sciences Central Designer attributes are shown in bold.
using System;
using Oracle.Designer.ExternalFunctions;
namespace Customer.Designer.ClinicalData.Functions
{
/// <summary>
/// Summary description of Clinical
/// </summary>
[DesignerFunctionClassification("Clinical")]
public class Clinical
{
    private Clinical()
    {
    }
    /// <summary>
    /// Returns the body-mass index based on height and weight
    /// </summary>
    /// <param name="height">Height of the patient</param>
    /// <param name="weight">Weight of the patient</param>
    /// <returns></returns>
    [DesignerFunction ("Returns the body-mass index based on height and weight")]
    public static float BMI (
         [DesignerParameter ("Patient height")]
         float height,
         [DesignerParameter ("Patient weight")]
         float weight)
        {
         return weight / (height * height);
        }
    }
}

Date Time difference function

The following code sample illustrates the definition of a function that returns the length of the interval between any two date time parts. If there are any unknown date time parts, the dates are normalized, and the results are rounded down. This function has a classification of Date Manipulation Function. It takes two dates and a date part as parameters. Descriptive text for the function and its parameters appears in the Rule Wizard and the Invoke Function dialog box.

Note:

Oracle Health Sciences Central Designer attributes are shown in bold.
    using System;
    using Oracle.Designer.ExternalFunctions;
    namespace Customer.Designer.DateTime.Functions
{
    /// <summary>
    /// Returns the number of units between two dates, based on the requested interval.
    /// The units sign will be negative if date1 is before date2
    /// </summary>
    /// <param name="date1">The earlier date</param>
    /// <param name="date2">The later date</param>
    /// <param name="units">The interval to calculate</param>
    /// <returns>The integer number of interval units. The value will be negative if date1 is before date2.</returns>
    /// <remarks>Valid units are taken from the DateTimeParts enumerator, and include:
    /// <list type="bullet">
    /// <item>Years</item>
    /// <item>Months</item>
    /// <item>Days</item>
    /// <item>Hours</item>
    /// <item>Minutes</item>
    /// <item>Seconds</item>
    /// </list>
    /// Dates are normalized if either date has any UNKNOWN parts. Results are rounded down
    /// to the nearest integer. For example, <code>_GetDateDifference (new DateTime(2007,1,10,0,0,0),
    /// new DateTime (2007,2,1,0,0,0), DateTimeParts.Months)</code> will return 0, not one</remarks>
    [DesignerFunctionClassification("Date Manipulation Functions")]
     [DesignerFunction("Returns the length of the interval between two dates. The dates are normalized if there are any unknown parts. Results are rounded down.")]
     public static int _GetDateDifference (
        [DesignerParameter("Date 1 for compare")]
        PFDateTime date1,
        [DesignerParameter("Date 2 for compare")]
        PFDateTime date2,
        [DesignerParameter(". Valid intervals are taken from DateTimeParts enum: Years, Months, Days, Hours, Minutes, Seconds")]
        int units)
      {
        if (date1 == null || date2 == null)
        {
          throw new ArgumentException("null parameter to GetDateDifference");
        }

        double difference = 0.0;

        DateTime normalizedEarlierDate = (DateTime)_NormalizeDate(date1, date2, new DateTime(2000,1,1,12,0,0));
        DateTime normalizedLaterDate = (DateTime)_NormalizeDate(date2, date1, new DateTime(2000,1,1,12,0,0));
        if (normalizedEarlierDate > normalizedLaterDate)
        {
         DateTime temp = normalizedEarlierDate;
         normalizedEarlierDate = normalizedLaterDate;
         normalizedLaterDate = temp;
         sign = -1;
        }
        TimeSpan ts = (DateTime) normalizedLaterDate - (DateTime) normalizedEarlierDate;
        switch (units)
        {
          case DateTimeParts.Hours:
            difference = ts.TotalHours;
            break;
          case DateTimeParts.Minutes:
            difference = ts.TotalMinutes;
            break;
          case DateTimeParts.Seconds:
            difference = ts.TotalSeconds;
            break;
          case DateTimeParts.Days:
            difference = ts.TotalDays;
            break;
            // Rounds down to nearest month
          case DateTimeParts.Months:
          case DateTimeParts.Years:
            difference = (normalizedLaterDate.Year - normalizedEarlierDate.Year)*12 + (normalizedLaterDate.Month - normalizedEarlierDate.Month);
            if (normalizedLaterDate.Day < normalizedEarlierDate.Day)
            {
              difference -= 1;
            }
            break;
                }
                if (units == DateTimeParts.Years)
                {
            difference /= 12;
                }
                int ret = ( (int)difference * sign);
                return ret;
              }