Introduction
In the blog titled
"CAS Stored Process" with my Favorite Action Hero runCas I discussed how to script the Cas server using casl.
In this blog I show how to use the restaflib library to simplify that code
Main tools used in this blog
restaflib is a Javascript library build on top of SAS REST APIs and restaf library to address several common use cases.
Setup
Web application
Include the following two script tags.
<script src="https://unpkg.com/restaf@dev/dist/restaf.min.js"></script>
<script src="https://unpkg.com/restaflib@dev/dist/restaflib.min.js"></script>
Two globals restaf and restaflib will be available for use in your script tags.
Nodejs application
Install restaf and restaflib using the following command
npm install restaf@dev restaflib@dev
Import these into your nodejs application.
let restaf = require('restaf');
let restaflib = require('restaflib');
Flow
The flow for scoring is shown in the diagram below. Notice this is similar to the flow discussed in the blog on scoring(link).
IMG here
Sample Code
Sidebar
The payload to store.logon depends on your authentication flow. Please see this link for details on the payload.
A typical code is shown below.
let store = restaf.initStore(); /* (1) */
store.logon(payload)
.then ( () => example())
.catch(err) => console.log(e)
async function example() {
let {session} = await restaflib.casSetup (store); /* (2) */
let casl = ` /* (3) */
print 'input values';
print _args_;
action datastep.runcode/ single='YES' code = 'data casuser.a; x=1; run;';
action table.fetch r=r1/
table= { caslib= 'casuser', name= 'a' } ;
run;
action datastep.runcode/ single='YES' code = 'data casuser.b; y=1; run;';
action table.fetch r=r2/
table= { caslib= 'casuser', name= 'b' } ;
run;
c = {a=10, b=20};
send_response({a=r1, b=r2, c=c});
`;
let args = {a: "this is arguments", b: "more data"};
let result = await restaflib.caslRun (store, session, casl, args);
print.object(result.c, 'Selected Cas Results');
await store.apiCall(session.links('delete'));
`;
let r = await caslRun(store, session, src, args); /* (4) */
console.log(JSON.stringify(r, null,4));
}
The 4th argument to caslRun is passed to the casl program as a dictionary named _args_.
The result is a Javascript object with the standard output from a cas action call. The
{
"days_out_of_service": 5,
"sensor_ratio": 4.3,
"_Index_": 1,
"EM_EVENTPROBABILITY": 0.10521221221641,
"I_FAILURE": " 0",
"P_FAILURE0": 0.89478778778358,
"P_FAILURE1": 0.10521221221641,
"_WARN_": "",
"EM_CLASSIFICATION": " 0",
"EM_PROBABILITY": 0.89478778778358
}
Other scoring use cases
The code is identical to the one show above. The modelName is ignored in these cases. The model refers to the sashdat where the datastep
code or the astore is saved. See this link for examples of all 3 use cases.
Notes
- caslScore method will load the model if it is not in memory
- If there are multiple models whose name only vary in case, the first one found will be used
Finally
The next blog will discuss executing any casl program on the cas server using a flow similar to the one
discussed here.
All comments are welcomed.
Cheers...