A get or set invocation can be done with any number of parameters and the parameters can be of any type.

The JavaScript function helper parameters are passed as an array. This means that each specific parameter is accessed using array dereferencing syntax. For example, if you want to build a custom operator to concatenate any number of strings, a custom function could be used in Corticon studio Rulesheets :

MyEntity.str3 = getString('concatenate', 'demo1 - ', 'demo2')

To concatenate three strings in Corticon studio:

MyEntity.str4 = getString('concatenate','demo1 - ', 'demo2 - ', 'demo 3' )

Let’s look at how the custom function could be implemented, and how the string parameters would be accessed:

function concatMany ( helper, params ) {
  let result = "";  
  for ( let i=0; i<params.length; i++ )
       result += params[i];  

  return result; 
}

Notice:

  • Only one variable is declared, params in line 1.

  • Each parameter is specified by the modeler within the array using params[i]in line 4.

  • Lines 3 and 4 iterate through all the inputs and concatenate them into a single string that is returned in result on line 6.

Handling missing parameters or unexpected parameter types

A custom function might require a specific number of parameters, each of a specific type. Corticon does not have any way of enforcing that the exact number of parameters are always passed in, and that parameters are of the expected type.

For example, if you want to compute the future value of an investment, you would typically have a custom function taking three parameters (presentValue, interestRate, numberOfYears) where each of these parameters are decimals.

This function would typically be specified in Corticon like this:

MyEntity.futureValue =
   getDecimal('FutureValue', MyEntity.investment,
               MyEntity.interestRate, MyEntity.numberOfYears) 

If the modeler writes this function with missing parameters, like this:

MyEntity.futureValue =
    getDecimal('FutureValue', MyEntity.investment)

or if the modeler writes this function that passes a Date attribute instead of a Decimal attribute, like this:

MyEntity.futureValue =
     getDecimal('FutureValue', MyEntity.investment,
                 MyEntity.interestRate, MyEntity.someDate) 

then there needs to be a way to handle these cases gracefully.

Let’s explore the options.

Missing Parameters

You can check for any missing parameters by testing for the undefined JavaScript keyword.

For example, when the parameter is undefined, you could default its value, like this:

let numberOfYears = params[2];
if ( numberOfYears === undefined )
	numberOfYears = 1;

Or you could throw an exception to stop processing, and inform the modeler that there are missing required parameters, like this:

if ( params === undefined || params[0] === undefined || params[1] === undefined )  
     throw Error('missing parameters, please ensure you pass an
                  investment value and an interest rate');

When running in Corticon studio tester, the user will then get this message box:

Checking Parameter Types

For parameter types, you can use:

  1. For number, boolean or string: the JavaScript built-in typeof operator

  2. For Decimal, DateTime and Date: the helper functions, such as operatorHelper.date.isDate(params[1])

The following code is a full example showing how to check parameter types. It is a custom extended operator that will output – in a single string - each of the passed-in parameter types:

function whatIsParam ( helper, params ) {
  if ( params === undefined || params[0] === undefined )  
   throw Error('missing parameters, be sure to pass at least one parameter to whatIsParam');

  let result = "";
  for ( let i=0; i<params.length; i++ ) {
      if ( typeof params[i] === 'string' )
          result += 'Parameter ' + i + ' is a string. ';  
      else if ( typeof params[i] === 'number' )  
          result += 'Parameter ' + i + ' is a number. ';  
      else if ( typeof params[i] === 'object' ) { 
          if ( helper.date.isDate( params[i] ) )      
              result += 'Parameter ' + i + ' is a Date. ';
      else if ( helper.dateTime.isDateTime( params[i] ) )
              result += 'Parameter ' + i + ' is a DateTime. ';
      else if ( helper.decimal.isDecimal( params[i] ) )  
              result += 'Parameter ' + i + ' is a Decimal. ';  
      else    result += 'Parameter ' + i + ' is unknow object. ';  
    }
    else             
     result += 'Parameter ' + i + ' is not known. ';  
   }  
   return result; 
}

Usage in a Corticon Studio Rulesheet could be:

Ent1.str1 = getString('whatIsParam', 1, 'ab',
                       Ent1.dateTime1, Ent1.date1 , Ent1.decimal1)

When a Testsheet in the project has that Rulesheet as its test subject, the output would be:

Parameter 0 is a number. 
Parameter 1 is a string. 
Parameter 2 is a DateTime.
Parameter 3 is a Date. 
Parameter 4 is a Decimal.

Handling Decimal, DateTime and Date data types

There are various helper functions for decimal, datetime, and date. See Helper functions for details.

Example Functions

Example 1: A simple concatenate function that takes any number of parameters to concatenate together:

function concatMany ( helper, params ) {
    let result = "";
    for ( let i=0; i<params.length; i++ )
        result += params[i];  
    return result; 
}

Example 2: A simple added function that takes any number of parameters, and adds them together. It assumes all parameters are JS numbers (Integer Corticon datatype):

function addAnyNumbers ( helper, params ) {
    let result = 0;
    for ( let i=0; i<params.length; i++ )
        result += params[i];   

    return result; 
}

Example 3: Using set and get operators:

let sum = 0;
function setSum ( helper, params ) {
     let result = 0;
     for ( let i=0; i<params.length; i++ )
         result += params[i];
   sum = result; 
} 

function getSum () {
   return sum;
}

Example 4: How to check input parameters:

function checkParams ( helper, params ) {
    if ( params === undefined || params[0] === undefined ||  params[1] === undefined )
         throw Error('missing parameters, 
           please ensure you pass an investment value and an interest rate');

    return params[0];   
}

Example 5: How to test parameters for all the supported data types:

function whatIsParam ( helper,params ) {
  if ( params === undefined || params[0] === undefined )
      throw Error('missing parameters, 
         please ensure you pass at least one parameter to whatIsParam');
  let result = ""; 
  for ( let i=0; i<params.length; i++ ) {
      if ( typeof params[i] === 'string' )
          result += 'Parameter ' + i + ' is a string. ';
      else if ( typeof params[i] === 'number' )
          result += 'Parameter ' + i + ' is a number. ';  
      else if ( typeof params[i] === 'object' ) {
          if ( helper.date.isDate( params[i] ) )
              result += 'Parameter ' + i + ' is a Date. ';  
      else if ( helper.dateTime.isDateTime( params[i] ) )
              result += 'Parameter ' + i + ' is a DateTime. ';  
      else if ( helper.decimal.isDecimal( params[i] ) ) 
              result += 'Parameter ' + i + ' is a Decimal. '; 
      else 
              result += 'Parameter ' + i + ' is unknown object. ';
    }  
    else 
      result += 'Parameter ' + i + ' is not known. ';

   }  
  return result; 
}