Corticon.js provides data access operators in a mechanism that will execute custom JavaScript functions. The custom functions are accessed as a group of built-in operators available from Studio's Rule operator tree under General > Functions as standalone GET and SET operators for each Corticon.js data type:



Each get operator works the same way; only the returned data type is different. Each set operator works in a fashion similar to the get operators yet each has an additional parameter, the value to set. Set operators do not return any data as they are void operators.

The project for SampleJS Get Set Operators

The sample JavaScript project you use provides examples of the operators as well as Rulesheets, a Ruleflow, and a Ruletest that demonstrate the features. To setup the project:
  1. Perform the tasks in Accessing sample Corticon.js projects.
  2. In Studio, create a new Rule Project, and name it GetSetOperators.
  3. Choose File > Import > Existing Projects into Workspace.
  4. In your staging location, navigate to ExtendedOperators > BasicSampleGetSetOperators.
  5. Select all the files at that location, and then drag the files to copy them into your Rule Project.

The sample's Rulesheet getData

A custom get function takes two parameters:
  1. The name of the custom function to call.
  2. A string parameter. For multiple parameters, the JSON string can encapsulate all the parameters to pass to the custom function.

The sample Rulesheet getData.ers shows each data type with data to fetch.



For example, to call the custom function getData with parameter key2 using the getString operator, the result is stored in the string attribute, Ent1.str2.

The sample's Rulesheet setData

A custom set function takes three parameters:

  1. The name of the custom function to call.
  2. A string parameter. If you need to pass more than one parameter, the JSON string can encapsulate all the parameters to pass to the custom function.
  3. The value to set. The type of this parameter corresponds to the operator name. For example, setDateTime, requires a DateTime object.

How to set a mock implementation for the Studio tester

Studio tester runs the Ruleflows and Rulesheets in the context of a local Node.js process. Consequently, it is possible that:

  • the production environment data may not be available
  • the production context is not available. For example, in studio tester the browser session context or the Serverless function context is not available .

However, you can specify a mock implementation to run local unit tests by appending the implementation to the project. The configuration of the extensions is in the sample's configuration file:

CODE: getSetData.js
const sessionData = new Map();

function getVarData(helper, name) {
    return name;
}

function getSessionData(helper, name) {
    // First we verify value exists for the key
    if (!sessionData.has(name))
        return 'ERROR - no session data for ' + name;

    //Second we verify if the key exists but value is null
    if (sessionData.get(name) == null)
        return null;

    // We are returning the value as is except for items like Date, DateTime or Decimal
    // as these are stored in a text representation and as Corticon.js expects them
    // as specific objects.
    if (name === 'key4') // example showing how to construct a DateTime
        return helper.dateTime.constructDateTime(sessionData.get(name));
    else if (name === 'key6') // example showing how to construct a Date
        return helper.date.constructDate(sessionData.get(name));
    else if (name === 'key5') // example showing how to construct a Decimal
        return helper.decimal.constructDecimal(sessionData.get(name));
    else
        return sessionData.get(name);
}

function setSessionData(helper, name, value) {
    console.log("Custom function setSessionData called for " + name);
    if (value === null) {
        sessionData.delete(name);
        sessionData.set(name, value);
    } else {
        if (name === 'key4') // example showing how to construct a DateTime
            sessionData.set('key4', helper.dateTime.outputToJson(value));
        if (name === 'key6') // example showing how to construct a Date
            sessionData.set('key6', helper.date.outputToJson(value));
        else if (name === 'key5') // example showing how to construct a Decimal
            sessionData.set('key5', helper.decimal.outputToJson(value));
        else
            sessionData.delete(name);

        sessionData.set(name, value);
    }
}

// This is where we specify the public names to be used in Corticon.js studio when using
// either the get or set operators.
module.exports = {
    getSessionData,
    setSessionData: setSessionData
};
Note: For example, to call the custom decimal function setData with the parameter key5 using the setDecimal operator, use the action sessionData.set('key5', helper.decimal.outputToJson(value) as shown in the mock implementation. The result is stored in the string attribute, Ent1.decimal1.

The functions from the configuration file are needed for testing as well bundling into production packaging.

To append the extension file to the project:
  1. In Corticon.js Studio, open your project.
  2. Click on your project, and then select Properties.
  3. Select Corticon.js Extensions.
  4. Click Add, and then select your .js implementation file, as illustrated:


  5. Click Apply and Close.

Running the sample's Ruletest

A Ruleflow puts the setters Rulesheet to process ahead of the getters Rulesheet:


Open the sample Ruletest, getSetDataTest.ert, and then run it: