In SAS Intelligent Decisioning you can write your own custom functions to enhance its set of functions if you have code that you want to use more often or in different Decision Flows.
Sometimes it may be useful to have one function that accepts different sets of input parameters. So, you can call the same function in a different way depending on its use case. For this a programming language needs to support ‘function overloading’. SAS DS2, the programming language in Intelligent Decisioning, does support ‘function overloading’.
To write an overload function in Intelligent Decisioning we have to use a custom context file rather than writing a custom function directly in Intelligent Decisioning.
To create a custom context file, go to Code files in Intelligent Decisioning click on ‘New Code File’ and choose file type ‘Custom Context’
In the file you can write different variants of your overload function which have the same name but different input parameters.
In this example we have a function (or method as it is called in DS2) that calculates the fuel economy for a given list of vehicles. We have two flavours of the same function:
When we have written the function (fuelEconomy()) we can call it from a DS2 code node, a custom function or from a Rules Set. Depending on the use case we can call the function in two different ways.
To call a function that is in a custom context file we need to call it via its qualifier ‘application’ using the format: application.method_name(arguments);
In a Rule Set for example we can call the function like this:
We pass in the list of vehicles in parameter vehicleList:
And receive the fuel economy in parameter ecoList:
Alternatively, we can call the function like this:
Where we only pass in the parameter vehicleList:
And receive the fuel economy also through parameter vehicleList:
To test the Rule Set we have to make the Rule Set aware of the custom context file, so it can find the function we have written. In the Rule Set go to Properties and set the custom context file in field: Test custom context.
(The same applies when testing DS2 code files)
Custom Context files can only be used with published Decision Flows and not Rule Sets.
For a Decision Flow to pick up a custom context file we need to associate it with the Decision Flow where we use it. In the Decision Flow go to Properties to set the custom context file. There are two entries for custom context files as you can use different files for executing the decision flow in CAS or MAS respectively.
To enrich the set of functions in Intelligent Decisioning you can write overload functions by using Custom Context files. These functions can then be called in DS2 code files, custom functions, and Rule Sets.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.