Implementing the function class

How to implement the function class.
Note: If you have a function that is written for Rational® Integration Tester v8.5 and want to convert it to work in v8.6, see Converting existing function classes with Eclipse.
Note: Source code for the example can be found in the examples\FunctionsSamplePlugin\src folder of your Rational Integration Tester installation.

Java class extending from function

Under the "src" folder of the plug-in project, create a Java™ class that extends from com.ghc.ghTester.expressions.Function (for example, com.samples.function.FormatDate).

Note: Ensure that the class is placed in a package other than the default package. To do so, make a valid package declaration at the top of the Java class.

Now the following methods need to be implemented:

Default constructor

Create a default, public constructor with an empty body. You can add other initialization as required, but it does not have to do anything, by default. The default constructor has to be public because of the way that functions are created.

public FormatDate() {
}

Override create (int size, Vector) parameters

The create(int, Vector) method is a factory method that creates an instance of the particular function with the parameters provided. The vector of parameters needs to be treated as functions themselves and are likely to need processing at run time.

public Function create(int size, Vector params) {
   Function outputFormat = null;
   if (size == 3) {
      outputFormat = (Function) params.get(2);
   }
   return new FormatDate((Function) params.get(0), (Function) params
                  .get(1), outputFormat);
}

The three-argument constructor that is used in the example can be found in the source that is provided in the examples folder of the Rational Integration Tester installation folder.

Override evaluate(Object) data

The evaluate(Object) method performs the actual work of the function. Rational Integration Tester passes the context of the current evaluation to your function. You can use this method to obtain the result from embedded functions and then return your function’s own result.

public Object evaluate(Object data) {
   String date = m_fDate.evaluateAsString(data);
   String inputFormat = m_fInputFormat.evaluateAsString(data);
   String outputFormat = "yyyy-MM-dd"; // Default format
      if (m_fOutputFormat != null) {
   outputFormat = m_fOutputFormat.evaluateAsString(data);
   }

   //...
   if (EvalUtils.isString(date)) {
      date = EvalUtils.getString(date);
   }
   if (EvalUtils.isString(inputFormat)) {
      inputFormat = EvalUtils.getString(inputFormat);
   }
   if (EvalUtils.isString(outputFormat)) {
      outputFormat = EvalUtils.getString(outputFormat);
   }

   SimpleDateFormat inputFormatter = new 
SimpleDateFormat(inputFormat);
   String formattedDate = "";
   try {
      Date d = inputFormatter.parse(date);
      SimpleDateFormat outputFormatter =new 
SimpleDateFormat(outputFormat);
      formattedDate = outputFormatter.format(d);
   } catch (ParseException ex) {
      // ...
   }

   return "\"" + formattedDate + "\"";
}
Note: When you are implementing your evaluate method, the vector of parameters that is passed to you are functions that must be evaluated to discover their value. Where a string literal is expected, the evaluated function must return a value that is surrounded by quotes. There is a helper function available to assist with the removal: EvalUtils.getString(Object string).

Feedback